Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   issues limiting a joystick input (http://www.chiefdelphi.com/forums/showthread.php?t=43769)

Unholy 11-02-2006 13:27

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 :o
-Danny

CronosPrime1 11-02-2006 13:56

Re: issues limiting a joystick input
 
I don't exactly understand your question - please clarify.

Unholy 11-02-2006 14:13

Re: issues limiting a joystick input
 
Quote:

Originally Posted by CronosPrime1
I don't exactly understand your question - please clarify.

ok. I have a motor running on the p3_x port. The pwm input has a range from 0-255, but I can't use that. What I'd like to have would be to have a range of 100-154 (so to say) while I'm using the joystick, so when it's fully deflected to the highest value, the pwm input would be 154 instead of 255, and when I deflect it to the lowest value, the pwm would be 100 instead of 0. I think I was approaching the problem wrong using multipliers.

Oumonkey 11-02-2006 14:19

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..... :p 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

Jared Russell 11-02-2006 15:49

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.

Calvin 11-02-2006 15:54

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:

if (p3_sw_top == 1)
{
     
pwm16 = ((11/51)*p3_x) + 100;
}
else
{
      
pwm16 p3_x;



Jared Russell 11-02-2006 16:05

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.

Unholy 12-02-2006 19:53

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.


All times are GMT -5. The time now is 01:35.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi