PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
ping.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <PropWare/gpio/pin.h>
30 
31 namespace PropWare {
32 
54 class Ping {
55  public:
61  Ping (const PropWare::Pin::Mask signalMask)
62  : m_trigger(signalMask),
63  m_echo(signalMask) {
64  };
65 
72  Ping (const PropWare::Pin::Mask triggerMask, const PropWare::Pin::Mask echoMask)
73  : m_trigger(triggerMask),
74  m_echo(echoMask) {
75  };
76 
86  unsigned int get_millimeters () const {
87  return this->get_microseconds() * 17;
88  }
89 
95  unsigned int get_centimeters () const {
96  return this->get_millimeters() / 1000;
97  }
98 
104  unsigned int get_inches () const {
105  return this->get_microseconds() / 148;
106  }
107 
113  unsigned int get_microseconds () const {
114  return this->get_clock_ticks() / MICROSECOND;
115  }
116 
123 #ifdef PROPWARE_TEST
124  virtual
125 #endif
126 
127  unsigned int get_clock_ticks () const {
128  this->m_trigger.set_dir_out();
129  this->m_trigger.set();
130  waitcnt(20 * MICROSECOND + CNT); // The spec for the ping sensor is 2us, but 20us is the fastest at CMM mode
131  this->m_trigger.clear();
132 
133  this->m_echo.set_dir_in();
134  waitpeq(this->m_echo.get_mask(), this->m_echo.get_mask());
135  const uint32_t start = CNT;
136  waitpne(this->m_echo.get_mask(), this->m_echo.get_mask());
137  return CNT - start;
138  }
139 
140  private:
141  PropWare::Pin m_trigger;
142  PropWare::Pin m_echo;
143 };
144 
145 };
PropWare::Ping::Ping
Ping(const PropWare::Pin::Mask signalMask)
Construct an instance for a 3-pin variant such as the Parallax PING))) sensor.
Definition: ping.h:61
PropWare::Port::set_dir_out
void set_dir_out() const
Set the port for output.
Definition: port.h:210
PropWare::Ping::get_microseconds
unsigned int get_microseconds() const
Time measured in microseconds for a sound wave to travel from the sensor to an object and back.
Definition: ping.h:113
waitpeq
#define waitpeq(state, mask)
Wait until INA equal state & mask.
Definition: propeller.h:190
PropWare::Port::Mask
Mask
Definition: port.h:43
PropWare::Ping
Ultrasonic distance sensor such as the Parallax PING)))
Definition: ping.h:54
PropWare::Ping::get_inches
unsigned int get_inches() const
Detect distance as measured in inches between sensor and object.
Definition: ping.h:104
utility.h
PropWare::Ping::get_millimeters
unsigned int get_millimeters() const
Detect distance as measured in millimeters between sensor and object.
Definition: ping.h:86
PropWare::Port::set_dir_in
void set_dir_in() const
Set the port for input.
Definition: port.h:217
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::Ping::get_clock_ticks
unsigned int get_clock_ticks() const
Time measured in system clock ticks for a sound wave to travel from the sensor to an object and back....
Definition: ping.h:127
PropWare::Ping::Ping
Ping(const PropWare::Pin::Mask triggerMask, const PropWare::Pin::Mask echoMask)
Construct an instance for a 4-pin variant, commonly found on eBay.
Definition: ping.h:72
PropWare::Ping::get_centimeters
unsigned int get_centimeters() const
Detect distance as measured in centimeters between sensor and object.
Definition: ping.h:95
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
pin.h
waitpne
#define waitpne(state, mask)
Wait until INA not equal state & mask.
Definition: propeller.h:197
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249