version for edu_robot TM
Fork of SRF02 by
SRF02.cpp@2:9da88e2889b6, 2014-02-26 (annotated)
- Committer:
- BjornVB
- Date:
- Wed Feb 26 08:29:45 2014 +0000
- Revision:
- 2:9da88e2889b6
- Parent:
- 0:8ac34d529a3b
Project files for EDU robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobboteck | 0:8ac34d529a3b | 1 | #include "SRF02.h" |
bobboteck | 0:8ac34d529a3b | 2 | |
bobboteck | 0:8ac34d529a3b | 3 | /* Creates an instance of class. Setting the pin used for I2C, the address of device and the measure range type. */ |
bobboteck | 0:8ac34d529a3b | 4 | SRF02::SRF02(PinName sda, PinName scl, int addr, char type) : _i2c(sda, scl), _addr(addr) |
bobboteck | 0:8ac34d529a3b | 5 | { |
BjornVB | 2:9da88e2889b6 | 6 | |
bobboteck | 0:8ac34d529a3b | 7 | _typem = type; |
bobboteck | 0:8ac34d529a3b | 8 | } |
bobboteck | 0:8ac34d529a3b | 9 | |
bobboteck | 0:8ac34d529a3b | 10 | /* Destroyer of class instance. */ |
bobboteck | 0:8ac34d529a3b | 11 | SRF02::~SRF02() |
bobboteck | 0:8ac34d529a3b | 12 | { |
bobboteck | 0:8ac34d529a3b | 13 | |
bobboteck | 0:8ac34d529a3b | 14 | } |
bobboteck | 0:8ac34d529a3b | 15 | |
bobboteck | 0:8ac34d529a3b | 16 | /* Start and return the range measure. */ |
bobboteck | 0:8ac34d529a3b | 17 | float SRF02::read() |
bobboteck | 0:8ac34d529a3b | 18 | { |
bobboteck | 0:8ac34d529a3b | 19 | char command[2]; |
bobboteck | 0:8ac34d529a3b | 20 | char result[2]; |
bobboteck | 0:8ac34d529a3b | 21 | |
bobboteck | 0:8ac34d529a3b | 22 | command[0] = 0x00; // Set the command register |
BjornVB | 2:9da88e2889b6 | 23 | |
bobboteck | 0:8ac34d529a3b | 24 | command[1] = _typem; // Ranging results in type indicated in a costructor |
bobboteck | 0:8ac34d529a3b | 25 | _i2c.write(_addr, command, 2); // Send the command to start a ranging burst |
bobboteck | 0:8ac34d529a3b | 26 | |
bobboteck | 0:8ac34d529a3b | 27 | wait_ms(70); // Wait 70mS for complete the measure |
bobboteck | 0:8ac34d529a3b | 28 | |
bobboteck | 0:8ac34d529a3b | 29 | command[0] = 0x02; // The start address of measure result |
bobboteck | 0:8ac34d529a3b | 30 | _i2c.write(_addr, command, 1, 1); // Send address to read a measure |
bobboteck | 0:8ac34d529a3b | 31 | _i2c.read(_addr, result, 2); // Read two byte of measure |
bobboteck | 0:8ac34d529a3b | 32 | |
bobboteck | 0:8ac34d529a3b | 33 | float range = (result[0]<<8)+result[1]; // Convert the two byte in a float value |
bobboteck | 0:8ac34d529a3b | 34 | |
bobboteck | 0:8ac34d529a3b | 35 | return range; |
BjornVB | 2:9da88e2889b6 | 36 | } |