Shop OBEX P1 Docs P2 Docs Learn Events
trigonometry — Parallax Forums

trigonometry

holyholy Posts: 9
edited 2007-03-22 00:25 in BASIC Stamp
i need some help about trig.
in mathematic term, 20 cos 30 = 17.32
however in bs2, i couldn't get the same result...
i have read about the brad, but i could not understand the explanation..
could som1 help me explain how to get the same result as my example?

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2007-03-16 11:51
    What code did you use in the BS2? Did you take into account the integer math of the Stamp?

    The sample from the help file shows this:

    Main:
    FOR degr = 0 TO 359 STEP 45 ' Use degrees
    cosine = COS (degr * 128 / 180) ' Convert to brads, do COS
    DEBUG "Angle: ", DEC degr, TAB ' Display results
    DEBUG "Cosine: ", SDEC cosine, CR
    NEXT
    END

    Considering what you want as an answer,you should be able to change the third line in the sample to: cosine = 20* (COS (degr*128/180))

    More info provided usually means a better chance of getting an accurate answer.

    Cheers

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • holyholy Posts: 9
    edited 2007-03-16 11:57
    if i use the code that you have provided such as
    '{$STAMP BS2}
    '{$PBASIC 2.5}

    x VAR word

    x=20 * COS (30*128/180)

    DEBUG DEC3 ? x,

    x=220

    when i use

    '{$STAMP BS2}
    '{$PBASIC 2.5}

    x VAR word

    x=20 * COS 30

    DEBUG DEC3 ? x

    x=880.

    the problem is i need the answer to be 20 cos 30 = 17.32. how to get this answer?? does basic stamp support floating number??
  • NewzedNewzed Posts: 2,503
    edited 2007-03-16 12:23
    Here is a little program that will give you answer of 17.22.· Is that close enough?· It is for the sin of an angle.· To use it for cos, subtract the angle you want from 90 and enter the result.· For cos 30, 90-30=60, so enter 60.

    sine VAR word
    degr VAR word
    x VAR word

    start:
    DEBUG "Enter degree", cr
    DEBUGIN DEC degr

    ·sine = SIN (degr */$00B6)' 128 / 180)-Convert to brads
    · DEBUG SDEC ? sine, cr
    · x = sine *79
    · DEBUG· ".",DEC ? x, cr
    · x = (x/10) * 20
    · DEBUG DEC x/1000, ".", DEC3 x, cr, cr
    · GOTO start

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • holyholy Posts: 9
    edited 2007-03-16 12:32
    thanks a lot brother. you are a hero to me for today. this make my job easier. i appreciate your experiment just for me.
  • stamptrolstamptrol Posts: 1,731
    edited 2007-03-16 13:20
    No floating point.

    The only way is work with whole numbers (so your answer is 1732) and then get it to be displayed digit by digit, inserting the "." as a character.

    Also, the result of COS(30*128/180) is about 110, as per the Help file explanantion of COS. In terms of the traditional COS, that should be 110/127=0.8661 and multiplied by 20 gives 17.32.

    In our earlier example, therefore we'd want x = 20 * (cos(30*128/180))/127 that would give the 17; then, use the // operator to get the remainder in the calculation: y=20*(cos(30*128/180))//127

    Cheers

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-16 17:31
    Here is a closed form:

    mult VAR Word
    deg VAR Word
    x VAR Word
    
    mult=20
    deg=30
    
    x = COS (deg */ 182) * 200 */ mult
    
    DEBUG DEC x/100, ".", DEC2 x   ' prints 17.34
    



    That uses the */ operator twice, once to convert degrees to brads (that is the same as Sid used, 182=$B6), then again as a fractional multiplier, with the extra factor of 100 thrown in to get the extra digits to the right of the decimal point. The correct answer would be 17.32, but 17.34 is pretty close and about the best that the Stamp COS operator can do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • holyholy Posts: 9
    edited 2007-03-17 03:08
    for newzed's source code, i tried it and it is only valid for multiplier less than 70 and the degree to be constant that is 20. if the multiplier goes higher than 70, for example 80, the answer would not be as expected and i have to minus the 80 from the 70 and make 2 calculation such as:

    sine VAR Word
    degr CON 70 'cos20=sin70
    sine2 VAR Word
    cmdist CON 150
    cm VAR Word


    IF (cmdist>70) THEN
    sine = SIN (degr */$00B6)' 128 / 180)-Convert to brads
    sine = sine *79
    sine = ((sine/10) * 70) /1000

    cm=cmdist-70
    sine2 = SIN (degr */$00B6)' 128 / 180)-Convert to brads
    sine2 = sine2 *79
    sine2 = ((sine2/10) * cm)/1000

    sine=sine+sine2
    ELSEIF (cmdist<=70) THEN
    sine = SIN (degr */$00B6)' 128 / 180)-Convert to brads
    sine = sine *79
    sine = (sine/10) * cmdist
    ENDIF

    DEBUG DEC sine, ".", DEC3 sine, CR, CR

    however tracy allen's calculation give a better solution compared to newzed [noparse]:)[/noparse]. it is more accurate and i dont have to make modification to it.

    thanks to those who has lend me a hand in solving this problem. you guys are helpful [noparse]:)[/noparse]
  • holyholy Posts: 9
    edited 2007-03-17 06:18
    the reason im asking on how to get the cos or sin is due to my problem...
    k let me start, im using a ping sensor which has an inclination of 34degree, k i need to detect an object in front of me, this part has already been solved, i could calculate the distance accurately.
    However the difficult part start from here on... the ping sensor needs to determine the height of the object in front of me. The ping sensor is mounted 95cm above ground, and inclined downward 34degree. so in order to get the height of the object in front of me, i need to determine the height by subtracting the 95cm with the cos34 multiplied by distance detected from ping sensor to the object. such as 95 - (cmdist * cos 34). this will give the height of the object in theory.

    however. this does not happen when i am implementing the ping sensor into action. for example, the distance from the ping to object or should we call hypotenus is 200, thereforeby implementing theorem pythagoras, sqrt(200^2-95^2)=180cm. this is correct from math and also with the ping. now to determine the height of the object, 95-(200cos34)=-71cm. This is very confusing. and i have experimented with many angle. the answer is the same for the distance. is it caused by the reflection of the ping sensor with the floor?


    can anyone suggest to me how to determine height of the object from this setup?? [noparse]:([/noparse]

    Post Edited (holy) : 3/17/2007 6:24:53 AM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-03-17 07:21
    If the angle is fixed, then your equation becomes much simpler.· If you pre-calculate the COS value, you can do something like this...
    ·
    ·
    COS(34) = 0.829
    ·
    ·
    1) Determine the hypotenuse of your offset height... (using your example 95cm)
    ·
    95cm / 0.829 = 114.596cm
    ·
    2) Read current Ping value in cm and subtract #1 above to determine hypotenuse relative to the objects height (using your example of 200cm)
    ·
    200cm - 114.595cm = 85.404cm
    ·
    3) Convert hypotenuse in #2 to object height
    ·
    85.404cm * 0.829 = 70.8cm
    ·
    ·
    ·
    What is the SIZE of the object you are trying to determine the height on?· Could you mount the PING sensor directly overhead and then use simple subtraction?

    attachment.php?attachmentid=45964

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 3/17/2007 7:51:34 AM GMT
    1103 x 639 - 149K
    PING.JPG 148.9K
  • holyholy Posts: 9
    edited 2007-03-17 08:15
    well.. your setup is not the same as mine. it should look like the pic below.
    the problem is that it can detect the distance accurately but not the height.

    i am stuck to this problem. taking the cos of this problem, 200cos34=165cm which
    is higher than the elevation of the ping which is 95cm. how can this be??
    i am only thinking that the refelcted wave caused this problem.. is it right??
    is there any solution to my problem??
    the object's height to be determined is 20cm. and the ping cannot be positioned as you have stated because the
    object will not be detected if the object is getting nearer the ping.

    Post Edited (holy) : 3/17/2007 8:21:37 AM GMT
    393 x 401 - 17K
  • Shawn LoweShawn Lowe Posts: 635
    edited 2007-03-17 14:25
    I dont think you can use the PING to calculate the height like this. The PING will go high when it sees the base of the object as well as the top, so getting the height of the object is impossible.

    Or so I think.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    My last words shall be - "NOT YET!!!"
  • LSBLSB Posts: 175
    edited 2007-03-17 14:38
    Assuming that the calculations don’t lie there must be something else affecting the result. You suggest it may be reflections, and I wonder if you’ve accounted for the fact that the ping does not emit a well focused beam. The ultrasonic beam propagates as a sphere (or fraction of) and will reflect, not from a point, but as an arc. Is it possible then that the reflection is coming from some unconsidered portion of the object being measured? I should think that the baseline measurement (in your example 200cm) would need to be monitored continually as the object approached and the distance should be measured, not when the object is first detected (while the beam may be reflected off the side of the object to the ‘floor’ and then back to the detector) but only when the distance measurement again becomes stable as the beam is reflected from the top. Or, I should think that the shortest measurement taken as the object passes would be the most accurate (any reflection would add distance).

    I imagine that if the measurement is longer than you believe it should be it doesn’t mean the calculation is incorrect, but rather that the signal is traveling farther than you expect.

    Perhaps it would be helpful to collect raw time data from your device, and then calculate the height using a calculator. Once the math is verified, duplicating it on the Stamp will indicate a calculation error versus a measurement error more surely. Perhaps, too, monitor the measurements ‘real time’ and perhaps see when (if) the calculation is correct.
    269 x 331 - 11K
  • Shawn LoweShawn Lowe Posts: 635
    edited 2007-03-17 14:39
    blush.gif
    Never mind. I'm thinking PIR not PING! Whats a few letters...

    and basis of operation...


    Beau's math is right. The line in his drawing represents where on the object the ping is sending its signal. Your height would be the shortest distance the sensor says it sees.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Shawn Lowe


    My last words shall be - "NOT YET!!!"
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-17 16:51
    I don't understand where the 200 comes from. The right triangle with with height 95 cm, base 64 cm and hypoteneuse 114.6 cm, that is the triangle that subtends an angle of 34 degrees at the top. The measurements should be 115cm, or less as an object enters the beam. Probably less at all times, because of the cone effect that LSB mentioned, and the PING will trigger on the first returning wavefront above threshold. I agree 100% with LSB's comments about collecting readings on the fly to look them over and maybe to look for the shortest distance reading. The signal reflected from a small or many faceted object will be kind of a mush.

    Am I missing something about the 200 cm reading?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • LSBLSB Posts: 175
    edited 2007-03-17 19:04
    My bad-- the 200cm is the hypotenuse of a right triangle with legs 95 and 176 -- per holy's drawing.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-17 20:01
    Not your bad. Okay, it is just that holy's diagram does not add up. That triangle does not have a top angle of 34 degrees. It is more like 61 degrees.

    Here is a diagram with those sides on the left, and the one with 34 degrees at the top on the right. They are very different triangles!!!

    attachment.php?attachmentid=45971

    Which is it, I wonder?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
    581 x 146 - 4K
  • holyholy Posts: 9
    edited 2007-03-18 03:15
    tracy allen is right... i agree with her that i have mistaken taking the degree of the triangle.
    the first diagram of tracy is the right one and bare in mind that the object is moving closer to the ping.

    just curious.... if i add an ir, and it is within the region of the ping, will it effect the measurement of
    the ping and ir? will using ir solve my problem??? or it is just making things worst?

    Post Edited (holy) : 3/18/2007 3:25:59 AM GMT
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-03-22 00:25
    It seems that for measuring the height of the obstacle, the PING that looks more down than out will do a better job of receiving a bounce from the top of the obstacle. How the sound bounces is going to depend on the shape of the object, with angle of reflection = angle of incidence at each tangent to the object and diffracted at sharp corners. The best thing might be two PINGs at two different angles or a PING on a servo turret that could scan back and forth.

    IR and ultrasound measurements shouldn't interfere with one another. However, the IR range finder also works best when it has a flat surface in front of it to look at, and the quality of the surface is even more picky than with ultrasound. (Thinking of the Sharp GP2D12) But that is not to say it can't be done, by scanning the environment with the narrow beam. Here is a nice tutorial that shows some nice results on object avoidence and comparison of ultrasound and IR:
    www.societyofrobots.com/sensors_sharpirrange.shtml

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.