Skip to content

Commit

Permalink
added demo SRF02 range code and altered it to write to a log file, in…
Browse files Browse the repository at this point in the history
…cluding microsecond timestamps and the current calibrated minimum range. The min range settles within a couple of seconds
  • Loading branch information
Dav Stott committed Mar 31, 2013
1 parent 95a3259 commit 93462d0
Show file tree
Hide file tree
Showing 3 changed files with 389 additions and 0 deletions.
301 changes: 301 additions & 0 deletions range/rangeOutput
@@ -0,0 +1,301 @@
time,range,min_range
1364694350.838337,33,25
1364694351.021270,244,22
1364694351.203128,31,20
1364694351.384847,28,17
1364694351.566601,26,15
1364694351.748303,26,15
1364694351.930024,26,15
1364694352.111727,29,12
1364694352.293428,31,9
1364694352.475204,34,11
1364694352.656909,36,11
1364694352.838654,34,11
1364694353.020360,33,11
1364694353.202080,33,12
1364694353.383818,33,12
1364694353.565522,34,12
1364694353.747284,34,12
1364694353.929047,33,12
1364694354.110761,33,12
1364694354.292488,33,12
1364694354.474217,34,12
1364694354.655933,34,12
1364694354.837664,33,12
1364694355.019422,34,11
1364694355.201140,34,12
1364694355.382891,33,12
1364694355.564657,34,12
1364694355.746445,34,12
1364694355.928191,33,12
1364694356.109892,33,12
1364694356.291618,32,12
1364694356.473373,32,12
1364694356.655120,32,12
1364694356.836851,33,12
1364694357.018603,32,12
1364694357.200337,32,12
1364694357.382054,32,11
1364694357.563823,33,12
1364694357.745540,41,12
1364694357.927240,104,11
1364694358.108976,56,12
1364694358.290688,66,12
1364694358.472394,73,12
1364694358.654096,77,12
1364694358.835812,82,12
1364694359.017569,91,12
1364694359.199340,234,12
1364694359.381080,193,12
1364694359.562790,179,12
1364694359.744495,168,12
1364694359.926242,191,12
1364694360.108038,63,12
1364694360.289791,191,12
1364694360.471542,195,12
1364694360.653246,191,12
1364694360.834966,192,12
1364694361.016708,192,11
1364694361.198474,191,12
1364694361.380237,191,11
1364694361.562000,191,12
1364694361.743724,191,12
1364694361.925457,191,12
1364694362.107184,84,12
1364694362.288948,83,12
1364694362.470659,162,12
1364694362.652377,78,12
1364694362.834102,76,12
1364694363.015862,74,12
1364694363.197629,74,12
1364694363.379409,74,11
1364694363.561151,78,12
1364694363.742867,88,12
1364694363.924571,192,12
1364694364.106324,191,12
1364694364.288049,191,12
1364694364.469860,192,12
1364694364.651614,192,12
1364694364.833323,192,12
1364694365.015021,192,12
1364694365.196781,192,12
1364694365.378478,65,12
1364694365.560194,176,12
1364694365.741945,83,12
1364694365.923666,93,12
1364694366.105404,97,12
1364694366.287076,100,12
1364694366.468846,102,12
1364694366.650682,105,12
1364694366.832405,193,12
1364694367.014131,193,12
1364694367.195870,112,12
1364694367.377538,118,11
1364694367.559273,117,12
1364694367.741003,120,11
1364694367.922740,117,12
1364694368.104503,117,12
1364694368.286187,118,12
1364694368.467954,118,12
1364694368.649805,120,11
1364694368.831558,122,12
1364694369.013320,126,12
1364694369.195040,126,12
1364694369.376701,128,12
1364694369.558429,130,12
1364694369.740153,132,12
1364694369.921888,134,12
1364694370.103602,136,12
1364694370.285271,139,12
1364694370.466983,139,12
1364694370.648745,141,12
1364694370.830464,139,12
1364694371.012203,133,12
1364694371.193936,126,12
1364694371.375603,123,12
1364694371.557351,118,12
1364694371.739164,114,12
1364694371.920866,192,12
1364694372.102564,109,12
1364694372.284331,105,12
1364694372.466025,105,12
1364694372.647724,100,12
1364694372.829535,95,12
1364694373.011260,90,12
1364694373.193015,84,12
1364694373.374701,78,12
1364694373.556441,72,12
1364694373.738192,67,12
1364694373.919910,61,12
1364694374.101612,53,12
1364694374.283321,45,12
1364694374.465053,36,12
1364694374.646767,26,12
1364694374.828475,18,12
1364694375.010199,14,12
1364694375.191921,15,12
1364694375.373656,21,12
1364694375.555378,47,12
1364694375.737087,195,12
1364694375.918828,193,12
1364694376.100560,193,12
1364694376.282233,154,12
1364694376.463945,155,12
1364694376.645686,140,12
1364694376.827428,118,12
1364694377.009188,98,12
1364694377.190892,83,12
1364694377.372586,74,12
1364694377.554302,65,12
1364694377.735996,58,12
1364694377.917685,51,12
1364694378.099433,43,11
1364694378.281137,35,12
1364694378.462927,31,12
1364694378.644620,32,12
1364694378.826337,38,12
1364694379.008043,43,12
1364694379.189888,52,12
1364694379.371587,60,12
1364694379.553446,70,12
1364694379.735252,78,12
1364694379.916948,85,11
1364694380.098724,93,12
1364694380.280411,102,11
1364694380.462135,112,12
1364694380.643862,117,11
1364694380.825607,122,12
1364694381.007304,128,12
1364694381.189458,131,12
1364694381.371126,135,12
1364694381.552857,147,12
1364694381.734560,143,12
1364694381.916241,140,12
1364694382.097966,137,12
1364694382.279685,120,12
1364694382.461423,191,12
1364694382.643138,191,12
1364694382.824885,113,12
1364694383.006589,117,12
1364694383.188327,123,12
1364694383.370010,129,12
1364694383.551728,191,12
1364694383.733477,140,12
1364694383.915191,150,12
1364694384.096921,148,12
1364694384.278649,153,12
1364694384.460395,154,12
1364694384.642118,152,12
1364694384.823844,143,12
1364694385.005592,135,12
1364694385.187319,128,12
1364694385.369091,122,12
1364694385.550789,117,12
1364694385.732514,111,12
1364694385.914219,102,11
1364694386.095950,96,12
1364694386.277668,89,12
1364694386.459472,82,11
1364694386.641295,183,12
1364694386.823321,191,12
1364694387.005095,62,12
1364694387.186850,53,12
1364694387.368538,45,12
1364694387.550263,33,12
1364694387.731967,25,12
1364694387.913657,22,12
1364694388.095417,21,12
1364694388.277152,24,12
1364694388.458885,28,12
1364694388.640597,34,12
1364694388.822355,39,12
1364694389.004050,45,12
1364694389.185724,49,12
1364694389.367424,57,12
1364694389.549182,64,12
1364694389.730917,72,12
1364694389.912621,77,12
1364694390.094346,81,12
1364694390.276090,87,12
1364694390.457892,192,12
1364694390.639734,192,11
1364694390.821458,108,12
1364694391.003163,192,11
1364694391.184883,113,12
1364694391.366595,117,12
1364694391.548322,191,12
1364694391.730047,124,12
1364694391.911800,128,12
1364694392.093541,131,12
1364694392.275240,131,12
1364694392.457005,133,12
1364694392.638725,137,12
1364694392.820424,191,12
1364694393.002116,143,12
1364694393.183818,145,12
1364694393.365518,146,12
1364694393.547240,147,12
1364694393.729006,149,12
1364694393.910738,153,12
1364694394.092460,160,12
1364694394.274169,164,12
1364694394.455909,166,12
1364694394.637612,167,12
1364694394.819313,164,12
1364694395.000998,160,12
1364694395.182739,152,12
1364694395.364489,147,12
1364694395.546248,139,12
1364694395.727970,132,12
1364694395.909780,128,12
1364694396.091535,121,12
1364694396.273263,118,12
1364694396.454970,112,12
1364694396.636699,191,12
1364694396.818392,100,12
1364694397.000109,93,12
1364694397.181864,86,12
1364694397.363565,76,12
1364694397.545282,67,12
1364694397.727037,57,12
1364694397.908803,50,12
1364694398.090504,42,12
1364694398.272178,34,12
1364694398.453920,28,12
1364694398.635573,23,12
1364694398.817276,22,12
1364694398.999032,19,12
1364694399.180779,20,12
1364694399.362474,23,12
1364694399.544188,29,12
1364694399.726079,35,12
1364694399.907874,43,12
1364694400.089623,50,12
1364694400.271332,58,12
1364694400.453068,66,12
1364694400.634759,74,12
1364694400.816471,80,12
1364694400.998184,88,12
1364694401.179899,102,12
1364694401.361616,104,12
1364694401.543352,191,12
1364694401.725091,191,12
1364694401.906810,121,12
1364694402.088524,123,12
1364694402.270260,127,12
1364694402.451944,130,12
1364694402.633725,137,12
1364694402.815447,144,12
1364694402.997167,151,12
1364694403.179024,159,12
1364694403.360734,163,12
1364694403.542490,169,12
1364694403.724218,172,12
1364694403.905927,176,12
1364694404.087681,178,12
1364694404.269429,176,12
1364694404.451113,168,12
1364694404.632822,161,12
1364694404.814588,154,12
1364694404.996285,147,12
1364694405.178005,191,12
88 changes: 88 additions & 0 deletions range/rpi_srf02.c
@@ -0,0 +1,88 @@
// SRF02 example coder for the Raspberry pi
//
// This code will work for the SRF02/ 10/ 235 and 08.
// It will take a ranging from the module and print results
// to the screen.
//
// By James Henderson, 2012.
// Hacked about slightly by Dav

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char **argv)
{

int fd; // File description
char *fileName = "/dev/i2c-1"; // Name of the port we will be using
int i;
FILE *fdOut; // handle to output file
char *outFileName = "rangeOutput";
struct timeval t;
int address = 0x70; // Address of the SRF02 shifted right one bit
unsigned char buf[10]; // Buffer for data being read/ written on the i2c bus
unsigned int range;
unsigned int minRange;

if ((fd = open(fileName, O_RDWR)) < 0) { // Open port for reading and writing
printf("Failed to open i2c port\n");
exit(1);
}

if (ioctl(fd, I2C_SLAVE, address) < 0) { // Set the port options and set the address of the device we wish to speak to
printf("Unable to get bus access to talk to slave\n");
exit(1);
}
// open our log file and print a header line
fdOut = fopen(outFileName, "w");
fprintf(fdOut, "time,range,min_range\n");

for (i=0;i<300;i++) {
buf[0] = 0; // Commands for performing a ranging
buf[1] = 81;

if ((write(fd, buf, 2)) != 2) { // Write ping command to the i2c port
printf("Error writing to i2c slave\n");
exit(1);
}
usleep(80000); // This sleep waits for the ping to come back
buf[0] = 0; // This is the register we wish to read from
if ((write(fd, buf, 1)) != 1) { // Send the register to read from
printf("Error writing to i2c slave\n");
exit(1);
}
if (read(fd, buf, 6) != 6) { // Read back data into buf[]
printf("Unable to read from slave\n");
exit(1);
}
else {
// buf[0] software version. If this is 255, then the ping has not yet returned
// buf[1] unused
// buf[2] high byte range
// buf[3] low byte range
// buf[4] high byte minimum auto tuned range
// buf[5] low byte minimum auto tuned range

range = (buf[2] <<8) + buf[3]; // Calculate range as a word value
minRange = (buf[4] <<8) + buf[5]; // Calculate range as a word value
}

printf("Range was: %u. Minimum: %u\n", range, minRange);
gettimeofday(&t, NULL);
fprintf(fdOut, "%u.%06d,%u,%u\n", t.tv_sec, t.tv_usec, range, minRange);

usleep(100000); // wait another 100ms and perform another ranging
} // end for 300
close(fd);
fclose(fdOut);
return 0;
}

Binary file added range/srf02
Binary file not shown.

0 comments on commit 93462d0

Please sign in to comment.