The system detects the distance of the bike from the traffic behind. If the traffic behind approaches too close the bike, system generates an alert signal that alerts the cyclist for traffic behind. Moreover, with the help of an app, a cyclist can track the location from where he/she started the journey so that he/she can trace the location back. In addition this android app also helps a biker to flash ‘turn left’ or ‘turn right’ using buttons on a mobile app and generate ‘stop’ signal automatically as the cyclist applies break.

Dependencies:   FXOS8700Q HCSR-04 Servo mbed

Committer:
nrrathi
Date:
Fri Dec 02 20:34:22 2016 +0000
Revision:
0:2f02c1515311
An embedded system that assists a cyclist on traffic conditions, positioning capabilities and wirelessly send traffic signals using LEDs. The system detects the distance of the bike from the traffic behind.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nrrathi 0:2f02c1515311 1 #include "mbed.h"
nrrathi 0:2f02c1515311 2 #include "hcsr04.h"
nrrathi 0:2f02c1515311 3 #include "Servo.h"
nrrathi 0:2f02c1515311 4 #include "FXOS8700Q.h"
nrrathi 0:2f02c1515311 5 #include "math.h"
nrrathi 0:2f02c1515311 6 #define M_PI 3.1415926535
nrrathi 0:2f02c1515311 7
nrrathi 0:2f02c1515311 8 Serial pc(USBTX, USBRX);
nrrathi 0:2f02c1515311 9 Serial device (PTC15,PTC14);
nrrathi 0:2f02c1515311 10 HCSR04 usensor(A4,A5);
nrrathi 0:2f02c1515311 11 I2C i2c(PTE25, PTE24);
nrrathi 0:2f02c1515311 12 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
nrrathi 0:2f02c1515311 13 DigitalOut RED (LED1);
nrrathi 0:2f02c1515311 14 DigitalOut GREEN (LED2);
nrrathi 0:2f02c1515311 15 DigitalOut BLUE (LED3);
nrrathi 0:2f02c1515311 16 int distance; //Variable to store distance from an object
nrrathi 0:2f02c1515311 17 int state = 0;
nrrathi 0:2f02c1515311 18 double pitch, roll;
nrrathi 0:2f02c1515311 19 const float alpha = 0.5;
nrrathi 0:2f02c1515311 20 double fxg = 0;
nrrathi 0:2f02c1515311 21 double fyg = 0;
nrrathi 0:2f02c1515311 22 double fzg = 0;
nrrathi 0:2f02c1515311 23 motion_data_counts_t acc_raw;
nrrathi 0:2f02c1515311 24
nrrathi 0:2f02c1515311 25 void filter_accelerometer()
nrrathi 0:2f02c1515311 26 {
nrrathi 0:2f02c1515311 27 double xg, yg, zg;
nrrathi 0:2f02c1515311 28
nrrathi 0:2f02c1515311 29 acc.getAxis(acc_raw);
nrrathi 0:2f02c1515311 30
nrrathi 0:2f02c1515311 31 xg = acc_raw.x;
nrrathi 0:2f02c1515311 32 yg = acc_raw.y;
nrrathi 0:2f02c1515311 33 zg = acc_raw.z;
nrrathi 0:2f02c1515311 34
nrrathi 0:2f02c1515311 35 // low pass filter
nrrathi 0:2f02c1515311 36 fxg = xg * alpha + (fxg * (1.0 - alpha));
nrrathi 0:2f02c1515311 37 fyg = yg * alpha + (fyg * (1.0 - alpha));
nrrathi 0:2f02c1515311 38 fzg = zg * alpha + (fzg * (1.0 - alpha));
nrrathi 0:2f02c1515311 39
nrrathi 0:2f02c1515311 40 // pitch and roll calculations
nrrathi 0:2f02c1515311 41 roll = (atan2(-fyg, fzg)*180.0)/M_PI;
nrrathi 0:2f02c1515311 42 pitch = (atan2(fxg, sqrt(fyg*fyg + fzg*fzg))*180.0)/M_PI;
nrrathi 0:2f02c1515311 43 printf(" %0.2f %0.2f \r\n", roll, pitch);
nrrathi 0:2f02c1515311 44 }
nrrathi 0:2f02c1515311 45
nrrathi 0:2f02c1515311 46 int main()
nrrathi 0:2f02c1515311 47 {
nrrathi 0:2f02c1515311 48 acc.enable();
nrrathi 0:2f02c1515311 49 GREEN = 1;
nrrathi 0:2f02c1515311 50 BLUE = 1;
nrrathi 0:2f02c1515311 51 RED = 1;
nrrathi 0:2f02c1515311 52 while(1)
nrrathi 0:2f02c1515311 53 {
nrrathi 0:2f02c1515311 54 usensor.start();
nrrathi 0:2f02c1515311 55 wait_ms(0.0025);
nrrathi 0:2f02c1515311 56 distance = usensor.get_dist_cm();
nrrathi 0:2f02c1515311 57 wait(0.5);
nrrathi 0:2f02c1515311 58 pc.printf(" distance = %d\r\n", distance);
nrrathi 0:2f02c1515311 59 if(distance <= 9 )
nrrathi 0:2f02c1515311 60 {
nrrathi 0:2f02c1515311 61 device.printf("ALERT VEHICLE");
nrrathi 0:2f02c1515311 62 }
nrrathi 0:2f02c1515311 63 filter_accelerometer();
nrrathi 0:2f02c1515311 64 if (roll > 22.00 && pitch > 3.00 )
nrrathi 0:2f02c1515311 65 {
nrrathi 0:2f02c1515311 66 GREEN = BLUE = 1;
nrrathi 0:2f02c1515311 67 RED = 0;
nrrathi 0:2f02c1515311 68
nrrathi 0:2f02c1515311 69 wait(0.5);
nrrathi 0:2f02c1515311 70 RED = 1;
nrrathi 0:2f02c1515311 71 }
nrrathi 0:2f02c1515311 72 if(device.readable() > 0)
nrrathi 0:2f02c1515311 73 {
nrrathi 0:2f02c1515311 74 state = device.getc();
nrrathi 0:2f02c1515311 75 pc.printf("Instruction: %i\n\r", state);
nrrathi 0:2f02c1515311 76 }
nrrathi 0:2f02c1515311 77 if (state == '1')
nrrathi 0:2f02c1515311 78 {
nrrathi 0:2f02c1515311 79 RED = BLUE = 1;
nrrathi 0:2f02c1515311 80 GREEN = 0;
nrrathi 0:2f02c1515311 81 wait(0.3);
nrrathi 0:2f02c1515311 82 GREEN = 1;
nrrathi 0:2f02c1515311 83 wait(0.3);
nrrathi 0:2f02c1515311 84 }
nrrathi 0:2f02c1515311 85 else if (state == '2')
nrrathi 0:2f02c1515311 86 {
nrrathi 0:2f02c1515311 87 RED = GREEN = 1;
nrrathi 0:2f02c1515311 88 BLUE = 0;
nrrathi 0:2f02c1515311 89 wait(0.3);
nrrathi 0:2f02c1515311 90 BLUE = 1;
nrrathi 0:2f02c1515311 91 wait (0.3);
nrrathi 0:2f02c1515311 92 }
nrrathi 0:2f02c1515311 93 else if (state == '3') {
nrrathi 0:2f02c1515311 94 RED = GREEN = 1;
nrrathi 0:2f02c1515311 95 BLUE = 1;
nrrathi 0:2f02c1515311 96
nrrathi 0:2f02c1515311 97 }
nrrathi 0:2f02c1515311 98
nrrathi 0:2f02c1515311 99 }
nrrathi 0:2f02c1515311 100 }