View Single Post
  #2   Spotlight this post!  
Unread 12-02-2008, 00:11
Chaos in a Can Chaos in a Can is offline
Joel Spadin
FRC #1736 (Robot Casserole)
Team Role: Programmer
 
Join Date: Mar 2007
Rookie Year: 2007
Location: Peoria, IL
Posts: 58
Chaos in a Can is an unknown quantity at this point
Send a message via MSN to Chaos in a Can
Re: problem with printing float

printf does not support %f.
Something like printf("%ld", (long)(ratio * 1000) ); might work, though you're likely to run into a lot of problems with floats.

You can avoid floats entirely by shifting the number up to store your decimal places, but in an integer type. Then in calculations, shift it back down.

For example:
Code:
long ratio;
ratio = (((long)p1_x)<<8 ) / S_PWM_SWING;

if(ratio>128 && magnitude_sum>MAX_SINGLE_MOT_MAG){
		RIGHT_WHEEL_PWM=NEUTRAL+MAX_SINGLE_MOT_MAG;
		LEFT_WHEEL_PWM=NEUTRAL-(((1<<8 - ratio) * MAX_SINGLE_MOT_MAG)>>8);
    }
Keep in mind that bit shifts only work on integers, so 0.5<<8 actually becomes 0<<8, so instead of 128, you get 0. You'll have to enter any decimal constants yourself.

(In case you don't already know, x<<8 = x*(2^8) and x>>8 = x/(2^8).)
__________________
2006 Xerox Creativity Award
2007 General Motors Industrial Design Award
2008 Judges' Award
2009 Chrysler Team Spirit Award