Case, IF, and Floating point
cnelson700
Posts: 31
I'm having an issue with some code that I wrote.
My goal is to:
1)·get a distance measurement from an IR distance measurement device (distled21a) - this is the hypotenuse of my imaginary triangle
2) calculate the z (distled21) from front surface of UGV to object - this is the adjacent side of my imaginary triangle
3) calculate the x (distled21x) of the point measured by the IR setup - this is the opposite side of my imaginary triangle
4) If the calculated z is less than 50 inches display the z on serial terminal (at the position determined by step 5 below)
5) if the calculated x is 0..3 Inches to the left of·the physical source point AND the z is·LESS THAN·previsous z for that 0..3 inch to the left area, display the z on serial terminal at cursor coordinates for the 0..3 zone (I am naming the 0..3 inch area 'zone12')
Number 5) above is not working and I don't know why.· You will also notice code for 'zone11' that is using the same code with different variables and constants.· I have zone1 through zone12 written this way and, of course, none of them work either.
Here is the code:
*****************************************************
·························
············· f.ffloat(distled21a)
············· f.ffloat(distled21)
··············f.ffloat(distled21x)
············· distled21a := f.fmul((f.cos(f.radians(servoangle))),(f.fmul((f.fsub((f.fdiv(1.0, (f.fadd(f.fmul(0.09, (f.fdiv((f.fmul((f.ffloat(dist2)),3.3)),4096.0))),0.07)))),2.9)),0.3)))
············· distled21 := f.fmul((f.cos(f.radians(27.0))), distled21a)
············· distled21x := f.fmul((f.tan(f.radians(servoangle))),distled21a)
············· case f.fround(distled21)
·················· 0..50 : case f.fround(distled21x)
······························· 0..3 : if f.fround(distled21) < zone12
········································· zone12 := f.fround(distled21)
········································· cursor(36,5)
········································· ser.str(num.ToStr(zone12, num#ddec))
······························· 4..6 : if f.fround(distled21) < zone11
········································· zone11 := f.fround(distled21)
········································· cursor(33,5)
········································· ser.str(num.ToStr(zone11, num#ddec))
*******************************************************
This code is tested, true, and beautiful EXCEPT for the·part after the "0..50 : case" where I try to put the calculated z in a zone determined by the x position if it is less than what is already in that zone.
It appears to me that I may not be using the 'if' command correctly.
I also tried to put parenthesis around the f.fround(distled21) on the 'if' line like this and it did not work:
······························· 0..3 : if (f.fround(distled21)) < zone12
········································· zone12 := f.fround(distled21)
········································· cursor(36,5)
········································· ser.str(num.ToStr(zone12, num#ddec))
Any thoughts, comments?
My goal is to:
1)·get a distance measurement from an IR distance measurement device (distled21a) - this is the hypotenuse of my imaginary triangle
2) calculate the z (distled21) from front surface of UGV to object - this is the adjacent side of my imaginary triangle
3) calculate the x (distled21x) of the point measured by the IR setup - this is the opposite side of my imaginary triangle
4) If the calculated z is less than 50 inches display the z on serial terminal (at the position determined by step 5 below)
5) if the calculated x is 0..3 Inches to the left of·the physical source point AND the z is·LESS THAN·previsous z for that 0..3 inch to the left area, display the z on serial terminal at cursor coordinates for the 0..3 zone (I am naming the 0..3 inch area 'zone12')
Number 5) above is not working and I don't know why.· You will also notice code for 'zone11' that is using the same code with different variables and constants.· I have zone1 through zone12 written this way and, of course, none of them work either.
Here is the code:
*****************************************************
·························
············· f.ffloat(distled21a)
············· f.ffloat(distled21)
··············f.ffloat(distled21x)
············· distled21a := f.fmul((f.cos(f.radians(servoangle))),(f.fmul((f.fsub((f.fdiv(1.0, (f.fadd(f.fmul(0.09, (f.fdiv((f.fmul((f.ffloat(dist2)),3.3)),4096.0))),0.07)))),2.9)),0.3)))
············· distled21 := f.fmul((f.cos(f.radians(27.0))), distled21a)
············· distled21x := f.fmul((f.tan(f.radians(servoangle))),distled21a)
············· case f.fround(distled21)
·················· 0..50 : case f.fround(distled21x)
······························· 0..3 : if f.fround(distled21) < zone12
········································· zone12 := f.fround(distled21)
········································· cursor(36,5)
········································· ser.str(num.ToStr(zone12, num#ddec))
······························· 4..6 : if f.fround(distled21) < zone11
········································· zone11 := f.fround(distled21)
········································· cursor(33,5)
········································· ser.str(num.ToStr(zone11, num#ddec))
*******************************************************
This code is tested, true, and beautiful EXCEPT for the·part after the "0..50 : case" where I try to put the calculated z in a zone determined by the x position if it is less than what is already in that zone.
It appears to me that I may not be using the 'if' command correctly.
I also tried to put parenthesis around the f.fround(distled21) on the 'if' line like this and it did not work:
······························· 0..3 : if (f.fround(distled21)) < zone12
········································· zone12 := f.fround(distled21)
········································· cursor(36,5)
········································· ser.str(num.ToStr(zone12, num#ddec))
Any thoughts, comments?
Comments
You might read the following:
http://forums.parallax.com/showthread.php?p=765024
Does f.fround return an integer? I seem to remember having trouble with this myself.
As for posting samples of your code, place your code between these two tags except remove ALL of the empty spaces
[noparse][[/noparse] code ]
[noparse][[/noparse] / code ]
The f.fround, in the float32 object, will round the floating point number to an integer.
This code works:
The only thing that I see different from the non-working code is the lack of the 'if' statement.
Changing funny to 2.4 lights up the other pattern. What are the actual values of zone12 and distled21 at the point when you do the comparison (e.g. printed as 8 digit hex integer)?
I use "if f.fround(valuea) < valueb" , where valuea is a float and valueb is an integer, like your code example.
zone12 is initialized at the beginning of the method with a value of 99.
distled21 is the distance in inches of an object from the front of my bot to the object and that range is 6 to less than 1000.
·
and tell me what it displays?
num is the numbers object
You would like to see the values of distled21 and zone12 displayed?
If so, will this work for your purposes?
[noparse][[/noparse]code]
··0..3: ser.str(num.ToStr(distled21, num#ddec))
······· ser.tx(13)
······· ser.str(num.ToStr(zone12, num#ddec))
······· ser.tx13
······· if f.fround(distled21) < zone12
················ zone12 := f.fround(distled21)
················ cursor(36,5)
················ ser.str(num.ToStr(zone12, num#ddec))
[noparse][[/noparse]/code]
Post Edited (cnelson700) : 4/27/2010 3:51:07 AM GMT
I got these values displayed:
distled21 in binary: 1000001000111111101010100100100
distled21 in decimal: 1,092,605,220
f.fround distled21 in dec: 10
zone12 in dec: 5
distled21x in dec: 3
but nothing was displayed at cursor(36,5) where the 'if' statement is supposed to make the rounded decimal value of distled21 display (if it is in the 0..3 inch zone of x and less than the previous z).
Post Edited (cnelson700) : 4/27/2010 5:23:15 AM GMT
The 5 for zone 12 came from another IR measurement.
I actually have an array of 15 IR led measurement devices for a field of view of 75 degrees from top to bottom and they are on a servo that moves in 5 degree increments from right to left for a field of view of about 80 degrees right to left.
I am building an variable array of 3 inch wide zones in front of my bot to use in obstacle avoidance (the Y coordinates don't matter because the bot can only move left or right, not over and under).
There should be a number displayed for zone12 if there is an obstacle 50 inches or less from the front of the bot and the obstacle is 0..3 inches left of the physical location of the IR measurement device. In the above case I would expect to see 5 in the space for zone12. If distled21 measures an object at 10 inches in zone12 and another distled measures an object at 5 inches in zone12, then I want to see the closest measurement displayed at the zone12 space. But there was nothing displayed in the space for zone12.
I am not sure what to do, but I heard shoving a sharpened pencil deep into your own eye socket won't help.
I am not sure why I had to do it this way, but I am very happy that it did work.· Now on to the next task.....
Thanks for your help, your posts did help guide my troubleshooting and ultimate success.
BTW, kuroneko, what is INA?
c