Return of the Inverse Trig Functions

Our team is not planning on using the optical sensors to track the goals, but maybe some of you might want to experiment with them and would find a use for inverse sine and cosine functions that work with the Basic Stamp’s idiosyncratic math processing abilities. The Stamp’s sine (sin) and cosine (cos) functions take an argument (an angle) in ‘binary radians’ (brads) and output an integer result in the range -127 to +127. The brad is an angular measure arrived at by dividing a circle into 256 arcs, instead of the customary 360 degrees. A brad is 360/256 = 1.406 degrees.
Integer output of the functions is used because the Stamp knows nothing of numbers between 0 and 1. The normal 0 to 1 range (abs) of the sine and cosine is thus scaled up to 0 to 127.

If we restrict the discussion to angles between 0 and 90 degrees (0 to 64 brads), the sin and cos functions map integers in the range 0 to 64 into other integers in the range 0 to 127. Going the other way, the inverse sine and cosine functions should map integers in the range 0 to 127 into integers in the range 0 to 64.

In order to accomplish that, I fit quadratic curves to the integers obtained from the Stamp’s sin and cos functions, such that the curves would take arguments in the range of 0 to 127, and return angles of 0 to 64. I used two curves for each inverse function, and pieced them together. I had to apply some ‘end correction’ to the inverse sine to get a closer agreement with the Stamp’s sine values, but in general the inverse functions have relatively small errors. They aren’t perfect; feel free to contact me for the details of how I arrived at them, if you would like to do some more work on the problem.

The inverse sine and cosine functions are presented below. They are valid for input in the range 0 to 127, and will output angles in brads in the range 0 to 64 (0 to 90 degrees). You will have to decide on what trig relationships will give you what you need if you want to work outside those ranges - but it is certainly possible. Also, you may wish to rename variables. The code needs to be all on one line - I have divided it for readability. Be very careful with the parentheses - think before changing them, or you may get some strange results. The quadratics are written in ‘nested form’ to keep intermediate values within the allowable range of the Stamp’s word-length work space. So, don’t be scared of the large integers inside the functions - everything works out.

arccosine:

arccos = ((1-(x/118))(((-x-181)x+48472)/769)) +
((x/118)
((((-3
x+707)*x-41331)/20)))

arcsine:

arcsin = ((1-(x/118))(((x+181)x+744)/769)) +
((x/118)
(((((3
x-707)*x+42611)/20))+((x-115)/3)))

Just one more note in this excessively long post. The (x/118) and (1 - x/118) use the Stamp’s integer math to turn the two curves on and off at the right time. To the Stamp, 117/118 =0, and
127/118 = 1.

Good luck, and let me know if you use these.

I thought about using the trig functions, then I arrived at a better conclusion. If you have the time, it would be considerably easier to make a lookup lookdown command going off the servo value itself. If you are making a ball launcher, you don’t need to know the exact distance away from the goal you are. You just need to know how fast to turn the motor. Instead of using two equations (one to find the distance and another to set the speed of the launcher), just use the lookup/down commands for getting the servo position then use the returned index value to set the speed of the motor.

Nice Job Leo we were looking for a better way to calculate our steering angle

Thanks. It was really just an ‘intellectual exercise’ for me during the off-season. Once I really got into it, I started learning all sorts of useful things that can be done with that little Stamp processor. The end result was just a couple of lines of code that we aren’t going to use (not this year, anyway). They were too good to just throw away, so I thought it would be a lot better to give them to my friends in the FIRST community. It certainly would make me happy to know that someone got some use out of them, if only to learn more about curve fitting and least squares.