View Full Version : need help with brads

04-19-2007, 03:01 PM
Hi all, I've been scratching my head for quite a while and just can't get this figured out. It seems like it ought to be simple, so now I ask for help...

I have a direction I want to go, in brads

I have a heading that I am going, in brads

I need to determine the difference in brads between the direction and heading

That's it, I hope someone can help with the solution

04-19-2007, 03:42 PM

what's wrong with a simple subtract ?


Post Edited (Skywalker49) : 4/19/2007 9:44:07 AM GMT

Bruce Bates
04-19-2007, 05:19 PM

I'm not quite sure what you mean by "I have a direction I want to go, in brads", but regardless, how about the following:

difference VAR BYTE
heading VAR BYTE
direction VAR BYTE

difference = abs(heading-direction)


Bruce Bates


04-19-2007, 05:28 PM
Bruce, W9,

Brad vars need to be Word type otherwise trig. functions will not work correctly.
See Allen Tracy's website: http://www.emesystems.com/BS2math3.htm#arcCos


Bruce Bates
04-19-2007, 05:58 PM
Skywalker -

I see no trig being used at present in the data given. so we can disregard that warning for now. Since brad values run from 0 to 255 the largest diffference that can be seen is 255 - 0 which is 255 and well within the capacity of a BYTE. Use WORDS _ONLY_ when you NEED to use WORDS, otherwise use BYTEs or NIBs as they are more appropriate and will conserve valuable RAM space.

If we had been adding or multiplying, there might have been a case for a WORD variable as the receiving field. That is clearly not the case here, as we're doing substraction.

Just for the record ABS means ABSOLUTE value, which has nothing to do with trig.


Bruce Bates


04-19-2007, 08:44 PM

your are ABSolutely right about the choise of vars.

On top ...
Reading the documentation again, byte is ok for angles in brads.
This is even ok for trig. functions which most likely will be used when angles are involved.

Where one MUST use word variables is in the result of trig. functions. These functions return a 16 bit result.
Sorry for the confusing message sent earlier but hopefully now that the message is complete, it adds some value.


04-19-2007, 10:02 PM
Thanks, I didn't think of using ABS in the result. But there is still a problem,

direction = 250
heading = 20 (the difference here should be 26)

difference = ABS(heading-dir)
difference = 230

If one is between 192 and 255 and the other is between 0 and 64, the difference will give a result too large.

Rich H

04-19-2007, 10:17 PM
Ok, I think I figured it out

IF difference > 128 THEN
difference =ABS((difference /2) - 128)*2

I'm gonna go try it...

Bruce Bates
04-19-2007, 10:28 PM

I'm not sure what you're trying to calculate here, as it's not the kind of navigation I've done, but if you're happy with the result above. I suspect Tracy Allen can show you a way to do it in one line of program code using the MAX and/or MIN function.

Tracy can shoot it right out, but it would take me an hour to ferret it out and test it to my and your satisfaction. What you essentially are doing is taking the larger or smaller of the calculations BOTH of which will appear in the extended instruction. This is possible because PBASIC is very adept about using and saving intermediate results. It's one of the more powerful aspects of PBASIC which are hardy "played up" enough. Tracy uses it all the time and thus it's fairly easy for him to see exactly what needs to be done.


Bruce Bates


Tracy Allen
04-19-2007, 10:31 PM
You can either rotate cw or ccw, and one of those will be the shortest.

difference = ABS(heading-dir)
difference = difference MAX (256 - difference)

will select the one that is shorter. It is a little more complicated if you want to maintain the sign, that is, which direction to rotate.

Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Chris Savage
04-19-2007, 10:36 PM
I have asked someone more familiar with this math to drop in with some possible information, but in the meantime a quick search on my own revealed this rather humorous document of a student's attempt to make use of BRADS.


Chris Savage
Parallax Tech Support

04-19-2007, 10:39 PM
W9, what about this ?

diff = ABS(heading-direction)

IF diff < 129 THEN done
diff = 256 - diff


04-19-2007, 11:12 PM
Thanks guys, yeah my solution was a bit convoluted, I like the brevity of using MAX.

I do want to determine which direction to rotate, I'm just starting to try to figure that one out...

I sympathize completely with the student who wrote that doc.

I have had to;
Figure out how to make lat/long coords fit into bytes and words,
Figure out how to calculate differences in lats and longs while using a byte and word for each
Reduce varying distances to destinations (in signed bytes) while maintaining maximum accuracy to be used with the ATN function
Change the direction of rotation of brads to cw for terrestrial navigation,
Rotate brads ccw by 64 so that 0 is north

I didn't expect any of these limitations when I started, I had never worked with brads before and didn't expect the -127 to 127 limit for the ATN nor had I had to use the large numbers which are required in lat/lon coords.

I'm getting there, slowly...

Rich H

Bruce Bates
04-20-2007, 01:07 AM
Rich -

You might want to consider using a math co-processor to save you more gray hair! Here are a couple you might consider:

Parallax: http://www.parallax.com/detail.asp?product_id=604-00030

Al Williams: http://www.awce.com/pak1.htm

Just as an aside, I thought some GPS units would do navigation calculations for you.


Bruce Bates


04-20-2007, 01:09 AM
Ok, now here is my current solution that seems to work well;

deviation = ABS(heading - direction)
deviation = deviation MAX (256 - deviation)

IF deviation < ABS(heading -(direction+1)) THEN
Steer Right
Steer Left

Does that look like a good way to do it?

Rich H

04-20-2007, 01:31 AM
I'm using the Parallax GPS unit which does not do those calculations - as far as I know.

That math co-processor looks like it would make things a lot easier, as long as I can figure out how to work it - I just ordered one.

I think I have my distance, heading, off course deviation and direction to steer figured out so now I'm on to learning how to use the GPS

You'll probably hear from me again http://forums.parallax.com/images/smilies/smile.gif

Thanks for all the help,

Rich H

Andy Lindsay (Parallax)
04-20-2007, 04:27 AM

Please look at these two articles.· They demonstrate the necessary conversions for both Cartesian to Polar and back again for robot navigation with a BASIC Stamp.

Boe-Bot + Ping))) Ultrasonic Rangefinder

· Ping)))Dar - a Radar Style Display (http://forums.parallax.com/showthread.php?p=590115)
· Scan for and Go to Closest Object (http://forums.parallax.com/showthread.php?p=590119)

These are just a few of the interesting things you will find on the Stamps in Class "Mini Projects" (http://forums.parallax.com/showthread.php?p=560570)·post.


Andy Lindsay

Education Department
Parallax, Inc.

04-20-2007, 07:43 PM

Remember, you're going to want a 'dead' zone, where you don't turn at all. If not, you will constantly zig-zag. Left, then right, then back again.

HTH, Chris