View Single Post
  #2   Spotlight this post!  
Unread 02-02-2013, 16:34
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: C++ Tank Drive Help

In C++, you can't initialize non-const member variables at declaration. They need to initialized in the constructor. An easy way to do this is to use C++ initialization lists (more info on which can be found here).

Instead of writing your code like this:
Code:
RobotDrive drivetrain(1, 2);
Joystick leftStick(1);
Joystick rightStick(2);

public:
RobotDemo()
{
    GetWatchdog().SetEnabled(false);
}
It would look more like this:
Code:
RobotDrive drivetrain;
Joystick leftStick;
Joystick rightStick;

public:
RobotDemo() : drivetrain(1, 2), leftStick(1), rightStick(2)
{
    GetWatchdog().SetEnabled(false);
}
Instead of initializing the variables when they are declared, we initialize them in a list after the name of the constructor. This should solve the compiler errors.

Also, if you want to access the second joystick on the XBox controller, you need to use the GetRawAxis() function instead of declaring a second joystick. This also means that you have to pass the values of the two axes to the TankDrive function instead of the joystick object. The button and joystick mappings for the XBox controller can be found in this post, but I'll copy the relevant portion below:

Quote:
  • 1: Left Stick X Axis
    -Left:Negative ; Right: Positive
  • 2: Left Stick Y Axis
    -Up: Negative ; Down: Positive
  • 3: Triggers
    -Left: Positive ; Right: Negative
  • 4: Right Stick X Axis
    -Left: Negative ; Right: Positive
  • 5: Right Stick Y Axis
    -Up: Negative ; Down: Positive
  • 6: Directional Pad (Not recommended, buggy)
For this example, if we wanted to have the XBox left Y axis control the left side of the tank drive and the right Y axis control the right side, it would look like this:

Code:
drivetrain.TankDrive(joystick.GetY(), joystick.GetRawAxis(5);
Here's the whole file for reference
Code:
#include "WPILib.h"

class DemoRobot : public SimpleRobot
{
	RobotDrive drivetrain;
	Joystick xbox;
public:
	DemoRobot() :
		drivetrain(1, 2), xbox(1)
	{
		GetWatchdog().SetEnabled(false);
	}
	
	void Autonomous() {}
	
	void OperatorControl()
	{
		while (IsOperatorControl()) {
			drivetrain.TankDrive(xbox.GetY(), xbox.GetRawAxis(5));
			Wait(0.005);
		}
	}
};

START_ROBOT_CLASS(DemoRobot);
Hope this helps. If you have any more questions, feel free to ask

- Domenic
Reply With Quote