Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Ping.cpp :: A library for using a Parallax PING))) sensor in robotic applications.
- *
- * @author Tom Malone <tmalone+arduino.software@gmail.com>
- * @copyright Copyright 2012 Tom Malone. Released into the public domain.
- */
- #include <string.h>
- #include "Ping.h"
- // Constructor
- Ping::Ping(uint8_t pin, String distanceUnit) {
- this->_pin = pin; // Save the pin number. NOTE: Parallax PING))) sensors are used with DIGITAL pins.
- this->_distanceUnit = distanceUnit;
- this->_centimetersPerSecond = 29.412;
- }
- // Public methods
- /**
- * Activates the Ping))) senor.
- * From the Ping))) documentation:
- * "The Ping))) sensor needs a start pulse from the micrcontroller to start its measurement.
- * A pulse to the sensor's pin that lasts for 10 microseconds is easily detected by the Ping)))
- * sensor."
- */
- void Ping::activate() {
- // Set pin to OUTPUT mode initially, so that it can be triggered during the activate() method.
- pinMode(this->_pin, OUTPUT);
- // Ensure the signal pin is set to LOW before attempting to activate it.
- digitalWrite(this->_pin, LOW);
- delayMicroseconds(5);
- // Send the trigger pulse.
- digitalWrite(this->_pin, HIGH);
- delayMicroseconds(10); // This is very important; it IS the trigger.
- digitalWrite(this->_pin, LOW); // End the trigger pulse.
- pinMode(this->_pin, INPUT); // Switch the sensor pin back to INPUT, so the microcontroller can receive the pulse duration.
- }
- unsigned long Ping::ping() {
- // Before taking a distance measurement, be sure to activate the sensor with a brief (10 microsecond) pulse.
- // See the datasheet for more information.
- this->activate();
- // Read the pulse duration that represents the time it took the ping echo to travel to the nearest obstacle & back.
- this->_pulseDuration = pulseIn(this->_pin, HIGH);
- if( this->_distanceUnit == "cm" ) {
- return this->pulseTimeToCentimeters(this->_pulseDuration);
- }
- if( this->_distanceUnit == "in" ) {
- return this->pulseTimeToInches(this->_pulseDuration);
- }
- // Give the Ping))) sensor time to stabilize (i.e. give any
- // resounding ping echos time to subside, before proceeding, so
- // they don't interfere with any subsequent pings).
- delay(250);
- return this->_pulseDuration;
- }
- uint16_t Ping::pulseTimeToCentimeters(unsigned long pulseDuration) {
- // The duration of the pulse represents the round trip the ping took.
- // We're only interested in the distance TO the obstacle.
- uint16_t distanceInTime = pulseDuration / 2;
- // The speed of sound is approximately 340 meters/second (but it varies based on a number of factors, including altitude, humidity, etc.),
- // or approximately centimeters per microsecond.
- return uint16_t( round(distanceInTime / this->_centimetersPerSecond) );
- }
- float Ping::pulseTimeToInches(unsigned long pulseDuration) {
- int cmValue = this->pulseTimeToCentimeters(pulseDuration);
- return this->_centimetersToInches(cmValue);
- }
- uint8_t Ping::getCollisionThreshold() {
- return this->_collisionThreshold;
- }
- void Ping::setCollisionThreshold(uint8_t newThresholdValue) {
- this->_collisionThreshold = newThresholdValue;
- }
- // Private methods
- float Ping::_centimetersToInches(float cmValue) {
- return cmValue * 0.393700787;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement