Marian Andrecki
/
nihao_helper
set of sensor managers
Revision 0:d9d8c810ba83, committed 2014-07-27
- Comitter:
- iramusa
- Date:
- Sun Jul 27 20:42:24 2014 +0000
- Commit message:
- Ultrasonic ranger class operational.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04.cpp Sun Jul 27 20:42:24 2014 +0000 @@ -0,0 +1,33 @@ +#include "mbed.h" +#include "HCSR04.h" + +HCSR04::HCSR04(PinName pin_trigger, PinName pin_echo, float &container_var): +caller(pin_trigger), +listener(pin_echo){ + range_storage = &container_var; //check how it's done + listener.rise(this, &HCSR04::start_counting); + listener.fall(this, &HCSR04::stop_counting); + time_keeper.start(); +} + +void HCSR04::start_measurement(void){ + caller = 1; + controller.attach_us(this, &HCSR04::stop_calling, TRIGGER_TIME); +} + +void HCSR04::stop_calling(void){ + caller = 0; + controller.detach(); +} + +void HCSR04::start_counting(void){ + time_keeper.reset(); +} + +void HCSR04::stop_counting(void){ + float temp_range = (float)time_keeper.read_us()/58.0f; //convert to centimeters + if (temp_range < MAX_RANGE) + *range_storage = temp_range; + else + *range_storage = -1.0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04.h Sun Jul 27 20:42:24 2014 +0000 @@ -0,0 +1,28 @@ +#ifndef __HCSR04_H__ +#define __HCSR04_H__ + +#include "mbed.h" + +#define TRIGGER_TIME 10 //us +#define MAX_RANGE 400 //cm + + +class HCSR04{ +public: + HCSR04(PinName pin_trigger, PinName pin_echo, float &container_var); + void start_measurement(void); + float* range_storage; + +private: + long unsigned int time_0, time_f; + Ticker controller; + Timer time_keeper; + DigitalOut caller; + InterruptIn listener; + + void stop_calling(); + void start_counting(); + void stop_counting(); +}; + +#endif /* __HCSR04_H__ */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jul 27 20:42:24 2014 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +#include "range.cpp" +#include "HCSR04.h" + +int main() +{ + DigitalOut myled(LED_GREEN); + Serial pc(USBTX, USBRX); + float range = 0.0f; + HCSR04 ranger(PTA12, PTD4, range); //trig, echo + pc.printf("Hello World!\n"); + + while (true) { + ranger.start_measurement(); + wait(0.5f); // wait a small period of time + pc.printf("range = %f cm \n", range); + myled = !myled; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jul 27 20:42:24 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file