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

main.cpp

Committer:
nrrathi
Date:
2016-12-02
Revision:
0:2f02c1515311

File content as of revision 0:2f02c1515311:

#include "mbed.h"
#include "hcsr04.h"
#include "Servo.h"
#include "FXOS8700Q.h"
#include "math.h"
#define M_PI 3.1415926535

Serial pc(USBTX, USBRX);
Serial device (PTC15,PTC14);
HCSR04  usensor(A4,A5);
I2C i2c(PTE25, PTE24);
FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
DigitalOut RED (LED1);
DigitalOut GREEN (LED2);
DigitalOut BLUE (LED3);
int distance;              //Variable to store distance from an object
int state = 0;
double pitch, roll;
const float alpha = 0.5;
double fxg = 0;
double fyg = 0;
double fzg = 0;
motion_data_counts_t acc_raw;
    
void filter_accelerometer()
{
    double xg, yg, zg;
    
    acc.getAxis(acc_raw);
    
    xg = acc_raw.x;
    yg = acc_raw.y;
    zg = acc_raw.z;
    
        // low pass filter
    fxg = xg * alpha + (fxg * (1.0 - alpha));
    fyg = yg * alpha + (fyg * (1.0 - alpha));
    fzg = zg * alpha + (fzg * (1.0 - alpha));

        // pitch and roll calculations
    roll  = (atan2(-fyg, fzg)*180.0)/M_PI;
    pitch = (atan2(fxg, sqrt(fyg*fyg + fzg*fzg))*180.0)/M_PI;
    printf(" %0.2f %0.2f \r\n", roll, pitch);
    }

int main()
{  
acc.enable();
    GREEN = 1;
    BLUE = 1;
    RED = 1;
    while(1) 
    {
        usensor.start();
        wait_ms(0.0025); 
        distance = usensor.get_dist_cm();
        wait(0.5);
        pc.printf(" distance = %d\r\n", distance);
        if(distance <= 9 )
        {
            device.printf("ALERT VEHICLE");  
        }
       filter_accelerometer();
       if (roll > 22.00 && pitch > 3.00 )
       {
         GREEN = BLUE = 1;
         RED = 0;
         
         wait(0.5);
         RED = 1;
       }
        if(device.readable() > 0) 
        {
            state = device.getc();
            pc.printf("Instruction: %i\n\r", state);    
        } 
        if (state == '1')
        {
            RED = BLUE = 1;
            GREEN = 0;
            wait(0.3);
            GREEN = 1;
            wait(0.3);
        }
        else if (state == '2') 
        {
            RED = GREEN = 1;
            BLUE = 0;
            wait(0.3);
            BLUE = 1;
            wait (0.3);
        }
        else if (state == '3') {
            RED = GREEN = 1;
            BLUE = 1;
            
        }
        
    }
}