mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
Cleaned up robot startup and cleanup/shutdown code (#77)
Cleaned up RobotBase, removed singleton list from SensorBase, and removed unused typedefs and NULL_TASK macro from HAL's Task.hpp. Making the robot class instance static fixed non-POD statics used by the instance during destruction from being destroyed first.
This commit is contained in:
committed by
Peter Johnson
parent
ecc210f99a
commit
d66c61a36e
@@ -58,8 +58,6 @@ DriverStation::DriverStation() {
|
||||
// It will signal when new packet data is available.
|
||||
HALSetNewDataSem(&m_packetDataAvailableCond);
|
||||
|
||||
AddToSingletonList();
|
||||
|
||||
m_task = Task("DriverStation", &DriverStation::Run, this);
|
||||
}
|
||||
|
||||
@@ -104,8 +102,8 @@ void DriverStation::Run() {
|
||||
* @return Pointer to the DS instance
|
||||
*/
|
||||
DriverStation& DriverStation::GetInstance() {
|
||||
static DriverStation* instance = new DriverStation();
|
||||
return *instance;
|
||||
static DriverStation instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,20 +17,6 @@
|
||||
#include "Utility.h"
|
||||
#include "networktables/NetworkTable.h"
|
||||
|
||||
RobotBase* RobotBase::m_instance = nullptr;
|
||||
|
||||
void RobotBase::setInstance(RobotBase* robot) {
|
||||
wpi_assert(m_instance == nullptr);
|
||||
m_instance = robot;
|
||||
}
|
||||
|
||||
RobotBase& RobotBase::getInstance() { return *m_instance; }
|
||||
|
||||
void RobotBase::robotSetup(RobotBase* robot) {
|
||||
std::printf("\n********** Robot program starting **********\n");
|
||||
robot->StartCompetition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for a generic robot program.
|
||||
*
|
||||
@@ -46,8 +32,6 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
|
||||
RobotState::SetImplementation(DriverStation::GetInstance());
|
||||
HLUsageReporting::SetImplementation(new HardwareHLReporting());
|
||||
|
||||
RobotBase::setInstance(this);
|
||||
|
||||
NetworkTable::SetNetworkIdentity("Robot");
|
||||
NetworkTable::SetPersistentFilename("/home/lvuser/networktables.ini");
|
||||
|
||||
@@ -60,18 +44,6 @@ RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources for a RobotBase class.
|
||||
* This includes deleting all classes that might have been allocated as
|
||||
* Singletons to they would never be deleted except here.
|
||||
*/
|
||||
RobotBase::~RobotBase() {
|
||||
SensorBase::DeleteSingletons();
|
||||
delete m_task;
|
||||
m_task = nullptr;
|
||||
m_instance = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the Robot is currently enabled.
|
||||
* @return True if the Robot is currently enabled by the field controls.
|
||||
@@ -111,17 +83,3 @@ bool RobotBase::IsTest() const { return m_ds.IsTest(); }
|
||||
* function was called?
|
||||
*/
|
||||
bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); }
|
||||
|
||||
/**
|
||||
* This class exists for the sole purpose of getting its destructor called when
|
||||
* the module unloads.
|
||||
* Before the module is done unloading, we need to delete the RobotBase derived
|
||||
* singleton. This should delete the other remaining singletons that were
|
||||
* registered. This should also stop all tasks that are using the Task class.
|
||||
*/
|
||||
class RobotDeleter {
|
||||
public:
|
||||
RobotDeleter() {}
|
||||
~RobotDeleter() { delete &RobotBase::getInstance(); }
|
||||
};
|
||||
static RobotDeleter g_robotDeleter;
|
||||
|
||||
@@ -19,7 +19,6 @@ const uint32_t SensorBase::kPwmChannels;
|
||||
const uint32_t SensorBase::kRelayChannels;
|
||||
const uint32_t SensorBase::kPDPChannels;
|
||||
const uint32_t SensorBase::kChassisSlots;
|
||||
SensorBase* SensorBase::m_singletonList = nullptr;
|
||||
|
||||
static bool portsInitialized = false;
|
||||
void* SensorBase::m_digital_ports[kDigitalChannels];
|
||||
@@ -57,37 +56,6 @@ SensorBase::SensorBase() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add sensor to the singleton list.
|
||||
*
|
||||
* Add this sensor to the list of singletons that need to be deleted when
|
||||
* the robot program exits. Each of the sensors on this list are singletons,
|
||||
* that is they aren't allocated directly with new, but instead are allocated
|
||||
* by the static GetInstance method. As a result, they are never deleted when
|
||||
* the program exits. Consequently these sensors may still be holding onto
|
||||
* resources and need to have their destructors called at the end of the
|
||||
* program.
|
||||
*/
|
||||
void SensorBase::AddToSingletonList() {
|
||||
m_nextSingleton = m_singletonList;
|
||||
m_singletonList = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all the singleton classes on the list.
|
||||
*
|
||||
* All the classes that were allocated as singletons need to be deleted so
|
||||
* their resources can be freed.
|
||||
*/
|
||||
void SensorBase::DeleteSingletons() {
|
||||
for (SensorBase* next = m_singletonList; next != nullptr;) {
|
||||
SensorBase* tmp = next;
|
||||
next = next->m_nextSingleton;
|
||||
delete tmp;
|
||||
}
|
||||
m_singletonList = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the solenoid module number is valid.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user