Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Programming a Joystick (http://www.chiefdelphi.com/forums/showthread.php?t=53845)

sbf2009 10-02-2007 14:50

Programming a Joystick
 
We have a basic joystick and don't know how to program the buttons on it, could someone explain or link us to a tutorial?
P.S. Would it be legal to switch joystick ports in the middle of a competition?

paulcd2000 10-02-2007 15:47

Re: Programming a Joystick
 
Well, first off, what are you using EasyC or MPlab?
in MPLab, here's a sample alias: p1_sw_trig

what this means:
"p1" is the port it's plugged into
"_sw_" means it's a switch (you always need
"trig" means the trigger button. There's also "top" (which is the side button), aux1(the top left button), aux2 (the top right button), and wheel (the wheel, but i don't know what it returns)
they all return a 1 or 0 depending on whether the button is pushed or not.

For EasyC... get someone else. I have only used MPLab

EOC 10-02-2007 15:55

Re: Programming a Joystick
 
joystick related code is delt with in user_routines.c. Each button has it's own variable. Joystick positions have two variables, 1 for the x directions and one for the y directions. The values for these positions are mapped like pwm values: 0 is full one direction, 127 means the joystick is in it's neutral position, and 255 means the joystick is being pushed as far as it goes in the other direction. Because of the nature of the values, putting the line of code:

pwm01 = p1_y;

... would be legit, because when the joystick is just sitting there, it doesn't move, and when the joystick is pushed forward/backward all the way, the motor goes as fast as it goes in the appropriate direction. This setup is a bit twitchy, because slight movements in the joystick gets responses from the motor.

Variable names are in the format *joystick_port_name*_*button/axis*.
This means p2_x is the variable dealing with the joystick plugged into the second control port, and the has to do with the x axis directions.

As for unplugging the joystick in the middle of a competion; don't. It confuses the robot, and bad things happen :yikes: .

paulcd2000 10-02-2007 15:59

Re: Programming a Joystick
 
Quote:

Originally Posted by EOC (Post 575545)
This setup is a bit twitchy, because slight movements in the joystick gets responses from the motor.

you can write a function to cancel out values near the middle range. we call ours, "DEADZONE":
Code:

unsigned char Deadzone(unsigned char x){
        if ( (x <= (127 + DEADZONE)) && (x >= (127 - DEADZONE)) ) // if variable is within DEADZONE, stop motor
                return STOP;
        else
                return x;
}

you need to #define deadzone as something, around 15-20
just implement it:
Code:

p1_y = Deadzone(p1_y);
  p1_x = Deadzone(p1_x);
  p2_y = Deadzone(p2_y);
  p2_x = Deadzone(p2_x);
  p3_y = Deadzone(p3_y);



All times are GMT -5. The time now is 13:17.

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