for the camera this year, i’d like to set the PAN and TILT values into degrees. Now im sure there is code already formulated somewhere within kevin code. but i’d rather wrap my mind around how to properly do it:).
Now. my question to u guys. i want to use floating points instead of intergers. i would also like to noe how to round the floating point number to 4 decimals. However. i do know that floating points do take alot of space and processing power! but! the maximum value being produced is 180. ( 180 degrees). If you could leave some input that would be great:)
I am personally using floating points in my code. It hasn’t posed a problem, except that you can expect problems with printf related to floating points. Just multiply it by however many decimal places * 10 and printf that typecast as an integer.
Now, I’ll warn you now, Al S. (I don’t remember all of the last name) is going to give you some heat for using floating points on our non-floating point-native controller… but I see no problems with it…yet. It is bad practice, and I agree with him wholeheartedly, except that I’m too lazy to figure out how to represent what I need as a long/integer. Al, if you have any suggestions, I’d be glad to hear them.
If you’re dealing with the FRC controller, you can’t deal with signed or floats (unless something’s changed), just integers. You’ll have to multiply your numbers by 10^n, n of the number of decimals you want. Please, correct me if I’m wrong, this is my first official year as a programmer.
you can most certainly deal with floating point numbers! just be careful about typecasting, because PIC C defaults to integers… so you have to typecast everything.
multiplying by 180/127 is not a good idea… first of all because the range on the servo is not 180… its either 160 or 210 (i forgot). multiply by the decimal… you wil run into less problems.
i really dont know at this point which way is the right way…
well i was moving the camera box up and down (tilt)… i was able to move it past 180 degrees… and if the values are 210… then the servos are able to move 210 degrees… if im not rite… i ono i could be wrong… im extremely tired…:eek:
If you are only going up to 180 i don’t see why a float is really necessary??? why not just multiply by 100? it’ll still get you two digit accuracy
i.e: 180.00 = 18000
as for rounding a floating point number down to four decimals… it is not too hard to do, but why would you need to do it? it’s not like reducing the number of decimal points will reduce the processing time more than the cost it takes to round the number.
I think the easiest way to round to four decimals would probably be to mask off the bits of the less significant digits using “&” and a masking variable
(look up bitwise operators)
The problem here is that on a processor that supports floating-point math, doing any floating-point arithmetic takes a single assembly operation. On processors that don’t support it (like the RC processor), it has to be done in software. So, each time you do anything with a floating point number, the compiler will end up generating a LOT of code to do it (something like 100 lines of assembly instead of just one). This uses up a lot of extra code space and is obviously a lot slower.
The answer has already been provided here. If you need 1/10 accuracy, just multiply all of your numbers by 10. Also, if you’re trying to multiply something by a fraction, but the result only needs to be an integer, keep this trick in mind. If you’re multiplying by a fraction, you can multiply by the numerator first and then divide by the denominator afterwards to avoid floating point (just be careful of overflows). So when someone here was talking about multiplying by 180/127, do this instead:
The compiler can do some of the floating point calculations you might like to do keep your code readable. Note I said compiler, not the controller. The calculations are done once at compile time, and the resulting long, short, char value is used in the code.
Here is an example using an encoder to control distance moved in autonomous code:
The macro INCHES_TO_COUNTS allows the compiler to do the floating point math once at compile time to determine how many encoder counts are required for the wheel to move 45 inches.
Now assuming a wheel base of 18 inches how would you code the macro for
DEGREES_TO_COUNTS so that we could write:
case 4:
// turn right 90 degrees
Move( MoveRotateCW, 0x20, DEGREES_TO_COUNTS( 90.0 ) );
break;