WPILibC++  2018.4.1-1217-gf0ac048
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Ultrasonic.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 "frc/Counter.h"
16 #include "frc/ErrorBase.h"
17 #include "frc/PIDSource.h"
18 #include "frc/smartdashboard/SendableBase.h"
19 
20 namespace frc {
21 
22 class DigitalInput;
23 class DigitalOutput;
24 
37 class Ultrasonic : public ErrorBase, public SendableBase, public PIDSource {
38  public:
39  enum DistanceUnit { kInches = 0, kMilliMeters = 1 };
40 
53  Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units = kInches);
54 
65  Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
66  DistanceUnit units = kInches);
67 
78  Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
79  DistanceUnit units = kInches);
80 
91  Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
92  std::shared_ptr<DigitalInput> echoChannel,
93  DistanceUnit units = kInches);
94 
95  ~Ultrasonic() override;
96 
97  Ultrasonic(Ultrasonic&&) = default;
98  Ultrasonic& operator=(Ultrasonic&&) = default;
99 
108  void Ping();
109 
117  bool IsRangeValid() const;
118 
132  static void SetAutomaticMode(bool enabling);
133 
141  double GetRangeInches() const;
142 
150  double GetRangeMM() const;
151 
152  bool IsEnabled() const;
153 
154  void SetEnabled(bool enable);
155 
162  void SetDistanceUnits(DistanceUnit units);
163 
169  DistanceUnit GetDistanceUnits() const;
170 
176  double PIDGet() override;
177 
178  void SetPIDSourceType(PIDSourceType pidSource) override;
179 
180  void InitSendable(SendableBuilder& builder) override;
181 
182  private:
191  void Initialize();
192 
203  static void UltrasonicChecker();
204 
205  // Time (sec) for the ping trigger pulse.
206  static constexpr double kPingTime = 10 * 1e-6;
207 
208  // Priority that the ultrasonic round robin task runs.
209  static constexpr int kPriority = 64;
210 
211  // Max time (ms) between readings.
212  static constexpr double kMaxUltrasonicTime = 0.1;
213  static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
214 
215  // Thread doing the round-robin automatic sensing
216  static std::thread m_thread;
217 
218  // Ultrasonic sensors
219  static std::vector<Ultrasonic*> m_sensors;
220 
221  // Automatic round-robin mode
222  static std::atomic<bool> m_automaticEnabled;
223 
224  std::shared_ptr<DigitalOutput> m_pingChannel;
225  std::shared_ptr<DigitalInput> m_echoChannel;
226  bool m_enabled = false;
227  Counter m_counter;
228  DistanceUnit m_units;
229 };
230 
231 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
void SetDistanceUnits(DistanceUnit units)
Set the current DistanceUnit that should be used for the PIDSource base object.
void SetPIDSourceType(PIDSourceType pidSource) override
Set which parameter you are using as a process control variable.
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:34
Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units=kInches)
Create an instance of the Ultrasonic Sensor.
double GetRangeMM() const
Get the range in millimeters from the ultrasonic sensor.
bool IsRangeValid() const
Check if there is a valid range measurement.
DistanceUnit GetDistanceUnits() const
Get the current DistanceUnit that is used for the PIDSource base object.
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
static void SetAutomaticMode(bool enabling)
Turn Automatic mode on/off.
Base class for most objects.
Definition: ErrorBase.h:74
Definition: SendableBase.h:19
Class to write to digital outputs.
Definition: DigitalOutput.h:24
double GetRangeInches() const
Get the range in inches from the ultrasonic sensor.
Definition: SendableBuilder.h:23
Class to read a digital input.
Definition: DigitalInput.h:25
double PIDGet() override
Get the range in the current DistanceUnit for the PIDSource base object.
void Ping()
Single ping to ultrasonic sensor.
Ultrasonic rangefinder class.
Definition: Ultrasonic.h:37