2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "SensorBase.h"
|
|
|
|
|
|
|
|
|
|
#include "NetworkCommunication/LoadOut.h"
|
|
|
|
|
#include "WPIErrors.h"
|
2014-08-08 17:05:49 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
const uint32_t SensorBase::kDigitalChannels;
|
2014-06-12 18:07:45 -04:00
|
|
|
const uint32_t SensorBase::kAnalogInputs;
|
2013-12-15 18:30:16 -05:00
|
|
|
const uint32_t SensorBase::kSolenoidChannels;
|
|
|
|
|
const uint32_t SensorBase::kSolenoidModules;
|
|
|
|
|
const uint32_t SensorBase::kPwmChannels;
|
|
|
|
|
const uint32_t SensorBase::kRelayChannels;
|
2014-05-30 14:04:05 -04:00
|
|
|
const uint32_t SensorBase::kPDPChannels;
|
2013-12-15 18:30:16 -05:00
|
|
|
const uint32_t SensorBase::kChassisSlots;
|
2015-06-25 15:07:55 -04:00
|
|
|
SensorBase* SensorBase::m_singletonList = NULL;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-07-21 16:32:36 -04:00
|
|
|
static bool portsInitialized = false;
|
|
|
|
|
void* SensorBase::m_digital_ports[kDigitalChannels];
|
|
|
|
|
void* SensorBase::m_relay_ports[kRelayChannels];
|
|
|
|
|
void* SensorBase::m_pwm_ports[kPwmChannels];
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Creates an instance of the sensor base and gets an FPGA handle
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
SensorBase::SensorBase() {
|
|
|
|
|
if (!portsInitialized) {
|
|
|
|
|
for (uint32_t i = 0; i < kDigitalChannels; i++) {
|
|
|
|
|
void* port = getPort(i);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_digital_ports[i] = initializeDigitalPort(port, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < kRelayChannels; i++) {
|
|
|
|
|
void* port = getPort(i);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_relay_ports[i] = initializeDigitalPort(port, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < kPwmChannels; i++) {
|
|
|
|
|
void* port = getPort(i);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_pwm_ports[i] = initializeDigitalPort(port, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Frees the resources for a SensorBase.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
SensorBase::~SensorBase() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2015-06-25 15:07:55 -04:00
|
|
|
* resources and need to have their destructors called at the end of the
|
|
|
|
|
* program.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SensorBase::AddToSingletonList() {
|
|
|
|
|
m_nextSingleton = m_singletonList;
|
|
|
|
|
m_singletonList = this;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SensorBase::DeleteSingletons() {
|
|
|
|
|
for (SensorBase* next = m_singletonList; next != NULL;) {
|
|
|
|
|
SensorBase* tmp = next;
|
|
|
|
|
next = next->m_nextSingleton;
|
|
|
|
|
delete tmp;
|
|
|
|
|
}
|
|
|
|
|
m_singletonList = NULL;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the solenoid module number is valid.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Solenoid module is valid and present
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckSolenoidModule(uint8_t moduleNumber) {
|
|
|
|
|
if (moduleNumber < 64) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the digital channel number is valid.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Verify that the channel number is one of the legal channel numbers. Channel
|
|
|
|
|
* numbers are
|
2013-12-15 18:30:16 -05:00
|
|
|
* 1-based.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Digital channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckDigitalChannel(uint32_t channel) {
|
|
|
|
|
if (channel < kDigitalChannels) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the digital channel number is valid.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Verify that the channel number is one of the legal channel numbers. Channel
|
|
|
|
|
* numbers are
|
2013-12-15 18:30:16 -05:00
|
|
|
* 1-based.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Relay channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckRelayChannel(uint32_t channel) {
|
|
|
|
|
if (channel < kRelayChannels) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that the digital channel number is valid.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Verify that the channel number is one of the legal channel numbers. Channel
|
|
|
|
|
* numbers are
|
2013-12-15 18:30:16 -05:00
|
|
|
* 1-based.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return PWM channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckPWMChannel(uint32_t channel) {
|
|
|
|
|
if (channel < kPwmChannels) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-12 18:07:45 -04:00
|
|
|
* Check that the analog input number is value.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Verify that the analog input number is one of the legal channel numbers.
|
|
|
|
|
* Channel numbers
|
2014-06-12 18:07:45 -04:00
|
|
|
* are 0-based.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Analog channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckAnalogInput(uint32_t channel) {
|
|
|
|
|
if (channel < kAnalogInputs) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-06-12 18:07:45 -04:00
|
|
|
/**
|
2014-06-24 10:37:02 -07:00
|
|
|
* Check that the analog output number is valid.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Verify that the analog output number is one of the legal channel numbers.
|
|
|
|
|
* Channel numbers
|
2014-06-12 18:07:45 -04:00
|
|
|
* are 0-based.
|
|
|
|
|
*
|
|
|
|
|
* @return Analog channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckAnalogOutput(uint32_t channel) {
|
|
|
|
|
if (channel < kAnalogOutputs) return true;
|
|
|
|
|
return false;
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Verify that the solenoid channel number is within limits.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Solenoid channel is valid
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckSolenoidChannel(uint32_t channel) {
|
|
|
|
|
if (channel < kSolenoidChannels) return true;
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-30 14:04:05 -04:00
|
|
|
/**
|
|
|
|
|
* Verify that the power distribution channel number is within limits.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
2014-06-24 10:37:02 -07:00
|
|
|
* @return PDP channel is valid
|
2014-05-30 14:04:05 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool SensorBase::CheckPDPChannel(uint32_t channel) {
|
|
|
|
|
if (channel < kPDPChannels) return true;
|
|
|
|
|
return false;
|
2014-05-30 14:04:05 -04:00
|
|
|
}
|