Updating code w/ DifferentialDrive?


I’m on a small rural team with no coding mentors. I know that the RobotDrive class has been deprecated this season, but when I try to implement use of the DifferentialDrive class, various parts of the code errors out. Here is basic TeleOp code that I know worked last season. Can someone walk me through what needs to change in order for this code to be updated with the DifferentialDrive class? Thanks!

#include "WPILib.h" //<-- Parker added this 2/9/17

#include <iostream>
#include <memory>
#include <string>
#include <VictorSP.h>
#include <Joystick.h>
#include <SampleRobot.h>
#include <SmartDashboard/SendableChooser.h>
#include <SmartDashboard/SmartDashboard.h>
#include <RobotDrive.h>
#include <Timer.h>
#include "RobotBase.h"

using namespace frc; //<-- Parker added this 2/9/17
using namespace std;

 * This is a demo program showing the use of the RobotDrive class.
 * The SampleRobot class is the base of a robot application that will
 * automatically call your Autonomous and OperatorControl methods at the right
 * time as controlled by the switches on the driver station or the field
 * controls.
 * WARNING: While it may look like a good choice to use for your code if you're
 * inexperienced, don't. Unless you know what you are doing, complex code will
 * be much more difficult under this system. Use IterativeRobot or Command-Based
 * instead if you're new.
class Robot: public SampleRobot {
	//frc::RobotDrive myRobot { 0, 1 }; // robot drive system --commented 2/10/17

	Joystick *stick; // declaring left joystick
	Joystick *rstick; // declaring right joystick

	VictorSP *lmotor1; // declaring one of two victors on the left side of the drivetrain
	VictorSP *lmotor2; // declaring one of two victors on the left side of the drivetrain
	VictorSP *rmotor1; // declaring one of two victors on the right side of the drivetrain
	VictorSP *rmotor2; // declaring one of two victors on the right side of the drivetrain

	RobotDrive *drivetrain; // declaring the drivetrain

	Robot() {
		//Note SmartDashboard is not initialized here, wait until RobotInit to make SmartDashboard calls

		stick = new Joystick(0); //initializing left joystick
		rstick = new Joystick(1); //initializing right joystick

		lmotor1 = new VictorSP(1);
		lmotor2 = new VictorSP(2);
		rmotor1 = new VictorSP(3);
		rmotor2 = new VictorSP(4);

		drivetrain = new RobotDrive(lmotor1, lmotor2, rmotor1, rmotor2);

	void RobotInit() {


	 * This autonomous (along with the chooser code above) shows how to select
	 * between different autonomous modes using the dashboard. The sendable
	 * chooser code works with the Java SmartDashboard. If you prefer the
	 * LabVIEW Dashboard, remove all of the chooser code and uncomment the
	 * GetString line to get the auto name from the text box below the Gyro.
	 * You can add additional auto modes by adding additional comparisons to the
	 * if-else structure below with additional strings. If using the
	 * SendableChooser make sure to add them to the chooser code above as well.


	 * Runs the motors with tank steering.
	void OperatorControl() override {
		while (IsOperatorControl() && IsEnabled())

			// drive with arcade style (use right stick)
			drivetrain->TankDrive(lstick, rstick);

			//float joystick_y = stick->GetY();
			//float joystick_x = stick->GetX();


	 * Runs during test mode
	void Test() override {





I don’t code in C++ but the code should be pretty similar you can look at the example below

m_left = new SpeedControllerGroup(Robotmap.frontLeftMotor, Robotmap.rearLeftMotor);
		m_right = new SpeedControllerGroup(Robotmap.frontRightMotor, Robotmap.rearRightMotor);


		Driver = new DifferentialDrive(m_left, m_right);

		driverJoystick = new OI(Robotmap.driverJoystick);


here’s the link for the doc : http://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/drive/DifferentialDrive.html

go to the doc you’ll get a better example.

Thanks a ton! I’ll try this out.

The main difference is that you now need to pass groups of speed controllers instead of individual speed controllers. You can pass more than just two in a group I believe (in case you have a six wheel tank drive).

You shouldn’t need to invert both sides, or either side for that matter

Since you’re using C++, you’ll find the C++ documentation a lot more useful. The way the Java examples create objects with new is definitely not the recommended way to do it in C++. See http://first.wpi.edu/FRC/roborio/release/docs/cpp/classfrc_1_1DifferentialDrive.html#details. Notice how value semantics are used instead of pointer semantics.

In a best case scenario, no.

In a scenario where the mechanical team has a penchant for plugging things in a different way each time they return the robot to you and expect you to make it driver properly each time, you end up inverting motors and joystick values quite frequently.