View Single Post
  #7   Spotlight this post!  
Unread 11-02-2006, 16:05
Jared Russell's Avatar
Jared Russell Jared Russell is offline
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,078
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: issues limiting a joystick input

Almost. It is MUCH easier to re-center the joystick around 0 first:

int p3_x_mod = (int)p3_x - 127;

You should then fix the "b" part of y=mx+b to 127 so that no matter what, not moving the joystick is neutral. Also, with integers, (11/51) will be evaluated first as ZERO, screwing everything up. Using p3_x_mod, which is an int, in the expression below also makes sure that you don't overflow the size of an unsigned char.

Try:

pwm16 = (11*p3_x_mod/51) + 127;

Your final code would look something like:

Code:
int p3_x_mod;
...

if( p3_sw_top )
{
  p3_x_mod = (int)p3_x - 127;
  pwm16 = (11*p3_x_mod/51) + 127;
}
else
{
  pwm16 = p3_x;
}
The code you wrote works fine on paper, but it suffers from both the rounding to zero and size overflow problems.

Last edited by Jared Russell : 11-02-2006 at 16:09.