diff --git a/SonarSRF.cpp b/SonarSRF.cpp index 4e14080..47e5add 100644 --- a/SonarSRF.cpp +++ b/SonarSRF.cpp @@ -5,22 +5,35 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #include "SonarSRF.h" -void SonarSRF::connect(int address, int gainRegister, int rangeLocation) +/// +/// Initiates the connection with the sensor and start I2C bus +/// +/// I2C Address +/// SRF Location 1 +/// SRF Location 2 +void SonarSRF::begin(int address, int gainRegister, int rangeLocation) { _address = address; _gainRegister = gainRegister; _rangeLocation = rangeLocation; - // start I2C bus Wire.begin(); } -// Sets Units for display / storage +/// +/// Sets Units for display / storage +/// +/// +/// * 'i' for inches +/// * 'c' for centimeters +/// * 'm' for microseconds +/// +/// Unit used void SonarSRF::startRanging(char unit) { switch (unit) @@ -40,70 +53,89 @@ void SonarSRF::startRanging(char unit) } } -// Communicates with Sonar to send commands +/// +/// Communicates with Sonar to send commands +/// +/// SRF Command +/// SRF Location 0 +/// void SonarSRF::sendCommand(int command, int addressRegister) { // start I2C transmission Wire.beginTransmission(_address); - // send command - Wire.write(addressRegister); // SRF Location 0 - if (command != NULL) + Wire.write(byte(addressRegister)); + if (command != 0) { - Wire.write(command); // SRF Command + // send command + Wire.write(byte(command)); if (_gainRegister && _rangeLocation) { - Wire.write(_gainRegister); // SRF Location 1 - Wire.write(_rangeLocation); // SRF Location 2 + Wire.write(byte(_gainRegister)); // SRF Location 1 + Wire.write(byte(_rangeLocation)); // SRF Location 2 } } // end I2C transmission Wire.endTransmission(); } -// Read data from register return result +/// +/// Read data from register return result +/// +/// Unit used +/// +/// The range number (two bytes) +/// int SonarSRF::getRange(char unit, bool andStart) { - int result = 0; // the result is two bytes long if (andStart) { startRanging(unit); waitForCompletion(); } - sendCommand(NULL, RESULT_REGISTER); + sendCommand(0, RANGE_REGISTER); Wire.requestFrom(_address, 2); // wait for two bytes to return while (Wire.available() < 2); // wait for result // read the two bytes, and combine them into one int byte highByte = Wire.read(); // Stores high byte from ranging byte lowByte = Wire.read(); // Stored low byte from ranging - result = (highByte << 8) + lowByte; - // return the result: - return result; + return (int)((highByte << 8) + lowByte); } +/// +/// Wait for completion +/// void SonarSRF::waitForCompletion() { - while (getSoft() == -1) + while (getVersion() == -1) { delay(1); } } -// Get software revision -int SonarSRF::getSoft() +/// +/// Get software revision +/// +/// The software revision (one byte) +int SonarSRF::getVersion() { - sendCommand(); + sendCommand(0, SOFTWARE_REVISION); Wire.requestFrom(_address, 1); // Request 1 byte while (Wire.available() < 0); // While byte available - int software = Wire.read(); // Get byte - return software; + return (int)(Wire.read()); } -void SonarSRF::changeAddress(int newAddress) +/// +/// Set a new address +/// +/// +/// The address given in Arduino 7bit has to be converted back into SRF 8bit +/// newAddress can be set to any of E0, E2, E4, E6, E8, EA, EC, EE, F0, F2, +/// F4, F6, F8, FA, FC, FE. +/// +/// The new address +void SonarSRF::setAddress(int newAddress) { - // The address given in Arduino 7bit has to be converted back into SRF 8bit - // newAddress << 1 can be set to any of E0, E2, E4, E6, E8, EA, EC, EE - // F0, F2, F4, F6, F8, FA, FC, FE sendCommand(0xA0); sendCommand(0xAA); sendCommand(0xA5); diff --git a/SonarSRF.h b/SonarSRF.h index fbf7c09..8c01043 100644 --- a/SonarSRF.h +++ b/SonarSRF.h @@ -5,8 +5,8 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // // Sensor connections: @@ -19,24 +19,29 @@ #include #include -#define INCHES 0x50 -#define CENTIMETERS 0x51 -#define MICROSECONDS 0x52 -#define COMMAND_REGISTER (byte)0x00 -#define RESULT_REGISTER 0x02 +// Read +#define SOFTWARE_REVISION 0x00 +#define LIGHT_SENSOR 0x01 +// Write +#define COMMAND_REGISTER 0x00 +#define MAX_GAIN_REGISTER 0x01 +#define RANGE_REGISTER 0x02 +// Units +#define INCHES 0x50 +#define CENTIMETERS 0x51 +#define MICROSECONDS 0x52 class SonarSRF { public: - void connect(int address, int gainRegister = NULL, int rangeLocation = NULL); + void begin(int address, int gainRegister = 0, int rangeLocation = 0); virtual void startRanging(char unit); virtual int getRange(char unit = 'c', bool andStart = true); - int getSoft(); - void changeAddress(int newAddress); - + int getVersion(); + void setAddress(int newAddress); protected: virtual void waitForCompletion(); - void sendCommand(int command = NULL, int addressRegister = COMMAND_REGISTER); + void sendCommand(int command = 0, int addressRegister = COMMAND_REGISTER); int _address; int _gainRegister; int _rangeLocation; diff --git a/SonarSRF02.cpp b/SonarSRF02.cpp index a01a142..16bfd6f 100644 --- a/SonarSRF02.cpp +++ b/SonarSRF02.cpp @@ -5,15 +5,20 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #include "SonarSRF02.h" +/// +/// Wait for completion +/// +/// +/// Completion wait on SRF02 takes 66ms, std way to check register 0 doesn't +/// seem to work with software revision 6. +/// void SonarSRF02::waitForCompletion() { delay(66); - // Completion wait on 02 takes 66ms, - // std way to check register 0 doesnt seem to work with software revision 6 } diff --git a/SonarSRF02.h b/SonarSRF02.h index 8678d0f..81f15b9 100644 --- a/SonarSRF02.h +++ b/SonarSRF02.h @@ -5,8 +5,8 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #ifndef SonarSRF02_h diff --git a/SonarSRF08.cpp b/SonarSRF08.cpp new file mode 100644 index 0000000..e51226c --- /dev/null +++ b/SonarSRF08.cpp @@ -0,0 +1,27 @@ +// +// SonarSRF +// Arduino Library for controlling SRF sonar sensors +// http://www.arduino.cc/playground/Main/SonarSrf08 +// +// MIT License +// Copyright(c) 2009 Zach Foresta +// Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro +// + +#include "SonarSRF08.h" + +/// +/// Get luminosity captured by SRF08 sensor +/// +/// The luminosity (one byte) +int SonarSRF08::getLuminosity() +{ + startRanging('i'); + delay(70); + + sendCommand(0, LIGHT_SENSOR); + Wire.requestFrom(_address, 1); + while (Wire.available() < 0); // While byte available + return (int)(Wire.read()); +} diff --git a/SonarSRF08.h b/SonarSRF08.h index 1de6dce..339e211 100644 --- a/SonarSRF08.h +++ b/SonarSRF08.h @@ -5,8 +5,8 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #ifndef SonarSRF08_h @@ -16,6 +16,8 @@ class SonarSRF08 : public SonarSRF { +public: + int getLuminosity(); }; #endif diff --git a/examples/SRF02/SRF02.ino b/examples/SRF02/SRF02.ino index 048591e..9cde4fd 100644 --- a/examples/SRF02/SRF02.ino +++ b/examples/SRF02/SRF02.ino @@ -5,8 +5,8 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #include @@ -22,11 +22,11 @@ char unit = 'c'; // 'i' for inches, 'c' for centimeters, 'm' for micro-seconds void setup() { Serial.begin(115200); - LeftSonar.connect(LEFT_02_ADDRESS); - isConnected("SRF02-left", LeftSonar.getSoft()); + LeftSonar.begin(LEFT_02_ADDRESS); + isConnected("SRF02-left", LeftSonar.getVersion()); - RightSonar.connect(RIGHT_02_ADDRESS); - isConnected("SRF02-right", RightSonar.getSoft()); + RightSonar.begin(RIGHT_02_ADDRESS); + isConnected("SRF02-right", RightSonar.getVersion()); } void loop() { diff --git a/examples/SRF08/SRF08.ino b/examples/SRF08/SRF08.ino index 29c5a9f..85d28da 100644 --- a/examples/SRF08/SRF08.ino +++ b/examples/SRF08/SRF08.ino @@ -5,8 +5,8 @@ // // MIT License // Copyright(c) 2009 Zach Foresta -// Copyright(c) 2012 Leo Colombaro // Copyright(c) 2012 Philipp A. Mohrenweiser +// Copyright(c) 2012-2016 Leo Colombaro // #include @@ -27,8 +27,8 @@ char unit = 'c'; // 'i' for inches, 'c' for centimeters, 'm' for micro-seconds void setup() { Serial.begin(9600); - MainSonar.connect(MAIN_08_ADDRESS, GAIN_REGISTER, LOCATION_REGISTER); - isConnected("SRF08", MainSonar.getSoft()); + MainSonar.begin(MAIN_08_ADDRESS, GAIN_REGISTER, LOCATION_REGISTER); + isConnected("SRF08", MainSonar.getVersion()); } void loop() { diff --git a/keywords.txt b/keywords.txt index 26deecc..32c21c0 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,11 +13,12 @@ SonarSRF08 KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -connect KEYWORD2 +begin KEYWORD2 startRanging KEYWORD2 getRange KEYWORD2 -getSoft KEYWORD2 -changeAddress KEYWORD2 +getVersion KEYWORD2 +setAddress KEYWORD2 +getLuminosity KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..eb69136 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=SonarSRF +version=3.0.0 +author=Léo Colombaro +maintainer=Léo Colombaro +sentence=Arduino interfacing with sonar sensor SRF +paragraph=This lib wants to give a quick shot on differences for usage of SRF 02 and 08 atm. +category=Sensors +url=https://github.com/LeoColomb/Arduino-SRF +architectures=*