Ghk

Dependencies:   HCSR04

Committer:
michaelrodriguezg
Date:
Tue Mar 02 00:17:32 2021 +0000
Revision:
0:9b19dc46ec16
Web

Who changed what in which revision?

UserRevisionLine numberNew contents of line
michaelrodriguezg 0:9b19dc46ec16 1 #include "mbed.h"
michaelrodriguezg 0:9b19dc46ec16 2 #include "platform/mbed_thread.h"
michaelrodriguezg 0:9b19dc46ec16 3 #include "HCSR04.h"
michaelrodriguezg 0:9b19dc46ec16 4 #include "rtos.h"
michaelrodriguezg 0:9b19dc46ec16 5
michaelrodriguezg 0:9b19dc46ec16 6 HCSR04 ultra (D8,D9); //Trig-Echo
michaelrodriguezg 0:9b19dc46ec16 7 Serial pc(USBTX,USBRX);
michaelrodriguezg 0:9b19dc46ec16 8 int distancia;
michaelrodriguezg 0:9b19dc46ec16 9 PwmOut servo(D6);
michaelrodriguezg 0:9b19dc46ec16 10 DigitalOut led(LED1);
michaelrodriguezg 0:9b19dc46ec16 11
michaelrodriguezg 0:9b19dc46ec16 12 Thread thread_servo;
michaelrodriguezg 0:9b19dc46ec16 13 Queue<uint32_t, 5> queue;
michaelrodriguezg 0:9b19dc46ec16 14 Timer timer;
michaelrodriguezg 0:9b19dc46ec16 15 float duracion;
michaelrodriguezg 0:9b19dc46ec16 16
michaelrodriguezg 0:9b19dc46ec16 17 void task_servo(){
michaelrodriguezg 0:9b19dc46ec16 18 while(true){
michaelrodriguezg 0:9b19dc46ec16 19 distancia = ultra.distance(CM);
michaelrodriguezg 0:9b19dc46ec16 20 if (distancia>=20){
michaelrodriguezg 0:9b19dc46ec16 21 distancia=20;
michaelrodriguezg 0:9b19dc46ec16 22 }
michaelrodriguezg 0:9b19dc46ec16 23 if(distancia<3){
michaelrodriguezg 0:9b19dc46ec16 24 distancia=1;
michaelrodriguezg 0:9b19dc46ec16 25 }
michaelrodriguezg 0:9b19dc46ec16 26 if(distancia<5){
michaelrodriguezg 0:9b19dc46ec16 27 servo.pulsewidth_us(2500);
michaelrodriguezg 0:9b19dc46ec16 28 }
michaelrodriguezg 0:9b19dc46ec16 29 else if(distancia>5 && distancia<8){
michaelrodriguezg 0:9b19dc46ec16 30 servo.pulsewidth_us(2100);
michaelrodriguezg 0:9b19dc46ec16 31 }
michaelrodriguezg 0:9b19dc46ec16 32 else if(distancia>8 && distancia<11){
michaelrodriguezg 0:9b19dc46ec16 33 servo.pulsewidth_us(1700);
michaelrodriguezg 0:9b19dc46ec16 34 }
michaelrodriguezg 0:9b19dc46ec16 35 else if(distancia>11 && distancia<14){
michaelrodriguezg 0:9b19dc46ec16 36 servo.pulsewidth_us(1300);
michaelrodriguezg 0:9b19dc46ec16 37 }
michaelrodriguezg 0:9b19dc46ec16 38 else if(distancia>14 && distancia<17){
michaelrodriguezg 0:9b19dc46ec16 39 servo.pulsewidth_us(900);
michaelrodriguezg 0:9b19dc46ec16 40 }
michaelrodriguezg 0:9b19dc46ec16 41 else if(distancia>17){
michaelrodriguezg 0:9b19dc46ec16 42 servo.pulsewidth_us(500);
michaelrodriguezg 0:9b19dc46ec16 43 }
michaelrodriguezg 0:9b19dc46ec16 44 Thread::wait(50);
michaelrodriguezg 0:9b19dc46ec16 45 }
michaelrodriguezg 0:9b19dc46ec16 46 }
michaelrodriguezg 0:9b19dc46ec16 47 void queue_isr(){
michaelrodriguezg 0:9b19dc46ec16 48 queue.put((uint32_t*)distancia);
michaelrodriguezg 0:9b19dc46ec16 49 }
michaelrodriguezg 0:9b19dc46ec16 50
michaelrodriguezg 0:9b19dc46ec16 51 int main (){
michaelrodriguezg 0:9b19dc46ec16 52 servo.period_ms(20);
michaelrodriguezg 0:9b19dc46ec16 53 thread_servo.start(task_servo);
michaelrodriguezg 0:9b19dc46ec16 54 Thread thread_servo(osPriorityNormal);
michaelrodriguezg 0:9b19dc46ec16 55 Ticker ticker;
michaelrodriguezg 0:9b19dc46ec16 56 ticker.attach(queue_isr,0.3);
michaelrodriguezg 0:9b19dc46ec16 57 while (true){
michaelrodriguezg 0:9b19dc46ec16 58 osEvent evt=queue.get();
michaelrodriguezg 0:9b19dc46ec16 59 if(evt.status != osEventMessage){
michaelrodriguezg 0:9b19dc46ec16 60 }
michaelrodriguezg 0:9b19dc46ec16 61 else {
michaelrodriguezg 0:9b19dc46ec16 62 printf("%d\n",evt.value.v);
michaelrodriguezg 0:9b19dc46ec16 63 }
michaelrodriguezg 0:9b19dc46ec16 64 }
michaelrodriguezg 0:9b19dc46ec16 65 }