|
Re: issues limiting a joystick input
Let's do some basic arithmetic here.
Right now, joystick input J maps to PWM output O on a 1-1 basis; that is, your transfer function is:
O = J;
You want to redefine this, so imagine the above equation as a line with slope 1. Slope*J is going to be your new PWM value. Intuitively, this would yield:
O = SLOPE * J;
However, you have to take into account the fact that the motor and joystick is centered around 127:
signed int J_S;
...
J_S = J - 127;
O = SLOPE * J_S + 127;
Thus, to have the max PWM value be 190, you would do:
signed int J_S;
...
J_S = J - 127;
O = J_S / 2 + 127;
Since 127/2+127 = 190.5 ~= 190. Because of the integer math on the PIC, you can reduce this to the general form:
#define SLOPE_NUM 1
#define SLOPE_DEN 2
signed int J_S;
...
J_S = J - 127;
O = J_S * SLOPE_NUM / SLOPE_DEN + 127;
Now simply manipulate SLOPE_NUM and SLOPE_DEN to come up with the slope you want (actual slope is SLOPE_NUM/SLOPE_DEN).
This lets you limit the PWM to whatever you'd like while still giving your joystick a full range of motion.
Last edited by Jared Russell : 11-02-2006 at 15:51.
|