Log in

View Full Version : Programming joysticks


NoRemorse
24-01-2004, 17:17
On the new joysticks, there is a mini - milt-direction "d-pad" i guess you could call it. How do you prgram this, or can you even. I can't find it anywhere in the code

Dave Flowerday
24-01-2004, 18:22
On the new joysticks, there is a mini - milt-direction "d-pad" i guess you could call it. How do you prgram this, or can you even. I can't find it anywhere in the code
It's called a "hat switch" and it'll show up as one of the analog joystick values to the robot controller. I can't remember if it appears as "Joystick Wheel" or "Joystick AUX" off the top of my head, though.

NoRemorse
24-01-2004, 18:36
well, the count of buttons on thejoistick does not equal the count of buttons in the program. and it is the odd man out.

FotoPlasma
24-01-2004, 19:14
The joystick's POV hat takes the place of the "pN_wheel", where N is the number of the joystick port you're using. I can only really speak for the joystick which I have right in front of me (the other is at my house, being used to play TIE Fighter :)), so YMMV. The two least significant bits of the value change too erratically to be of any use. Here's a listing of the values of the variable when the POV hat is in different positions:
Neutral: 001011xx
Up Left: 111111xx
Up: 111111xx
Up Right: 111111xx
Left: 011000xx
Right: 110010xx
Down Left: 101001xx
Down: 100101xx
Down Right: 110101xx

The x's take the place of the two least significant bits, which, as I said earlier, change randomly (at least with this single joystick).

Basically, if you can understand binary, you can take advantage of the POV hat for controlling your robot.

Hope this helps.

Smrtman5
27-01-2004, 18:12
I have two basic questions. First, can those old robot interfaces be programed for C or are they strictly the old stuff?

Second, does anyone have an old program with just the two joystick controls? All we want to do is hook up an old interface to a mock control board so we can make sure the trannys work.

(Note: Im the fabricator, the only thing i know about computers is how to break one)
Hopefully i can relay this answer to one of our more knowledgeable people. Thanks

Ryan M.
27-01-2004, 20:31
I have two basic questions. First, can those old robot interfaces be programed for C or are they strictly the old stuff? Do you mean the old robot controllers? If so, they can only do the old PBASIC stuff.

deltacoder1020
27-01-2004, 21:21
The joystick's POV hat takes the place of the "pN_wheel", where N is the number of the joystick port you're using. I can only really speak for the joystick which I have right in front of me
...
Neutral: 001011xx
Up Left: 111111xx
Up: 111111xx
Up Right: 111111xx
Left: 011000xx
Right: 110010xx
Down Left: 101001xx
Down: 100101xx
Down Right: 110101xx

our values for the joystick concur for the first 6 bits as well. (darn shame there is basically no difference between UL, U, and UR)

