han back
/
CLEO_UART_ULTRASONIC
SMART CLEO Uart Ultrasonic
Revision 0:5940ac32cd9f, committed 2017-09-28
- Comitter:
- SMART_CLEO
- Date:
- Thu Sep 28 03:42:40 2017 +0000
- Commit message:
- SMART_CLEO
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04/HCSR04.cpp Thu Sep 28 03:42:40 2017 +0000 @@ -0,0 +1,81 @@ +#include "mbed.h" +#include "HCSR04.h" + +HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { + init(); +} + +void HCSR04::init() { + distance = -1; // initial distance + minDistance = 2; + maxDistance = 400; + newDataReady = timerStarted = false; +} + +void HCSR04::startTimer() { + if (!timerStarted) { + timer.start(); // start the timer + timerStarted = true; + echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur + echo.fall(this, &HCSR04::stopTimer); + echo.rise(NULL); + } +} + +void HCSR04::stopTimer() { + timer.stop(); // stop the timer + if (timerStarted) { + distance = timer.read() * 1e6 / 58; + if (distance < minDistance) + distance = minDistance; + if (distance > maxDistance) + distance = maxDistance; + newDataReady = true; + } + timer.reset(); + timerStarted = false; + echoTimeout.detach(); + echo.fall(NULL); +} + +void HCSR04::turnOffTrigger() { + trigger = 0; +} + +void HCSR04::startMeasurement() { + trigger = 1; + triggerTimeout.attach_us(this, &HCSR04::turnOffTrigger, 10); + echo.rise(this, &HCSR04::startTimer); + newDataReady = false; +} + +float HCSR04::getDistance_cm() { + newDataReady = false; + return distance; +} + +float HCSR04::getDistance_mm() { + newDataReady = false; + return distance * 10; +} + +bool HCSR04::isNewDataReady() { + return newDataReady; +} + +void HCSR04::setRanges(float minRange, float maxRange) { + if (minRange < maxRange) { + if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected + minDistance = minRange; + if (maxRange <= 400) + maxDistance = maxRange; + } +} + +float HCSR04::getMinRange() { + return minDistance; +} + +float HCSR04::getMaxRange() { + return maxDistance; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04/HCSR04.h Thu Sep 28 03:42:40 2017 +0000 @@ -0,0 +1,103 @@ +#ifndef HCSR04_H_TVZMT +#define HCSR04_H_TVZMT + +/** A distance measurement class using ultrasonic sensor HC-SR04. + * + * Example of use: + * @code + * #include "mbed.h" + * #include "HCSR04.h" + * + * Serial pc(USBTX, USBRX); + * Timer timer; + * + * int main() { + * HCSR04 sensor(p5, p7); + * sensor.setRanges(10, 110); + * pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", + * sensor.getMinRange(), sensor.getMaxRange()); + * while(true) { + * timer.reset(); + * timer.start(); + * sensor.startMeasurement(); + * while(!sensor.isNewDataReady()) { + * // wait for new data + * // waiting time depends on the distance + * } + * pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); + * timer.stop(); + * wait_ms(500 - timer.read_ms()); // time the loop + * } + * } + * @endcode + */ +class HCSR04 { + + public: + + /** Receives two PinName variables. + * @param echoPin mbed pin to which the echo signal is connected to + * @param triggerPin mbed pin to which the trigger signal is connected to + */ + HCSR04(PinName echoPin, PinName triggerPin); + + /** Start the measurement. Measurement time depends on the distance. + * Maximum measurement time is limited to 25 ms (400 cm). + */ + void startMeasurement(); + + /** Returns the distance in cm. Requires previous call of startMeasurement(). + * @returns distance of the measuring object in cm. + */ + float getDistance_cm(); + + /** Returns the distance in mm. Requires previous call of startMeasurement(). + * @returns distance of the measuring object in mm. + */ + float getDistance_mm(); + + /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. + * @param minRange Minimum range in cm. Must be between 2 cm and maxRange. + * @param maxRange Maximum range in cm. Must be between minRange and 400 cm. + */ + void setRanges(float minRange, float maxRange); + + /** Retreives the minimum sensor range set by the user. + * @returns the minimum sensor range set by the user in cm. + */ + float getMinRange(); + + /** Retreives the maximum sensor range set by the user. + * @returns the maximum sensor range set by the user in cm. + */ + float getMaxRange(); + + /** Checks if the new data is ready. + * @returns true if new data is ready, false otherwise. + */ + bool isNewDataReady(); + + private: + + InterruptIn echo; // echo pin + DigitalOut trigger; // trigger pin + Timer timer; // echo pulsewidth measurement + float distance; // store the distance in cm + float minDistance; // minimum measurable distance + float maxDistance; // maximum measurable distance + Timeout triggerTimeout, echoTimeout; + bool newDataReady, timerStarted; + + /** Start the timer. */ + void startTimer(); + + /** Stop the timer. */ + void stopTimer(); + + /** Initialization. */ + void init(); + + void turnOffTrigger(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Sep 28 03:42:40 2017 +0000 @@ -0,0 +1,164 @@ +#include "mbed.h" +#include "HCSR04.h" + +struct UART_buf +{ + uint8_t STA; + uint8_t MODE; + uint8_t CMD; + uint8_t LEN; + uint8_t DATA[32]; + uint8_t END; + +}; + +PinName pin_ECHO = PB_7; +PinName pin_TRIG = PB_10; + +HCSR04 Ultrasonic(pin_ECHO, pin_TRIG); + +Serial SerialUART(PA_2, PA_3); + +uint8_t Buffer[37]; + +UART_buf RX_BUF; + +Timer Sensor_Timer; + +uint32_t T_period = 1000; +uint32_t timer_check = 0; + +void SerialUARTRX_ISR(void); +void Timer_setting(uint8_t cmd, uint8_t value); +void Sensor_Read(void); +float Ultrasonic_cm(void); + +int main() { + + SerialUART.baud(115200); + + SerialUART.attach(&SerialUARTRX_ISR); + + Timer_setting(0x06, 1); + + while(1) + { + //SerialUART.printf("%d", Sensor_Timer.read_ms()); + if((timer_check + T_period) < Sensor_Timer.read_ms()) + { + // SerialUART.printf("%d",T_period); + Sensor_Read(); + timer_check += T_period; + } + //wait(0.5); + } +} + +void SerialUARTRX_ISR(void) +{ + static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0; + uint8_t rx_da = SerialUART.getc(); + switch(RX_Status) + { + case 0: + if(rx_da == 0x76) + { + RX_BUF.STA = rx_da; + RX_Status++; + } + break; + case 1: + RX_BUF.MODE = rx_da; + RX_Status++; + break; + case 2: + RX_BUF.CMD = rx_da; + RX_Status++; + break; + case 3: + RX_BUF.LEN = rx_da; + RX_Len = RX_BUF.LEN; + RX_Status++; + if(RX_Len == 0) + RX_Status++; + break; + case 4: + RX_BUF.DATA[RX_count] = rx_da; + RX_count++; + if(RX_count == RX_Len) + { + RX_Status++; + RX_count = 0; + RX_Len = 32; + } + break; + case 5: + if(rx_da == 0x3E) + { + RX_BUF.END = rx_da; + RX_Status = 0; + switch(RX_BUF.MODE) + { + case 0x04: + Timer_setting(RX_BUF.CMD, RX_BUF.DATA[0]); + break; + } + } + break; + } +} + +void Timer_setting(uint8_t cmd, uint8_t value) +{ + double Time_value = 0; + Sensor_Timer.stop(); + Sensor_Timer.reset(); + switch(cmd) + { + case 0x01: + Time_value = 30; + break; + case 0x02: + Time_value = 60; + break; + case 0x03: + Time_value = 120; + break; + case 0x04: + Time_value = 300; + break; + case 0x05: + Time_value = 600; + break; + case 0x06: + Time_value = value; + Time_value = 1.0/Time_value; + break; + } + Sensor_Timer.start(); + timer_check = Sensor_Timer.read_ms(); + T_period = Time_value * 1000; +} + +void Sensor_Read(void) +{ + uint16_t dist = Ultrasonic_cm(); + Buffer[0] = 0x76; + Buffer[1] = 0x01; + Buffer[2] = 0x07; + Buffer[3] = 0x02; + Buffer[4] = dist >> 8; + Buffer[5] = dist & 0xFF; + Buffer[6] = 0x3E; + for(int i=0; i<7; i++) + SerialUART.putc(Buffer[i]); +} + +float Ultrasonic_cm(void) +{ + Ultrasonic.startMeasurement(); + + while(!Ultrasonic.isNewDataReady()); + + return Ultrasonic.getDistance_cm(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Sep 28 03:42:40 2017 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#ca661f9d28526ca8f874b05432493a489c9671ea