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 "Ultrasonic.h" |
| 9 | |
| 10 | #include "Counter.h" |
| 11 | #include "DigitalInput.h" |
| 12 | #include "DigitalOutput.h" |
| 13 | #include "Timer.h" |
| 14 | #include "Utility.h" |
| 15 | #include "WPIErrors.h" |
| 16 | #include "LiveWindow/LiveWindow.h" |
| 17 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 18 | // Time (sec) for the ping trigger pulse. |
| 19 | constexpr double Ultrasonic::kPingTime; |
| 20 | // Priority that the ultrasonic round robin task runs. |
| 21 | const uint32_t Ultrasonic::kPriority; |
| 22 | // Max time (ms) between readings. |
| 23 | constexpr double Ultrasonic::kMaxUltrasonicTime; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 24 | constexpr double Ultrasonic::kSpeedOfSoundInchesPerSec; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 25 | Task Ultrasonic::m_task; |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 26 | // automatic round robin mode |
| 27 | std::atomic<bool> Ultrasonic::m_automaticEnabled{false}; |
| 28 | std::set<Ultrasonic*> Ultrasonic::m_sensors; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Background task that goes through the list of ultrasonic sensors and pings |
| 32 | * each one in turn. The counter |
| 33 | * is configured to read the timing of the returned echo pulse. |
| 34 | * |
| 35 | * DANGER WILL ROBINSON, DANGER WILL ROBINSON: |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 36 | * This code runs as a task and assumes that none of the ultrasonic sensors |
| 37 | * will change while it's running. Make sure to disable automatic mode before |
| 38 | * touching the list. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 39 | */ |
| 40 | void Ultrasonic::UltrasonicChecker() { |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 41 | while (m_automaticEnabled) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 42 | for (auto& sensor : m_sensors) { |
| 43 | if (!m_automaticEnabled) break; |
| 44 | |
| 45 | if (sensor->IsEnabled()) { |
| 46 | sensor->m_pingChannel->Pulse(kPingTime); // do the ping |
| 47 | } |
| 48 | |
| 49 | Wait(0.1); // wait for ping to return |
| 50 | } |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Initialize the Ultrasonic Sensor. |
| 56 | * This is the common code that initializes the ultrasonic sensor given that |
| 57 | * there |
| 58 | * are two digital I/O channels allocated. If the system was running in |
| 59 | * automatic mode (round robin) |
| 60 | * when the new sensor is added, it is stopped, the sensor is added, then |
| 61 | * automatic mode is |
| 62 | * restored. |
| 63 | */ |
| 64 | void Ultrasonic::Initialize() { |
| 65 | bool originalMode = m_automaticEnabled; |
| 66 | SetAutomaticMode(false); // kill task when adding a new sensor |
| 67 | // link this instance on the list |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 68 | m_sensors.insert(this); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 69 | |
| 70 | m_counter.SetMaxPeriod(1.0); |
| 71 | m_counter.SetSemiPeriodMode(true); |
| 72 | m_counter.Reset(); |
| 73 | m_enabled = true; // make it available for round robin scheduling |
| 74 | SetAutomaticMode(originalMode); |
| 75 | |
| 76 | static int instances = 0; |
| 77 | instances++; |
| 78 | HALReport(HALUsageReporting::kResourceType_Ultrasonic, instances); |
| 79 | LiveWindow::GetInstance()->AddSensor("Ultrasonic", |
| 80 | m_echoChannel->GetChannel(), this); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Create an instance of the Ultrasonic Sensor |
| 85 | * This is designed to supchannel the Daventech SRF04 and Vex ultrasonic |
| 86 | * sensors. |
| 87 | * @param pingChannel The digital output channel that sends the pulse to |
| 88 | * initiate the sensor sending |
| 89 | * the ping. |
| 90 | * @param echoChannel The digital input channel that receives the echo. The |
| 91 | * length of time that the |
| 92 | * echo is high represents the round trip time of the ping, and the distance. |
| 93 | * @param units The units returned in either kInches or kMilliMeters |
| 94 | */ |
| 95 | Ultrasonic::Ultrasonic(uint32_t pingChannel, uint32_t echoChannel, |
| 96 | DistanceUnit units) |
| 97 | : m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)), |
| 98 | m_echoChannel(std::make_shared<DigitalInput>(echoChannel)), |
| 99 | m_counter(m_echoChannel) { |
| 100 | m_units = units; |
| 101 | Initialize(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo |
| 106 | * channel and a DigitalOutput |
| 107 | * for the ping channel. |
| 108 | * @param pingChannel The digital output object that starts the sensor doing a |
| 109 | * ping. Requires a 10uS pulse to start. |
| 110 | * @param echoChannel The digital input object that times the return pulse to |
| 111 | * determine the range. |
| 112 | * @param units The units returned in either kInches or kMilliMeters |
| 113 | */ |
| 114 | Ultrasonic::Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, |
| 115 | DistanceUnit units) |
| 116 | : m_pingChannel(pingChannel, NullDeleter<DigitalOutput>()), |
| 117 | m_echoChannel(echoChannel, NullDeleter<DigitalInput>()), |
| 118 | m_counter(m_echoChannel) { |
| 119 | if (pingChannel == nullptr || echoChannel == nullptr) { |
| 120 | wpi_setWPIError(NullParameter); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 121 | m_units = units; |
| 122 | return; |
| 123 | } |
| 124 | m_units = units; |
| 125 | Initialize(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo |
| 130 | * channel and a DigitalOutput |
| 131 | * for the ping channel. |
| 132 | * @param pingChannel The digital output object that starts the sensor doing a |
| 133 | * ping. Requires a 10uS pulse to start. |
| 134 | * @param echoChannel The digital input object that times the return pulse to |
| 135 | * determine the range. |
| 136 | * @param units The units returned in either kInches or kMilliMeters |
| 137 | */ |
| 138 | Ultrasonic::Ultrasonic(DigitalOutput &pingChannel, DigitalInput &echoChannel, |
| 139 | DistanceUnit units) |
| 140 | : m_pingChannel(&pingChannel, NullDeleter<DigitalOutput>()), |
| 141 | m_echoChannel(&echoChannel, NullDeleter<DigitalInput>()), |
| 142 | m_counter(m_echoChannel) { |
| 143 | m_units = units; |
| 144 | Initialize(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo |
| 149 | * channel and a DigitalOutput |
| 150 | * for the ping channel. |
| 151 | * @param pingChannel The digital output object that starts the sensor doing a |
| 152 | * ping. Requires a 10uS pulse to start. |
| 153 | * @param echoChannel The digital input object that times the return pulse to |
| 154 | * determine the range. |
| 155 | * @param units The units returned in either kInches or kMilliMeters |
| 156 | */ |
| 157 | Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel, |
| 158 | std::shared_ptr<DigitalInput> echoChannel, |
| 159 | DistanceUnit units) |
| 160 | : m_pingChannel(pingChannel), |
| 161 | m_echoChannel(echoChannel), |
| 162 | m_counter(m_echoChannel) { |
| 163 | m_units = units; |
| 164 | Initialize(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Destructor for the ultrasonic sensor. |
| 169 | * Delete the instance of the ultrasonic sensor by freeing the allocated digital |
| 170 | * channels. |
| 171 | * If the system was in automatic mode (round robin), then it is stopped, then |
| 172 | * started again |
| 173 | * after this sensor is removed (provided this wasn't the last sensor). |
| 174 | */ |
| 175 | Ultrasonic::~Ultrasonic() { |
| 176 | bool wasAutomaticMode = m_automaticEnabled; |
| 177 | SetAutomaticMode(false); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 178 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 179 | // No synchronization needed because the background task is stopped. |
| 180 | m_sensors.erase(this); |
| 181 | |
| 182 | if (!m_sensors.empty() && wasAutomaticMode) { |
| 183 | SetAutomaticMode(true); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 184 | } |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Turn Automatic mode on/off. |
| 189 | * When in Automatic mode, all sensors will fire in round robin, waiting a set |
| 190 | * time between each sensor. |
| 191 | * @param enabling Set to true if round robin scheduling should start for all |
| 192 | * the ultrasonic sensors. This |
| 193 | * scheduling method assures that the sensors are non-interfering because no two |
| 194 | * sensors fire at the same time. |
| 195 | * If another scheduling algorithm is prefered, it can be implemented by |
| 196 | * pinging the sensors manually and waiting |
| 197 | * for the results to come back. |
| 198 | */ |
| 199 | void Ultrasonic::SetAutomaticMode(bool enabling) { |
| 200 | if (enabling == m_automaticEnabled) return; // ignore the case of no change |
| 201 | |
| 202 | m_automaticEnabled = enabling; |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 203 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 204 | if (enabling) { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 205 | /* Clear all the counters so no data is valid. No synchronization is needed |
| 206 | * because the background task is stopped. |
| 207 | */ |
| 208 | for (auto& sensor : m_sensors) { |
| 209 | sensor->m_counter.Reset(); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 210 | } |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 211 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 212 | m_task = Task("UltrasonicChecker", &Ultrasonic::UltrasonicChecker); |
| 213 | |
| 214 | // TODO: Currently, lvuser does not have permissions to set task priorities. |
| 215 | // Until that is the case, uncommenting this will break user code that calls |
| 216 | // Ultrasonic::SetAutomicMode(). |
| 217 | //m_task.SetPriority(kPriority); |
| 218 | } else { |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 219 | // Wait for background task to stop running |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 220 | m_task.join(); |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 221 | |
| 222 | /* Clear all the counters (data now invalid) since automatic mode is |
| 223 | * disabled. No synchronization is needed because the background task is |
| 224 | * stopped. |
| 225 | */ |
| 226 | for (auto& sensor : m_sensors) { |
| 227 | sensor->m_counter.Reset(); |
| 228 | } |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Single ping to ultrasonic sensor. |
| 234 | * Send out a single ping to the ultrasonic sensor. This only works if automatic |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 235 | * (round robin) mode is disabled. A single ping is sent out, and the counter |
| 236 | * should count the semi-period when it comes in. The counter is reset to make |
| 237 | * the current value invalid. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 238 | */ |
| 239 | void Ultrasonic::Ping() { |
| 240 | wpi_assert(!m_automaticEnabled); |
| 241 | m_counter.Reset(); // reset the counter to zero (invalid data now) |
| 242 | m_pingChannel->Pulse( |
| 243 | kPingTime); // do the ping to start getting a single range |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Check if there is a valid range measurement. |
| 248 | * The ranges are accumulated in a counter that will increment on each edge of |
| 249 | * the echo (return) |
| 250 | * signal. If the count is not at least 2, then the range has not yet been |
| 251 | * measured, and is invalid. |
| 252 | */ |
| 253 | bool Ultrasonic::IsRangeValid() const { return m_counter.Get() > 1; } |
| 254 | |
| 255 | /** |
| 256 | * Get the range in inches from the ultrasonic sensor. |
| 257 | * @return double Range in inches of the target returned from the ultrasonic |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 258 | * sensor. If there is no valid value yet, i.e. at least one measurement hasn't |
| 259 | * completed, then return 0. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 260 | */ |
| 261 | double Ultrasonic::GetRangeInches() const { |
| 262 | if (IsRangeValid()) |
| 263 | return m_counter.GetPeriod() * kSpeedOfSoundInchesPerSec / 2.0; |
| 264 | else |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get the range in millimeters from the ultrasonic sensor. |
| 270 | * @return double Range in millimeters of the target returned by the ultrasonic |
| 271 | * sensor. |
| 272 | * If there is no valid value yet, i.e. at least one measurement hasn't |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 273 | * completed, then return 0. |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 274 | */ |
| 275 | double Ultrasonic::GetRangeMM() const { return GetRangeInches() * 25.4; } |
| 276 | |
| 277 | /** |
| 278 | * Get the range in the current DistanceUnit for the PIDSource base object. |
| 279 | * |
| 280 | * @return The range in DistanceUnit |
| 281 | */ |
| 282 | double Ultrasonic::PIDGet() { |
| 283 | switch (m_units) { |
| 284 | case Ultrasonic::kInches: |
| 285 | return GetRangeInches(); |
| 286 | case Ultrasonic::kMilliMeters: |
| 287 | return GetRangeMM(); |
| 288 | default: |
| 289 | return 0.0; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void Ultrasonic::SetPIDSourceType(PIDSourceType pidSource) { |
| 294 | if (wpi_assert(pidSource == PIDSourceType::kDisplacement)) { |
| 295 | m_pidSource = pidSource; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Set the current DistanceUnit that should be used for the PIDSource base |
| 301 | * object. |
| 302 | * |
| 303 | * @param units The DistanceUnit that should be used. |
| 304 | */ |
| 305 | void Ultrasonic::SetDistanceUnits(DistanceUnit units) { m_units = units; } |
| 306 | |
| 307 | /** |
| 308 | * Get the current DistanceUnit that is used for the PIDSource base object. |
| 309 | * |
| 310 | * @return The type of DistanceUnit that is being used. |
| 311 | */ |
| 312 | Ultrasonic::DistanceUnit Ultrasonic::GetDistanceUnits() const { |
| 313 | return m_units; |
| 314 | } |
| 315 | |
| 316 | void Ultrasonic::UpdateTable() { |
| 317 | if (m_table != nullptr) { |
| 318 | m_table->PutNumber("Value", GetRangeInches()); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void Ultrasonic::StartLiveWindowMode() {} |
| 323 | |
| 324 | void Ultrasonic::StopLiveWindowMode() {} |
| 325 | |
| 326 | std::string Ultrasonic::GetSmartDashboardType() const { return "Ultrasonic"; } |
| 327 | |
| 328 | void Ultrasonic::InitTable(std::shared_ptr<ITable> subTable) { |
| 329 | m_table = subTable; |
| 330 | UpdateTable(); |
| 331 | } |
| 332 | |
| 333 | std::shared_ptr<ITable> Ultrasonic::GetTable() const { return m_table; } |