Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Ultrasonic.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include <utility> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | #include <hal/FRCUsageReporting.h> |
| 10 | #include <wpi/NullDeleter.h> |
| 11 | #include <wpi/sendable/SendableBuilder.h> |
| 12 | #include <wpi/sendable/SendableRegistry.h> |
| 13 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | #include "frc/Counter.h" |
| 15 | #include "frc/DigitalInput.h" |
| 16 | #include "frc/DigitalOutput.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 17 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
| 20 | using namespace frc; |
| 21 | |
| 22 | // Automatic round robin mode |
| 23 | std::atomic<bool> Ultrasonic::m_automaticEnabled{false}; |
| 24 | |
| 25 | std::vector<Ultrasonic*> Ultrasonic::m_sensors; |
| 26 | std::thread Ultrasonic::m_thread; |
| 27 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 28 | Ultrasonic::Ultrasonic(int pingChannel, int echoChannel) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | : m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)), |
| 30 | m_echoChannel(std::make_shared<DigitalInput>(echoChannel)), |
| 31 | m_counter(m_echoChannel) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 32 | Initialize(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | wpi::SendableRegistry::AddChild(this, m_pingChannel.get()); |
| 34 | wpi::SendableRegistry::AddChild(this, m_echoChannel.get()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel) |
| 38 | : m_pingChannel(pingChannel, wpi::NullDeleter<DigitalOutput>()), |
| 39 | m_echoChannel(echoChannel, wpi::NullDeleter<DigitalInput>()), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | m_counter(m_echoChannel) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 41 | if (!pingChannel) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 42 | throw FRC_MakeError(err::NullParameter, "pingChannel"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 44 | if (!echoChannel) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 45 | throw FRC_MakeError(err::NullParameter, "echoChannel"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 46 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 47 | Initialize(); |
| 48 | } |
| 49 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 50 | Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel) |
| 51 | : m_pingChannel(&pingChannel, wpi::NullDeleter<DigitalOutput>()), |
| 52 | m_echoChannel(&echoChannel, wpi::NullDeleter<DigitalInput>()), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | m_counter(m_echoChannel) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 54 | Initialize(); |
| 55 | } |
| 56 | |
| 57 | Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 58 | std::shared_ptr<DigitalInput> echoChannel) |
| 59 | : m_pingChannel(std::move(pingChannel)), |
| 60 | m_echoChannel(std::move(echoChannel)), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | m_counter(m_echoChannel) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 62 | Initialize(); |
| 63 | } |
| 64 | |
| 65 | Ultrasonic::~Ultrasonic() { |
| 66 | // Delete the instance of the ultrasonic sensor by freeing the allocated |
| 67 | // digital channels. If the system was in automatic mode (round robin), then |
| 68 | // it is stopped, then started again after this sensor is removed (provided |
| 69 | // this wasn't the last sensor). |
| 70 | |
| 71 | bool wasAutomaticMode = m_automaticEnabled; |
| 72 | SetAutomaticMode(false); |
| 73 | |
| 74 | // No synchronization needed because the background task is stopped. |
| 75 | m_sensors.erase(std::remove(m_sensors.begin(), m_sensors.end(), this), |
| 76 | m_sensors.end()); |
| 77 | |
| 78 | if (!m_sensors.empty() && wasAutomaticMode) { |
| 79 | SetAutomaticMode(true); |
| 80 | } |
| 81 | } |
| 82 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 83 | int Ultrasonic::GetEchoChannel() const { |
| 84 | return m_echoChannel->GetChannel(); |
| 85 | } |
| 86 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | void Ultrasonic::Ping() { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 88 | SetAutomaticMode(false); // turn off automatic round-robin if pinging |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 89 | |
| 90 | // Reset the counter to zero (invalid data now) |
| 91 | m_counter.Reset(); |
| 92 | |
| 93 | // Do the ping to start getting a single range |
| 94 | m_pingChannel->Pulse(kPingTime); |
| 95 | } |
| 96 | |
| 97 | bool Ultrasonic::IsRangeValid() const { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 98 | if (m_simRangeValid) { |
| 99 | return m_simRangeValid.Get(); |
| 100 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 101 | return m_counter.Get() > 1; |
| 102 | } |
| 103 | |
| 104 | void Ultrasonic::SetAutomaticMode(bool enabling) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 105 | if (enabling == m_automaticEnabled) { |
| 106 | return; // ignore the case of no change |
| 107 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | |
| 109 | m_automaticEnabled = enabling; |
| 110 | |
| 111 | if (enabling) { |
| 112 | /* Clear all the counters so no data is valid. No synchronization is needed |
| 113 | * because the background task is stopped. |
| 114 | */ |
| 115 | for (auto& sensor : m_sensors) { |
| 116 | sensor->m_counter.Reset(); |
| 117 | } |
| 118 | |
| 119 | m_thread = std::thread(&Ultrasonic::UltrasonicChecker); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 120 | } else { |
| 121 | // Wait for background task to stop running |
| 122 | if (m_thread.joinable()) { |
| 123 | m_thread.join(); |
| 124 | } |
| 125 | |
| 126 | // Clear all the counters (data now invalid) since automatic mode is |
| 127 | // disabled. No synchronization is needed because the background task is |
| 128 | // stopped. |
| 129 | for (auto& sensor : m_sensors) { |
| 130 | sensor->m_counter.Reset(); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 135 | units::meter_t Ultrasonic::GetRange() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 136 | if (IsRangeValid()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 137 | if (m_simRange) { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 138 | return units::inch_t{m_simRange.Get()}; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | } |
| 140 | return m_counter.GetPeriod() * kSpeedOfSound / 2.0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 141 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 142 | return 0_m; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 146 | bool Ultrasonic::IsEnabled() const { |
| 147 | return m_enabled; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 150 | void Ultrasonic::SetEnabled(bool enable) { |
| 151 | m_enabled = enable; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 154 | void Ultrasonic::InitSendable(wpi::SendableBuilder& builder) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 155 | builder.SetSmartDashboardType("Ultrasonic"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 156 | builder.AddDoubleProperty( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 157 | "Value", [=, this] { return units::inch_t{GetRange()}.value(); }, |
| 158 | nullptr); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void Ultrasonic::Initialize() { |
| 162 | m_simDevice = hal::SimDevice("Ultrasonic", m_echoChannel->GetChannel()); |
| 163 | if (m_simDevice) { |
| 164 | m_simRangeValid = m_simDevice.CreateBoolean("Range Valid", false, true); |
| 165 | m_simRange = m_simDevice.CreateDouble("Range (in)", false, 0.0); |
| 166 | m_pingChannel->SetSimDevice(m_simDevice); |
| 167 | m_echoChannel->SetSimDevice(m_simDevice); |
| 168 | } |
| 169 | |
| 170 | bool originalMode = m_automaticEnabled; |
| 171 | SetAutomaticMode(false); // Kill task when adding a new sensor |
| 172 | // Link this instance on the list |
| 173 | m_sensors.emplace_back(this); |
| 174 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 175 | m_counter.SetMaxPeriod(1_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 176 | m_counter.SetSemiPeriodMode(true); |
| 177 | m_counter.Reset(); |
| 178 | m_enabled = true; // Make it available for round robin scheduling |
| 179 | SetAutomaticMode(originalMode); |
| 180 | |
| 181 | static int instances = 0; |
| 182 | instances++; |
| 183 | HAL_Report(HALUsageReporting::kResourceType_Ultrasonic, instances); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 184 | wpi::SendableRegistry::AddLW(this, "Ultrasonic", m_echoChannel->GetChannel()); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void Ultrasonic::UltrasonicChecker() { |
| 188 | while (m_automaticEnabled) { |
| 189 | for (auto& sensor : m_sensors) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 190 | if (!m_automaticEnabled) { |
| 191 | break; |
| 192 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 193 | |
| 194 | if (sensor->IsEnabled()) { |
| 195 | sensor->m_pingChannel->Pulse(kPingTime); // do the ping |
| 196 | } |
| 197 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 198 | Wait(100_ms); // wait for ping to return |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |