mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add cpp examples (#659)
* Added C++ robot project examples and set up sub .clang-format for them * Ran formatter
This commit is contained in:
committed by
Peter Johnson
parent
66002d6cac
commit
0291a95f68
@@ -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 "ExampleCommand.h"
|
||||
|
||||
ExampleCommand::ExampleCommand() {
|
||||
// Use Requires() here to declare subsystem dependencies
|
||||
// eg. Requires(Robot::chassis.get());
|
||||
}
|
||||
|
||||
// Called just before this Command runs the first time
|
||||
void ExampleCommand::Initialize() {}
|
||||
|
||||
// Called repeatedly when this Command is scheduled to run
|
||||
void ExampleCommand::Execute() {}
|
||||
|
||||
// Make this return true when this Command no longer needs to run execute()
|
||||
bool ExampleCommand::IsFinished() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Called once after isFinished returns true
|
||||
void ExampleCommand::End() {}
|
||||
|
||||
// Called when another command which requires one or more of the same
|
||||
// subsystems is scheduled to run
|
||||
void ExampleCommand::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 ExampleCommand : public frc::Command {
|
||||
public:
|
||||
ExampleCommand();
|
||||
void Initialize() override;
|
||||
void Execute() override;
|
||||
bool IsFinished() override;
|
||||
void End() override;
|
||||
void Interrupted() override;
|
||||
};
|
||||
14
wpilibcExamples/src/main/cpp/templates/commandbased/OI.cpp
Normal file
14
wpilibcExamples/src/main/cpp/templates/commandbased/OI.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "OI.h"
|
||||
|
||||
#include <WPILib.h>
|
||||
|
||||
OI::OI() {
|
||||
// Process operator interface input here.
|
||||
}
|
||||
13
wpilibcExamples/src/main/cpp/templates/commandbased/OI.h
Normal file
13
wpilibcExamples/src/main/cpp/templates/commandbased/OI.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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
|
||||
|
||||
class OI {
|
||||
public:
|
||||
OI();
|
||||
};
|
||||
101
wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp
Normal file
101
wpilibcExamples/src/main/cpp/templates/commandbased/Robot.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 <memory>
|
||||
|
||||
#include <Commands/Command.h>
|
||||
#include <Commands/Scheduler.h>
|
||||
#include <IterativeRobot.h>
|
||||
#include <LiveWindow/LiveWindow.h>
|
||||
#include <SmartDashboard/SendableChooser.h>
|
||||
#include <SmartDashboard/SmartDashboard.h>
|
||||
|
||||
#include "Commands/ExampleCommand.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);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called once each time the robot enters Disabled
|
||||
* mode.
|
||||
* You can use it to reset any subsystem information you want to clear
|
||||
* when
|
||||
* the robot is disabled.
|
||||
*/
|
||||
void DisabledInit() override {}
|
||||
|
||||
void DisabledPeriodic() override {
|
||||
frc::Scheduler::GetInstance()->Run();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 code to get the auto name from the text box below the Gyro.
|
||||
*
|
||||
* You can add additional auto modes by adding additional commands to
|
||||
* the
|
||||
* chooser code above (like the commented example) or additional
|
||||
* comparisons
|
||||
* to the if-else structure below with additional strings & commands.
|
||||
*/
|
||||
void AutonomousInit() override {
|
||||
/* std::string autoSelected =
|
||||
frc::SmartDashboard::GetString("Auto Selector", "Default");
|
||||
if (autoSelected == "My Auto") {
|
||||
autonomousCommand.reset(new MyAutoCommand());
|
||||
}
|
||||
else {
|
||||
autonomousCommand.reset(new ExampleCommand());
|
||||
} */
|
||||
|
||||
autonomousCommand = chooser.GetSelected();
|
||||
|
||||
if (autonomousCommand != nullptr) {
|
||||
autonomousCommand->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void AutonomousPeriodic() override {
|
||||
frc::Scheduler::GetInstance()->Run();
|
||||
}
|
||||
|
||||
void TeleopInit() override {
|
||||
// This makes sure that the autonomous stops running when
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
void TeleopPeriodic() override { frc::Scheduler::GetInstance()->Run(); }
|
||||
|
||||
void TestPeriodic() override { frc::LiveWindow::GetInstance()->Run(); }
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
@@ -0,0 +1,25 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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
|
||||
|
||||
/**
|
||||
* The RobotMap is a mapping from the ports sensors and actuators are wired into
|
||||
* to a variable name. This provides flexibility changing wiring, makes checking
|
||||
* the wiring easier and significantly reduces the number of magic numbers
|
||||
* floating around.
|
||||
*/
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
@@ -0,0 +1,21 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "ExampleSubsystem.h"
|
||||
|
||||
#include "../RobotMap.h"
|
||||
|
||||
ExampleSubsystem::ExampleSubsystem()
|
||||
: frc::Subsystem("ExampleSubsystem") {}
|
||||
|
||||
void ExampleSubsystem::InitDefaultCommand() {
|
||||
// Set the default command for a subsystem here.
|
||||
// SetDefaultCommand(new MySpecialCommand());
|
||||
}
|
||||
|
||||
// Put methods for controlling this subsystem
|
||||
// here. Call these from Commands.
|
||||
@@ -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/Subsystem.h>
|
||||
|
||||
class ExampleSubsystem : public frc::Subsystem {
|
||||
public:
|
||||
ExampleSubsystem();
|
||||
void InitDefaultCommand() override;
|
||||
|
||||
private:
|
||||
// It's desirable that everything possible under private except
|
||||
// for methods that implement subsystem capabilities
|
||||
};
|
||||
74
wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp
Normal file
74
wpilibcExamples/src/main/cpp/templates/iterative/Robot.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 <memory>
|
||||
#include <string>
|
||||
|
||||
#include <IterativeRobot.h>
|
||||
#include <LiveWindow/LiveWindow.h>
|
||||
#include <SmartDashboard/SendableChooser.h>
|
||||
#include <SmartDashboard/SmartDashboard.h>
|
||||
|
||||
class Robot : public frc::IterativeRobot {
|
||||
public:
|
||||
void RobotInit() {
|
||||
chooser.AddDefault(autoNameDefault, autoNameDefault);
|
||||
chooser.AddObject(autoNameCustom, autoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &chooser);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
void AutonomousInit() override {
|
||||
autoSelected = chooser.GetSelected();
|
||||
// std::string autoSelected = SmartDashboard::GetString("Auto
|
||||
// Selector", autoNameDefault);
|
||||
std::cout << "Auto selected: " << autoSelected << std::endl;
|
||||
|
||||
if (autoSelected == autoNameCustom) {
|
||||
// Custom Auto goes here
|
||||
} else {
|
||||
// Default Auto goes here
|
||||
}
|
||||
}
|
||||
|
||||
void AutonomousPeriodic() {
|
||||
if (autoSelected == autoNameCustom) {
|
||||
// Custom Auto goes here
|
||||
} else {
|
||||
// Default Auto goes here
|
||||
}
|
||||
}
|
||||
|
||||
void TeleopInit() {}
|
||||
|
||||
void TeleopPeriodic() {}
|
||||
|
||||
void TestPeriodic() { 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;
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
109
wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp
Normal file
109
wpilibcExamples/src/main/cpp/templates/sample/Robot.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 <memory>
|
||||
#include <string>
|
||||
|
||||
#include <Joystick.h>
|
||||
#include <RobotDrive.h>
|
||||
#include <SampleRobot.h>
|
||||
#include <SmartDashboard/SendableChooser.h>
|
||||
#include <SmartDashboard/SmartDashboard.h>
|
||||
#include <Timer.h>
|
||||
|
||||
/**
|
||||
* 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 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);
|
||||
}
|
||||
|
||||
void RobotInit() {
|
||||
chooser.AddDefault(autoNameDefault, autoNameDefault);
|
||||
chooser.AddObject(autoNameCustom, autoNameCustom);
|
||||
frc::SmartDashboard::PutData("Auto Modes", &chooser);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
void Autonomous() {
|
||||
auto autoSelected = chooser.GetSelected();
|
||||
// std::string autoSelected =
|
||||
// frc::SmartDashboard::GetString("Auto Selector",
|
||||
// autoNameDefault);
|
||||
std::cout << "Auto selected: " << autoSelected << std::endl;
|
||||
|
||||
if (autoSelected == autoNameCustom) {
|
||||
// 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
|
||||
} 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
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Runs the motors with arcade steering.
|
||||
*/
|
||||
void OperatorControl() override {
|
||||
myRobot.SetSafetyEnabled(true);
|
||||
while (IsOperatorControl() && IsEnabled()) {
|
||||
// drive with arcade style (use right stick)
|
||||
myRobot.ArcadeDrive(stick);
|
||||
|
||||
// wait for a motor update time
|
||||
frc::Wait(0.005);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Runs during test mode
|
||||
*/
|
||||
void Test() override {}
|
||||
};
|
||||
|
||||
START_ROBOT_CLASS(Robot)
|
||||
Reference in New Issue
Block a user