mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Cleaned up C++ examples (#672)
This commit is contained in:
committed by
Peter Johnson
parent
6401aa1fde
commit
45d48d6b5a
@@ -9,7 +9,7 @@
|
||||
|
||||
ExampleCommand::ExampleCommand() {
|
||||
// Use Requires() here to declare subsystem dependencies
|
||||
// eg. Requires(Robot::chassis.get());
|
||||
// eg. Requires(&Robot::chassis);
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "MyAutoCommand.h"
|
||||
|
||||
MyAutoCommand::MyAutoCommand() {
|
||||
// Use Requires() here to declare subsystem dependencies
|
||||
// eg. Requires(&Robot::chassis);
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
void MyAutoCommand::Initialize() {}
|
||||
|
||||
// Called repeatedly when this Command is scheduled to run
|
||||
void MyAutoCommand::Execute() {}
|
||||
|
||||
// Make this return true when this Command no longer needs to run execute()
|
||||
bool MyAutoCommand::IsFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Called once after isFinished returns true
|
||||
void MyAutoCommand::End() {}
|
||||
|
||||
// Called when another command which requires one or more of the same
|
||||
// subsystems is scheduled to run
|
||||
void MyAutoCommand::Interrupted() {}
|
||||
@@ -0,0 +1,20 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Commands/Command.h>
|
||||
|
||||
class MyAutoCommand : public frc::Command {
|
||||
public:
|
||||
MyAutoCommand();
|
||||
void Initialize() override;
|
||||
void Execute() override;
|
||||
bool IsFinished() override;
|
||||
void End() override;
|
||||
void Interrupted() override;
|
||||
};
|
||||
@@ -5,8 +5,6 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <Commands/Command.h>
|
||||
#include <Commands/Scheduler.h>
|
||||
#include <IterativeRobot.h>
|
||||
@@ -15,15 +13,14 @@
|
||||
#include <SmartDashboard/SmartDashboard.h>
|
||||
|
||||
#include "Commands/ExampleCommand.h"
|
||||
#include "Commands/MyAutoCommand.h"
|
||||
|
||||
class Robot : public frc::IterativeRobot {
|
||||
public:
|
||||
void RobotInit() override {
|
||||
defaultAuto.reset(new ExampleCommand());
|
||||
chooser.AddDefault("Default Auto", defaultAuto.get());
|
||||
// myAuto.reset(new MyAutoCommand());
|
||||
// chooser.AddObject("My Auto", myAuto.get());
|
||||
frc::SmartDashboard::PutData("Auto Modes", &chooser);
|
||||
m_chooser.AddDefault("Default Auto", &m_defaultAuto);
|
||||
m_chooser.AddObject("My Auto", &m_myAuto);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &m_chooser);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,19 +51,18 @@ public:
|
||||
* to the if-else structure below with additional strings & commands.
|
||||
*/
|
||||
void AutonomousInit() override {
|
||||
/* std::string autoSelected =
|
||||
frc::SmartDashboard::GetString("Auto Selector", "Default");
|
||||
std::string autoSelected = frc::SmartDashboard::GetString(
|
||||
"Auto Selector", "Default");
|
||||
if (autoSelected == "My Auto") {
|
||||
autonomousCommand.reset(new MyAutoCommand());
|
||||
m_autonomousCommand = &m_myAuto;
|
||||
} else {
|
||||
m_autonomousCommand = &m_defaultAuto;
|
||||
}
|
||||
else {
|
||||
autonomousCommand.reset(new ExampleCommand());
|
||||
} */
|
||||
|
||||
autonomousCommand = chooser.GetSelected();
|
||||
m_autonomousCommand = m_chooser.GetSelected();
|
||||
|
||||
if (autonomousCommand != nullptr) {
|
||||
autonomousCommand->Start();
|
||||
if (m_autonomousCommand != nullptr) {
|
||||
m_autonomousCommand->Start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,9 +75,9 @@ public:
|
||||
// teleop starts running. If you want the autonomous to
|
||||
// continue until interrupted by another command, remove
|
||||
// this line or comment it out.
|
||||
if (autonomousCommand != nullptr) {
|
||||
autonomousCommand->Cancel();
|
||||
autonomousCommand = nullptr;
|
||||
if (m_autonomousCommand != nullptr) {
|
||||
m_autonomousCommand->Cancel();
|
||||
m_autonomousCommand = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +88,10 @@ public:
|
||||
private:
|
||||
// Have it null by default so that if testing teleop it
|
||||
// doesn't have undefined behavior and potentially crash.
|
||||
frc::Command* autonomousCommand = nullptr;
|
||||
std::unique_ptr<frc::Command> defaultAuto;
|
||||
// std::unique_ptr<frc::Command> myAuto;
|
||||
frc::SendableChooser<frc::Command*> chooser;
|
||||
frc::Command* m_autonomousCommand = nullptr;
|
||||
ExampleCommand m_defaultAuto;
|
||||
MyAutoCommand m_myAuto;
|
||||
frc::SendableChooser<frc::Command*> m_chooser;
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
// For example to map the left and right motors, you could define the
|
||||
// following variables to use with your drivetrain subsystem.
|
||||
// constexpr int LEFTMOTOR = 1;
|
||||
// constexpr int RIGHTMOTOR = 2;
|
||||
// constexpr int kLeftMotor = 1;
|
||||
// constexpr int kRightMotor = 2;
|
||||
|
||||
// If you are using multiple modules, make sure to define both the port
|
||||
// number and the module. For example you with a rangefinder:
|
||||
// constexpr int RANGE_FINDER_PORT = 1;
|
||||
// constexpr int RANGE_FINDER_MODULE = 1;
|
||||
// constexpr int kRangeFinderPort = 1;
|
||||
// constexpr int kRangeFinderModule = 1;
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <IterativeRobot.h>
|
||||
@@ -17,9 +16,9 @@
|
||||
class Robot : public frc::IterativeRobot {
|
||||
public:
|
||||
void RobotInit() {
|
||||
chooser.AddDefault(autoNameDefault, autoNameDefault);
|
||||
chooser.AddObject(autoNameCustom, autoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &chooser);
|
||||
m_chooser.AddDefault(kAutoNameDefault, kAutoNameDefault);
|
||||
m_chooser.AddObject(kAutoNameCustom, kAutoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &m_chooser);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -37,12 +36,12 @@ public:
|
||||
* well.
|
||||
*/
|
||||
void AutonomousInit() override {
|
||||
autoSelected = chooser.GetSelected();
|
||||
// std::string autoSelected = SmartDashboard::GetString("Auto
|
||||
// Selector", autoNameDefault);
|
||||
std::cout << "Auto selected: " << autoSelected << std::endl;
|
||||
m_autoSelected = m_chooser.GetSelected();
|
||||
// m_autoSelected = SmartDashboard::GetString(
|
||||
// "Auto Selector", kAutoNameDefault);
|
||||
std::cout << "Auto selected: " << m_autoSelected << std::endl;
|
||||
|
||||
if (autoSelected == autoNameCustom) {
|
||||
if (m_autoSelected == kAutoNameCustom) {
|
||||
// Custom Auto goes here
|
||||
} else {
|
||||
// Default Auto goes here
|
||||
@@ -50,7 +49,7 @@ public:
|
||||
}
|
||||
|
||||
void AutonomousPeriodic() {
|
||||
if (autoSelected == autoNameCustom) {
|
||||
if (m_autoSelected == kAutoNameCustom) {
|
||||
// Custom Auto goes here
|
||||
} else {
|
||||
// Default Auto goes here
|
||||
@@ -61,14 +60,14 @@ public:
|
||||
|
||||
void TeleopPeriodic() {}
|
||||
|
||||
void TestPeriodic() { lw->Run(); }
|
||||
void TestPeriodic() { m_lw.Run(); }
|
||||
|
||||
private:
|
||||
frc::LiveWindow* lw = LiveWindow::GetInstance();
|
||||
frc::SendableChooser<std::string> chooser;
|
||||
const std::string autoNameDefault = "Default";
|
||||
const std::string autoNameCustom = "My Auto";
|
||||
std::string autoSelected;
|
||||
frc::LiveWindow& m_lw = *LiveWindow::GetInstance();
|
||||
frc::SendableChooser<std::string> m_chooser;
|
||||
const std::string kAutoNameDefault = "Default";
|
||||
const std::string kAutoNameCustom = "My Auto";
|
||||
std::string m_autoSelected;
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <Drive/DifferentialDrive.h>
|
||||
#include <Joystick.h>
|
||||
#include <RobotDrive.h>
|
||||
#include <SampleRobot.h>
|
||||
#include <SmartDashboard/SendableChooser.h>
|
||||
#include <SmartDashboard/SmartDashboard.h>
|
||||
#include <Spark.h>
|
||||
#include <Timer.h>
|
||||
|
||||
/**
|
||||
@@ -29,23 +29,17 @@
|
||||
* instead if you're new.
|
||||
*/
|
||||
class Robot : public frc::SampleRobot {
|
||||
frc::RobotDrive myRobot{0, 1}; // robot drive system
|
||||
frc::Joystick stick{0}; // only joystick
|
||||
frc::SendableChooser<std::string> chooser;
|
||||
const std::string autoNameDefault = "Default";
|
||||
const std::string autoNameCustom = "My Auto";
|
||||
|
||||
public:
|
||||
Robot() {
|
||||
// Note SmartDashboard is not initialized here, wait until
|
||||
// RobotInit to make SmartDashboard calls
|
||||
myRobot.SetExpiration(0.1);
|
||||
m_robotDrive.SetExpiration(0.1);
|
||||
}
|
||||
|
||||
void RobotInit() {
|
||||
chooser.AddDefault(autoNameDefault, autoNameDefault);
|
||||
chooser.AddObject(autoNameCustom, autoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &chooser);
|
||||
m_chooser.AddDefault(kAutoNameDefault, kAutoNameDefault);
|
||||
m_chooser.AddObject(kAutoNameCustom, kAutoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &m_chooser);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -63,26 +57,32 @@ public:
|
||||
* well.
|
||||
*/
|
||||
void Autonomous() {
|
||||
auto autoSelected = chooser.GetSelected();
|
||||
// std::string autoSelected =
|
||||
// frc::SmartDashboard::GetString("Auto Selector",
|
||||
// autoNameDefault);
|
||||
std::string autoSelected = frc::SmartDashboard::GetString(
|
||||
"Auto Selector", kAutoNameDefault);
|
||||
std::cout << "Auto selected: " << autoSelected << std::endl;
|
||||
|
||||
if (autoSelected == autoNameCustom) {
|
||||
if (autoSelected == kAutoNameCustom) {
|
||||
// Custom Auto goes here
|
||||
std::cout << "Running custom Autonomous" << std::endl;
|
||||
myRobot.SetSafetyEnabled(false);
|
||||
myRobot.Drive(-0.5, 1.0); // spin at half speed
|
||||
frc::Wait(2.0); // for 2 seconds
|
||||
myRobot.Drive(0.0, 0.0); // stop robot
|
||||
m_robotDrive.SetSafetyEnabled(false);
|
||||
|
||||
// spin at half speed for two seconds
|
||||
m_robotDrive.ArcadeDrive(0.0, 1.0);
|
||||
frc::Wait(2.0);
|
||||
|
||||
// stop robot
|
||||
m_robotDrive.ArcadeDrive(0.0, 0.0);
|
||||
} else {
|
||||
// Default Auto goes here
|
||||
std::cout << "Running default Autonomous" << std::endl;
|
||||
myRobot.SetSafetyEnabled(false);
|
||||
myRobot.Drive(-0.5, 0.0); // drive forwards half speed
|
||||
frc::Wait(2.0); // for 2 seconds
|
||||
myRobot.Drive(0.0, 0.0); // stop robot
|
||||
m_robotDrive.SetSafetyEnabled(false);
|
||||
|
||||
// drive forwards at half speed for two seconds
|
||||
m_robotDrive.ArcadeDrive(-0.5, 0.0);
|
||||
frc::Wait(2.0);
|
||||
|
||||
// stop robot
|
||||
m_robotDrive.ArcadeDrive(0.0, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,10 +90,11 @@ public:
|
||||
* Runs the motors with arcade steering.
|
||||
*/
|
||||
void OperatorControl() override {
|
||||
myRobot.SetSafetyEnabled(true);
|
||||
m_robotDrive.SetSafetyEnabled(true);
|
||||
while (IsOperatorControl() && IsEnabled()) {
|
||||
// drive with arcade style (use right stick)
|
||||
myRobot.ArcadeDrive(stick);
|
||||
m_robotDrive.ArcadeDrive(
|
||||
m_stick.GetY(), m_stick.GetX());
|
||||
|
||||
// wait for a motor update time
|
||||
frc::Wait(0.005);
|
||||
@@ -104,6 +105,18 @@ public:
|
||||
* Runs during test mode
|
||||
*/
|
||||
void Test() override {}
|
||||
|
||||
private:
|
||||
// Robot drive system
|
||||
frc::Spark m_leftMotor{0};
|
||||
frc::Spark m_rightMotor{1};
|
||||
frc::DifferentialDrive m_robotDrive{m_leftMotor, m_rightMotor};
|
||||
|
||||
frc::Joystick m_stick{0};
|
||||
|
||||
frc::SendableChooser<std::string> m_chooser;
|
||||
const std::string kAutoNameDefault = "Default";
|
||||
const std::string kAutoNameCustom = "My Auto";
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
|
||||
Reference in New Issue
Block a user