dad
Dependencies: mbed-os FXAS21000 LED_Bar FXOS8700Q
main.cpp@3:9ab65e6d4cd4, 2022-04-26 (annotated)
- Committer:
- maeconn
- Date:
- Tue Apr 26 23:07:20 2022 +0000
- Revision:
- 3:9ab65e6d4cd4
- Parent:
- 2:d08625231a9f
- Child:
- 4:91893b37450e
FinalProject
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
priyank12p | 0:838685d68d89 | 1 | #include "mbed.h" |
priyank12p | 0:838685d68d89 | 2 | #include "FXOS8700Q.h" |
priyank12p | 0:838685d68d89 | 3 | #include "FXAS21000.h" |
mukundy8 | 2:d08625231a9f | 4 | #include "hcsr04.h" |
maeconn | 3:9ab65e6d4cd4 | 5 | #include "LED_Bar.h" |
priyank12p | 0:838685d68d89 | 6 | I2C i2c(PTE25, PTE24); |
priyank12p | 0:838685d68d89 | 7 | Serial pc(USBTX,USBRX); |
priyank12p | 0:838685d68d89 | 8 | FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1); // Configured for the FRDM-K64F with onboard sensors |
priyank12p | 0:838685d68d89 | 9 | FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1); |
priyank12p | 0:838685d68d89 | 10 | FXAS21000 gyro(D14,D15); |
mukundy8 | 2:d08625231a9f | 11 | HCSR04 usensor1(D8,D9); //ECHO Pin=D9, TRIG Pin=D8 |
mukundy8 | 2:d08625231a9f | 12 | HCSR04 usensor2(D7,D6); //ECHO Pin=D7, TRIG Pin=D6 |
maeconn | 3:9ab65e6d4cd4 | 13 | LED_Bar bar(D5,D4); |
maeconn | 3:9ab65e6d4cd4 | 14 | DigitalOut buzzer(D2); |
maeconn | 3:9ab65e6d4cd4 | 15 | int buzz_on=1, buzz_off=0; |
mukundy8 | 2:d08625231a9f | 16 | |
priyank12p | 0:838685d68d89 | 17 | int main(void) |
priyank12p | 0:838685d68d89 | 18 | { |
mukundy8 | 2:d08625231a9f | 19 | int num = 0; |
mukundy8 | 2:d08625231a9f | 20 | int distance1, distance2; |
maeconn | 3:9ab65e6d4cd4 | 21 | int led_speed; |
mukundy8 | 2:d08625231a9f | 22 | float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2; |
mukundy8 | 2:d08625231a9f | 23 | char snd[255],rcv[1000]; //snd: send command to ESP8266 |
mukundy8 | 2:d08625231a9f | 24 | //rcv: receive response from ESP8266 |
mukundy8 | 2:d08625231a9f | 25 | |
mukundy8 | 2:d08625231a9f | 26 | //Ultrasound Sensor (HC-SR04) #1 Initialization |
mukundy8 | 2:d08625231a9f | 27 | int a = 30; |
priyank12p | 0:838685d68d89 | 28 | float gyro_data[3]; |
priyank12p | 0:838685d68d89 | 29 | motion_data_units_t acc_data, mag_data; |
priyank12p | 0:838685d68d89 | 30 | motion_data_counts_t acc_raw, mag_raw; |
priyank12p | 0:838685d68d89 | 31 | float faX, faY, faZ, fmX, fmY, fmZ, tmp_float; |
priyank12p | 0:838685d68d89 | 32 | int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int; |
priyank12p | 0:838685d68d89 | 33 | acc.enable(); |
priyank12p | 0:838685d68d89 | 34 | mag.enable(); |
maeconn | 3:9ab65e6d4cd4 | 35 | //DigitalOut Red(LED1); |
maeconn | 3:9ab65e6d4cd4 | 36 | // Red=1; |
maeconn | 3:9ab65e6d4cd4 | 37 | // DigitalOut Blue(LED3); |
maeconn | 3:9ab65e6d4cd4 | 38 | // DigitalOut Green(LED2); |
maeconn | 3:9ab65e6d4cd4 | 39 | // Blue=1; |
maeconn | 3:9ab65e6d4cd4 | 40 | // Green=1; |
mukundy8 | 1:7a49fd0692fd | 41 | |
priyank12p | 0:838685d68d89 | 42 | while (true) { |
priyank12p | 0:838685d68d89 | 43 | // counts based results |
priyank12p | 0:838685d68d89 | 44 | acc.getAxis(acc_raw); |
priyank12p | 0:838685d68d89 | 45 | mag.getAxis(mag_raw); |
priyank12p | 0:838685d68d89 | 46 | acc.getX(raX); |
priyank12p | 0:838685d68d89 | 47 | acc.getY(raY); |
priyank12p | 0:838685d68d89 | 48 | acc.getZ(raZ); |
priyank12p | 0:838685d68d89 | 49 | mag.getX(rmX); |
priyank12p | 0:838685d68d89 | 50 | mag.getY(rmY); |
priyank12p | 0:838685d68d89 | 51 | mag.getZ(rmZ); |
priyank12p | 0:838685d68d89 | 52 | // unit based results |
priyank12p | 0:838685d68d89 | 53 | acc.getAxis(acc_data); |
priyank12p | 0:838685d68d89 | 54 | mag.getAxis(mag_data); |
priyank12p | 0:838685d68d89 | 55 | acc.getX(faX); |
priyank12p | 0:838685d68d89 | 56 | acc.getY(faY); |
priyank12p | 0:838685d68d89 | 57 | acc.getZ(faZ); |
priyank12p | 0:838685d68d89 | 58 | mag.getX(fmX); |
priyank12p | 0:838685d68d89 | 59 | mag.getY(fmY); |
priyank12p | 0:838685d68d89 | 60 | mag.getZ(fmZ); |
mukundy8 | 1:7a49fd0692fd | 61 | pc.printf("FXOSQ8700Q ACC: X=%1.4f Y=%1.4f Z=%1.4f", acc.getX(faX),acc.getY(faY),acc.getZ(faZ)); |
mukundy8 | 1:7a49fd0692fd | 62 | pc.printf(" MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", mag.getX(fmX),mag.getY(fmY), mag.getZ(fmZ)); |
priyank12p | 0:838685d68d89 | 63 | gyro.ReadXYZ(gyro_data); |
mukundy8 | 1:7a49fd0692fd | 64 | pc.printf("FXAS21000 Gyro: X=%6.2f Y=%6.2f Z=%6.2f\r\n", gyro_data[0],gyro_data[1], gyro_data[2]); |
mukundy8 | 1:7a49fd0692fd | 65 | |
maeconn | 3:9ab65e6d4cd4 | 66 | |
maeconn | 3:9ab65e6d4cd4 | 67 | usensor1.start(); |
maeconn | 3:9ab65e6d4cd4 | 68 | wait_ms(500); |
maeconn | 3:9ab65e6d4cd4 | 69 | |
maeconn | 3:9ab65e6d4cd4 | 70 | //Calculating Distance Percentage Remaining for Sensor # 1 |
maeconn | 3:9ab65e6d4cd4 | 71 | distance1 = usensor1.get_dist_cm(); |
maeconn | 3:9ab65e6d4cd4 | 72 | dist_remaining1 = a-distance1; |
maeconn | 3:9ab65e6d4cd4 | 73 | dist_percent1 = (dist_remaining1/30)*100; |
maeconn | 3:9ab65e6d4cd4 | 74 | |
maeconn | 3:9ab65e6d4cd4 | 75 | ////LED and Tera Term Output |
maeconn | 3:9ab65e6d4cd4 | 76 | // if (distance1<30 && distance2<30) { |
maeconn | 3:9ab65e6d4cd4 | 77 | // |
maeconn | 3:9ab65e6d4cd4 | 78 | // pc.printf("You are colse!\n\r"); while(1) { |
maeconn | 3:9ab65e6d4cd4 | 79 | // pc.putc(pc.getc() + 1);} |
maeconn | 3:9ab65e6d4cd4 | 80 | // //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2); |
maeconn | 3:9ab65e6d4cd4 | 81 | // } else { |
maeconn | 3:9ab65e6d4cd4 | 82 | // //GLed = 1; |
maeconn | 3:9ab65e6d4cd4 | 83 | //// BLed = 1; |
maeconn | 3:9ab65e6d4cd4 | 84 | //// RLed = 0; |
maeconn | 3:9ab65e6d4cd4 | 85 | // pc.printf("You are far!\n\r"); while(1) { |
maeconn | 3:9ab65e6d4cd4 | 86 | // pc.putc(pc.getc() + 1);} |
maeconn | 3:9ab65e6d4cd4 | 87 | // } |
mukundy8 | 1:7a49fd0692fd | 88 | |
maeconn | 3:9ab65e6d4cd4 | 89 | if((abs(acc.getX(faX)) > 1.5 || abs(acc.getZ(faZ)) > 1.3 || abs(acc.getY(faY)) > 0.1) || (distance1<30 && distance2<30)) |
mukundy8 | 1:7a49fd0692fd | 90 | { |
mukundy8 | 1:7a49fd0692fd | 91 | |
mukundy8 | 2:d08625231a9f | 92 | //Red = 0; |
mukundy8 | 2:d08625231a9f | 93 | // Blue = 1; |
mukundy8 | 2:d08625231a9f | 94 | // Green = 1; |
maeconn | 3:9ab65e6d4cd4 | 95 | |
maeconn | 3:9ab65e6d4cd4 | 96 | // pc.printf("Going too fast!\n\r"); //while(1) { |
maeconn | 3:9ab65e6d4cd4 | 97 | // pc.putc(pc.getc() + 1);} |
maeconn | 3:9ab65e6d4cd4 | 98 | |
maeconn | 3:9ab65e6d4cd4 | 99 | buzzer=buzz_on; |
maeconn | 3:9ab65e6d4cd4 | 100 | wait(0.5); |
maeconn | 3:9ab65e6d4cd4 | 101 | buzzer=buzz_off; |
maeconn | 3:9ab65e6d4cd4 | 102 | wait(0.5); |
maeconn | 3:9ab65e6d4cd4 | 103 | |
maeconn | 3:9ab65e6d4cd4 | 104 | for(led_speed=0; led_speed<=10; led_speed++){ |
maeconn | 3:9ab65e6d4cd4 | 105 | bar.setLevel(led_speed); |
maeconn | 3:9ab65e6d4cd4 | 106 | wait(0.001); |
maeconn | 3:9ab65e6d4cd4 | 107 | } |
mukundy8 | 1:7a49fd0692fd | 108 | |
mukundy8 | 1:7a49fd0692fd | 109 | } |
mukundy8 | 1:7a49fd0692fd | 110 | |
mukundy8 | 1:7a49fd0692fd | 111 | |
mukundy8 | 1:7a49fd0692fd | 112 | else{ |
mukundy8 | 1:7a49fd0692fd | 113 | |
mukundy8 | 2:d08625231a9f | 114 | //Red = 1; |
mukundy8 | 2:d08625231a9f | 115 | // Green = 0; |
mukundy8 | 2:d08625231a9f | 116 | // Blue = 1; |
maeconn | 3:9ab65e6d4cd4 | 117 | |
maeconn | 3:9ab65e6d4cd4 | 118 | // pc.printf("You are safe!\n\r"); while(1) { |
maeconn | 3:9ab65e6d4cd4 | 119 | // pc.putc(pc.getc() + 1);} |
maeconn | 3:9ab65e6d4cd4 | 120 | |
maeconn | 3:9ab65e6d4cd4 | 121 | for(led_speed=0; led_speed<=10; led_speed++){ |
maeconn | 3:9ab65e6d4cd4 | 122 | bar.setLevel(led_speed); |
maeconn | 3:9ab65e6d4cd4 | 123 | wait(0.2); |
maeconn | 3:9ab65e6d4cd4 | 124 | } |
maeconn | 3:9ab65e6d4cd4 | 125 | |
mukundy8 | 1:7a49fd0692fd | 126 | }; |
priyank12p | 0:838685d68d89 | 127 | wait(1.0f); |
priyank12p | 0:838685d68d89 | 128 | } |
priyank12p | 0:838685d68d89 | 129 | } |