blob: 8a4cd4dbca2b56f1920c5e0dd2051546be840ed9 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#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
41class IterativeRobot : public RobotBase
42{
43public:
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
68protected:
69 virtual ~IterativeRobot() = default;
70 IterativeRobot() = default;
71
72private:
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};