blob: ec4c9c3c1401d8a0487501486c77e1767981d358 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Ultrasonic.h"
6
Austin Schuh812d0d12021-11-04 20:16:48 -07007#include <utility>
Brian Silverman8fce7482020-01-05 13:18:21 -08008
Austin Schuh812d0d12021-11-04 20:16:48 -07009#include <hal/FRCUsageReporting.h>
10#include <wpi/NullDeleter.h>
11#include <wpi/sendable/SendableBuilder.h>
12#include <wpi/sendable/SendableRegistry.h>
13
Brian Silverman8fce7482020-01-05 13:18:21 -080014#include "frc/Counter.h"
15#include "frc/DigitalInput.h"
16#include "frc/DigitalOutput.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070017#include "frc/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080018#include "frc/Timer.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080019
20using namespace frc;
21
22// Automatic round robin mode
23std::atomic<bool> Ultrasonic::m_automaticEnabled{false};
24
25std::vector<Ultrasonic*> Ultrasonic::m_sensors;
26std::thread Ultrasonic::m_thread;
27
Austin Schuh812d0d12021-11-04 20:16:48 -070028Ultrasonic::Ultrasonic(int pingChannel, int echoChannel)
Brian Silverman8fce7482020-01-05 13:18:21 -080029 : m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)),
30 m_echoChannel(std::make_shared<DigitalInput>(echoChannel)),
31 m_counter(m_echoChannel) {
Brian Silverman8fce7482020-01-05 13:18:21 -080032 Initialize();
Austin Schuh812d0d12021-11-04 20:16:48 -070033 wpi::SendableRegistry::AddChild(this, m_pingChannel.get());
34 wpi::SendableRegistry::AddChild(this, m_echoChannel.get());
Brian Silverman8fce7482020-01-05 13:18:21 -080035}
36
Austin Schuh812d0d12021-11-04 20:16:48 -070037Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel)
38 : m_pingChannel(pingChannel, wpi::NullDeleter<DigitalOutput>()),
39 m_echoChannel(echoChannel, wpi::NullDeleter<DigitalInput>()),
Brian Silverman8fce7482020-01-05 13:18:21 -080040 m_counter(m_echoChannel) {
Austin Schuh812d0d12021-11-04 20:16:48 -070041 if (!pingChannel) {
James Kuszmaulcf324122023-01-14 14:07:17 -080042 throw FRC_MakeError(err::NullParameter, "pingChannel");
Brian Silverman8fce7482020-01-05 13:18:21 -080043 }
Austin Schuh812d0d12021-11-04 20:16:48 -070044 if (!echoChannel) {
James Kuszmaulcf324122023-01-14 14:07:17 -080045 throw FRC_MakeError(err::NullParameter, "echoChannel");
Austin Schuh812d0d12021-11-04 20:16:48 -070046 }
Brian Silverman8fce7482020-01-05 13:18:21 -080047 Initialize();
48}
49
Austin Schuh812d0d12021-11-04 20:16:48 -070050Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel)
51 : m_pingChannel(&pingChannel, wpi::NullDeleter<DigitalOutput>()),
52 m_echoChannel(&echoChannel, wpi::NullDeleter<DigitalInput>()),
Brian Silverman8fce7482020-01-05 13:18:21 -080053 m_counter(m_echoChannel) {
Brian Silverman8fce7482020-01-05 13:18:21 -080054 Initialize();
55}
56
57Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
Austin Schuh812d0d12021-11-04 20:16:48 -070058 std::shared_ptr<DigitalInput> echoChannel)
59 : m_pingChannel(std::move(pingChannel)),
60 m_echoChannel(std::move(echoChannel)),
Brian Silverman8fce7482020-01-05 13:18:21 -080061 m_counter(m_echoChannel) {
Brian Silverman8fce7482020-01-05 13:18:21 -080062 Initialize();
63}
64
65Ultrasonic::~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 Schuh812d0d12021-11-04 20:16:48 -070083int Ultrasonic::GetEchoChannel() const {
84 return m_echoChannel->GetChannel();
85}
86
Brian Silverman8fce7482020-01-05 13:18:21 -080087void Ultrasonic::Ping() {
James Kuszmaulb13e13f2023-11-22 20:44:04 -080088 SetAutomaticMode(false); // turn off automatic round-robin if pinging
Brian Silverman8fce7482020-01-05 13:18:21 -080089
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
97bool Ultrasonic::IsRangeValid() const {
Austin Schuh812d0d12021-11-04 20:16:48 -070098 if (m_simRangeValid) {
99 return m_simRangeValid.Get();
100 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800101 return m_counter.Get() > 1;
102}
103
104void Ultrasonic::SetAutomaticMode(bool enabling) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 if (enabling == m_automaticEnabled) {
106 return; // ignore the case of no change
107 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800108
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 Silverman8fce7482020-01-05 13:18:21 -0800120 } 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 Schuh812d0d12021-11-04 20:16:48 -0700135units::meter_t Ultrasonic::GetRange() const {
Brian Silverman8fce7482020-01-05 13:18:21 -0800136 if (IsRangeValid()) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700137 if (m_simRange) {
James Kuszmaulb13e13f2023-11-22 20:44:04 -0800138 return units::inch_t{m_simRange.Get()};
Austin Schuh812d0d12021-11-04 20:16:48 -0700139 }
140 return m_counter.GetPeriod() * kSpeedOfSound / 2.0;
Brian Silverman8fce7482020-01-05 13:18:21 -0800141 } else {
Austin Schuh812d0d12021-11-04 20:16:48 -0700142 return 0_m;
Brian Silverman8fce7482020-01-05 13:18:21 -0800143 }
144}
145
Austin Schuh812d0d12021-11-04 20:16:48 -0700146bool Ultrasonic::IsEnabled() const {
147 return m_enabled;
Brian Silverman8fce7482020-01-05 13:18:21 -0800148}
149
Austin Schuh812d0d12021-11-04 20:16:48 -0700150void Ultrasonic::SetEnabled(bool enable) {
151 m_enabled = enable;
Brian Silverman8fce7482020-01-05 13:18:21 -0800152}
153
Austin Schuh812d0d12021-11-04 20:16:48 -0700154void Ultrasonic::InitSendable(wpi::SendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800155 builder.SetSmartDashboardType("Ultrasonic");
Austin Schuh1e69f942020-11-14 15:06:14 -0800156 builder.AddDoubleProperty(
James Kuszmaulcf324122023-01-14 14:07:17 -0800157 "Value", [=, this] { return units::inch_t{GetRange()}.value(); },
158 nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -0800159}
160
161void 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 Schuh812d0d12021-11-04 20:16:48 -0700175 m_counter.SetMaxPeriod(1_s);
Brian Silverman8fce7482020-01-05 13:18:21 -0800176 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 Schuh812d0d12021-11-04 20:16:48 -0700184 wpi::SendableRegistry::AddLW(this, "Ultrasonic", m_echoChannel->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800185}
186
187void Ultrasonic::UltrasonicChecker() {
188 while (m_automaticEnabled) {
189 for (auto& sensor : m_sensors) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700190 if (!m_automaticEnabled) {
191 break;
192 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800193
194 if (sensor->IsEnabled()) {
195 sensor->m_pingChannel->Pulse(kPingTime); // do the ping
196 }
197
Austin Schuh812d0d12021-11-04 20:16:48 -0700198 Wait(100_ms); // wait for ping to return
Brian Silverman8fce7482020-01-05 13:18:21 -0800199 }
200 }
201}