Drivers for Pololu irs09a short range infra red sensor.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Hypna
Date:
Fri Mar 25 22:09:43 2016 +0000
Commit message:
Initial upload

Changed in this revision

ShortRangeSensor.cpp Show annotated file Show diff for this revision Revisions of this file
ShortRangeSensor.h Show annotated file Show diff for this revision Revisions of this file
ShortRangeSensor_Test.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fba97a03cd3c ShortRangeSensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShortRangeSensor.cpp	Fri Mar 25 22:09:43 2016 +0000
@@ -0,0 +1,54 @@
+#include "ShortRangeSensor.h"
+
+ShortRangeSensor::ShortRangeSensor(PinName sda, PinName scl) : i2c(sda, scl)
+{
+    //disables ECE
+    i2c.write(ADDR, SYSRANGE__RANGE_CHECK_ENABLES, 3);
+}
+
+//returns range in mm
+int ShortRangeSensor::getRange()
+{
+    char data;
+    
+    i2c.write(ADDR, SYSRANGE__START_SAMPLE, 3);
+    wait_ms(20);
+    i2c.write(ADDR, RESULT__RANGE_VAL, 2);
+    i2c.read(ADDR, &data, 1);
+    
+    return data;
+}
+
+int ShortRangeSensor::getStatus()
+{
+    char data;
+    
+    i2c.write(ADDR, RESULT__RANGE_STATUS, 2);
+    i2c.read(ADDR, &data, 1);
+    
+    return data;
+}
+
+int ShortRangeSensor::getPPOffset()
+{
+    char data;
+    
+    i2c.write(ADDR, SYSRANGE__PART_TO_PART_RANGE_OFFSET, 2);
+    i2c.read(ADDR, &data, 1);
+    
+    return data;
+}
+
+void ShortRangeSensor::setPPOffset(int offset)
+{
+    if(offset < -128 || offset > 128)
+        return;
+        
+    char cmd[3];
+    
+    cmd[0] = SYSRANGE__PART_TO_PART_RANGE_OFFSET[0];
+    cmd[1] = SYSRANGE__PART_TO_PART_RANGE_OFFSET[1];
+    cmd[2] = offset;
+    
+    i2c.write(ADDR, cmd, 3);
+}
\ No newline at end of file
diff -r 000000000000 -r fba97a03cd3c ShortRangeSensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShortRangeSensor.h	Fri Mar 25 22:09:43 2016 +0000
@@ -0,0 +1,32 @@
+#ifndef SHORTRANGESENSOR_H
+#define SHORTRANGESENSOR_H
+
+#include "mbed.h"
+
+//constants for i2c communication
+const int ADDR = 0x52;
+const char SYSRANGE__START_SAMPLE[] = {0x00,0x18,0x01};
+const char RESULT__RANGE_VAL[] = {0x00,0x62};
+const char RESULT__RANGE_STATUS[] = {0x00,0x4D};
+const char SYSRANGE__RANGE_CHECK_ENABLES[] = {0x00,0x2D,0x10};
+const char SYSRANGE__PART_TO_PART_RANGE_OFFSET_INIT[] = {0x00,0x24,0x00}; //once correct offset is found, chage 0x00 to correct offset
+const char SYSRANGE__PART_TO_PART_RANGE_OFFSET[] = {0x00,0x24};
+
+class ShortRangeSensor
+{
+    public:
+    ShortRangeSensor(PinName sda, PinName scl);
+    //returns range in mm
+    int getRange();
+    //returns value of status register
+    int getStatus();
+    //returns value of part to part range offset register
+    int getPPOffset();
+    //sets value of part to part range offset register, -128 to 128
+    void setPPOffset(int offset);
+    
+    private:
+    I2C i2c;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r fba97a03cd3c ShortRangeSensor_Test.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ShortRangeSensor_Test.cpp	Fri Mar 25 22:09:43 2016 +0000
@@ -0,0 +1,20 @@
+#include "mbed.h"
+#include "ShortRangeSensor.h"
+
+int main()
+{
+    ShortRangeSensor srs(PTC11, PTC10);
+    Serial pc(USBTX, USBRX);
+    
+    pc.printf("Part to Part Offset = %X\r\n\n", srs.getPPOffset());
+    srs.setPPOffset(32);
+    pc.printf("Part to Part Offset = %X\r\n\n", srs.getPPOffset());
+    
+    while(true)
+    {
+        pc.printf("%i mm   %X\n\r", srs.getRange(), srs.getStatus());
+        wait(3);
+    }
+    
+    return 0;
+}
\ No newline at end of file
diff -r 000000000000 -r fba97a03cd3c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 25 22:09:43 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/c0f6e94411f5
\ No newline at end of file