FotoPlasma
28-01-2004, 05:51
Second, does anyone have an old program with just the two joystick controls? All we want to do is hook up an old interface to a mock control board so we can make sure the trannys work.
InnovationFirst has "Legacy Documentation" on their website (www.innovationfirst.com). They have default code, written in PBASIC (for previous years' control systems), for both single and dual joystick driving. Here's a link (http://innovationfirst.com/FIRSTRobotics/documentation-legacy.htm) to download those programs. They're listed under "PBASIC Code", but you will also need a copy of Parallax's computer program, in order to program your controller. Links are listed on that same website under "Programming Tools".

If you need any more assistance, feel free to ask.

Astronouth7303
28-01-2004, 20:53
So the bit masks are:

const unsigned char Hat_None = 0x2C;
const unsigned char Hat_Up = 0xFC;
const unsigned char Hat_Left = 0x60;
const unsigned char Hat_Right = 0xC0;
const unsigned char Hat_Down = 0x94;

//Unless values are off, Combos aren't OR'ed
const unsigned char Hat_Up_Left = 0xFC;
const unsigned char Hat_Up_Right = 0xFC;
const unsigned char Hat_Down_Left = 0xA4;
const unsigned char Hat_Down_Right = 0xD4;


This seems a little stupid, but that's life. :(

Chris Hleva
01-02-2004, 23:59
I have a question, how could I add the right lines of code to make the POV hats work with the new C language this year? Amonst other things, where would I have to define them and map them to the right IO ports to work with the hex/bin input signal?

Ryan M.
02-02-2004, 11:11
Basically, if you can understand binary, you can take advantage of the POV hat for controlling your robot.
You also have to good at bit manipulation in C! :)

Joe Ross
02-02-2004, 11:15
So the bit masks are:

const unsigned char Hat_None = 0x2C;
const unsigned char Hat_Up = 0xFC;
const unsigned char Hat_Left = 0x60;
const unsigned char Hat_Right = 0xC0;
const unsigned char Hat_Down = 0x94;

//Unless values are off, Combos aren't OR'ed
const unsigned char Hat_Up_Left = 0xFC;
const unsigned char Hat_Up_Right = 0xFC;
const unsigned char Hat_Down_Left = 0xA4;
const unsigned char Hat_Down_Right = 0xD4;


Why not #define them, instead of taking up memory?

deltacoder1020
02-02-2004, 12:45
I have a question, how could I add the right lines of code to make the POV hats work with the new C language this year? Amonst other things, where would I have to define them and map them to the right IO ports to work with the hex/bin input signal?

example:

if( (p1_wheel & 0xC0) == 0xC0 )
{
//hat control is pushed right
}
else if( (p1_wheel & 0xA4) == 0xA4 )
{
//hat control is pushed down-left
}
...
et cetera
...


just use the bitwise AND function (&) to determine if the correct bits are set.

Chris Hleva
02-02-2004, 16:59
Thanks, that helped a lot. I am just starting out with C so i'm still a little sketchy with it.

deltacoder1020
02-02-2004, 17:21
no prob. if you want a review on the syntax of C, you could try the various guides in the whitepapers section, or the guide that I'm working on (see the thread in this forum titled "FIRST Programming Tutorial")

Erico
11-02-2004, 16:15
If u can make me a favor, can u teach me how to program the joysticks. I will appreciated. thank u

GOOD LUCK THIS YEAR

deltacoder1020
11-02-2004, 17:02
the inputs from the joysticks are held in a range of variables (actually aliases), set up in ifi_aliases.h:

for instance, the x-axis of joystick one is p1_x

if you want direct joystick-to-pwm control, just use something like the following line:

pwm01 = p1_x;

Ryan M.
11-02-2004, 17:15
If u can make me a favor, can u teach me how to program the joysticks. I will appreciated. thank u

GOOD LUCK THIS YEAR Just to expanded on what DeltaCoder said, the joysticks are refered to by pX_x and pX_y, where X is the port number the joystick is attched to. For example to access the x and y axises of the port one joystick you would use p1_x and p1_y.

The value that comes in on these pins is between 0 and 255 (an unsigned char). 0 is full to one side, 127 is center, and 255 is full to the other side. For instance, if you have the joystick all the way forward, p1_x would be 255 and p1_y would be 127. If you had it all the way to the right, p1_y would be 127 and p1_y would be 255.

You could also have a combination of the two, of course. For instance, if you where in the forward, right corner, you would get p1_x and p1_y both reading 255.

You can use these values in any way in your program. Just don't try to change their value in code; it won't work!

Hope that helps. :) Good luck.

Astronouth7303
11-02-2004, 18:27
Why not #define them, instead of taking up memory?

I don't like using #define for constants. Besides, we've got memory to kill.

deltacoder1020
11-02-2004, 21:19
I don't like using #define for constants. Besides, we've got memory to kill.

hmm... apparently the forum system managed to place a reply in a different thread... right.

Tridelvior
12-02-2004, 17:54
Do you know the non-binary values of the pX_wheel in their up- and down positions?

I am trying to use their values with a pwm and do not know what to use.

temp = p1_wheel;

if (temp==WHAT VALUE?[UP]){
pwm02=200;
}
else[DOWN]{
pwm02=55;
}

