Joystick Assigning/ Change to Tank Drive

Hello
Our team next year will be using C++ instead of LabView, so we’re learning C++ now.
I am not sure in the MyRobot.cpp file how you assign a joystick to be on usb 1 or 2. Or atleast different from the original joystick in the file. I’ve added this line under the first joystick:

Joystick *stick2; //Right joystick

Will this make it different than the first? And for LabView users, would the “stick2” in that line be equivalent to a RefNum names? I’ve also changed the arcade mode line to:

myRobot->TankDrive(stick,stick2); //

Will this use the two joysticks to drive each side? Also I’m not sure how you tell which PWM you use for each Victor or Jaguar.

A lot of questions, so I thank you for reading it and for any help and clarificaition you can give me.
D. Wirth 2509

The sample programs are an excellent resource for looking at how all this works

For your specific question, no the names have no bearing on what port they are on. In fact, with what you have right now your code will not work and will crash as soon as you start Teleop (possibly sooner)

This must be in the class constructor:

stick2 = new Joystick(2);

Put that below the one for the first joystick. What this does is allocate the memory and create the actual Joystick that you will be interacting with. The first statement only declares that the stick2 variable exists and is a pointer to a Joystick object.

To assign usb ports, simply change the number inside the parenthases in “new Joystick(2)” to the port number and it will assign the port

EDIT: missed the question about PWM

There are 2 ways that the Victor/Jaguar ports are defined. Both occur when the “new RobotDrive(…);” happens.

  1. (cannot be used with CAN controllers)
drive = new RobotDrive(1,2);

This tells the drive object that the motor on the left is PWM port 1, and right is PWM port 2. A 2nd version of this exists as well, defining a 4 wheel drive system (2 independent motors on each side). The declaration, instead of 2 numbers, has 4 numbers, which are the following: front left motor, rear left motor, front right motor, rear right motor.

  1. (CAN-compatible, PWM works too)
leftJag = new CANJaguar(1);
rightJag = new CANJaguar(2);
drive = new RobotDrive(leftJag, rightJag);

This version, instead of creating the motor objects itself, has you create the objects and give them to the RobotDrive (useful if you switch the RobotDrive on and off during a match to manually run the motors instead for some reason). The constructor can take a Victor, Jaguar, or CANJaguar object, as well as anything else you come across that inherits the SpeedController class. This also has a 4-motor variant identical to the one above

the stick2 would be the “refNum” names. But if you are serious about C++, call them objects or instances.
//note: the lines (or ends of lines) that start with a // are comments
//They don’t change the functionality of the code, but add to it’s understanding, and cleanness if used correctly (code below not an example)

class RobotDemo : public SimpleRobot //this is required, 
{
//This area is where you put objects
   		RobotDrive driver;
		Joystick stick;
		Joystick stick2; //new Joystick

	public:
		RobotDemo() :
//this is the "init.vi" area
//this is where you assign pwm ports, analog ports, usb ports, etc...

			driver(1,2),//creates the RobotDrive (a group of 2 or 4 Jaguars (but victors work as well)
//as having 2 motors, on PWM 1 and 2 (left and right


 stick(1), stick2(4)
//stick is on usb 1, stick 2 is in usb 4

 // these must be initialized in the same order
// as they are declared above.
//Note, the inits are a list of, comma, seperated,     and,space,and, comments,  as, nesescearry, but, the, last, item, does, not, end, with, a, comma
		{
			GetWatchdog().SetExpiration(0.9);
		}
		
//Code ommited		

Yes, the TankDrive is what you are expecting
I also notice you are using pointers (Joystick *joy vs Joystick joy)
the init area would be different:

class RobotDemo : public SimpleRobot //this is required, 
{
//This area is where you put objects
   		RobotDrive *driver;
		Joystick *stick;
		Joystick *stick2; //new Joystick

	public:
		RobotDemo() :
		{

//this is the "init.vi" area for pointers (after { above

			driver = new RobotDrive(1,2);//creates the RobotDrive
//as having 2 motors, on PWM 1 and 2 (left and right

//note: this is not a comma sep. list, each thing ends with a semicolin ;


 stick = new Joystick(1);
stick2 = new Joystick(4);
//stick is on usb 1, stick 2 is in usb 4

			GetWatchdog().SetExpiration(0.9);
		}
		
//Code ommited		

Also note, you can have a mix of the two

One more point:
if using pointers:
stick->GetY();
if not using pointers:
stick.GetY();

One other tip:
Press Ctrl+Space for help completing or finding an item (ex: GetTwist). It is automatically launched for . -> and (

One more thing. I’d reccomend doing a google search about “object oriented programming” and possibly append c++ to the end of it. A lot of these things will make much more sense if you understand the basics of an OO language (which this year’s labview is not)

Just to add clarification to what byteit said, about the leftStick.GetY();

For the tank drive function accepts 2 float values.

What you said, is myRobot->TankDrive(stick,stick2); This is passing the 2 joysticks themselves, which would not work. Instead, you should pass their Y Values.

leftStick.GetY() returns a float value from -1(all the way foward) to 1 (all the way backwards).

So in order to drive the robot, (assuming everything else is correct) you would do this:

myRobot.TankDrive( leftStick.GetY(), rightStick.GetY() );

Note: You may need to make one of those values negative (put a “-” infront of one, such as -leftStick.GetY()) so the robot will drive straight when pushed full fowards, not turn.

There are five overloaded TankDrive methods in RobotDrive.h. One of them takes two joystick pointers. Another one takes two jockstick references. So TankDrive(stick, stick2) would work.


void TankDrive(GenericHID *leftStick, GenericHID *rightStick);
void TankDrive(GenericHID &leftStick, GenericHID &rightStick);
void TankDrive(GenericHID *leftStick, UINT32 leftAxis, GenericHID *rightStick, UINT32 rightAxis);
void TankDrive(GenericHID &leftStick, UINT32 leftAxis, GenericHID &rightStick, UINT32 rightAxis);
void TankDrive(float leftValue, float rightValue);

Ah! I stand corrected, nevermind!