WPILib 2012
WPILibRoboticsLibraryforFRC
Ultrasonic.h
00001 /*----------------------------------------------------------------------------*/
00002 /* Copyright (c) FIRST 2008. All Rights Reserved.                                                         */
00003 /* Open Source Software - may be modified and shared by FRC teams. The code   */
00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib.  */
00005 /*----------------------------------------------------------------------------*/
00006 
00007 #ifndef ULTRASONIC_H_
00008 #define ULTRASONIC_H_
00009 
00010 #include "SensorBase.h"
00011 #include "Task.h"
00012 #include "PIDSource.h"
00013 
00014 class Counter;
00015 class DigitalInput;
00016 class DigitalOutput;
00017 
00028 class Ultrasonic: public SensorBase, public PIDSource
00029 {
00030 public:
00031         typedef enum {
00032                 kInches = 0,
00033                 kMilliMeters = 1
00034         } DistanceUnit;
00035         
00036         Ultrasonic(DigitalOutput *pingChannel, DigitalInput *echoChannel, DistanceUnit units = kInches);
00037         Ultrasonic(DigitalOutput &pingChannel, DigitalInput &echoChannel, DistanceUnit units = kInches);
00038         Ultrasonic(UINT32 pingChannel, UINT32 echoChannel, DistanceUnit units = kInches);
00039         Ultrasonic(UINT8 pingModuleNumber, UINT32 pingChannel,
00040                                                         UINT8 echoModuleNumber, UINT32 echoChannel, DistanceUnit units = kInches);
00041         virtual ~Ultrasonic();
00042 
00043         void Ping();
00044         bool IsRangeValid();
00045         static void SetAutomaticMode(bool enabling);
00046         double GetRangeInches();
00047         double GetRangeMM();
00048         bool IsEnabled() { return m_enabled; }
00049         void SetEnabled(bool enable) { m_enabled = enable; }
00050         
00051         double PIDGet();
00052         void SetDistanceUnits(DistanceUnit units);
00053         DistanceUnit GetDistanceUnits();
00054 
00055 private:
00056         void Initialize();
00057 
00058         static void UltrasonicChecker();
00059 
00060         static const double kPingTime = 10 * 1e-6;      
00061         static const UINT32 kPriority = 90;     
00062         static const double kMaxUltrasonicTime = 0.1;   
00063         static const double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
00064 
00065         static Task m_task; // task doing the round-robin automatic sensing
00066         static Ultrasonic *m_firstSensor; // head of the ultrasonic sensor list
00067         static bool m_automaticEnabled; // automatic round robin mode
00068         static SEM_ID m_semaphore; // synchronize access to the list of sensors
00069 
00070         DigitalInput *m_echoChannel;
00071         DigitalOutput *m_pingChannel;
00072         bool m_allocatedChannels;
00073         bool m_enabled;
00074         Counter *m_counter;
00075         Ultrasonic *m_nextSensor;
00076         
00077         DistanceUnit m_units;
00078 };
00079 
00080 #endif
00081 
 All Classes Functions Variables