Working with Joysticks.

This post will introduce the basics of programming for the logitech joysticks in RobotC. I will talk about include files in another post, but for now you need to know that whenever you want to use a joystick with RobotC you need to include the following line in your program.

#include "JoystickDriver.c"

Adding this line to your program gives you access to all of the functions and variables that RobotC needs to communicate with the logitech controllers.  So what do we need to know in order to use the joystick? The most important function you need to be familiar with is called getJoystickSettings,what it does is read in the current information from a joystick to your program each time you call it. The most important variable we need to deal with joysticks is named joystick, this variable is an example of what is called a struct* in C, but for now all you need to know is that it bundles together a set of variables that deal with the current state of the joystick. Let’s look at a simple example of this:

#include "JoystickDriver.c"

task main() {
    motor[left] = joystick.joy1_y1;
    motor[right] = joystick.joy1_y2;

Now I’m sure you are wondering what joystick.joy1_y1, and joystick.joy1_y2 are, and the answer is that they are examples of those variables I mentioned above that are bundled up in joystick. Specifically the value joystick.joy1_y1 corresponds to the y-value of the left joystick on the logitech controller, and respectively the value joystick.joy1_y2 corresponds to the y-value of the right joystick. It is worth noting that joystick.joy1_y1 and joystick.joy1_y2 take on integer values between -128 and 127 (I will talk about types in RobotC in another post).

Now let’s walk through this program line by line and figure out what it does. We already know what lines 1-3 do since we already talked about #include, and we know what task main() is for, so we can start by looking at line 4.


What does this line do? Well it looks sort of like a function call, and that’s exactly what it is, we are applying the getJoystickSettings function to the value joystick, and remember that joystick is one of those struct things that encapsulate a bunch of information about the state of the joysticks we are using. The result of calling the getJoystickSettings function is that the program looks at the current state of the joystick(s) you are using (which buttons are pressed, what value are the joysticks set at, etc…) and stores all of that data inside the joystick struct. For example if we are pushing the right joystick on our controller all the forward then getJoystickSettings(joystick)will cause joystick.joy1_y2 to take on the value -127 (this is just how the joysticks happen to work, and unfortunately it doesn’t quite match up with our expectations). Let’s look at the last lines of the program.

motor[left] = joystick.joy1_y1;
motor[right] = joystick.joy1_y2;

We learned about controlling motors in this post so we know what is happening here is that we are setting the speed of the motors (in percentage) based on the values of the left and right joysticks on the controller (joystick.joy1_y1 and joystick.joy2_y2 respectively).

What happens next in the program?

Nothing, actually. The program just ends after the second motor assignment, clearly this isn’t what we would like to happen, we would prefer if we could continually update the motor speeds based on whether or not we are moving the joysticks. To handle this we will need a new programming construct called a loop. The next post in this series will pick up with a discussion of making things in a program repeat using loops.

*: When I say that joystick is a struct here, what I actually mean is that the documentation doesn’t list the actual type of joystick, but a struct is the only thing that makes sense.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s