AP_RangeFinder library. Simple library to support the Sharpe IR range finder and the Maxsonic XL sonar

git-svn-id: https://arducopter.googlecode.com/svn/trunk@1147 f9c3cf11-9bcb-44bc-f272-b75c42450872
This commit is contained in:
rmackay9@yahoo.com 2010-12-17 03:01:29 +00:00
parent 363d9156d1
commit 173afc91d1
8 changed files with 235 additions and 0 deletions

View File

@ -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"

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -0,0 +1,24 @@
#ifndef RangeFinder_h
#define RangeFinder_h
#include <inttypes.h>
//#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

View File

@ -0,0 +1,30 @@
/*
AP_RangeFinder_test
Code by DIYDrones.com
*/
#include <AP_RangeFinder.h> // 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);
}

View File

@ -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