blob: fcd016ef09d0f31cab69e590f7b4e6ad5c0e2b0d [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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 "frc/Ultrasonic.h"
9
James Kuszmaul4b81d302019-12-14 20:53:14 -080010#include <hal/FRCUsageReporting.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080011
12#include "frc/Counter.h"
13#include "frc/DigitalInput.h"
14#include "frc/DigitalOutput.h"
15#include "frc/Timer.h"
16#include "frc/Utility.h"
17#include "frc/WPIErrors.h"
18#include "frc/smartdashboard/SendableBuilder.h"
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080019#include "frc/smartdashboard/SendableRegistry.h"
Brian Silverman41cdd3e2019-01-19 19:48:58 -080020
21using namespace frc;
22
23// Automatic round robin mode
24std::atomic<bool> Ultrasonic::m_automaticEnabled{false};
25
26std::vector<Ultrasonic*> Ultrasonic::m_sensors;
27std::thread Ultrasonic::m_thread;
28
29Ultrasonic::Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units)
30 : m_pingChannel(std::make_shared<DigitalOutput>(pingChannel)),
31 m_echoChannel(std::make_shared<DigitalInput>(echoChannel)),
32 m_counter(m_echoChannel) {
33 m_units = units;
34 Initialize();
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080035 auto& registry = SendableRegistry::GetInstance();
36 registry.AddChild(this, m_pingChannel.get());
37 registry.AddChild(this, m_echoChannel.get());
Brian Silverman41cdd3e2019-01-19 19:48:58 -080038}
39
40Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
41 DistanceUnit units)
42 : m_pingChannel(pingChannel, NullDeleter<DigitalOutput>()),
43 m_echoChannel(echoChannel, NullDeleter<DigitalInput>()),
44 m_counter(m_echoChannel) {
45 if (pingChannel == nullptr || echoChannel == nullptr) {
46 wpi_setWPIError(NullParameter);
47 m_units = units;
48 return;
49 }
50 m_units = units;
51 Initialize();
52}
53
54Ultrasonic::Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
55 DistanceUnit units)
56 : m_pingChannel(&pingChannel, NullDeleter<DigitalOutput>()),
57 m_echoChannel(&echoChannel, NullDeleter<DigitalInput>()),
58 m_counter(m_echoChannel) {
59 m_units = units;
60 Initialize();
61}
62
63Ultrasonic::Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
64 std::shared_ptr<DigitalInput> echoChannel,
65 DistanceUnit units)
66 : m_pingChannel(pingChannel),
67 m_echoChannel(echoChannel),
68 m_counter(m_echoChannel) {
69 m_units = units;
70 Initialize();
71}
72
73Ultrasonic::~Ultrasonic() {
74 // Delete the instance of the ultrasonic sensor by freeing the allocated
75 // digital channels. If the system was in automatic mode (round robin), then
76 // it is stopped, then started again after this sensor is removed (provided
77 // this wasn't the last sensor).
78
79 bool wasAutomaticMode = m_automaticEnabled;
80 SetAutomaticMode(false);
81
82 // No synchronization needed because the background task is stopped.
83 m_sensors.erase(std::remove(m_sensors.begin(), m_sensors.end(), this),
84 m_sensors.end());
85
86 if (!m_sensors.empty() && wasAutomaticMode) {
87 SetAutomaticMode(true);
88 }
89}
90
91void Ultrasonic::Ping() {
92 wpi_assert(!m_automaticEnabled);
93
94 // Reset the counter to zero (invalid data now)
95 m_counter.Reset();
96
97 // Do the ping to start getting a single range
98 m_pingChannel->Pulse(kPingTime);
99}
100
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800101bool Ultrasonic::IsRangeValid() const {
102 if (m_simRangeValid) return m_simRangeValid.Get();
103 return m_counter.Get() > 1;
104}
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800105
106void Ultrasonic::SetAutomaticMode(bool enabling) {
107 if (enabling == m_automaticEnabled) return; // ignore the case of no change
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);
120
121 // TODO: Currently, lvuser does not have permissions to set task priorities.
122 // Until that is the case, uncommenting this will break user code that calls
123 // Ultrasonic::SetAutomicMode().
124 // m_task.SetPriority(kPriority);
125 } else {
126 // Wait for background task to stop running
127 if (m_thread.joinable()) {
128 m_thread.join();
129 }
130
131 // Clear all the counters (data now invalid) since automatic mode is
132 // disabled. No synchronization is needed because the background task is
133 // stopped.
134 for (auto& sensor : m_sensors) {
135 sensor->m_counter.Reset();
136 }
137 }
138}
139
140double Ultrasonic::GetRangeInches() const {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800141 if (IsRangeValid()) {
142 if (m_simRange) return m_simRange.Get();
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800143 return m_counter.GetPeriod() * kSpeedOfSoundInchesPerSec / 2.0;
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800144 } else {
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800145 return 0;
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800146 }
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800147}
148
149double Ultrasonic::GetRangeMM() const { return GetRangeInches() * 25.4; }
150
151bool Ultrasonic::IsEnabled() const { return m_enabled; }
152
153void Ultrasonic::SetEnabled(bool enable) { m_enabled = enable; }
154
155void Ultrasonic::SetDistanceUnits(DistanceUnit units) { m_units = units; }
156
157Ultrasonic::DistanceUnit Ultrasonic::GetDistanceUnits() const {
158 return m_units;
159}
160
161double Ultrasonic::PIDGet() {
162 switch (m_units) {
163 case Ultrasonic::kInches:
164 return GetRangeInches();
165 case Ultrasonic::kMilliMeters:
166 return GetRangeMM();
167 default:
168 return 0.0;
169 }
170}
171
172void Ultrasonic::SetPIDSourceType(PIDSourceType pidSource) {
173 if (wpi_assert(pidSource == PIDSourceType::kDisplacement)) {
174 m_pidSource = pidSource;
175 }
176}
177
178void Ultrasonic::InitSendable(SendableBuilder& builder) {
179 builder.SetSmartDashboardType("Ultrasonic");
180 builder.AddDoubleProperty("Value", [=]() { return GetRangeInches(); },
181 nullptr);
182}
183
184void Ultrasonic::Initialize() {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800185 m_simDevice = hal::SimDevice("Ultrasonic", m_echoChannel->GetChannel());
186 if (m_simDevice) {
187 m_simRangeValid = m_simDevice.CreateBoolean("Range Valid", false, true);
188 m_simRange = m_simDevice.CreateDouble("Range (in)", false, 0.0);
189 m_pingChannel->SetSimDevice(m_simDevice);
190 m_echoChannel->SetSimDevice(m_simDevice);
191 }
192
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800193 bool originalMode = m_automaticEnabled;
194 SetAutomaticMode(false); // Kill task when adding a new sensor
195 // Link this instance on the list
196 m_sensors.emplace_back(this);
197
198 m_counter.SetMaxPeriod(1.0);
199 m_counter.SetSemiPeriodMode(true);
200 m_counter.Reset();
201 m_enabled = true; // Make it available for round robin scheduling
202 SetAutomaticMode(originalMode);
203
204 static int instances = 0;
205 instances++;
206 HAL_Report(HALUsageReporting::kResourceType_Ultrasonic, instances);
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800207 SendableRegistry::GetInstance().AddLW(this, "Ultrasonic",
208 m_echoChannel->GetChannel());
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800209}
210
211void Ultrasonic::UltrasonicChecker() {
212 while (m_automaticEnabled) {
213 for (auto& sensor : m_sensors) {
214 if (!m_automaticEnabled) break;
215
216 if (sensor->IsEnabled()) {
217 sensor->m_pingChannel->Pulse(kPingTime); // do the ping
218 }
219
220 Wait(0.1); // wait for ping to return
221 }
222 }
223}