diff --git a/libraries/AP_RangeFinder/AP_RangeFinder.h b/libraries/AP_RangeFinder/AP_RangeFinder.h new file mode 100644 index 0000000000..0f1da2fc08 --- /dev/null +++ b/libraries/AP_RangeFinder/AP_RangeFinder.h @@ -0,0 +1,7 @@ +// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*- + +/// @file AP_RangeFinder.h +/// @brief Catch-all header that defines all supported RangeFinder classes. + +#include "AP_RangeFinder_SharpGP2Y.h" +#include "AP_RangeFinder_MaxsonarXL.h" diff --git a/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.cpp b/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.cpp new file mode 100644 index 0000000000..c9dcb2b173 --- /dev/null +++ b/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.cpp @@ -0,0 +1,68 @@ +// -*- tab-width: 4; Mode: C++; c-basic-offset: 3; indent-tabs-mode: t -*- +/* + AP_RangeFinder_MaxsonarXL.cpp - Arduino Library for Sharpe GP2Y0A02YK0F + infrared proximity sensor + Code by Jose Julio and Randy Mackay. DIYDrones.com + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + Sparkfun URL: http://www.sparkfun.com/products/9495 + datasheet: http://www.sparkfun.com/datasheets/Sensors/Proximity/XL-EZ4-Datasheet.pdf + + Sensor should be connected to one of the analog ports + + Variables: + int raw_value : raw value from the sensor + int distance : distance in meters + int max_distance : maximum measurable distance (in cm) + int min_distance : minimum measurable distance (in cm) + + Methods: + init(int analogPort) : Initialization of sensor + read() : read value from analog port and returns the distance (in cm) + +*/ + +// AVR LibC Includes +#include "WConstants.h" + +#include "AP_RangeFinder_MaxsonarXL.h" + +// Public Methods ////////////////////////////////////////////////////////////// +void AP_RangeFinder_MaxsonarXL::init(int analogPort) +{ + // initialise everything + _analogPort = analogPort; + max_distance = AP_RANGEFINDER_MAXSONARXL_MAX_DISTANCE; + min_distance = AP_RANGEFINDER_MAXSONARXL_MIN_DISTANCE; + + // make first call to read to get initial distance + read(); +} + +// Read Sensor data +int AP_RangeFinder_MaxsonarXL::read() +{ + // read raw sensor value and convert to distance + raw_value = analogRead(_analogPort); + + // for this sensor, the sensor value is the distance in cm! nice and easy! + distance = constrain(raw_value,min_distance,max_distance); + + // implement filter + //switch( _filterType ) { + // case AP_RANGEFINDER_FILTER_LIMITED_CHANGE: + // distance = constrain( + // break; + // case AP_RANGEFINDER_FILTER_NONE: + // default: + // distance = tempDistance; + // break; + //} + + // return distance + return distance; +} diff --git a/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.h b/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.h new file mode 100644 index 0000000000..611e65cbca --- /dev/null +++ b/libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarXL.h @@ -0,0 +1,15 @@ +#ifndef AP_RangeFinder_MaxsonarXL_H +#define AP_RangeFinder_MaxsonarXL_H + +#include "RangeFinder.h" + +#define AP_RANGEFINDER_MAXSONARXL_MIN_DISTANCE 20 +#define AP_RANGEFINDER_MAXSONARXL_MAX_DISTANCE 700 + +class AP_RangeFinder_MaxsonarXL : public RangeFinder +{ + public: + void init(int analogPort); + int read(); // read value from analog port and return distance in cm +}; +#endif diff --git a/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.cpp b/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.cpp new file mode 100644 index 0000000000..1072536d17 --- /dev/null +++ b/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.cpp @@ -0,0 +1,66 @@ +// -*- tab-width: 4; Mode: C++; c-basic-offset: 3; indent-tabs-mode: t -*- +/* + AP_RangeFinder_SharpGP2Y.cpp - Arduino Library for Sharpe GP2Y0A02YK0F + infrared proximity sensor + Code by Jose Julio and Randy Mackay. DIYDrones.com + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + Sensor should be conected to one of the analog ports + + Sparkfun URL: http://www.sparkfun.com/products/8958 + datasheet: http://www.sparkfun.com/datasheets/Sensors/Infrared/gp2y0a02yk_e.pdf + + Variables: + int raw_value : raw value from the sensor + int distance : distance in meters + int max_distance : maximum measurable distance (in cm) + int min_distance : minimum measurable distance (in cm) + + Methods: + init(int analogPort) : Initialization of sensor + read() : read value from analog port + +*/ + +// AVR LibC Includes +#include "WConstants.h" + +#include "AP_RangeFinder_SharpGP2Y.h" + +// Public Methods ////////////////////////////////////////////////////////////// +void AP_RangeFinder_SharpGP2Y::init(int analogPort) +{ + // initialise everything + _analogPort = analogPort; + max_distance = AP_RANGEFINDER_SHARPEGP2Y_MAX_DISTANCE; + min_distance = AP_RANGEFINDER_SHARPEGP2Y_MIN_DISTANCE; + + // make first call to read to get initial distance + read(); +} + +// Read Sensor data +int AP_RangeFinder_SharpGP2Y::read() +{ + // read raw sensor value and convert to distance + raw_value = analogRead(_analogPort); + distance = constrain(14500/raw_value,min_distance,max_distance); + + // implement filter + //switch( _filterType ) { + // case AP_RANGEFINDER_FILTER_LIMITED_CHANGE: + // distance = constrain( + // break; + // case AP_RANGEFINDER_FILTER_NONE: + // default: + // distance = tempDistance; + // break; + //} + + // return distance + return distance; +} diff --git a/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.h b/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.h new file mode 100644 index 0000000000..1f211f7411 --- /dev/null +++ b/libraries/AP_RangeFinder/AP_RangeFinder_SharpGP2Y.h @@ -0,0 +1,15 @@ +#ifndef AP_RangeFinder_SharpGP2Y_H +#define AP_RangeFinder_SharpGP2Y_H + +#include "RangeFinder.h" + +#define AP_RANGEFINDER_SHARPEGP2Y_MIN_DISTANCE 20 +#define AP_RANGEFINDER_SHARPEGP2Y_MAX_DISTANCE 150 + +class AP_RangeFinder_SharpGP2Y : public RangeFinder +{ + public: + void init(int analogPort); + int read(); // read value from analog port and return distance in cm +}; +#endif diff --git a/libraries/AP_RangeFinder/RangeFinder.h b/libraries/AP_RangeFinder/RangeFinder.h new file mode 100644 index 0000000000..3012aba640 --- /dev/null +++ b/libraries/AP_RangeFinder/RangeFinder.h @@ -0,0 +1,24 @@ +#ifndef RangeFinder_h +#define RangeFinder_h + +#include + +//#define AP_RANGEFINDER_FILTER_NONE 0 +//#define AP_RANGEFINDER_FILTER_LIMITED_CHANGE 1 + +class RangeFinder +{ + protected: + int _analogPort; // the port to which the sensor is connected + int _filterType; + public: + int raw_value; // raw value from the sensor + int distance; // distance in cm + int max_distance; // maximum measurable distance (in cm) + int min_distance; // minimum measurable distance (in cm) + + virtual void init(int analogPort); + //virtual void set_filter(int filterType) { _filterType = filterType; }; + virtual int read(); // read value from analog port and return distance in cm +}; +#endif diff --git a/libraries/AP_RangeFinder/examples/AP_RangeFinder_test/AP_RangeFinder_test.pde b/libraries/AP_RangeFinder/examples/AP_RangeFinder_test/AP_RangeFinder_test.pde new file mode 100644 index 0000000000..d156b4698a --- /dev/null +++ b/libraries/AP_RangeFinder/examples/AP_RangeFinder_test/AP_RangeFinder_test.pde @@ -0,0 +1,30 @@ +/* + AP_RangeFinder_test + Code by DIYDrones.com +*/ + +#include // Sonar library + +#define RF_PIN 5 // the far back-right pin on the oilpan (near the CLI switch) + +// create the range finder object +//AP_RangeFinder_SharpGP2Y aRF; +AP_RangeFinder_MaxsonarXL aRF; + +void setup() +{ + Serial.begin(115200); + Serial.println("Range Finder Test v1.0"); + aRF.init(RF_PIN); +} + +void loop() +{ + Serial.print("dist:"); + Serial.print(aRF.read()); + Serial.print("\traw:"); + Serial.print(aRF.raw_value); + Serial.println(); + delay(100); +} + diff --git a/libraries/AP_RangeFinder/keywords.txt b/libraries/AP_RangeFinder/keywords.txt new file mode 100644 index 0000000000..e2a4bbaf85 --- /dev/null +++ b/libraries/AP_RangeFinder/keywords.txt @@ -0,0 +1,10 @@ +RangeFinder KEYWORD1 +AP_RangeFinder KEYWORD1 +AP_RangeFinder_SharpGP2Y KEYWORD1 +AP_RangeFinder_MaxsonarXL KEYWORD1 +init KEYWORD2 +read KEYWORD2 +raw_value KEYWORD2 +distance KEYWORD2 +max_distance KEYWORD2 +min_distance KEYWORD2