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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2014-07-22 18:04:00 -04:00
|
|
|
#include "AnalogAccelerometer.h"
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common function for initializing the accelerometer.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogAccelerometer::InitAccelerometer() {
|
|
|
|
|
m_table = NULL;
|
|
|
|
|
m_voltsPerG = 1.0;
|
|
|
|
|
m_zeroGVoltage = 2.5;
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Accelerometer,
|
|
|
|
|
m_AnalogInput->GetChannel());
|
|
|
|
|
LiveWindow::GetInstance()->AddSensor("Accelerometer",
|
|
|
|
|
m_AnalogInput->GetChannel(), this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new instance of an accelerometer.
|
2014-06-13 17:45:10 -04:00
|
|
|
* The constructor allocates desired analog input.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The channel number for the analog input the accelerometer is
|
|
|
|
|
* connected to
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogAccelerometer::AnalogAccelerometer(int32_t channel) {
|
|
|
|
|
m_AnalogInput = new AnalogInput(channel);
|
|
|
|
|
m_allocatedChannel = true;
|
|
|
|
|
InitAccelerometer();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-12 18:07:45 -04:00
|
|
|
* Create a new instance of Accelerometer from an existing AnalogInput.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Make a new instance of accelerometer given an AnalogInput. This is
|
|
|
|
|
* particularly
|
|
|
|
|
* useful if the port is going to be read as an analog channel as well as
|
|
|
|
|
* through
|
2013-12-15 18:30:16 -05:00
|
|
|
* the Accelerometer class.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The existing AnalogInput object for the analog input the
|
|
|
|
|
* accelerometer is connected to
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogAccelerometer::AnalogAccelerometer(AnalogInput *channel) {
|
|
|
|
|
if (channel == NULL) {
|
|
|
|
|
wpi_setWPIError(NullParameter);
|
|
|
|
|
} else {
|
|
|
|
|
m_AnalogInput = channel;
|
|
|
|
|
InitAccelerometer();
|
|
|
|
|
}
|
|
|
|
|
m_allocatedChannel = false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Delete the analog components used for the accelerometer.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogAccelerometer::~AnalogAccelerometer() {
|
|
|
|
|
if (m_allocatedChannel) {
|
|
|
|
|
delete m_AnalogInput;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the acceleration in Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* The acceleration is returned units of Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current acceleration of the sensor in Gs.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float AnalogAccelerometer::GetAcceleration() const {
|
|
|
|
|
return (m_AnalogInput->GetAverageVoltage() - m_zeroGVoltage) / m_voltsPerG;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the accelerometer sensitivity.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This sets the sensitivity of the accelerometer used for calculating the
|
|
|
|
|
* acceleration.
|
|
|
|
|
* The sensitivity varies by accelerometer model. There are constants defined
|
|
|
|
|
* for various models.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param sensitivity The sensitivity of accelerometer in Volts per G.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogAccelerometer::SetSensitivity(float sensitivity) {
|
|
|
|
|
m_voltsPerG = sensitivity;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the voltage that corresponds to 0 G.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* The zero G voltage varies by accelerometer model. There are constants defined
|
|
|
|
|
* for various models.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param zero The zero G voltage.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogAccelerometer::SetZero(float zero) { m_zeroGVoltage = zero; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Acceleration for the PID Source parent.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current acceleration in Gs.
|
2014-06-13 17:45:10 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double AnalogAccelerometer::PIDGet() const { return GetAcceleration(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-07-22 18:04:00 -04:00
|
|
|
void AnalogAccelerometer::UpdateTable() {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_table != NULL) {
|
|
|
|
|
m_table->PutNumber("Value", GetAcceleration());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogAccelerometer::StartLiveWindowMode() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogAccelerometer::StopLiveWindowMode() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string AnalogAccelerometer::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "Accelerometer";
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-07-22 18:04:00 -04:00
|
|
|
void AnalogAccelerometer::InitTable(ITable *subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *AnalogAccelerometer::GetTable() const { return m_table; }
|