Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 7 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 8 | #pragma once |
| 9 | |
| 10 | #include "Timer.h" |
| 11 | #include "RobotBase.h" |
| 12 | |
| 13 | /** |
| 14 | * IterativeRobot implements a specific type of Robot Program framework, extending the RobotBase class. |
| 15 | * |
| 16 | * The IterativeRobot class is intended to be subclassed by a user creating a robot program. |
| 17 | * |
| 18 | * This class is intended to implement the "old style" default code, by providing |
| 19 | * the following functions which are called by the main loop, StartCompetition(), at the appropriate times: |
| 20 | * |
| 21 | * RobotInit() -- provide for initialization at robot power-on |
| 22 | * |
| 23 | * Init() functions -- each of the following functions is called once when the |
| 24 | * appropriate mode is entered: |
| 25 | * - DisabledInit() -- called only when first disabled |
| 26 | * - AutonomousInit() -- called each and every time autonomous is entered from another mode |
| 27 | * - TeleopInit() -- called each and every time teleop is entered from another mode |
| 28 | * - TestInit() -- called each and every time test is entered from another mode |
| 29 | * |
| 30 | * Periodic() functions -- each of these functions is called iteratively at the |
| 31 | * appropriate periodic rate (aka the "slow loop"). The default period of |
| 32 | * the iterative robot is synced to the driver station control packets, |
| 33 | * giving a periodic frequency of about 50Hz (50 times per second). |
| 34 | * - DisabledPeriodic() |
| 35 | * - AutonomousPeriodic() |
| 36 | * - TeleopPeriodic() |
| 37 | * - TestPeriodic() |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | class IterativeRobot : public RobotBase |
| 42 | { |
| 43 | public: |
| 44 | /* |
| 45 | * The default period for the periodic function calls (seconds) |
| 46 | * Setting the period to 0.0 will cause the periodic functions to follow |
| 47 | * the Driver Station packet rate of about 50Hz. |
| 48 | */ |
| 49 | static const double kDefaultPeriod; |
| 50 | |
| 51 | virtual void StartCompetition(); |
| 52 | |
| 53 | virtual void RobotInit(); |
| 54 | virtual void DisabledInit(); |
| 55 | virtual void AutonomousInit(); |
| 56 | virtual void TeleopInit(); |
| 57 | virtual void TestInit(); |
| 58 | |
| 59 | virtual void DisabledPeriodic(); |
| 60 | virtual void AutonomousPeriodic(); |
| 61 | virtual void TeleopPeriodic(); |
| 62 | virtual void TestPeriodic(); |
| 63 | |
| 64 | void SetPeriod(double period); |
| 65 | double GetPeriod(); |
| 66 | double GetLoopsPerSec(); |
| 67 | |
| 68 | protected: |
| 69 | virtual ~IterativeRobot() = default; |
| 70 | IterativeRobot() = default; |
| 71 | |
| 72 | private: |
| 73 | bool NextPeriodReady(); |
| 74 | |
| 75 | bool m_disabledInitialized = false; |
| 76 | bool m_autonomousInitialized = false; |
| 77 | bool m_teleopInitialized = false; |
| 78 | bool m_testInitialized = false; |
| 79 | double m_period = kDefaultPeriod; |
| 80 | Timer m_mainLoopTimer; |
| 81 | }; |