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