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
|
||||
};
|
||||
Reference in New Issue
Block a user