blob: 2bd1d13acf996e754e8d310084a6d94a32054292 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "RobotBase.h"
9
10#include "DriverStation.h"
11#include "RobotState.h"
12#include "HLUsageReporting.h"
13#include "Internal/HardwareHLReporting.h"
14#include "Utility.h"
15#include "networktables/NetworkTable.h"
16#include <cstring>
17#include "HAL/HAL.hpp"
18#include <cstdio>
19
20RobotBase *RobotBase::m_instance = nullptr;
21
22void RobotBase::setInstance(RobotBase *robot) {
23 wpi_assert(m_instance == nullptr);
24 m_instance = robot;
25}
26
27RobotBase &RobotBase::getInstance() { return *m_instance; }
28
29void RobotBase::robotSetup(RobotBase *robot) {
30 robot->StartCompetition();
31}
32
33/**
34 * Constructor for a generic robot program.
35 * User code should be placed in the constructor that runs before the Autonomous
36 * or Operator
37 * Control period starts. The constructor will run to completion before
38 * Autonomous is entered.
39 *
40 * This must be used to ensure that the communications code starts. In the
41 * future it would be
42 * nice to put this code into it's own task that loads on boot so ensure that it
43 * runs.
44 */
45RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
46 RobotState::SetImplementation(DriverStation::GetInstance());
47 HLUsageReporting::SetImplementation(new HardwareHLReporting());
48
49 RobotBase::setInstance(this);
50
51 NetworkTable::SetNetworkIdentity("Robot");
52
53 FILE *file = nullptr;
54 file = fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
55
56 if (file != nullptr) {
57 fputs("2016 C++ Beta5.0", file);
58 fclose(file);
59 }
60}
61
62/**
63 * Free the resources for a RobotBase class.
64 * This includes deleting all classes that might have been allocated as
65 * Singletons to they
66 * would never be deleted except here.
67 */
68RobotBase::~RobotBase() {
69 SensorBase::DeleteSingletons();
70 delete m_task;
71 m_task = nullptr;
72 m_instance = nullptr;
73}
74
75/**
76 * Determine if the Robot is currently enabled.
77 * @return True if the Robot is currently enabled by the field controls.
78 */
79bool RobotBase::IsEnabled() const { return m_ds.IsEnabled(); }
80
81/**
82 * Determine if the Robot is currently disabled.
83 * @return True if the Robot is currently disabled by the field controls.
84 */
85bool RobotBase::IsDisabled() const { return m_ds.IsDisabled(); }
86
87/**
88 * Determine if the robot is currently in Autonomous mode.
89 * @return True if the robot is currently operating Autonomously as determined
90 * by the field controls.
91 */
92bool RobotBase::IsAutonomous() const { return m_ds.IsAutonomous(); }
93
94/**
95 * Determine if the robot is currently in Operator Control mode.
96 * @return True if the robot is currently operating in Tele-Op mode as
97 * determined by the field controls.
98 */
99bool RobotBase::IsOperatorControl() const { return m_ds.IsOperatorControl(); }
100
101/**
102 * Determine if the robot is currently in Test mode.
103 * @return True if the robot is currently running tests as determined by the
104 * field controls.
105 */
106bool RobotBase::IsTest() const { return m_ds.IsTest(); }
107
108/**
109 * Indicates if new data is available from the driver station.
110 * @return Has new data arrived over the network since the last time this
111 * function was called?
112 */
113bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); }
114
115/**
116 * This class exists for the sole purpose of getting its destructor called when
117 * the module unloads.
118 * Before the module is done unloading, we need to delete the RobotBase derived
119 * singleton. This should delete
120 * the other remaining singletons that were registered. This should also stop
121 * all tasks that are using
122 * the Task class.
123 */
124class RobotDeleter {
125 public:
126 RobotDeleter() {}
127 ~RobotDeleter() { delete &RobotBase::getInstance(); }
128};
129static RobotDeleter g_robotDeleter;