
radar
main.cpp@2:fdb856775f71, 2020-12-03 (annotated)
- Committer:
- DAVIID222
- Date:
- Thu Dec 03 13:19:37 2020 +0000
- Revision:
- 2:fdb856775f71
- Parent:
- 1:5b8b3a6e9f8c
hola
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DAVIID222 | 0:24034ea3555d | 1 | #include "mbed.h" |
DAVIID222 | 0:24034ea3555d | 2 | #include "rtos.h" |
DAVIID222 | 0:24034ea3555d | 3 | #include "Servo.h" |
DAVIID222 | 0:24034ea3555d | 4 | |
intersect20 | 1:5b8b3a6e9f8c | 5 | Serial pc(USBTX, USBRX); |
DAVIID222 | 0:24034ea3555d | 6 | DigitalOut trig(D3); |
DAVIID222 | 0:24034ea3555d | 7 | DigitalIn echo(D2); |
DAVIID222 | 0:24034ea3555d | 8 | Servo myservo(D4); |
DAVIID222 | 0:24034ea3555d | 9 | Thread hilo_tarea1; |
DAVIID222 | 0:24034ea3555d | 10 | Thread hilo_tarea2; |
DAVIID222 | 0:24034ea3555d | 11 | Timer senal; |
DAVIID222 | 0:24034ea3555d | 12 | float distancia; |
intersect20 | 1:5b8b3a6e9f8c | 13 | float angle; |
DAVIID222 | 0:24034ea3555d | 14 | void tarea1(void); |
DAVIID222 | 0:24034ea3555d | 15 | void tarea2(void); |
DAVIID222 | 0:24034ea3555d | 16 | |
DAVIID222 | 0:24034ea3555d | 17 | int main() |
DAVIID222 | 0:24034ea3555d | 18 | { |
intersect20 | 1:5b8b3a6e9f8c | 19 | |
DAVIID222 | 0:24034ea3555d | 20 | hilo_tarea1.start(tarea1); |
DAVIID222 | 0:24034ea3555d | 21 | hilo_tarea2.start(tarea2); |
DAVIID222 | 0:24034ea3555d | 22 | hilo_tarea1.set_priority(osPriorityNormal); |
DAVIID222 | 0:24034ea3555d | 23 | while (true) { |
intersect20 | 1:5b8b3a6e9f8c | 24 | |
DAVIID222 | 0:24034ea3555d | 25 | } |
DAVIID222 | 0:24034ea3555d | 26 | } |
DAVIID222 | 0:24034ea3555d | 27 | |
intersect20 | 1:5b8b3a6e9f8c | 28 | void tarea1(void) |
intersect20 | 1:5b8b3a6e9f8c | 29 | { |
intersect20 | 1:5b8b3a6e9f8c | 30 | while(true) { |
DAVIID222 | 0:24034ea3555d | 31 | senal.reset(); |
DAVIID222 | 0:24034ea3555d | 32 | trig = 0; |
DAVIID222 | 0:24034ea3555d | 33 | wait_us(2); |
DAVIID222 | 0:24034ea3555d | 34 | trig = 1; |
DAVIID222 | 0:24034ea3555d | 35 | wait_us(10); |
DAVIID222 | 0:24034ea3555d | 36 | trig = 0; |
DAVIID222 | 0:24034ea3555d | 37 | while(echo==0); |
DAVIID222 | 0:24034ea3555d | 38 | senal.start(); |
DAVIID222 | 0:24034ea3555d | 39 | while(echo==1); |
DAVIID222 | 0:24034ea3555d | 40 | senal.stop(); |
DAVIID222 | 0:24034ea3555d | 41 | distancia = (senal.read_us())/58; |
DAVIID222 | 2:fdb856775f71 | 42 | pc.printf("' ',%f,%f,\r\n' '",angle,distancia); |
DAVIID222 | 0:24034ea3555d | 43 | ThisThread::sleep_for(50); |
DAVIID222 | 0:24034ea3555d | 44 | } |
DAVIID222 | 0:24034ea3555d | 45 | } |
DAVIID222 | 0:24034ea3555d | 46 | |
intersect20 | 1:5b8b3a6e9f8c | 47 | void tarea2(void) |
intersect20 | 1:5b8b3a6e9f8c | 48 | { |
intersect20 | 1:5b8b3a6e9f8c | 49 | float range = 0.0009; |
intersect20 | 1:5b8b3a6e9f8c | 50 | bool direction = true; |
intersect20 | 1:5b8b3a6e9f8c | 51 | float position = 0.0; |
intersect20 | 1:5b8b3a6e9f8c | 52 | myservo.calibrate(range, 45.0); |
intersect20 | 1:5b8b3a6e9f8c | 53 | while(true) { |
intersect20 | 1:5b8b3a6e9f8c | 54 | if(direction) { |
intersect20 | 1:5b8b3a6e9f8c | 55 | for (int i=-90; i< 90; i++) { |
intersect20 | 1:5b8b3a6e9f8c | 56 | angle= i+90; |
intersect20 | 1:5b8b3a6e9f8c | 57 | myservo.position(i); |
intersect20 | 1:5b8b3a6e9f8c | 58 | wait_ms(17); |
intersect20 | 1:5b8b3a6e9f8c | 59 | } |
intersect20 | 1:5b8b3a6e9f8c | 60 | direction=false; |
intersect20 | 1:5b8b3a6e9f8c | 61 | } else { |
intersect20 | 1:5b8b3a6e9f8c | 62 | for (int i=90; i> -90; i--) { |
intersect20 | 1:5b8b3a6e9f8c | 63 | angle = i+90; |
intersect20 | 1:5b8b3a6e9f8c | 64 | myservo.position(i); |
intersect20 | 1:5b8b3a6e9f8c | 65 | wait_ms(17); |
intersect20 | 1:5b8b3a6e9f8c | 66 | } |
intersect20 | 1:5b8b3a6e9f8c | 67 | direction=true; |
intersect20 | 1:5b8b3a6e9f8c | 68 | |
DAVIID222 | 0:24034ea3555d | 69 | } |
DAVIID222 | 0:24034ea3555d | 70 | } |
DAVIID222 | 0:24034ea3555d | 71 | } |