Robot team project
Dependencies: QEI Motordriver ros_lib_melodic
Diff: sensor/Sensor.cpp
- Revision:
- 5:8ef79eebbc97
- Parent:
- 4:cb50c6fa340b
diff -r cb50c6fa340b -r 8ef79eebbc97 sensor/Sensor.cpp --- a/sensor/Sensor.cpp Tue Oct 22 11:52:32 2019 +0000 +++ b/sensor/Sensor.cpp Tue Oct 22 15:06:44 2019 +0000 @@ -3,6 +3,9 @@ #include "Sensor.h" +/////////////////////////////////////////////////////////////////// +// Constructor +/////////////////////////////////////////////////////////////////// Sensor::Sensor(PinName sda, PinName scl, PinName shdn) : i2c(sda, scl), SHDN(shdn) { @@ -13,6 +16,9 @@ wait_ms(0.5); } +/////////////////////////////////////////////////////////////////// +// Public methods +/////////////////////////////////////////////////////////////////// int Sensor::init() { // Turn on the device @@ -68,12 +74,9 @@ addr = address << 1; } -/////////////////////////////////////////////////////////////////// -// New measurement -/////////////////////////////////////////////////////////////////// float Sensor::read() { - float range; + int range; // Start range measurement startRange(); @@ -83,14 +86,43 @@ // Read range result range = readRange(); + + if (range < 255) + { + isObstacle = true; + } + else + { + isObstacle = false; + } // Clear the interrupt on VL6180 clearInterrupts(); + // Display result return range; } +void Sensor::turnOff() +{ + SHDN = 0; +} + +void Sensor::turnOn() +{ + SHDN = 1; +} + +bool Sensor::getIsObstacle() +{ + return isObstacle; +} + + +/////////////////////////////////////////////////////////////////// +// Private methods +/////////////////////////////////////////////////////////////////// void Sensor::startRange() { writeByte(0x018,0x01); @@ -114,7 +146,7 @@ float Sensor::readRange() { - char range; + float range; range = readByte(RESULT_RANGE_VAL); return (float)range; } @@ -124,16 +156,6 @@ writeByte(0x015,0x07); } -void Sensor::turnOff() -{ - SHDN = 0; -} - -void Sensor::turnOn() -{ - SHDN = 1; -} - /////////////////////////////////////////////////////////////////// // Update a VL6180X register ///////////////////////////////////////////////////////////////////