|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
issues limiting a joystick input
I'm trying to limit the speed in a motor through programming in in such a way that when the joystick is fully deflected, i will only get a response of, let's say, 93 instead of 0, and 150 instead of 255. The way the joystick is set to work is that it won't be engaged unless the trigger is closed. Initially, as soon as I clicked the joystick trigger, my value would change automatically to 0 from 127 using a command looking like this:
if (p3_sw_top == 1 ) { pwm03 = (((p3_x) *2) / 10) } Initially I thought I had issues with the trim, but I checked the trim using pwm03 = p3_x;, set it to neutral, went back to the line above, and as soon as I clicked again, my value was 0 again. I'd appreciate any help I can help, and sorry for this noobish question -Danny |
|
#2
|
|||
|
|||
|
Re: issues limiting a joystick input
I don't exactly understand your question - please clarify.
|
|
#3
|
|||
|
|||
|
Re: issues limiting a joystick input
Quote:
|
|
#4
|
||||
|
||||
|
Re: issues limiting a joystick input
I guess this wasn't what ya needed, oh well. You saved me a few hours of anger now. I love you..... j/k but thanks you help me. Now I can finish the rest of my code. we use something similar in our codes. Except ours is opposite, to get it to max. if (pwm16>154) pwm16 = 154; if (pwm16<100) pwm16 = 100; Try that, I don't know much about programming, but ours also has a little trim so if its within 3 or 4 points of 127 it equals 127; if ((pwm16<130)&&(pwm16>124)) pwm16 = 127; I'm pretty sure thats what I have, I don't have my code with me but it should work, if anyone else can point out a mistake go ahead....I almost forgot the semi-colons Last edited by Oumonkey : 11-02-2006 at 21:52. |
|
#5
|
|||||
|
|||||
|
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. |
|
#6
|
|||
|
|||
|
Re: issues limiting a joystick input
so when:
input -> output x -> y 0 -> 100 255 -> 155 Slope: y2-y1/x2-x1 Slope: 155-100/255-0 Slope: (11/51) y = mx + b y = 11/51x + b 100 = 11/51(0) + b b = 100 so: y = (11/51)x + 100 so: pwm16 = ((11/51)*p3_x) + 100; PHP Code:
Last edited by Calvin : 11-02-2006 at 16:01. |
|
#7
|
|||||
|
|||||
|
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;
}
Last edited by Jared Russell : 11-02-2006 at 16:09. |
|
#8
|
|||
|
|||
|
Re: issues limiting a joystick input
thank you to everyone who posted. I really appreciate the help ^_^. I will try some of this stuff when I get back to school.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2006 CMUcam2 Code | Kevin Watson | Programming | 81 | 10-02-2007 20:17 |
| Free programming help | Cuog | Programming | 24 | 24-01-2006 16:43 |
| Change joystick to pwm mapping | Joe Lewis | Programming | 3 | 30-03-2005 19:27 |
| Changing 1 joystick code to 2 (rookie team) | Brawler006 | Programming | 5 | 20-02-2004 17:00 |
| joystick problem | archiver | 2000 | 12 | 23-06-2002 23:08 |