blob: f3a457cb19ae60a7c6117640f014609414c4242b [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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
21namespace frc {
22
23class DigitalInput;
24class DigitalOutput;
25
26/**
27 * Ultrasonic rangefinder class.
28 * The Ultrasonic rangefinder measures absolute distance based on the round-trip
29 * time of a ping generated by the controller. These sensors use two
30 * transducers, a speaker and a microphone both tuned to the ultrasonic range. A
31 * common ultrasonic sensor, the Daventech SRF04 requires a short pulse to be
32 * generated on a digital channel. This causes the chirp to be emitted. A second
33 * line becomes high as the ping is transmitted and goes low when the echo is
34 * received. The time that the line is high determines the round trip distance
35 * (time of flight).
36 */
37class Ultrasonic : public SensorBase,
38 public PIDSource,
39 public LiveWindowSendable {
40 public:
41 enum DistanceUnit { kInches = 0, kMilliMeters = 1 };
42
43 Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
44 DistanceUnit units = kInches);
45
46 Ultrasonic(DigitalOutput& pingChannel, DigitalInput& echoChannel,
47 DistanceUnit units = kInches);
48
49 Ultrasonic(std::shared_ptr<DigitalOutput> pingChannel,
50 std::shared_ptr<DigitalInput> echoChannel,
51 DistanceUnit units = kInches);
52 Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units = kInches);
53 virtual ~Ultrasonic();
54
55 void Ping();
56 bool IsRangeValid() const;
57 static void SetAutomaticMode(bool enabling);
58 double GetRangeInches() const;
59 double GetRangeMM() const;
60 bool IsEnabled() const { return m_enabled; }
61 void SetEnabled(bool enable) { m_enabled = enable; }
62
63 double PIDGet() override;
64 void SetPIDSourceType(PIDSourceType pidSource) override;
65 void SetDistanceUnits(DistanceUnit units);
66 DistanceUnit GetDistanceUnits() const;
67
68 void UpdateTable() override;
69 void StartLiveWindowMode() override;
70 void StopLiveWindowMode() override;
71 std::string GetSmartDashboardType() const override;
72 void InitTable(std::shared_ptr<ITable> subTable) override;
73 std::shared_ptr<ITable> GetTable() const 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 std::shared_ptr<ITable> m_table;
100};
101
102} // namespace frc