blob: b7c667ac000d1906312271ecd21a6df31941cfc7 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#ifndef ULTRASONIC_H_
8#define ULTRASONIC_H_
9
10#include "SensorBase.h"
11#include "Task.h"
12#include "PIDSource.h"
13#include "LiveWindow/LiveWindowSendable.h"
14
15class Counter;
16class DigitalInput;
17class DigitalOutput;
18
19/**
20 * Ultrasonic rangefinder class.
21 * The Ultrasonic rangefinder measures absolute distance based on the round-trip time
22 * of a ping generated by the controller. These sensors use two transducers, a speaker and
23 * a microphone both tuned to the ultrasonic range. A common ultrasonic sensor, the Daventech SRF04
24 * requires a short pulse to be generated on a digital channel. This causes the chirp to be
25 * emmitted. A second line becomes high as the ping is transmitted and goes low when
26 * the echo is received. The time that the line is high determines the round trip distance
27 * (time of flight).
28 */
29class Ultrasonic: public SensorBase, public PIDSource, public LiveWindowSendable
30{
31public:
32 typedef enum {
33 kInches = 0,
34 kMilliMeters = 1
35 } DistanceUnit;
36
37 Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, DistanceUnit units = kInches);
38 Ultrasonic(DigitalOutput &pingChannel, DigitalInput &echoChannel, DistanceUnit units = kInches);
39 Ultrasonic(UINT32 pingChannel, UINT32 echoChannel, DistanceUnit units = kInches);
40 Ultrasonic(UINT8 pingModuleNumber, UINT32 pingChannel,
41 UINT8 echoModuleNumber, UINT32 echoChannel, DistanceUnit units = kInches);
42 virtual ~Ultrasonic();
43
44 void Ping();
45 bool IsRangeValid();
46 static void SetAutomaticMode(bool enabling);
47 double GetRangeInches();
48 double GetRangeMM();
49 bool IsEnabled() { return m_enabled; }
50 void SetEnabled(bool enable) { m_enabled = enable; }
51
52 double PIDGet();
53 void SetDistanceUnits(DistanceUnit units);
54 DistanceUnit GetDistanceUnits();
55
56 void UpdateTable();
57 void StartLiveWindowMode();
58 void StopLiveWindowMode();
59 std::string GetSmartDashboardType();
60 void InitTable(ITable *subTable);
61 ITable * GetTable();
62
63private:
64 void Initialize();
65
66 static void UltrasonicChecker();
67
68 static const double kPingTime = 10 * 1e-6; ///< Time (sec) for the ping trigger pulse.
69 static const UINT32 kPriority = 90; ///< Priority that the ultrasonic round robin task runs.
70 static const double kMaxUltrasonicTime = 0.1; ///< Max time (ms) between readings.
71 static const double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
72
73 static Task m_task; // task doing the round-robin automatic sensing
74 static Ultrasonic *m_firstSensor; // head of the ultrasonic sensor list
75 static bool m_automaticEnabled; // automatic round robin mode
76 static SEM_ID m_semaphore; // synchronize access to the list of sensors
77
78 DigitalInput *m_echoChannel;
79 DigitalOutput *m_pingChannel;
80 bool m_allocatedChannels;
81 bool m_enabled;
82 Counter *m_counter;
83 Ultrasonic *m_nextSensor;
84 DistanceUnit m_units;
85
86 ITable *m_table;
87};
88
89#endif
90