Log in

View Full Version : Help with implementing Joysticks into C++ command based code


Ebslgpy
02-02-2015, 00:55
Hello, I am one of the two programmers in a rookie team starting out this year. We have never programmed for a robot before, but did have experience with C++. We successfully connected commands to joystick buttons, but do not know how to declare the axis of the joysticks in our code (we are using 2 joysticks). Any help in regards of that and how to code the joystick to control the drive train would be appreciated.

P.S. Please do ask if the above paragraph was not clear enough. I hope we can learn more through communicating with experienced programmers.

kylelanman
02-02-2015, 07:39
Assuming you already have the joystick defined in your OI class you can access joystick axis in a command like this.

#ifndef DriveCommand_H
#define DriveCommand_H

#include "../CommandBase.h"
#include "WPILib.h"

class DriveCommand: public CommandBase
{
public:
DriveCommand();
void Initialize();
void Execute() {
driveTrain->Crab(oi->GetDriverStick()->GetRawAxis(1),
oi->GetDriverStick()->GetRawAxis(2),
oi->GetDriverStick()->GetRawAxis(3));
}
bool IsFinished(){
return false;
}
void End();
void Interrupted();
};

#endif

In this particular command, driveTrain has been defined in CommandBase as a static. DriveTrain has a method defined called Crab that is passed float parameters. In this case we are passing in 3 joystick axis.

Optionally you may want this command to be running on the subsystem as the default behavior when no other command is running. To achieve this ensure you have an InitDefaultCommand() method defined in your subsystem that calls SetDefaultCommand(new DriveCommand);


void DriveTrain::InitDefaultCommand() {
SetDefaultCommand(new DriveCommand());
}


You might want to read through http://wpilib.screenstepslive.com/s/4485/m/13810/c/88685. It tells you everything you need to know about command based programming.

JohnSmooth42
03-02-2015, 22:13
The GearsBot example project also has some very good examples on how to do this.