WPILibC++  2020.3.2-60-g3011ebe
Ultrasonic.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
3 /* 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 #pragma once
9 
10 #include <atomic>
11 #include <memory>
12 #include <thread>
13 #include <vector>
14 
15 #include <hal/SimDevice.h>
16 
17 #include "frc/Counter.h"
18 #include "frc/ErrorBase.h"
19 #include "frc/PIDSource.h"
20 #include "frc/smartdashboard/Sendable.h"
21 #include "frc/smartdashboard/SendableHelper.h"
22 
23 namespace frc {
24 
25 class DigitalInput;
26 class DigitalOutput;
27 
40 class Ultrasonic : public ErrorBase,
41  public Sendable,
42  public PIDSource,
43  public SendableHelper<Ultrasonic> {
44  public:
45  enum DistanceUnit { kInches = 0, kMilliMeters = 1 };
46 
59  Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units = kInches);
60 
71  Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
72  DistanceUnit units = kInches);
73 
84  Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
85  DistanceUnit units = kInches);
86 
97  Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
98  std::shared_ptr<DigitalInput> echoChannel,
99  DistanceUnit units = kInches);
100 
101  ~Ultrasonic() override;
102 
103  Ultrasonic(Ultrasonic&&) = default;
104  Ultrasonic& operator=(Ultrasonic&&) = default;
105 
114  void Ping();
115 
123  bool IsRangeValid() const;
124 
138  static void SetAutomaticMode(bool enabling);
139 
147  double GetRangeInches() const;
148 
156  double GetRangeMM() const;
157 
158  bool IsEnabled() const;
159 
160  void SetEnabled(bool enable);
161 
168  void SetDistanceUnits(DistanceUnit units);
169 
175  DistanceUnit GetDistanceUnits() const;
176 
182  double PIDGet() override;
183 
184  void SetPIDSourceType(PIDSourceType pidSource) override;
185 
186  void InitSendable(SendableBuilder& builder) override;
187 
188  private:
197  void Initialize();
198 
209  static void UltrasonicChecker();
210 
211  // Time (sec) for the ping trigger pulse.
212  static constexpr double kPingTime = 10 * 1e-6;
213 
214  // Priority that the ultrasonic round robin task runs.
215  static constexpr int kPriority = 64;
216 
217  // Max time (ms) between readings.
218  static constexpr double kMaxUltrasonicTime = 0.1;
219  static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
220 
221  // Thread doing the round-robin automatic sensing
222  static std::thread m_thread;
223 
224  // Ultrasonic sensors
225  static std::vector<Ultrasonic*> m_sensors;
226 
227  // Automatic round-robin mode
228  static std::atomic<bool> m_automaticEnabled;
229 
230  std::shared_ptr<DigitalOutput> m_pingChannel;
231  std::shared_ptr<DigitalInput> m_echoChannel;
232  bool m_enabled = false;
233  Counter m_counter;
234  DistanceUnit m_units;
235 
236  hal::SimDevice m_simDevice;
237  hal::SimBoolean m_simRangeValid;
238  hal::SimDouble m_simRange;
239 };
240 
241 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
frc::Ultrasonic::Ping
void Ping()
Single ping to ultrasonic sensor.
frc::DigitalOutput
Class to write to digital outputs.
Definition: DigitalOutput.h:27
frc::Ultrasonic::GetRangeMM
double GetRangeMM() const
Get the range in millimeters from the ultrasonic sensor.
frc::Ultrasonic
Ultrasonic rangefinder class.
Definition: Ultrasonic.h:40
frc::Ultrasonic::SetDistanceUnits
void SetDistanceUnits(DistanceUnit units)
Set the current DistanceUnit that should be used for the PIDSource base object.
frc::Ultrasonic::GetRangeInches
double GetRangeInches() const
Get the range in inches from the ultrasonic sensor.
frc::DigitalInput
Class to read a digital input.
Definition: DigitalInput.h:28
frc::Ultrasonic::Ultrasonic
Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units=kInches)
Create an instance of the Ultrasonic Sensor.
frc::Ultrasonic::SetAutomaticMode
static void SetAutomaticMode(bool enabling)
Turn Automatic mode on/off.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::Ultrasonic::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::Ultrasonic::IsRangeValid
bool IsRangeValid() const
Check if there is a valid range measurement.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
frc::Counter
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:37
frc::Ultrasonic::PIDGet
double PIDGet() override
Get the range in the current DistanceUnit for the PIDSource base object.
frc::Ultrasonic::GetDistanceUnits
DistanceUnit GetDistanceUnits() const
Get the current DistanceUnit that is used for the PIDSource base object.
frc::Ultrasonic::SetPIDSourceType
void SetPIDSourceType(PIDSourceType pidSource) override
Set which parameter you are using as a process control variable.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23