Skip to content

Commit

Permalink
Merge pull request #5 from LeoColomb/v3.0-wip
Browse files Browse the repository at this point in the history
v3.0
  • Loading branch information
LeoColomb authored Jun 11, 2016
2 parents fcff5b9 + 877f483 commit c08e064
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 54 deletions.
86 changes: 59 additions & 27 deletions SonarSRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// Initiates the connection with the sensor and start I2C bus
/// </summary>
/// <param name="address">I2C Address</param>
/// <param name="gainRegister">SRF Location 1</param>
/// <param name="rangeLocation">SRF Location 2</param>
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
/// <summary>
/// Sets Units for display / storage
/// </summary>
/// <remarks>
/// * 'i' for inches
/// * 'c' for centimeters
/// * 'm' for microseconds
/// </remarks>
/// <param name="unit">Unit used</param>
void SonarSRF::startRanging(char unit)
{
switch (unit)
Expand All @@ -40,70 +53,89 @@ void SonarSRF::startRanging(char unit)
}
}

// Communicates with Sonar to send commands
/// <summary>
/// Communicates with Sonar to send commands
/// </summary>
/// <param name="command">SRF Command</param>
/// <param name="addressRegister">SRF Location 0</param>
/// <seealso cref="connect"/>
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
/// <summary>
/// Read data from register return result
/// </summary>
/// <param name="unit">Unit used</param>
/// <param name="andStart"></param>
/// <returns>The range number (two bytes)</returns>
/// <seealso cref="startRanging"/>
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);
}

/// <summary>
/// Wait for completion
/// </summary>
void SonarSRF::waitForCompletion()
{
while (getSoft() == -1)
while (getVersion() == -1)
{
delay(1);
}
}

// Get software revision
int SonarSRF::getSoft()
/// <summary>
/// Get software revision
/// </summary>
/// <returns>The software revision (one byte)</returns>
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)
/// <summary>
/// Set a new address
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="newAddress">The new address</param>
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);
Expand Down
27 changes: 16 additions & 11 deletions SonarSRF.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -19,24 +19,29 @@
#include <Wire.h>
#include <Arduino.h>

#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;
Expand Down
11 changes: 8 additions & 3 deletions SonarSRF02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/// <summary>
/// Wait for completion
/// </summary>
/// <remarks>
/// Completion wait on SRF02 takes 66ms, std way to check register 0 doesn't
/// seem to work with software revision 6.
/// </remarks>
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
}
2 changes: 1 addition & 1 deletion SonarSRF02.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions SonarSRF08.cpp
Original file line number Diff line number Diff line change
@@ -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"

/// <summary>
/// Get luminosity captured by SRF08 sensor
/// </summary>
/// <returns>The luminosity (one byte)</returns>
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());
}
4 changes: 3 additions & 1 deletion SonarSRF08.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +16,8 @@

class SonarSRF08 : public SonarSRF
{
public:
int getLuminosity();
};

#endif
10 changes: 5 additions & 5 deletions examples/SRF02/SRF02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Wire.h>
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions examples/SRF08/SRF08.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Wire.h>
Expand All @@ -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() {
Expand Down
7 changes: 4 additions & 3 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=SonarSRF
version=3.0.0
author=Léo Colombaro
maintainer=Léo Colombaro <[email protected]>
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=*

0 comments on commit c08e064

Please sign in to comment.