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