View Single Post
  #5   Spotlight this post!  
Unread 29-03-2004, 00:17
Jay Lundy Jay Lundy is offline
Programmer/Driver 2001-2004
FRC #0254 (The Cheesy Poofs)
Team Role: Alumni
 
Join Date: Jun 2001
Rookie Year: 2001
Location: Berkeley, CA
Posts: 320
Jay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to allJay Lundy is a name known to all
Re: Speed limiting code from last year in C?

Looks good, though I would suggest you take advantage of signed math in the following way.
Code:
 int tempPwm01;
 int topSpeed = 127;
 
 tempPwm01 = (int)pwm01 - 127; //0 center [-127 to 127]
 tempPwm01 = tempPwm01 * topSpeed / 254; //Reduce range to
					 //[-topSpeed / 2 to topSpeed / 2]
 pwm01 = tempPwm01 + 127; //127 center
I did wrote it spread out like that to make it easier to understand, but it can all be reduced to 1 statement:
Code:
pwm01 = 127 + ( ((int)pwm01 - 127) * topSpeed / 254 );
As long as you are sure topSpeed will never exceed 254 and will never go below 0 you don't need to min/max anything.

By the way, just to confirm that this is what you are trying to achieve. The above code will turn a pwm value of 254 into 127 + (topSpeed / 2) and a pwm value of 0 into 127 - (topSpeed / 2). It's the same as the C code that you posted but I just want to make sure that's what you want.