blob: 02035dd14a805f81cde692ff6452f5c2bf6d0108 [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) {
42 throw FRC_MakeError(err::NullParameter, "{}", "pingChannel");
Brian Silverman8fce7482020-01-05 13:18:21 -080043 }
Austin Schuh812d0d12021-11-04 20:16:48 -070044 if (!echoChannel) {
45 throw FRC_MakeError(err::NullParameter, "{}", "echoChannel");
46 }
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() {
Austin Schuh812d0d12021-11-04 20:16:48 -070088 if (m_automaticEnabled) {
89 throw FRC_MakeError(err::IncompatibleMode, "{}",
90 "cannot call Ping() in automatic mode");
91 }
Brian Silverman8fce7482020-01-05 13:18:21 -080092
93 // Reset the counter to zero (invalid data now)
94 m_counter.Reset();
95
96 // Do the ping to start getting a single range
97 m_pingChannel->Pulse(kPingTime);
98}
99
100bool Ultrasonic::IsRangeValid() const {
Austin Schuh812d0d12021-11-04 20:16:48 -0700101 if (m_simRangeValid) {
102 return m_simRangeValid.Get();
103 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800104 return m_counter.Get() > 1;
105}
106
107void Ultrasonic::SetAutomaticMode(bool enabling) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700108 if (enabling == m_automaticEnabled) {
109 return; // ignore the case of no change
110 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800111
112 m_automaticEnabled = enabling;
113
114 if (enabling) {
115 /* Clear all the counters so no data is valid. No synchronization is needed
116 * because the background task is stopped.
117 */
118 for (auto& sensor : m_sensors) {
119 sensor->m_counter.Reset();
120 }
121
122 m_thread = std::thread(&Ultrasonic::UltrasonicChecker);
123
124 // TODO: Currently, lvuser does not have permissions to set task priorities.
125 // Until that is the case, uncommenting this will break user code that calls
126 // Ultrasonic::SetAutomicMode().
127 // m_task.SetPriority(kPriority);
128 } else {
129 // Wait for background task to stop running
130 if (m_thread.joinable()) {
131 m_thread.join();
132 }
133
134 // Clear all the counters (data now invalid) since automatic mode is
135 // disabled. No synchronization is needed because the background task is
136 // stopped.
137 for (auto& sensor : m_sensors) {
138 sensor->m_counter.Reset();
139 }
140 }
141}
142
Austin Schuh812d0d12021-11-04 20:16:48 -0700143units::meter_t Ultrasonic::GetRange() const {
Brian Silverman8fce7482020-01-05 13:18:21 -0800144 if (IsRangeValid()) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700145 if (m_simRange) {
146 return units::meter_t{m_simRange.Get()};
147 }
148 return m_counter.GetPeriod() * kSpeedOfSound / 2.0;
Brian Silverman8fce7482020-01-05 13:18:21 -0800149 } else {
Austin Schuh812d0d12021-11-04 20:16:48 -0700150 return 0_m;
Brian Silverman8fce7482020-01-05 13:18:21 -0800151 }
152}
153
Austin Schuh812d0d12021-11-04 20:16:48 -0700154bool Ultrasonic::IsEnabled() const {
155 return m_enabled;
Brian Silverman8fce7482020-01-05 13:18:21 -0800156}
157
Austin Schuh812d0d12021-11-04 20:16:48 -0700158void Ultrasonic::SetEnabled(bool enable) {
159 m_enabled = enable;
Brian Silverman8fce7482020-01-05 13:18:21 -0800160}
161
Austin Schuh812d0d12021-11-04 20:16:48 -0700162void Ultrasonic::InitSendable(wpi::SendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800163 builder.SetSmartDashboardType("Ultrasonic");
Austin Schuh1e69f942020-11-14 15:06:14 -0800164 builder.AddDoubleProperty(
Austin Schuh812d0d12021-11-04 20:16:48 -0700165 "Value", [=] { return units::inch_t{GetRange()}.value(); }, nullptr);
Brian Silverman8fce7482020-01-05 13:18:21 -0800166}
167
168void Ultrasonic::Initialize() {
169 m_simDevice = hal::SimDevice("Ultrasonic", m_echoChannel->GetChannel());
170 if (m_simDevice) {
171 m_simRangeValid = m_simDevice.CreateBoolean("Range Valid", false, true);
172 m_simRange = m_simDevice.CreateDouble("Range (in)", false, 0.0);
173 m_pingChannel->SetSimDevice(m_simDevice);
174 m_echoChannel->SetSimDevice(m_simDevice);
175 }
176
177 bool originalMode = m_automaticEnabled;
178 SetAutomaticMode(false); // Kill task when adding a new sensor
179 // Link this instance on the list
180 m_sensors.emplace_back(this);
181
Austin Schuh812d0d12021-11-04 20:16:48 -0700182 m_counter.SetMaxPeriod(1_s);
Brian Silverman8fce7482020-01-05 13:18:21 -0800183 m_counter.SetSemiPeriodMode(true);
184 m_counter.Reset();
185 m_enabled = true; // Make it available for round robin scheduling
186 SetAutomaticMode(originalMode);
187
188 static int instances = 0;
189 instances++;
190 HAL_Report(HALUsageReporting::kResourceType_Ultrasonic, instances);
Austin Schuh812d0d12021-11-04 20:16:48 -0700191 wpi::SendableRegistry::AddLW(this, "Ultrasonic", m_echoChannel->GetChannel());
Brian Silverman8fce7482020-01-05 13:18:21 -0800192}
193
194void Ultrasonic::UltrasonicChecker() {
195 while (m_automaticEnabled) {
196 for (auto& sensor : m_sensors) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700197 if (!m_automaticEnabled) {
198 break;
199 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800200
201 if (sensor->IsEnabled()) {
202 sensor->m_pingChannel->Pulse(kPingTime); // do the ping
203 }
204
Austin Schuh812d0d12021-11-04 20:16:48 -0700205 Wait(100_ms); // wait for ping to return
Brian Silverman8fce7482020-01-05 13:18:21 -0800206 }
207 }
208}