WPILibC++  unspecified
Ultrasonic.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 <set>
13 #include <string>
14 #include <thread>
15 
16 #include "Counter.h"
17 #include "LiveWindow/LiveWindowSendable.h"
18 #include "PIDSource.h"
19 #include "SensorBase.h"
20 #include "networktables/NetworkTableEntry.h"
21 
22 namespace frc {
23 
24 class DigitalInput;
25 class DigitalOutput;
26 
38 class Ultrasonic : public SensorBase,
39  public PIDSource,
40  public LiveWindowSendable {
41  public:
42  enum DistanceUnit { kInches = 0, kMilliMeters = 1 };
43 
44  Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
45  DistanceUnit units = kInches);
46 
47  Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
48  DistanceUnit units = kInches);
49 
50  Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
51  std::shared_ptr<DigitalInput> echoChannel,
52  DistanceUnit units = kInches);
53  Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units = kInches);
54  virtual ~Ultrasonic();
55 
56  void Ping();
57  bool IsRangeValid() const;
58  static void SetAutomaticMode(bool enabling);
59  double GetRangeInches() const;
60  double GetRangeMM() const;
61  bool IsEnabled() const { return m_enabled; }
62  void SetEnabled(bool enable) { m_enabled = enable; }
63 
64  double PIDGet() override;
65  void SetPIDSourceType(PIDSourceType pidSource) override;
66  void SetDistanceUnits(DistanceUnit units);
67  DistanceUnit GetDistanceUnits() const;
68 
69  void UpdateTable() override;
70  void StartLiveWindowMode() override;
71  void StopLiveWindowMode() override;
72  std::string GetSmartDashboardType() const override;
73  void InitTable(std::shared_ptr<nt::NetworkTable> subTable) override;
74 
75  private:
76  void Initialize();
77 
78  static void UltrasonicChecker();
79 
80  // Time (sec) for the ping trigger pulse.
81  static constexpr double kPingTime = 10 * 1e-6;
82  // Priority that the ultrasonic round robin task runs.
83  static const int kPriority = 64;
84  // Max time (ms) between readings.
85  static constexpr double kMaxUltrasonicTime = 0.1;
86  static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
87 
88  static std::thread
89  m_thread; // thread doing the round-robin automatic sensing
90  static std::set<Ultrasonic*> m_sensors; // ultrasonic sensors
91  static std::atomic<bool> m_automaticEnabled; // automatic round robin mode
92 
93  std::shared_ptr<DigitalOutput> m_pingChannel;
94  std::shared_ptr<DigitalInput> m_echoChannel;
95  bool m_enabled = false;
96  Counter m_counter;
97  DistanceUnit m_units;
98 
99  nt::NetworkTableEntry m_valueEntry;
100 };
101 
102 } // namespace frc
Definition: Timer.cpp:18
Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, DistanceUnit units=kInches)
Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutp...
Definition: Ultrasonic.cpp:116
double PIDGet() override
Get the range in the current DistanceUnit for the PIDSource base object.
Definition: Ultrasonic.cpp:287
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
Base class for all sensors.
Definition: SensorBase.h:20
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:19
bool IsRangeValid() const
Check if there is a valid range measurement.
Definition: Ultrasonic.cpp:257
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:36
DistanceUnit GetDistanceUnits() const
Get the current DistanceUnit that is used for the PIDSource base object.
Definition: Ultrasonic.cpp:317
void SetDistanceUnits(DistanceUnit units)
Set the current DistanceUnit that should be used for the PIDSource base object.
Definition: Ultrasonic.cpp:310
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: Ultrasonic.cpp:329
double GetRangeInches() const
Get the range in inches from the ultrasonic sensor.
Definition: Ultrasonic.cpp:266
void Ping()
Single ping to ultrasonic sensor.
Definition: Ultrasonic.cpp:243
static void SetAutomaticMode(bool enabling)
Turn Automatic mode on/off.
Definition: Ultrasonic.cpp:202
std::string GetSmartDashboardType() const override
Definition: Ultrasonic.cpp:331
Class to write to digital outputs.
Definition: DigitalOutput.h:27
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: Ultrasonic.cpp:327
void UpdateTable() override
Update the table for this sendable object with the latest values.
Definition: Ultrasonic.cpp:321
NetworkTables Entry.
Definition: NetworkTableEntry.h:30
double GetRangeMM() const
Get the range in millimeters from the ultrasonic sensor.
Definition: Ultrasonic.cpp:280
virtual ~Ultrasonic()
Destructor for the ultrasonic sensor.
Definition: Ultrasonic.cpp:177
Class to read a digital input.
Definition: DigitalInput.h:29
void SetPIDSourceType(PIDSourceType pidSource) override
Set which parameter you are using as a process control variable.
Definition: Ultrasonic.cpp:298
Ultrasonic rangefinder class.
Definition: Ultrasonic.h:38
void InitTable(std::shared_ptr< nt::NetworkTable > subTable) override
Initializes a table for this sendable object.
Definition: Ultrasonic.cpp:333