Log in

View Full Version : Wondering if anyone else is having an issue with Joysticks


mjcoss
14-01-2009, 14:29
I'm writing some code to help the team prototype some drivetrain ideas. As a quick hack, I defined 2 joysticks and was going to use the joystick triggers to control some steering motors. I'm using the WindRiver workbench, and I used the simple template. In the OperatorControl() function I created 2 Joystick objects, and used GetTrigger(). Doing something like this:


Joystick *left_stick, *right_stick;
left_stick = Joystick::GetStickfromPort(1);
right_stick = Joystick::GetStickfromPort(2);

while(isOperatorControll()) {
if(left_stick->GetTrigger()) {
left_steer.Set(.5);
right_steer.Set(.5);
}
else {
left_steer.Set(0.0);
right_steer.Set(0.0);
}

if(right_stick->GetTrigger()) {
left_steer.Set(-.5);
right_steer.Set(-.5);
}
else {
left_steer.Set(0.0);
right_steer.Set(0.0);
}
}

And yes I know that both triggers down isn't being handled.

The odd thing is that the left_stick trigger works just fine (turning on/off the motors), but the right_stick trigger doesn't work at all. Commenting out the left_stick GetTrigger() block of code, and now the right trigger starts working. I've tried allocating the joystick on the stack, using new directly, etc. Bottom line is that I can get only one trigger working at a time. I'm using the KOP joysticks. I'm going to hook up a serial port and put some debug prints but was curious if anyone else is seeing the same thing. I do know that the the GetAxis functions are working because, using the same declarations for the joysticks, I was able to get the TankDrive() function to work properly.

Eric Finn
14-01-2009, 18:53
Try this:
Joystick *left_stick, *right_stick;
left_stick = Joystick::GetStickfromPort(1);
right_stick = Joystick::GetStickfromPort(2);
float SetValue = 0;

while(isOperatorControll()) {
SetValue = 0;
if(left_stick->GetTrigger()) SetValue = .5;
if(right_stick->GetTrigger()) SetValue = -.5;
left_steer.Set(SetValue);
right_steer.Set(SetValue);
}

I recall something about only being able to set speed controllers every (5 or 10, not sure exactly how many) milliseconds. I'm not sure if the Set method will prevent you from setting a motor speed too often, or if that is handled in the FPGA. If this is handled in the Set method, then this should work where the code you showed us didn't.

byteit101
14-01-2009, 19:00
You don't have to declare them like this
Joystick *left_stick, *right_stick;
left_stick = Joystick::GetStickfromPort(1);
right_stick = Joystick::GetStickfromPort(2);
You can do the same like this:
Joystick left_stick(1);//type joystick, variable name left_stick, port 1
Joystick right_stick(2);

mjcoss
15-01-2009, 10:52
I recall something about only being able to set speed controllers every (5 or 10, not sure exactly how many) milliseconds. I'm not sure if the Set method will prevent you from setting a motor speed too often, or if that is handled in the FPGA. If this is handled in the Set method, then this should work where the code you showed us didn't.

I'm an idiot. :ahh:

Obvious logic problem with the second logic block, which will stop the motor via the else clause because only one trigger was pushed, at a time. And you get the results I was seeing. One trigger works. As a side note, I had already changed to code to something similar to what you proposed, and it does indeed work because it removes the logic flaw, and has nothing to do with the frequency of the call to the set method. Although, now I'm curious about timing issues with the OperatorControl() function. Do you need to control update rates?

And to byteit101, I'm aware that there are several methods, no pun intended, for declaring and allocating variables. One is allocated on the stack, the other is a pointer and is allocated in memory. Either method works just fine.