HELP (talons, robotDrive issues)

We are trying to modify our 2017 code to fit the 2018 standards because we want to test some stuff on the old robot. We included the phoenix stuff, we’ve cleaned the project, we’ve updated everything, we’ve switched out the names of TalonSRX etc to conform to the new guidelines. When we try to deploy, we are told that the build fails and that there is no executable binary found to upload to the robot. We think this may be because of RobotDrive or the talons. PLEASE HELP us find the issues in our code. We do not have mentors who know C++ and we’re basically self-taught to program. If we don’t figure out our issues now, we’re gonna have a huge problem trying to get the new robot programmed:( . Link with the problematic code is below. Thank you.

https://gist.github.com/anonymous/e6...16f4ddf7326fd9

Looks like the Talons were set to PWM in one spot, and CAN in the other. I replaced with WPI_Talon so that it uses CANbus and WPILIB’s drivetrain class.

You were missing some headers as well.

Also the new WPI DifferentialDrive takes two motor controllers, so I set the other two rear MCs to follow the front MCs.

Diff this against your original and study the changes so you can understand them. Use your favorite diffing software (GitHub, git, WinMerge, etc.).

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017 FIRST. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

#include <iostream>
#include <string>

#include <IterativeRobot.h>
#include <LiveWindow/LiveWindow.h>
#include <SmartDashboard/SendableChooser.h>
#include <SmartDashboard/SmartDashboard.h>
#include "WPILib.h"
#include <SmartDashboard/SmartDashboard.h>
#include "ctre/Phoenix.h"
#include "DriverStation.h"
#include "Drive/DifferentialDrive.h"

class Robot: public IterativeRobot {
private:
	// default stuff

	LiveWindow *lw = LiveWindow::GetInstance();
	Preferences *prefs;

//initialize variables
	//drivers station
	Joystick *m_leftStick;
	Joystick *m_rightStick;
	Joystick *m_gamepad;
	SmartDashboard *smart;

	//drivetrain
	WPI_TalonSRX *m_leftFront;
	WPI_TalonSRX *m_rightFront;
	WPI_TalonSRX *m_leftBack;
	WPI_TalonSRX *m_rightBack;
	DifferentialDrive *m_robotDrive;

	//shifters
	Solenoid *m_leftshiftUp;
	Solenoid *m_leftshiftDown;
	Solenoid *m_rightshiftUp;
	Solenoid *m_rightshiftDown;

	//pickup
	PWMTalonSRX *m_pickup;

	//gear
	Solenoid *m_gearExtend;
	Solenoid *m_gearRetract;

	//timers

	Timer *driveTimer;
	Timer *autonTimer;
	Timer *shiftTimer;
	Timer *gearTimer;

	//ints floats bools
	int autoChoose;
	int autonSwitch;
	float leftStickValue;
	float rightStickValue;
	int driveDirection;
	int shiftValue;
	int gearValue;

	void RobotInit() {
		prefs = Preferences::GetInstance();

		CameraServer::GetInstance()->StartAutomaticCapture();

		m_leftStick = new Joystick(0);
		m_rightStick = new Joystick(1);
		m_gamepad = new Joystick(2);

		//drivetrain
		m_leftFront = new WPI_TalonSRX(3);
		m_leftBack = new WPI_TalonSRX(1);
		m_rightFront = new WPI_TalonSRX(4);
		m_rightBack = new WPI_TalonSRX(2);
		m_robotDrive = new DifferentialDrive(*m_leftFront, *m_rightFront);
		m_robotDrive->SetSafetyEnabled(false);

		m_leftBack->Follow(*m_leftFront);
		m_rightBack->Follow(*m_rightFront);
		//shifters
		m_leftshiftUp = new Solenoid(4);		//could be 5
		m_leftshiftDown = new Solenoid(5);		//could be 4
		m_rightshiftUp = new Solenoid(0);		//0 or 1
		m_rightshiftDown = new Solenoid(1);

		//alignment
		m_gearExtend = new Solenoid(7); 		//6 or 7
		m_gearRetract = new Solenoid(6);		//6 or 7

		//timers

		driveTimer = new Timer();	//must time, button 3s on joysticks
		autonTimer = new Timer();
		shiftTimer = new Timer();
		gearTimer = new Timer();

	}

	void AutonomousInit() {
		//AUTONOMOUS SELECTION
		//middle gear
		if (m_leftStick->GetZ() > 0.9 && m_rightStick->GetZ() != 0
				&& m_rightStick->GetZ() != 1) {
			autoChoose = 2; //4, baseline actually
		}

		//gear left
		if (m_leftStick->GetZ() < 0.1 && m_rightStick->GetZ() != 0
				&& m_rightStick->GetZ() != 1) {
			autoChoose = 1;
		}

		//baseline
		if (m_rightStick->GetZ() > 0.9 && m_leftStick->GetZ() != 0
				&& m_leftStick->GetZ() != 1) {
			autoChoose = 4;	//2, actually the middle gear
		}

		//gear right
		if (m_rightStick->GetZ() < 0.1 && m_leftStick->GetZ() != 0
				&& m_leftStick->GetZ() != 1) {
			autoChoose = 3; //1, actually left gear
		}

		autonSwitch = autoChoose;
		smart->PutNumber("autonSwitch: ", autonSwitch);
	}

	void AutonomousPeriodic() {
		//theRealAuto();
		Wait(0.001);
	}

	void TeleopInit() {

		driveTimer->Start();
		shiftTimer->Start();
		gearTimer->Start();
		shiftValue = 0;
		driveDirection = 1;
		gearValue = 1;

	}

	void TeleopPeriodic() {
		TeleopDrive();
		//craig();
		//pusher();
		//shooter();
		//gear();
		//shifters();
		//lift();
		Wait(.01);
		smart->PutNumber(" Drive Direction: ", driveDirection);
	}

	void TestPeriodic() {
		lw->Run();
	}

	void TeleopDrive() {
		leftStickValue = m_leftStick->GetY();
		rightStickValue = m_rightStick->GetY();

		if (m_leftStick->GetRawButton(3) == 1
				&& m_rightStick->GetRawButton(3) == 1 && driveDirection == 1
				&& driveTimer->Get() > 0.3) {
			driveDirection = -1;
			driveTimer->Reset();
		} else if (m_leftStick->GetRawButton(3) == 1
				&& m_rightStick->GetRawButton(3) == 1 && driveDirection == -1
				&& driveTimer->Get() > 0.3) {
			driveDirection = 1;
			driveTimer->Reset();
		}

		if (driveDirection == 1) {
			m_robotDrive->TankDrive(-rightStickValue, -leftStickValue);
		} else if (driveDirection == -1) {
			m_robotDrive->TankDrive(leftStickValue, rightStickValue);
		}
	}

//we had more stuff but it isn't marked with errors so we are not concerned with it at the moment
};
START_ROBOT_CLASS(Robot);

Thank you so much. We just packed up for the night, I will test this tomorrow and tell you how it goes! Your help is very very appreciated.

Also here is our tested example…

https://github.com/CrossTheRoadElec/Phoenix-Examples-Languages/blob/master/C%2B%2B/DifferentialDrive/src/Robot.cpp

Got it working. Thank you.

Take this advice or leave it, but I have some comments for improving your team’s code. I’m assuming some of it came from Screensteps, which has outdated examples relative to the ones in the Eclipse plugins, but still.

  • Use references instead of pointers. It deduplicates a lot of code.
  • Use override keyword for overriden member functions
  • GetRawButton() returns a bool. You don’t need to check that the return value equals 1
  • That logic in TelopDrive() flips the robot’s driving direction every 0.3 seconds if you hold the buttons down. That’s probably not what you want. Consider using GetRawButtonPressed() or GetRawButtonReleased().
  • Calling frc::Wait() isn’t necessary because IterativeRobot already delays for DriverStation packets
  • Either include WPILib.h or include every header you need. Not both.
  • Consider using a SendableChooser for selecting your autonomous.
  • You shouldn’t declare a built-in type (like double) in class scope when it’s used as a temporary at the function scope. The compiler is smart enough to optimize the stack allocation away.
    Here’s an example of what all that would look like:

/*----------------------------------------------------------------------------*/
 /* Copyright (c) 2017 FIRST. All Rights Reserved.                             */
 /* Open Source Software - may be modified and shared by FRC teams. The code   */
 /* must be accompanied by the FIRST BSD license file in the root directory of */
 /* the project.                                                               */
 /*----------------------------------------------------------------------------*/
  
 #include <iostream>
 #include <string>
  
 #include <WPILib.h>
 #include <ctre/Phoenix.h>
 
 class Robot : public frc::IterativeRobot {
 public:
   ~Robot() override = default;
 
  private:
   Preferences& prefs = *Preferences::GetInstance();
 
   //initialize variables
   // drivers station
   Joystick m_leftStick{0};
   Joystick m_rightStick{1};
   Joystick m_gamepad{2}; 
 
   // drivetrain
   WPI_TalonSRX m_leftFront{3};
   WPI_TalonSRX m_rightFront{1};
   WPI_TalonSRX m_leftBack{4};
   WPI_TalonSRX m_rightBack{2};
   DifferentialDrive m_robotDrive{m_leftFront, m_rightFront}; 
 
   // shifters
   Solenoid m_leftshiftUp{4}; // could be 5
   Solenoid m_leftshiftDown{5}; // could be 4
   Solenoid m_rightshiftUp{0}; // 0 or 1
   Solenoid m_rightshiftDown{1}; 
 
   // pickup
   PWMTalonSRX m_pickup{10}; // FIXME: determine real ID 
 
   // gear alignment
   Solenoid m_gearExtend{7}; // 6 or 7
   Solenoid m_gearRetract{6}; // 6 or 7 
 
   // timers
   Timer driveTimer; // must time, button 3s on joysticks
   Timer autonTimer;
   Timer shiftTimer;
   Timer gearTimer; 
 
   //ints floats bools
   int autoChoose;
   int autonSwitch;
   int driveDirection;
   int shiftValue;
   int gearValue; 
 
   void RobotInit() override {
     CameraServer::GetInstance()->StartAutomaticCapture(); 
 
     driveTimer.Start();
     shiftTimer.Start();
     gearTimer.Start(); 
 
     // drivetrain
     m_robotDrive.SetSafetyEnabled(false); 
 
     m_leftBack.Follow(m_leftFront);
     m_rightBack.Follow(m_rightFront);
   } 
 
   void AutonomousInit() override {
     // AUTONOMOUS SELECTION
     // middle gear
     if (m_leftStick.GetZ() > 0.9 && m_rightStick.GetZ() != 0
         && m_rightStick.GetZ() != 1) {
       autoChoose = 2; // 4, baseline actually
     } 
 
     // gear left
     if (m_leftStick.GetZ() < 0.1 && m_rightStick.GetZ() != 0
         && m_rightStick.GetZ() != 1) {
       autoChoose = 1;
     } 
 
     //baseline
     if (m_rightStick.GetZ() > 0.9 && m_leftStick.GetZ() != 0
         && m_leftStick.GetZ() != 1) {
       autoChoose = 4;  // 2, actually the middle gear
     } 
 
     //gear right
     if (m_rightStick.GetZ() < 0.1 && m_leftStick.GetZ() != 0
         && m_leftStick.GetZ() != 1) {
       autoChoose = 3; // 1, actually left gear
     } 
 
     autonSwitch = autoChoose;
     SmartDashboard::PutNumber("autonSwitch: ", autonSwitch);
   } 
 
   void AutonomousPeriodic() override {
     // theRealAuto();
   } 
 
   void TeleopInit() override {
     shiftValue = 0;
     driveDirection = 1;
     gearValue = 1;
   } 
 
   void TeleopPeriodic() override {
     TeleopDrive();
     // craig();
     // pusher();
     // shooter();
     // gear();
     // shifters();
     // lift();
     SmartDashboard::PutNumber(" Drive Direction: ", driveDirection);
   } 
 
   void TeleopDrive() {
     double leftStickValue = m_leftStick.GetY();
     double rightStickValue = m_rightStick.GetY(); 
 
     if ((m_leftStick.GetRawButtonPressed(3)
           || m_rightStick.GetRawButtonPressed(3)) && driveDirection == 1) {
       driveDirection = -1;
     } else if ((m_leftStick.GetRawButtonPressed(3)
           || m_rightStick.GetRawButtonPressed(3)) && driveDirection == -1) {
       driveDirection = 1;
     }
  
     if (driveDirection == 1) {
       m_robotDrive.TankDrive(-rightStickValue, -leftStickValue);
     } else if (driveDirection == -1) {
       m_robotDrive.TankDrive(leftStickValue, rightStickValue);
     }
   }
  };
 
 START_ROBOT_CLASS(Robot)