blob: a1087c0b472dd6c7702142ed8f343e23ffb61a9e [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/*----------------------------------------------------------------------------*/
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) {
Brian Silverman1a675112016-02-20 20:42:49 -050030 printf("\n********** Robot program starting **********\n");
Brian Silverman26e4e522015-12-17 01:56:40 -050031 robot->StartCompetition();
32}
33
34/**
35 * Constructor for a generic robot program.
36 * User code should be placed in the constructor that runs before the Autonomous
37 * or Operator
38 * Control period starts. The constructor will run to completion before
39 * Autonomous is entered.
40 *
41 * This must be used to ensure that the communications code starts. In the
42 * future it would be
43 * nice to put this code into it's own task that loads on boot so ensure that it
44 * runs.
45 */
46RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
47 RobotState::SetImplementation(DriverStation::GetInstance());
48 HLUsageReporting::SetImplementation(new HardwareHLReporting());
49
50 RobotBase::setInstance(this);
51
52 NetworkTable::SetNetworkIdentity("Robot");
Brian Silverman1a675112016-02-20 20:42:49 -050053 NetworkTable::SetPersistentFilename("/home/lvuser/networktables.ini");
Brian Silverman26e4e522015-12-17 01:56:40 -050054
55 FILE *file = nullptr;
56 file = fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
57
58 if (file != nullptr) {
Brian Silverman1a675112016-02-20 20:42:49 -050059 fputs("2016 C++ Release 4", file);
Brian Silverman26e4e522015-12-17 01:56:40 -050060 fclose(file);
61 }
62}
63
64/**
65 * Free the resources for a RobotBase class.
66 * This includes deleting all classes that might have been allocated as
67 * Singletons to they
68 * would never be deleted except here.
69 */
70RobotBase::~RobotBase() {
71 SensorBase::DeleteSingletons();
72 delete m_task;
73 m_task = nullptr;
74 m_instance = nullptr;
75}
76
77/**
78 * Determine if the Robot is currently enabled.
79 * @return True if the Robot is currently enabled by the field controls.
80 */
81bool RobotBase::IsEnabled() const { return m_ds.IsEnabled(); }
82
83/**
84 * Determine if the Robot is currently disabled.
85 * @return True if the Robot is currently disabled by the field controls.
86 */
87bool RobotBase::IsDisabled() const { return m_ds.IsDisabled(); }
88
89/**
90 * Determine if the robot is currently in Autonomous mode.
91 * @return True if the robot is currently operating Autonomously as determined
92 * by the field controls.
93 */
94bool RobotBase::IsAutonomous() const { return m_ds.IsAutonomous(); }
95
96/**
97 * Determine if the robot is currently in Operator Control mode.
98 * @return True if the robot is currently operating in Tele-Op mode as
99 * determined by the field controls.
100 */
101bool RobotBase::IsOperatorControl() const { return m_ds.IsOperatorControl(); }
102
103/**
104 * Determine if the robot is currently in Test mode.
105 * @return True if the robot is currently running tests as determined by the
106 * field controls.
107 */
108bool RobotBase::IsTest() const { return m_ds.IsTest(); }
109
110/**
111 * Indicates if new data is available from the driver station.
112 * @return Has new data arrived over the network since the last time this
113 * function was called?
114 */
115bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); }
116
117/**
118 * This class exists for the sole purpose of getting its destructor called when
119 * the module unloads.
120 * Before the module is done unloading, we need to delete the RobotBase derived
121 * singleton. This should delete
122 * the other remaining singletons that were registered. This should also stop
123 * all tasks that are using
124 * the Task class.
125 */
126class RobotDeleter {
127 public:
128 RobotDeleter() {}
129 ~RobotDeleter() { delete &RobotBase::getInstance(); }
130};
131static RobotDeleter g_robotDeleter;