Shop OBEX P1 Docs P2 Docs Learn Events
need help with brads — Parallax Forums

need help with brads

W9GFOW9GFO Posts: 4,010
edited 2007-04-20 12:43 in BASIC Stamp
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

Comments

  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-19 08:42
    W9,

    what's wrong with a simple subtract ?

    Ed

    Post Edited (Skywalker49) : 4/19/2007 9:44:07 AM GMT
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-04-19 10:19
    W9GFO -

    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)

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-19 10:28
    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

    Ed
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-04-19 10:58
    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.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-19 13:44
    Bruce,

    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.

    Ed
  • W9GFOW9GFO Posts: 4,010
    edited 2007-04-19 15:02
    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
  • W9GFOW9GFO Posts: 4,010
    edited 2007-04-19 15:17
    Ok, I think I figured it out

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

    I'm gonna go try it...
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-04-19 15:28
    W9GFO -

    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.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-04-19 15:31
    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
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-04-19 15:36
    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.

    http://web.mit.edu/don_eng/Public/SciFair/SciFair05/Dilemmas.doc

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Skywalker49Skywalker49 Posts: 172
    edited 2007-04-19 15:39
    W9, what about this ?

    diff = ABS(heading-direction)

    IF diff < 129 THEN done
    diff = 256 - diff
    Done:

    Ed
  • W9GFOW9GFO Posts: 4,010
    edited 2007-04-19 16:12
    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 BatesBruce Bates Posts: 3,045
    edited 2007-04-19 18:07
    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.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • W9GFOW9GFO Posts: 4,010
    edited 2007-04-19 18:09
    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
    ELSE
    Steer Left
    ENDIF

    Does that look like a good way to do it?

    Rich H
  • W9GFOW9GFO Posts: 4,010
    edited 2007-04-19 18:31
    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 smile.gif

    Thanks for all the help,

    Rich H
  • edited 2007-04-19 21:27
    W9GFO,

    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
    · Scan for and Go to Closest Object

    These are just a few of the interesting things you will find on the Stamps in Class "Mini Projects"·post.

    Andy


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • CCraigCCraig Posts: 163
    edited 2007-04-20 12:43
    W9GFO,

    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
Sign In or Register to comment.