We recently got CAN working on our test platform. I hope we can save all of the c++ teams a bit of time with this:
One Black Jaguar
#include "WPILib.h" //includes everything
/**
WORKING CAN JAGUAR - CYBERHAWKS 706
Built from simple template
*/
class RobotDemo : public SimpleRobot
{
RobotDrive myRobot; // robot drive system, NOT USED
Joystick stick; // only joystick
CANJaguar jag;
public:
RobotDemo(void):
myRobot(3, 4), // these must be initialized in the same order
stick(1), // as they are declared above.
jag(1, CANJaguar::kSpeed)
//jag(1)
{
myRobot.SetExpiration(0.1);
}
/**
* Drive left & right motors for 2 seconds then stop, NOT CAN
*/
void Autonomous(void)
{
myRobot.SetSafetyEnabled(false);
myRobot.Drive(0.5, 0.0); // drive forwards half speed
Wait(2.0); // for 2 seconds
myRobot.Drive(0.0, 0.0); // stop robot
}
/**
* Runs the motors with arcade steering.
*/
void OperatorControl(void)
{
//Set up CAN
myRobot.SetSafetyEnabled(false);//on a dev board
jag.SetPID(0.4, 0.005, 0.0);//PID
jag.ConfigEncoderCodesPerRev(200);//This can change
jag.SetSpeedReference(CANJaguar::kSpeedRef_Encoder);//NEED
jag.EnableControl();//NEED
DriverStationLCD *d_st = DriverStationLCD::GetInstance();//DS
while (IsOperatorControl())
{
//myRobot.ArcadeDrive(stick); // simple template
//jag.Set(20);
jag.Set(stick.GetY()*150.0);//Set Jag
//The rest is DS output
//printf("%f
",stick.GetY());
//printf("jag temp %f speed %f %f %f ref=%d
",jag.GetTemperature(),
// jag.GetSpeed(),jag.GetOutputVoltage(),
// jag.GetOutputCurrent(), jag.GetSpeedReference());
d_st->Printf(DriverStationLCD::kUser_Line1,1,"speed = %3.2f ", jag.GetSpeed());
d_st->Printf(DriverStationLCD::kUser_Line2,1,"set speed = %3.2f ", stick.GetY()*150.0);
d_st->Printf(DriverStationLCD::kUser_Line3,1,"current = %3.2f ", jag.GetOutputCurrent());
d_st->UpdateLCD();
Wait(0.005); // wait for a motor update time
}
}
};
START_ROBOT_CLASS(RobotDemo);
Master/Slave:
#include "WPILib.h"
/**
* CAN JAGUAR
* Uses A master slave setup. We are using 2CAN controller connected to the bridge. The master jaguar has an encoder. The slave does not. The slave gets output from the master.
*/
class RobotDemo : public SimpleRobot
{
//RobotDrive myRobot; // drive system
Joystick rightstick; // right joystick
Joystick leftstick;// left joystick
CANJaguar jag1; //Jaguars
CANJaguar jag2;
public:
RobotDemo(void):
//old my robot //myRobot(3, 4), // Reminants of Simple template
rightstick(1), //right stick
leftstick(2), //left stick
//Declare CAN jaguar channels
jag1(1, CANJaguar::kSpeed),//jag master left
jag2(2, CANJaguar::kVoltage)//jag slave left
{
jag1.SetExpiration(0.1);
jag2.SetExpiration(0.1);
}
void Autonomous(void)
{
//none
}
void OperatorControl(void)
{
//Jag 1 set up
jag1.SetSafetyEnabled(false);//This is on a dev board
jag1.SetPID(0.4, 0.005, 0.0);//PID
jag1.ConfigEncoderCodesPerRev(200);//Changes per encoder
jag1.SetSpeedReference(CANJaguar::kSpeedRef_Encoder);//This is on a dev board
jag1.EnableControl();
//jag 2 set up
jag2.SetSafetyEnabled(false);//This is on a dev board
jag2.EnableControl();
//DS stuff
DriverStationLCD *d_st = DriverStationLCD::GetInstance();
while (IsOperatorControl())
{
jag1.Set(rightstick.GetY()*150.0);
jag2.Set(jag1.GetOutputVoltage());
//printf("%f
",stick.GetY());
//printf("jag temp %f speed %f %f %f ref=%d
",jag.GetTemperature(),
// jag.GetSpeed(),jag.GetOutputVoltage(),
// jag.GetOutputCurrent(), jag.GetSpeedReference());
d_st->Printf(DriverStationLCD::kUser_Line1,1,"speed = %3.2f ", jag1.GetSpeed());
d_st->Printf(DriverStationLCD::kUser_Line2,1,"set speed = %3.2f ", rightstick.GetY()*150.0);
d_st->Printf(DriverStationLCD::kUser_Line3,1,"current = %3.2f ", jag1.GetOutputCurrent());
d_st->UpdateLCD();
Wait(0.005); // wait for a motor update time
}
}
};
START_ROBOT_CLASS(RobotDemo);