Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2016-2017. 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 the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "HAL/AnalogAccumulator.h" |
| 9 | |
| 10 | #include "AnalogInternal.h" |
| 11 | #include "HAL/HAL.h" |
| 12 | |
| 13 | using namespace hal; |
| 14 | |
| 15 | extern "C" { |
| 16 | /** |
| 17 | * Is the channel attached to an accumulator. |
| 18 | * |
| 19 | * @param analogPortHandle Handle to the analog port. |
| 20 | * @return The analog channel is attached to an accumulator. |
| 21 | */ |
| 22 | HAL_Bool HAL_IsAccumulatorChannel(HAL_AnalogInputHandle analogPortHandle, |
| 23 | int32_t* status) { |
| 24 | auto port = analogInputHandles.Get(analogPortHandle); |
| 25 | if (port == nullptr) { |
| 26 | *status = HAL_HANDLE_ERROR; |
| 27 | return false; |
| 28 | } |
| 29 | for (int32_t i = 0; i < kNumAccumulators; i++) { |
| 30 | if (port->channel == kAccumulatorChannels[i]) return true; |
| 31 | } |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Initialize the accumulator. |
| 37 | * |
| 38 | * @param analogPortHandle Handle to the analog port. |
| 39 | */ |
| 40 | void HAL_InitAccumulator(HAL_AnalogInputHandle analogPortHandle, |
| 41 | int32_t* status) { |
| 42 | if (!HAL_IsAccumulatorChannel(analogPortHandle, status)) { |
| 43 | *status = HAL_INVALID_ACCUMULATOR_CHANNEL; |
| 44 | return; |
| 45 | } |
| 46 | HAL_SetAccumulatorCenter(analogPortHandle, 0, status); |
| 47 | HAL_ResetAccumulator(analogPortHandle, status); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Resets the accumulator to the initial value. |
| 52 | * |
| 53 | * @param analogPortHandle Handle to the analog port. |
| 54 | */ |
| 55 | void HAL_ResetAccumulator(HAL_AnalogInputHandle analogPortHandle, |
| 56 | int32_t* status) { |
| 57 | auto port = analogInputHandles.Get(analogPortHandle); |
| 58 | if (port == nullptr) { |
| 59 | *status = HAL_HANDLE_ERROR; |
| 60 | return; |
| 61 | } |
| 62 | if (port->accumulator == nullptr) { |
| 63 | *status = NULL_PARAMETER; |
| 64 | return; |
| 65 | } |
| 66 | port->accumulator->strobeReset(status); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set the center value of the accumulator. |
| 71 | * |
| 72 | * The center value is subtracted from each A/D value before it is added to the |
| 73 | * accumulator. This is used for the center value of devices like gyros and |
| 74 | * accelerometers to make integration work and to take the device offset into |
| 75 | * account when integrating. |
| 76 | * |
| 77 | * This center value is based on the output of the oversampled and averaged |
| 78 | * source from channel 1. Because of this, any non-zero oversample bits will |
| 79 | * affect the size of the value for this field. |
| 80 | * |
| 81 | * @param analogPortHandle Handle to the analog port. |
| 82 | * @param center The center value of the accumulator. |
| 83 | */ |
| 84 | void HAL_SetAccumulatorCenter(HAL_AnalogInputHandle analogPortHandle, |
| 85 | int32_t center, int32_t* status) { |
| 86 | auto port = analogInputHandles.Get(analogPortHandle); |
| 87 | if (port == nullptr) { |
| 88 | *status = HAL_HANDLE_ERROR; |
| 89 | return; |
| 90 | } |
| 91 | if (port->accumulator == nullptr) { |
| 92 | *status = NULL_PARAMETER; |
| 93 | return; |
| 94 | } |
| 95 | port->accumulator->writeCenter(center, status); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set the accumulator's deadband. |
| 100 | * |
| 101 | * @param analogPortHandle Handle to the analog port. |
| 102 | * @param deadband The deadband of the accumulator. |
| 103 | */ |
| 104 | void HAL_SetAccumulatorDeadband(HAL_AnalogInputHandle analogPortHandle, |
| 105 | int32_t deadband, int32_t* status) { |
| 106 | auto port = analogInputHandles.Get(analogPortHandle); |
| 107 | if (port == nullptr) { |
| 108 | *status = HAL_HANDLE_ERROR; |
| 109 | return; |
| 110 | } |
| 111 | if (port->accumulator == nullptr) { |
| 112 | *status = NULL_PARAMETER; |
| 113 | return; |
| 114 | } |
| 115 | port->accumulator->writeDeadband(deadband, status); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Read the accumulated value. |
| 120 | * |
| 121 | * Read the value that has been accumulating on channel 1. |
| 122 | * The accumulator is attached after the oversample and average engine. |
| 123 | * |
| 124 | * @param analogPortHandle Handle to the analog port. |
| 125 | * @return The 64-bit value accumulated since the last Reset(). |
| 126 | */ |
| 127 | int64_t HAL_GetAccumulatorValue(HAL_AnalogInputHandle analogPortHandle, |
| 128 | int32_t* status) { |
| 129 | auto port = analogInputHandles.Get(analogPortHandle); |
| 130 | if (port == nullptr) { |
| 131 | *status = HAL_HANDLE_ERROR; |
| 132 | return 0; |
| 133 | } |
| 134 | if (port->accumulator == nullptr) { |
| 135 | *status = NULL_PARAMETER; |
| 136 | return 0; |
| 137 | } |
| 138 | int64_t value = port->accumulator->readOutput_Value(status); |
| 139 | return value; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Read the number of accumulated values. |
| 144 | * |
| 145 | * Read the count of the accumulated values since the accumulator was last |
| 146 | * Reset(). |
| 147 | * |
| 148 | * @param analogPortHandle Handle to the analog port. |
| 149 | * @return The number of times samples from the channel were accumulated. |
| 150 | */ |
| 151 | int64_t HAL_GetAccumulatorCount(HAL_AnalogInputHandle analogPortHandle, |
| 152 | int32_t* status) { |
| 153 | auto port = analogInputHandles.Get(analogPortHandle); |
| 154 | if (port == nullptr) { |
| 155 | *status = HAL_HANDLE_ERROR; |
| 156 | return 0; |
| 157 | } |
| 158 | if (port->accumulator == nullptr) { |
| 159 | *status = NULL_PARAMETER; |
| 160 | return 0; |
| 161 | } |
| 162 | return port->accumulator->readOutput_Count(status); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Read the accumulated value and the number of accumulated values atomically. |
| 167 | * |
| 168 | * This function reads the value and count from the FPGA atomically. |
| 169 | * This can be used for averaging. |
| 170 | * |
| 171 | * @param analogPortHandle Handle to the analog port. |
| 172 | * @param value Pointer to the 64-bit accumulated output. |
| 173 | * @param count Pointer to the number of accumulation cycles. |
| 174 | */ |
| 175 | void HAL_GetAccumulatorOutput(HAL_AnalogInputHandle analogPortHandle, |
| 176 | int64_t* value, int64_t* count, int32_t* status) { |
| 177 | auto port = analogInputHandles.Get(analogPortHandle); |
| 178 | if (port == nullptr) { |
| 179 | *status = HAL_HANDLE_ERROR; |
| 180 | return; |
| 181 | } |
| 182 | if (port->accumulator == nullptr) { |
| 183 | *status = NULL_PARAMETER; |
| 184 | return; |
| 185 | } |
| 186 | if (value == nullptr || count == nullptr) { |
| 187 | *status = NULL_PARAMETER; |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | tAccumulator::tOutput output = port->accumulator->readOutput(status); |
| 192 | |
| 193 | *value = output.Value; |
| 194 | *count = output.Count; |
| 195 | } |
| 196 | } |