Shop OBEX P1 Docs P2 Docs Learn Events
math on the prop — Parallax Forums

math on the prop

P!-RoP!-Ro Posts: 1,189
edited 2009-02-19 18:46 in Propeller 1
I have created an equation to take ping distances and turn them into x and y coordinates. My only problem is I don't know how to make the propeller·solve the equation.
attachment.php?attachmentid=58702
I tried writing code to calculate it·recently but I just got error messages. Could you show me the correct way of doing it?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PG

Comments

  • edited 2009-02-15 21:09
    For a quick and simple solution, I usually use Cam Thompson's·Floating Point package. ·Go to obex.parallax.com, and click Math.·

    For an example of simple floating point, see chapter 6 in the Propeller Education Kit Labs:Fundamentals Kit· book·(.pdf ~7MB)· source code (zip).· For Cam Thompson's object, you will have to call its start method before calling its conversion and calculation methods.


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

    Education Department
    Parallax, Inc.

    Post Edited (Andy Lindsay (Parallax)) : 2/15/2009 9:31:03 PM GMT
  • RubrChickenRubrChicken Posts: 39
    edited 2009-02-15 21:33
    Try this:

    VAR
         long x
         long y
         long distance1
         long distance2
         long angle1
         long angle2
    
    .........
    
    y = ^^((distance1*distance1)/angle1)
    x = ^^((distance2*distance2)/angle2)
    
    
    
    





    If you want floating point, just multiply by 1000 at the beginning and divide at the end.
    I don't know if this is what you were looking for, but I hope it helps!
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-16 00:12
    Thanks for the help! I think between the two of you all of my questions have been answered..for now. Again, thanks!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-02-16 05:38
    I'm most interested to know how you derived those equations. What's the geometry of the situation? Im trying without success (doubtless from a lack of imagination) to visualize a geometry that would lead to equations like those. Hmmm?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • hal2000hal2000 Posts: 66
    edited 2009-02-16 15:06
    Hi
    It's good
    With your permission
    a website with equations for program?
    Equations for linear motion etc.
    Sorry for my English

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Did the curiosity kill the cat?
    ......................................
  • JCeeJCee Posts: 36
    edited 2009-02-16 19:55
    Carl Hayes said...
    I'm most interested to know how you derived those equations. What's the geometry of the situation? Im trying without success (doubtless from a lack of imagination) to visualize a geometry that would lead to equations like those. Hmmm?

    I dont quite follow the geometry either but there is a good post about ping radar "PingDar" that may be useful.
    http://forums.parallax.com/showthread.php?p=590115
    ·
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-17 02:41
    It's a little hard to explain, so I drew a picture of how you would find the parts of the equation. The only difference is the angles will be found in decimal places, so you will have to multiply them instead of dividing.

    As far as I know this equation isn't on the internet anywhere. I just created it by modifying the Pythagorean Theorem.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
    324 x 243 - 9K
  • Jack CrenshawJack Crenshaw Posts: 46
    edited 2009-02-17 20:17
    I think you'll find that these equations will do what you want. In the equations, x and y are the ranges measured by the right- and left-handed sensors. d is the distance to a wall, and h is the relative heading, positive counter-clockwise.

    h = atan2(x,y) - 45 degrees
    d = (x*y)/sqrt(x^2+y^2)

    Note: Better double-check the sense of the two arguments of atan2. In the Fortran and C versions, the first argument is the sine part, the second the cosine part. Mathcad does it the other way. I'm not sure which convention the Propeller version uses, but the formula I gave is the formula for (sin, cos).

    I have the analysis in HTML format, if anyone wants me to upload it.

    Jack
    Jack
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-17 23:56
    There are two differences, so I'm not sure it will work. The first one is my equation is made to work with only one sensor, and the second one is it is meant to find the coordinates of objects so they can be mapped out. Yours looks to be more for navigating around objects, am I correct?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • Jack CrenshawJack Crenshaw Posts: 46
    edited 2009-02-18 02:55
    Pi Guy said...
    There are two differences, so I'm not sure it will work. The first one is my equation is made to work with only one sensor, and the second one is it is meant to find the coordinates of objects so they can be mapped out. Yours looks to be more for navigating around objects, am I correct?

    No. I guess I misinterpreted your figure. I saw the triangle, and assumed that the two axes were lines of sight of two sensors. I assumed that the hypotenuse was some wall. Given that assumption, my equations would give the heading to, and distance from, the wall.

    Maybe it's time you told us what that figure is supposed to represent.

    Or (also a possibility) maybe everyone else already knows.

    Jack
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-18 05:31
    Thanks for the equasion anyway, Jack. It gave me something to wrap my head around. BTW, what does atan mean?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2009-02-18 09:53
    Pi Guy, atan is the inverse tangent: Given x and y, what is the angle?
    theta=atan(y/x) is the inverse of
    y/x = tan(theta)

    A while ago on this forum, there was an extended sidetrack (within a CORDIC thread) about how one might do vector additions without resorting to trig to go back and forth painfully from polar to rectangular coordinates.
    http://forums.parallax.com/showthread.php?p=609449
    If I recall correctly, no super duper tricks were forthcoming.

    But Pi Guy, I don't understand your equation or the geometry of the mapping problem you are trying to solve. I'm thinking of something like the following diagram, where the PING)) knows where it is in X and Y and also its angle, and it detects an object at that angle at distance d, and needs to put it on the map. The new coordinates x= d*cos(&#937[noparse];)[/noparse] and y=d*sin(&#937[noparse];)[/noparse] need to be added (signed) to the current coordinates of the PING)). Is that off base?

    attachment.php?attachmentid=58785

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
    243 x 223 - 5K
  • heaterheater Posts: 3,370
    edited 2009-02-18 10:28
    Seems quite clear Tracy. I also don't see where those first equations come from either.

    Assume the ping sensor rotates to determine the angle we now only have to deal with the fact that the sensor will probably detect the object over a range of angles either side of its direction. So we need to find the mean angle. If there is more than one object the problem is going to get interesting.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • edited 2009-02-18 16:55
    This also harkens back to the Floating Point objects I mentioned earlier because they have trig and inverse trig functions. In terms of coding, they get the job done quickly.

    I published how to take these kind of measurements with the BASIC Stamp for Boe-Bot + Ping))) navigation (polar to Cartesian and Cartesian to polar coordinate conversions). In fact, I think I based some of it on Tracy Allen's articles on inverse trig functions with the BASIC Stamp. You can find these articles under the Boe-Bot + Ping))) heading at forums.parallax.com -> Stamps in Class -> Stamps in Class "Mini Projects".

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

    Education Department
    Parallax, Inc.
  • Jack CrenshawJack Crenshaw Posts: 46
    edited 2009-02-18 17:34
    Guys, I've got so many things to say, I don't know which one to say first. How about:

    @tracy: I just read the thread from 2006. Sorry, guys, but there's no shortcut to using Cartesian coordinates. Everything that looks like a shortcut is really a longcut. If folks insist on using polar coordinates, get used to calling sin/cos.

    Better choice: Ditch polar coordinates from the get-go, and do everything in Cartesian. If your concern is relating a vector measured in body (sensor) coordinates, you only need to do a 2x2 matrix multiply to get it. Conventionally, we write the matrix
    | cos a -sin a |
    C = | sin a cos a|

    But that's more for our benefit than the computers. There are lots of ways to generate C directly from sensor measurements, without ever really having to MENTION the word "angle."

    @tracy and heater
    Ok, I get the geometry of the single sensor, but that leaves me wondering what Pi Guy's "angle1" and "angle 2" are.

    In any case, the situation tracy drew only gives you a single isolated point in space. If you're interested in mapping the environment, don't you need to somehow recognize extended shapes? This is hard to do, even for us lower-order animals (humans). Can we say "optical illusion"?

    On the other hand, ping (or laser) radar gives you something your eyeballs can't, which is range. That's really valuable.

    It gets even more valuable if you can scan something. That's why I was thinking two sensors. Better yet, how about a single sensor, on a swivel. You could sweep the sensor, sort of like the chrome-plated Cylons do. Or Robbie the Robot.

    Even better yet, sweep the ROBOT, or at least his head, as birds, lizards, and people do.

    A few years ago, I saw a demonstration of a laser system that generates a 3-d image (!). Using mirrors, they sweep a scene using a plain ol' raster scan. But the laser's not only giving you returns from the target, but range for each pixel. In one 60 Hz sweep, they got a 3-d picture <!>. Take a bunch, and you've got a 3-d movie. Then they had software that could take a convention color picture of the same scene, and 3-d-ize it. Then view it from any direction (within reason -- still can't see the back side). Way cool.

    Sorry this post was so long. I didn't have time to post a short one.

    Jack
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2009-02-18 17:47
    Anyone care to do the math for Vorbis decode ! now wouldn't that be nice a Ported 'Tremor' implementation of a vorbis decoder ..

    Rgds,
    ······ John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'

    Those who can, do.Those who can’t, teach.
    711 x 443 - 53K
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-19 00:15
    Sorry, Tracy, but you're just going to have to wait until I take trig next trimester before I will understand what you are talking about. As for knowing what angle1 and angle2 are, it isn't too hard. Angle1 is the difference of 1 and the angle of the sensor and angle2 is the difference of 0 and the angle of the sensor. The equation only works for only one quadrant before you have to change the signs on x and y to match it. For example quadrant 1, which the picture was drawn in, has both x and y coordinates positive. In quadrant 2 x would be negative and y would be positive and so on for the rest of the 4 quadrants.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • SRLMSRLM Posts: 5,045
    edited 2009-02-19 01:42
    Okay, I get what you're going for. Not going to work. Here's what I think you were looking to do:

    Pythagorean Theorem
    x^2 + y^2 = h^2

    Where h^2 is the hypotenuse. You did something like

    h^2/angleY = x^2
    x = (h^2/angleY)^(1/2)

    Correct analysis?
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-19 04:12
    I'm not seeing any similarities, SLRM. I think part of the problem you guys are all having is the way I wrote the equation. Instead of having distance divided by angle, just multiply it since the angle is going to be a fraction. Also, make distance1 and distance2 just distance. I made a mistake when I wrote it, they are supposed to be the same thing. One last thing is switch x and y. Something's not right there either [noparse]:)[/noparse]

    If you don't believe it will work, then punch in some numbers and try it. If you get a reading of 10" at 90 deg. angle1 = 1 angle2 = 0. Since I made a mistake the equation for angle 1 will now find you y. If you square 10 it will become 100, multiplied by 1, equals 100. The square root gets you ten again. If you do it for x, it will get you 0. Correct is it not? Now how about 45 deg. 10"? You get ~7.07 for both x and y. If you graph the coordinates they will line up no matter what angle you throw into the equation. Need anymore proof that it works?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • SRLMSRLM Posts: 5,045
    edited 2009-02-19 04:59
    Can you show us how you derived the equation? Step by step? I tried a reference triangle, and your equation didn't work.

    Triangle in question:sides of 1, root(3), and 2 and angles of 30,60, and 90. Picture:

    trig_30_60_90.gif

    y should return 1
    x should return root(3)
    d should be 2

    To calculate x, I use the first equation (since you told us to switch the x and y)
    I change 30 degrees to fit on the 0 to 1 scale that you have so my angle is 1/3. My equation is:

    x = root(2^2 / (1/3))

    x = root(4*3/1)
    x = root(12)

    x = root(12) != root(3)

    Yes, your equation works for multiples of 45 degrees, but not anything else (most likely based on the fact that 45 and 90 degrees are easily simplified in your equation). You'll have to use trigonometric functions (or their approximations) to calculate the x and y coordinates of from a hypotenuse and angle.

    Post Edited (SRLM) : 2/19/2009 6:24:43 AM GMT
  • Jack CrenshawJack Crenshaw Posts: 46
    edited 2009-02-19 05:47
    JCee said...
    I dont quite follow the geometry either but there is a good post about ping radar "PingDar" that may be useful.
    http://forums.parallax.com/forums/default.aspx?f=6&m=129099

    Dang! Forget all my ideas about swiveling a sensor. I see that you guys were about 3 years ahead of me.

    Story of my life.

    Sounds like what Pi Guy needs is a sin/cos decoder. Not sure how much they cost, but I can think of a couple of cheap versions, one using slide pots, one magnetic.

    Nah, forget both. Just use a stepper motor, and turn counts into sin-cos using table lookup. Multiply by the range, and you've got your Cartesian vector.

    Jack
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-19 06:33
    There certainly does seem to be a problem, slrm. And part of it is in your faulty computing. However, even if you do it properly my equation is coming off a little to the decimal place. I guess it's back to the drawing board..for now.

    BTW I think my equation works for all 45-45-90 triangles, but it will take some fixing to work for others as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-19 06:37
    @Jack- I have no clue what a cartesian filter is. Can it find me x and y coordinates?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • SRLMSRLM Posts: 5,045
    edited 2009-02-19 07:21
    Pi Guy said...
    There certainly does seem to be a problem, slrm. And part of it is in your faulty computing.

    Do explain...

    BTW, this is not meant to be an attack on you or your intelligence or anything like that. I'm just pointing out that possible errors.
  • P!-RoP!-Ro Posts: 1,189
    edited 2009-02-19 07:54
    Yup, and I think I've solved it! If I'm correct this is how you would do it:
    find the regular answer using my equation (for yours it's sqroot 1.333 for y, sqroot 2.666 for x)
    Next you would subtract the larger deg from the smaller one (90 - 60) and if the deg for the sensor is smaller than 45 deg (.5) you add the fraction to x and subtract from y, if it is less it would be the reverse. In your equation you would get 30 for the sensors angle, or 30/90 which is 1/3. So, sqroot 1.333 becomes sqroot 1 and sqroot 2.666 becomes sqroot 3. So, your coordinates are (sqroot 3, 1).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    PG
  • SRLMSRLM Posts: 5,045
    edited 2009-02-19 17:16
    Can you post your new and corrected equation? Perhaps with an example? I'm still not following.
  • Jack CrenshawJack Crenshaw Posts: 46
    edited 2009-02-19 18:46
    Pi Guy said...
    @Jack- I have no clue what a cartesian filter is.

    PG, I said "Cartesian vector," not "cartesian filter."

    >Can it find me x and y coordinates?

    The x- and y-coordinates _ARE_ the Cartesian vector. You seem to be trying to compute these coordinates from the range and angle info. The conversion is simply:

    x = d cos(angle)
    y = d sin(angle)

    Cartesian vector = [noparse][[/noparse]x, y]

    I was suggesting that you might get the sine and cosine from a sin/cos encoder, or simply by a table lookup, given the angle. I'm afraid there is no shortcut. You can't do trig without trig functions, no matter how hard you try.

    Jack
Sign In or Register to comment.