Ryan Cumings
14-02-2004, 23:50
example:

if( (p1_wheel & 0xC0) == 0xC0 )
{
//hat control is pushed right
}
else if( (p1_wheel & 0xA4) == 0xA4 )
{
//hat control is pushed down-left
}
...
et cetera
...


just use the bitwise AND function (&) to determine if the correct bits are set.

Haven't been on for a while, but here goes (Sorry delta!)
If you push the hat up by your code then it will default to the first conditional statement you put in.

A more correct way might be to do it this way (using constants provided courtesy of Astronouth7303)

switch (px_wheel & 0xFC) {
case Hat_Up_Left: {
// ... Code here
break;
}
case Hat_Up_Right: {
// ... Code here
break;
}
// etc...
default: {
break;
}
}

Robert Hafner
15-02-2004, 23:35
Could someone help me out?

What I need is for someone to do out the etc. part of the

if( (p1_wheel & 0xC0) == 0xC0 )
{
//hat control is pushed right
}
else if( (p1_wheel & 0xA4) == 0xA4 )
{
//hat control is pushed down-left
}
...
et cetera
...


thing so I can do this right. I'm a tad but slow when it comes to binary and hex :(

deltacoder1020
16-02-2004, 00:05
here they are in binary, decimal, and hex forms...

Neutral: 001011xx 44 0x2C
Up Left: 111111xx 252 0xFC
Up: 111111xx 252 0xFC
Up Right: 111111xx 252 0xFC
Left: 011000xx 96 0x60
Right: 110010xx 200 0xCB
Down Left: 101001xx 164 0xA4
Down: 100101xx 148 0x94
Down Right: 110101xx 212 0xD4

Guest
16-02-2004, 00:17
Here's a diagram that gives the full range of values and the value to test with AND is bold.

252-255*252-255*252-255
*96-99***44-47**200-203
164-167*148-151*212-215

Robert Hafner
16-02-2004, 00:20
Because the last bits are unreliable (from what i've read), how will this affect the program? Should I stick with a general area? example:

if (p1_wheel >= 42 && p1_wheel <= 46)
// I am neutral
if (p1_wheel >= 250 && p1_wheel <= 254)
// I am upperleft;

deltacoder1020
16-02-2004, 00:30
your previous code with the bitwise AND (&) should work fine - just use the hex or decimal codes I posted above (because you AND it with the original constant, the lower bits don't matter, and thus it will be true for the entire range).

Robert Hafner
16-02-2004, 22:39
at the risk of sounding dumb, how does the AND thing work, and what do I need to do with it?

deltacoder1020
17-02-2004, 00:06
if( (p1_wheel & 0xC0) == 0xC0 )


"&" is the "bitwise AND" operator. that means that it looks at individual bits of a value, and does an AND operation. AND is defined as follows: true if both inputs are true, otherwise false. a logic table:

0, 0 = 0
1, 0 = 0
0, 1 = 0
1, 1 = 1

so as you can see, it will only return 1's in the places where both numbers have ones. thus, it will only return all of the bits of one value if the same bits are 1's in the other value. the if statement essentially checks to see that all of the bits that are 1 in 0xC0 are also 1 in p1_wheel. replace 0xC0 with whatever "bitmask" (set of bits) you want to test as being 1.

iamjarret
24-02-2004, 20:26
hey, i have a question
we plugged a motor to the pwn port that corresponds to p1_wheel(the wheel)
and teh operator interface shows that when we move the wheel, the values are changing (input), but it is not moving the motor. It is correctly mapped (pwm09 = p1_wheel;)

how can i get it to work



also, i would like to make code the made a pwm got to speen 255 when p4_sw_trig (trigger) is pushed(equal to 1).

my code so far is
if (p4_sw_trig == 1)
{
pwm12 = 255; (for this i commented out pwm12 = p4_wheel;)

i also tried p4_wheel = 255;

and i tried p4_wheel = 0xFC;


}

lastly, the y axis dose'nt work on ports 3 and 4 on any joystick i try



please help me fix my code, wheels, and y-axes

thanx