Added TimedRobot (#520)

This commit is contained in:
Tyler Veness
2017-07-08 10:50:56 -04:00
committed by Peter Johnson
parent f826216a28
commit 89d3b08e77
16 changed files with 722 additions and 582 deletions

View File

@@ -7,67 +7,25 @@
#pragma once
#include "RobotBase.h"
#include "IterativeRobotBase.h"
namespace frc {
/**
* IterativeRobot implements a specific type of Robot Program framework,
* extending the RobotBase class.
* IterativeRobot implements the IterativeRobotBase robot program framework.
*
* The IterativeRobot class is intended to be subclassed by a user creating a
* robot program.
*
* This class is intended to implement the "old style" default code, by
* providing the following functions which are called by the main loop,
* StartCompetition(), at the appropriate times:
*
* RobotInit() -- provide for initialization at robot power-on
*
* Init() functions -- each of the following functions is called once when the
* appropriate mode is entered:
* - DisabledInit() -- called only when first disabled
* - AutonomousInit() -- called each and every time autonomous is entered from
* another mode
* - TeleopInit() -- called each and every time teleop is entered from
* another mode
* - TestInit() -- called each and every time test is entered from
* another mode
*
* Periodic() functions -- each of these functions is called each time a
* new packet is received from the driver station:
* - RobotPeriodic()
* - DisabledPeriodic()
* - AutonomousPeriodic()
* - TeleopPeriodic()
* - TestPeriodic()
*
* Periodic() functions from the base class are called each time a new packet is
* received from the driver station.
*/
class IterativeRobot : public RobotBase {
class IterativeRobot : public IterativeRobotBase {
public:
void StartCompetition() override;
virtual void RobotInit();
virtual void DisabledInit();
virtual void AutonomousInit();
virtual void TeleopInit();
virtual void TestInit();
virtual void RobotPeriodic();
virtual void DisabledPeriodic();
virtual void AutonomousPeriodic();
virtual void TeleopPeriodic();
virtual void TestPeriodic();
protected:
IterativeRobot() = default;
IterativeRobot();
virtual ~IterativeRobot() = default;
private:
bool m_disabledInitialized = false;
bool m_autonomousInitialized = false;
bool m_teleopInitialized = false;
bool m_testInitialized = false;
void StartCompetition() override;
};
} // namespace frc

View File

@@ -0,0 +1,69 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 "RobotBase.h"
namespace frc {
/**
* IterativeRobotBase implements a specific type of robot program framework,
* extending the RobotBase class.
*
* The IterativeRobotBase class does not implement StartCompetition(), so it
* should not be used by teams directly.
*
* This class provides the following functions which are called by the main
* loop, StartCompetition(), at the appropriate times:
*
* RobotInit() -- provide for initialization at robot power-on
*
* Init() functions -- each of the following functions is called once when the
* appropriate mode is entered:
* - DisabledInit() -- called only when first disabled
* - AutonomousInit() -- called each and every time autonomous is entered from
* another mode
* - TeleopInit() -- called each and every time teleop is entered from
* another mode
* - TestInit() -- called each and every time test is entered from
* another mode
*
* Periodic() functions -- each of these functions is called on an interval:
* - RobotPeriodic()
* - DisabledPeriodic()
* - AutonomousPeriodic()
* - TeleopPeriodic()
* - TestPeriodic()
*/
class IterativeRobotBase : public RobotBase {
public:
virtual void RobotInit();
virtual void DisabledInit();
virtual void AutonomousInit();
virtual void TeleopInit();
virtual void TestInit();
virtual void RobotPeriodic();
virtual void DisabledPeriodic();
virtual void AutonomousPeriodic();
virtual void TeleopPeriodic();
virtual void TestPeriodic();
protected:
IterativeRobotBase();
virtual ~IterativeRobotBase() = default;
void LoopFunc();
private:
enum class Mode { kNone, kDisabled, kAutonomous, kTeleop, kTest };
Mode m_lastMode = Mode::kNone;
};
} // namespace frc

View File

@@ -25,8 +25,8 @@ class DriverStation;
} \
HAL_Report(HALUsageReporting::kResourceType_Language, \
HALUsageReporting::kLanguage_CPlusPlus); \
static _ClassName_ robot; \
llvm::outs() << "\n********** Robot program starting **********\n"; \
static _ClassName_ robot; \
robot.StartCompetition(); \
}

View File

@@ -27,7 +27,7 @@ class SampleRobot : public RobotBase {
virtual ~SampleRobot() = default;
private:
bool m_robotMainOverridden;
bool m_robotMainOverridden = true;
};
} // namespace frc

View File

@@ -0,0 +1,47 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2017. 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 <memory>
#include "IterativeRobotBase.h"
#include "Notifier.h"
namespace frc {
/**
* TimedRobot implements the IterativeRobotBase robot program framework.
*
* The TimedRobot class is intended to be subclassed by a user creating a
* robot program.
*
* Periodic() functions from the base class are called on an interval by a
* Notifier instance.
*/
class TimedRobot : public IterativeRobotBase {
public:
static constexpr double kDefaultPeriod = 0.02;
void StartCompetition() override;
void SetPeriod(double seconds);
protected:
TimedRobot();
virtual ~TimedRobot();
private:
double m_period = kDefaultPeriod;
// Prevents loop from starting if user calls SetPeriod() in RobotInit()
bool m_startLoop = false;
std::unique_ptr<Notifier> m_loop;
};
} // namespace frc

View File

@@ -77,6 +77,7 @@
#include "Talon.h"
#include "TalonSRX.h"
#include "Threads.h"
#include "TimedRobot.h"
#include "Timer.h"
#include "Ultrasonic.h"
#include "Utility.h"