Jeremy Leach
Senior Member
Here's a little routine to give ArcTan. It's suitable for the Picaxes like the 28X1 that can use the Sin and Cos functions.
It's a simple iterative technique and isn't particularly efficient speedwise because it always goes through 7 iterations.
To use the routine:
1. Load up TanTarget100 with the Tan value (times 100) that you are wanting to get the ArcTan result for.
2. Then Gosub ArcTan
Note: The code below includes some test code you can delete. I've used the test code on the simulator and produced the attached error chart.
It's a simple iterative technique and isn't particularly efficient speedwise because it always goes through 7 iterations.
To use the routine:
1. Load up TanTarget100 with the Tan value (times 100) that you are wanting to get the ArcTan result for.
2. Then Gosub ArcTan
Note: The code below includes some test code you can delete. I've used the test code on the simulator and produced the attached error chart.
Code:
#picaxe 28x1
#rem
#############################################################################
# #
# TITLE : ArcTan Routine using iterative approximation #
# #
# Code Version : 1A #
# Date : April 2009 #
# PICAXE Type : 28X1 (or any supporting Sin and Cos) #
# Firmware : Unlikely to matter #
# Editor Software : 5.2.0 #
# Author : Jeremy Leach #
# #
#############################################################################
#endrem
'VARIABLES
'---------
'Word0 (b0 and b1)
Symbol Angle90 = b0
Symbol Angle128 = b1
'Word2 (b4 and b5)
Symbol Sin100 = b4
Symbol Cos100 = b5
'Word4 (b8 and b9)
Symbol StepSize = b8
'Word6 (b12 and b13)
Symbol Tan100 = w6
'Word8
Symbol TanTarget100 = w8
TEST:
TanTarget100 = 10000
Gosub ArcTan
Stop
ArcTan:
'ON ENTRY: TanTarget100 holds the value you want to get the ArcTan value for (positive word only 0 to 9000).
'ON EXIT: Angle90 holds the result in degrees (0 to 90).
'Initialise
StepSize = 64
Angle128 = 64
Do
Angle90 = Angle128 * 90 / 128
'Calculate Tan of Angle90
Sin100 = Sin Angle90
Cos100 = Cos Angle90
If Cos100 = 0 Then
Tan100 = 10000
Else
Tan100 = Sin100 * 100 /Cos100
EndIf
'Adjust the Angle128
If Tan100 > TanTarget100 Then
Angle128 = Angle128 - StepSize
Else
Angle128 = Angle128 + StepSize
EndIf
'Halve the stepsize
StepSize = StepSize / 2
Loop Until StepSize = 0
Return
Attachments
-
47.8 KB Views: 52