:
Robot.h :
#ifndef ROBOT_H
#define ROBOT_H
//Importing Liraries
#include <memory>
#include <Commands/Command.h>
#include <IterativeRobot.h>
#include <LiveWindow/LiveWindow.h>
//Importing OI and Subsystem file
#include "OI.h"
#include "Subsystems/DriveTrain.h"
//Creating the class robot, inherting from the class IterativeRobot
class Robot: public frc::IterativeRobot {
public:
//Creating public pointer to the drivetrain subsystem and the oi
static std::shared_ptr<DriveTrain> drivetrain;
static std::unique_ptr<OI> oi;
private:
//Creating a private instance of the live window
frc::LiveWindow* lw = frc::LiveWindow::GetInstance();
//Overidding all of the default method
void RobotInit() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void TeleopInit() override;
void TeleopPeriodic() override;
void TestPeriodic() override;
};
#endif // ROBOT_H_
Robot.cpp:
#include "Robot.h"
#include <iostream>
//Setting up the share Subsystems and oi so that the commands can use it
std::shared_ptr<DriveTrain> Robot::drivetrain = std::make_shared<DriveTrain>();
std::unique_ptr<OI> Robot::oi = std::make_unique<OI>();
//Run once on robot start up
void Robot::RobotInit() {
//Putting the data of the drivetrain onto the smartdashboard
frc::SmartDashboard::PutData(drivetrain.get());
}
//Run once when robot is in autonomous mode
void Robot::AutonomousInit() {
//Put autonomous startup here
}
//Keep running when the robot is in autonomous mode
void Robot::AutonomousPeriodic() {
//Running an instance of the scheduler
frc::Scheduler::GetInstance()->Run();
}
//Run once when the robot go into teleoperated mode
void Robot::TeleopInit() {
//End autonomous code here
}
//Keep running while the robot is in Teleoperated mode
void Robot::TeleopPeriodic() {
//Running an instance of the scheduler
frc::Scheduler::GetInstance()->Run();
}
//Test Mode
void Robot::TestPeriodic() {
lw->Run();
}
//Start the robot?
START_ROBOT_CLASS(Robot)
OI.h:
#ifndef OI_H_
#define OI_H_
#include <Buttons/JoystickButton.h>
#include <Joystick.h>
//Creating the class operater Interface for inputs
class OI {
public:
//Public contructor
OI();
//Public method getJoystick that return the address of joystick
frc::Joystick* GetJoystick();
private:
//Private object joy of class joystick
frc::Joystick joy { 0 };
};
#endif // OI_H_
OI.cpp:
#include "OI.h"
#include <WPILib.h>
OI::OI() {
// Connect the buttons to commands
}
//Returning the address of the Joystick
frc::Joystick* OI::GetJoystick() {
return &joy;
}
Subsystems/DriveTrain.h:
#ifndef DriveTrain_H
#define DriveTrain_H
//Importing classes
#include <Commands/Subsystem.h>
#include <Talon.h>
#include <RobotDrive.h>
//Creating a class joystick under the namespace of FRC
namespace frc {
class Joystick;
}
//Creating a class Drivetrain inherting from the class Subsystem
class DriveTrain : public frc::Subsystem {
public:
//Public contructor
DriveTrain();
//overring the start up command
void InitDefaultCommand() override;
//Creating the Tank style drive method
/**
* Tank style driving for the DriveTrain.
* @param left Speed in range -1,1]
* @param right Speed in range -1,1]
*/
void Drive(double left, double right);
//creating an arcade style drive method
/**
* @param joy
*/
void Drive(frc::Joystick* joy);
private:
//Setting up the drive train
frc::Talon frontLeft { 2 };
frc::Talon rearLeft { 3 };
frc::Talon frontRight { 4 };
frc::Talon rearRight { 5 };
frc::RobotDrive driveTrain { frontLeft, rearLeft, frontRight, rearRight };
};
#endif // DriveTrain_H
Subsystems/DriveTrain.cpp:
//Including Files
#include "DriveTrain.h"
#include <Joystick.h>
#include <LiveWindow/LiveWindow.h>
//Including the command file
#include "Commands/ArcadeDriveWithJoystick.h"
//Creating the contructor
DriveTrain::DriveTrain()
: Subsystem("DriveTrain") {
//Inverting all of the motor because the motor are wired in reverse
driveTrain.SetInvertedMotor(RobotDrive::kFrontLeftMotor, true);
driveTrain.SetInvertedMotor(RobotDrive::kRearLeftMotor, true);
driveTrain.SetInvertedMotor(RobotDrive::kFrontRightMotor, true);
driveTrain.SetInvertedMotor(RobotDrive::kRearRightMotor, true);
}
//Run on robot start up
void DriveTrain::InitDefaultCommand (){
//Settting the default command to be drving this drive train with a Joystick
SetDefaultCommand(new ArcadeDriveWithJoystick());
}
//Setting up the drive method
void DriveTrain::Drive(double left, double right){
//Driving with the left and the right parameter
driveTrain.TankDrive(left, right);
}
//Setting up the arcadedrive method
void DriveTrain::Drive(frc::Joystick* joy){
//Driving with the Joystick address
driveTrain.ArcadeDrive(joy);
}
Commands/ArcadeDriveWithJoystick.h
#ifndef ArcadeDriveWithJoystick_H
#define ArcadeDriveWithJoystick_H
//Importing the command class
#include <Commands/Command.h>
//Creating the class inherting from the class command
class ArcadeDriveWithJoystick : public Command {
public:
//Calling public contructor
ArcadeDriveWithJoystick();
//Overidding methods
void Execute() override;
bool IsFinished() override;
void End() override;
};
#endif // ArcadeDriveWithJoystick_H
Commands/ArcadeDriveWithJoystick.cpp
//Including files
#include "ArcadeDriveWithJoystick.h"
#include "Robot.h"
//Calling the public contructor
ArcadeDriveWithJoystick::ArcadeDriveWithJoystick() :
frc::Command("ArcadeDriveWithJoystick") {
//Getting the DriveTrain subsystem.
Requires(Robot::drivetrain.get());
}
//Run continously while IsFinished return false
void ArcadeDriveWithJoystick::Execute() {
//Driving the drivetrain with the joystick)
Robot::drivetrain->Drive(Robot::oi->GetJoystick());
}
// Make this return true when this Command no longer needs to run execute()
bool ArcadeDriveWithJoystick::IsFinished() {
return false;
}
// Called once after isFinished returns true
void ArcadeDriveWithJoystick::End() {
}
Sorry, I was waiting for the admin to verify my thread and forgot about the post.