Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 | |
| 7 | #include "RobotBase.h" |
| 8 | #include "RobotState.h" |
| 9 | #include "Utility.h" |
| 10 | |
| 11 | #include <cstring> |
| 12 | |
| 13 | RobotBase* RobotBase::m_instance = nullptr; |
| 14 | |
| 15 | void RobotBase::setInstance(RobotBase* robot) |
| 16 | { |
| 17 | wpi_assert(m_instance == nullptr); |
| 18 | m_instance = robot; |
| 19 | } |
| 20 | |
| 21 | RobotBase &RobotBase::getInstance() |
| 22 | { |
| 23 | return *m_instance; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Constructor for a generic robot program. |
| 28 | * User code should be placed in the constuctor that runs before the Autonomous or Operator |
| 29 | * Control period starts. The constructor will run to completion before Autonomous is entered. |
| 30 | * |
| 31 | * This must be used to ensure that the communications code starts. In the future it would be |
| 32 | * nice to put this code into it's own task that loads on boot so ensure that it runs. |
| 33 | */ |
| 34 | RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) |
| 35 | { |
| 36 | RobotState::SetImplementation(DriverStation::GetInstance()); |
| 37 | transport::SubscriberPtr time_pub = MainNode::Subscribe("time", &wpilib::internal::time_callback); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Determine if the Robot is currently enabled. |
| 42 | * @return True if the Robot is currently enabled by the field controls. |
| 43 | */ |
| 44 | bool RobotBase::IsEnabled() const |
| 45 | { |
| 46 | return m_ds.IsEnabled(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Determine if the Robot is currently disabled. |
| 51 | * @return True if the Robot is currently disabled by the field controls. |
| 52 | */ |
| 53 | bool RobotBase::IsDisabled() const |
| 54 | { |
| 55 | return m_ds.IsDisabled(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Determine if the robot is currently in Autnomous mode. |
| 60 | * @return True if the robot is currently operating Autonomously as determined by the field controls. |
| 61 | */ |
| 62 | bool RobotBase::IsAutonomous() const |
| 63 | { |
| 64 | return m_ds.IsAutonomous(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Determine if the robot is currently in Operator Control mode. |
| 69 | * @return True if the robot is currently operating in Tele-Op mode as determined by the field controls. |
| 70 | */ |
| 71 | bool RobotBase::IsOperatorControl() const |
| 72 | { |
| 73 | return m_ds.IsOperatorControl(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Determine if the robot is currently in Test mode. |
| 78 | * @return True if the robot is currently running tests as determined by the field controls. |
| 79 | */ |
| 80 | bool RobotBase::IsTest() const |
| 81 | { |
| 82 | return m_ds.IsTest(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * This class exists for the sole purpose of getting its destructor called when the module unloads. |
| 87 | * Before the module is done unloading, we need to delete the RobotBase derived singleton. This should delete |
| 88 | * the other remaining singletons that were registered. This should also stop all tasks that are using |
| 89 | * the Task class. |
| 90 | */ |
| 91 | class RobotDeleter |
| 92 | { |
| 93 | public: |
| 94 | ~RobotDeleter() |
| 95 | { |
| 96 | delete &RobotBase::getInstance(); |
| 97 | } |
| 98 | }; |
| 99 | static RobotDeleter g_robotDeleter; |