Operator Control Help

I’m a total newbie at programming,

so I want to know how to give motors different speed based on the position of the analog sticks on the xbox 360 Controller (in Mode 1)

and how to use the Right Trigger on the controller(which I mapped to “trigger”), to extend a pneumatic arm when pressed, and retract it when released.

Also how to control a motor using buttons on the controller (one for up, another for down)

Thanks in advance

for the the trigger have an if statement that checks the trigger and the else would release it

use the IFI dachboard to see what the analog sticks on the joystick are and then pull that value into the motors
the code would look like
/* this grabs the y axis value of the port 1 joystick and makes the pwm # 1 equal to it

Check out this for now.

I’m in the lab working right now, but if when I get home nobody has answered this in detail, I will address all of your questions :]

You also want to make sure that you are using analog values and that neutral is at 127, meaning nothing is moving, the joysticks are in their normal position. 255 should be max (or at least near it), so going full speed forward. 0 should be your full speed reverse, make sure to calibrate your controller for that.

After that, you can use loops (like while, for, or switch) or conditionals like if, else statements.


if(pwm01 == 255)
full speed ahead;

of course that’s pseudo code. At www.ifirobotics.com you can find default code for the controller, that already defines all that for you, so you don’t have to worry about doing all that.

Everything that has been said here so far is good advice. A couple extra points:

  1. While 127 is “true” neutral for a PWM, there is a dead band where the motors won’t respond. This depends on the motor attached to that PWM and the load attached to the motor, so you have to test your code on the completed mechanisms.

  2. As for your “press to extend, release to retract” idea, you need to monitor the trigger for a state change. For example, this snippet should work for what you suggest:

if(trigger == 1){
if(last_trigger == 1){
//remain extended
if(last_trigger == 1){
//remain retracted

Something along those lines would work for your trigger. Keep in mind that the user becomes involved too. If that finger slips, even for an instant, the retraction will occur. It may be possible to stop and reverse that action, but if it is absolutely necessary to keep the pneumatic extended, a switch is the way to go.


The simple answer is, it depends :slight_smile: But i’ll try to lay things out for you in a way that you can figure out from your situation.

First, a brief overview of the controller. If you’re using a 360 controller, then i assume you’re using the USB chicklet from IFI? if so, then this document will help:

The controller is plugged into one of your joystick ports (lets say 1, for the sake of argument). That port has a number of variables associated with it:


Now, if you look at the mappings on page 5 of the document above, you’ll see what actions map to which variables. for example, button 1 maps to p1_trig. So, if you press on button 1 (which ever button that’s assigned to), the value for p1_trig will change to 1. when you release, it’ll go back to 0. That gives you a very easy way to tell when a button is pressed and released. Note that if you have in your code something like:
if (p1_trig == 1)
//do something
then you’ll do that multiple times per button press, because each press is so long that the code loops through several times. This may or may not be a problem for your situation (but it’s easily solvable).

Now, the joysticks are similar. You read those in from their variables. They’ll have a value of 0-255, with 127 indicating the dead center, 255 the extreme positive end, and 0 the extreme negative end.

All of these variables are updated very often (as high as 40 times a second, i believe), so you don’t have to worry about that!

So, once you get your inputs, you need to be able to output them. This is also pretty easy. Your motors will be plugged into one of the PWM ports on the RC. For the sake of argument, we’ll assume they’re in port 01. Conveniently, that maps in the code to a variable called pwm_01. All you have to do is set that variable to whatever speed you want (or even to the joystick input!) It follows the same conventions as the joysticks, with 127 being stop, 0 being full speed reverse, and 255 being full speed forward.

If you’re using KoP materials for your pneumatics, then your piston should be connected through one of the relay ports. Again, i’ll assume it’s port 1. This port actually has two variables associated with it - relay1_fwd and relay1_rev. The tricky thing with working with pneumatics is knowing how the solenoids work. Basically, You can apply power to two sides of it. When you provide power to one side, it opens up the valve in that direction, letting the air flow through that port, and closing the other. provide power to the other side, and the same thing happens over there. provide power to both sides, and you can’t be sure what will happen - they’ll both try to open their side and close the other. Provide power to neither side, and you have the same situation where you don’t know what will happen.

For that reason, whenever you’re working with pneumatics in this fashion, you always want to set the variables together as a pair. To move the piston in one direction, you’ll set relay1_fwd = 1 and relay1_rev = 0. For the other direction, relay1_fwd = 0 and relay1_rev = 1.

As for the motor up and down, it depends on how it’s wired up - those can be wired to either victors or spikes. Luckily for you, those are both controlled the same way as I described above.

So, some very simple code to do what you described (but maybe not exactly what you want, your questions were a little generic) might be:

pwm_01 = p1_x;
//control the piston
if (p1_trig == 1)
    relay1_fwd = 1;
    relay1_rev = 0;
    relay1_fwd = 0;
    relay1_rev = 1;
//control the motor
//I'm leaving this portion up to you to figure out :)

Thanks Everyone for the help!

First time using these forums, and it has been the most helpful.

Thanks again!