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 | /*----------------------------------------------------------------------------*/ |
| 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 | |
| 20 | RobotBase *RobotBase::m_instance = nullptr; |
| 21 | |
| 22 | void RobotBase::setInstance(RobotBase *robot) { |
| 23 | wpi_assert(m_instance == nullptr); |
| 24 | m_instance = robot; |
| 25 | } |
| 26 | |
| 27 | RobotBase &RobotBase::getInstance() { return *m_instance; } |
| 28 | |
| 29 | void RobotBase::robotSetup(RobotBase *robot) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 30 | printf("\n********** Robot program starting **********\n"); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 31 | 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 | */ |
| 46 | RobotBase::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 Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 53 | NetworkTable::SetPersistentFilename("/home/lvuser/networktables.ini"); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 54 | |
| 55 | FILE *file = nullptr; |
| 56 | file = fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w"); |
| 57 | |
| 58 | if (file != nullptr) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 59 | fputs("2016 C++ Release 4", file); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 60 | 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 | */ |
| 70 | RobotBase::~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 | */ |
| 81 | bool 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 | */ |
| 87 | bool 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 | */ |
| 94 | bool 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 | */ |
| 101 | bool 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 | */ |
| 108 | bool 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 | */ |
| 115 | bool 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 | */ |
| 126 | class RobotDeleter { |
| 127 | public: |
| 128 | RobotDeleter() {} |
| 129 | ~RobotDeleter() { delete &RobotBase::getInstance(); } |
| 130 | }; |
| 131 | static RobotDeleter g_robotDeleter; |