WPILibC++  2018.4.1-20180819203220-1159-g83cfb8b
 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 
105  void Ping();
106 
114  bool IsRangeValid() const;
115 
129  static void SetAutomaticMode(bool enabling);
130 
138  double GetRangeInches() const;
139 
147  double GetRangeMM() const;
148 
149  bool IsEnabled() const;
150 
151  void SetEnabled(bool enable);
152 
159  void SetDistanceUnits(DistanceUnit units);
160 
166  DistanceUnit GetDistanceUnits() const;
167 
173  double PIDGet() override;
174 
175  void SetPIDSourceType(PIDSourceType pidSource) override;
176 
177  void InitSendable(SendableBuilder& builder) override;
178 
179  private:
188  void Initialize();
189 
200  static void UltrasonicChecker();
201 
202  // Time (sec) for the ping trigger pulse.
203  static constexpr double kPingTime = 10 * 1e-6;
204 
205  // Priority that the ultrasonic round robin task runs.
206  static constexpr int kPriority = 64;
207 
208  // Max time (ms) between readings.
209  static constexpr double kMaxUltrasonicTime = 0.1;
210  static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
211 
212  // Thread doing the round-robin automatic sensing
213  static std::thread m_thread;
214 
215  // Ultrasonic sensors
216  static std::vector<Ultrasonic*> m_sensors;
217 
218  // Automatic round-robin mode
219  static std::atomic<bool> m_automaticEnabled;
220 
221  std::shared_ptr<DigitalOutput> m_pingChannel;
222  std::shared_ptr<DigitalInput> m_echoChannel;
223  bool m_enabled = false;
224  Counter m_counter;
225  DistanceUnit m_units;
226 };
227 
228 } // 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