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 "AnalogAccelerometer.h" |
| 9 | #include "WPIErrors.h" |
| 10 | #include "LiveWindow/LiveWindow.h" |
| 11 | |
| 12 | /** |
| 13 | * Common function for initializing the accelerometer. |
| 14 | */ |
| 15 | void AnalogAccelerometer::InitAccelerometer() { |
| 16 | HALReport(HALUsageReporting::kResourceType_Accelerometer, |
| 17 | m_analogInput->GetChannel()); |
| 18 | LiveWindow::GetInstance()->AddSensor("Accelerometer", |
| 19 | m_analogInput->GetChannel(), this); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Create a new instance of an accelerometer. |
| 24 | * The constructor allocates desired analog input. |
| 25 | * @param channel The channel number for the analog input the accelerometer is |
| 26 | * connected to |
| 27 | */ |
| 28 | AnalogAccelerometer::AnalogAccelerometer(int32_t channel) { |
| 29 | m_analogInput = std::make_shared<AnalogInput>(channel); |
| 30 | InitAccelerometer(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Create a new instance of Accelerometer from an existing AnalogInput. |
| 35 | * Make a new instance of accelerometer given an AnalogInput. This is |
| 36 | * particularly useful if the port is going to be read as an analog channel as |
| 37 | * well as through the Accelerometer class. |
| 38 | * @param channel The existing AnalogInput object for the analog input the |
| 39 | * accelerometer is connected to |
| 40 | */ |
| 41 | AnalogAccelerometer::AnalogAccelerometer(AnalogInput *channel) |
| 42 | : m_analogInput(channel, NullDeleter<AnalogInput>()) { |
| 43 | if (channel == nullptr) { |
| 44 | wpi_setWPIError(NullParameter); |
| 45 | } else { |
| 46 | InitAccelerometer(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Create a new instance of Accelerometer from an existing AnalogInput. |
| 52 | * Make a new instance of accelerometer given an AnalogInput. This is |
| 53 | * particularly useful if the port is going to be read as an analog channel as |
| 54 | * well as through the Accelerometer class. |
| 55 | * @param channel The existing AnalogInput object for the analog input the |
| 56 | * accelerometer is connected to |
| 57 | */ |
| 58 | AnalogAccelerometer::AnalogAccelerometer(std::shared_ptr<AnalogInput> channel) |
| 59 | : m_analogInput(channel) { |
| 60 | if (channel == nullptr) { |
| 61 | wpi_setWPIError(NullParameter); |
| 62 | } else { |
| 63 | InitAccelerometer(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return the acceleration in Gs. |
| 69 | * |
| 70 | * The acceleration is returned units of Gs. |
| 71 | * |
| 72 | * @return The current acceleration of the sensor in Gs. |
| 73 | */ |
| 74 | float AnalogAccelerometer::GetAcceleration() const { |
| 75 | return (m_analogInput->GetAverageVoltage() - m_zeroGVoltage) / m_voltsPerG; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Set the accelerometer sensitivity. |
| 80 | * |
| 81 | * This sets the sensitivity of the accelerometer used for calculating the |
| 82 | * acceleration. |
| 83 | * The sensitivity varies by accelerometer model. There are constants defined |
| 84 | * for various models. |
| 85 | * |
| 86 | * @param sensitivity The sensitivity of accelerometer in Volts per G. |
| 87 | */ |
| 88 | void AnalogAccelerometer::SetSensitivity(float sensitivity) { |
| 89 | m_voltsPerG = sensitivity; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the voltage that corresponds to 0 G. |
| 94 | * |
| 95 | * The zero G voltage varies by accelerometer model. There are constants defined |
| 96 | * for various models. |
| 97 | * |
| 98 | * @param zero The zero G voltage. |
| 99 | */ |
| 100 | void AnalogAccelerometer::SetZero(float zero) { m_zeroGVoltage = zero; } |
| 101 | |
| 102 | /** |
| 103 | * Get the Acceleration for the PID Source parent. |
| 104 | * |
| 105 | * @return The current acceleration in Gs. |
| 106 | */ |
| 107 | double AnalogAccelerometer::PIDGet() { return GetAcceleration(); } |
| 108 | |
| 109 | void AnalogAccelerometer::UpdateTable() { |
| 110 | if (m_table != nullptr) { |
| 111 | m_table->PutNumber("Value", GetAcceleration()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void AnalogAccelerometer::StartLiveWindowMode() {} |
| 116 | |
| 117 | void AnalogAccelerometer::StopLiveWindowMode() {} |
| 118 | |
| 119 | std::string AnalogAccelerometer::GetSmartDashboardType() const { |
| 120 | return "Accelerometer"; |
| 121 | } |
| 122 | |
| 123 | void AnalogAccelerometer::InitTable(std::shared_ptr<ITable> subTable) { |
| 124 | m_table = subTable; |
| 125 | UpdateTable(); |
| 126 | } |
| 127 | |
| 128 | std::shared_ptr<ITable> AnalogAccelerometer::GetTable() const { return m_table; } |