Robot using IMU and IR sensor data to drive autonomously and create a map

Dependencies:   mbed mbed-rtos LSM9DS1_Library_cal Motor

main.cpp

Committer:
wschon
Date:
2016-04-28
Revision:
7:cbccf5c5da6d
Parent:
6:a51f4ac6f42b
Child:
8:198568d5746b

File content as of revision 7:cbccf5c5da6d:

#include "mbed.h"
#include "rtos.h"
#include "LSM9DS1.h"
#define DECLINATION -4.94
#define PI 3.14159

// Timer example: https://developer.mbed.org/handbook/Timer
AnalogIn infraredR(p20); //Right infrared distance sensor
AnalogIn infraredL(p19); //Left infrared distance sensor
AnalogIn infraredF(p17); //Front infrared distance sensor
Serial pc(USBTX, USBRX);
Timer t;
char str[40];
    
float leftDist, rightDist, frontDist; //Distances from robot to obstacles
float xaccel, yaccel, zaccel;   //Acceleration in the x, y, and z directions
float xmag, ymag, zmag, head;         //Magnetic readings
int ldist, rdist, fdist;

//dual H bridge Polulu
PwmOut PWMA(p21);           //connect p21 to PWMA
PwmOut PWMB(p22);           //connect p22 to PWMB
DigitalOut Ain1(p23);       //connect p23 to Ain1   
DigitalOut Ain2(p24);       //connect p24 to Ain2




void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
{
    float roll = atan2(ay, az);
    float pitch = atan2(-ax, sqrt(ay * ay + az * az));
// touchy trig stuff to use arctan to get compass heading (scale is 0..360)
    mx = -mx;
    float heading;
    if (my == 0.0)
        heading = (mx < 0.0) ? 180.0 : 0.0;
    else
        heading = atan2(mx, my)*360.0/(2.0*PI);
    //pc.printf("heading atan=%f \n\r",heading);
    heading -= DECLINATION; //correct for geo location
    if(heading>180.0) heading = heading - 360.0;
    else if(heading<-180.0) heading = 360.0 + heading;
    else if(heading<0.0) heading = 360.0  + heading;


    // Convert everything from radians to degrees:
    //heading *= 180.0 / PI;
    pitch *= 180.0 / PI;
    roll  *= 180.0 / PI;

    //pc.printf("Pitch: %f,    Roll: %f degress\n\r",pitch,roll);
    //pc.printf("Magnetic Heading: %f degress\n\r",heading);
    head = heading;
}

void IMU_thread(void const *args) {
    LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
    IMU.begin();
    if (!IMU.begin()) {
        pc.printf("Failed to communicate with LSM9DS1.\n");
    }
    IMU.calibrate(1);
    IMU.calibrateMag(0);
    while(1) {
        while(!IMU.accelAvailable());
        IMU.readAccel();
        while(!IMU.magAvailable(X_AXIS));
        IMU.readMag();
        xaccel = IMU.calcAccel(IMU.ax);
        yaccel = IMU.calcAccel(IMU.ay);
        zaccel = IMU.calcAccel(IMU.az);
        pc.printf("%f\r\n",zaccel);
        xmag = IMU.calcMag(IMU.mx);
        ymag = IMU.calcMag(IMU.my);
        zmag = IMU.calcMag(IMU.mz);
        printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz));
        Thread::wait(500); //Wait 1/2 second
    }    
}

void left_thread(void const *args) {
    while (1) {
        leftDist = infraredL * 80.0f;  
        leftDist = 80.0f - leftDist;
        ldist = (int)leftDist;
        Thread::wait(500);     //wait 1/2 second before updating
        //pc.printf("Left distance %d\r\n", ldist);
        }
}

void right_thread(void const *args) {
    while (1) {
        rightDist = infraredR * 80.0f;
        rightDist = 80.0f - rightDist;
        rdist = (int)rightDist;
        rdist = (int)rightDist;
        Thread::wait(500);     //wait 1/2 second before updating
        //pc.printf("Right distance %d\r\n", rdist);
        }
}

void front_thread(void const *args) {
    while (1) {
        frontDist = infraredF * 80.0f;
        frontDist = 80.0f - frontDist;
        fdist = (int)frontDist;
        Thread::wait(500);     //wait 1/2 second before updating
        pc.printf("Front distance %d\r\n", fdist);
        }
}    

void send_thread(void const *args) {
    while (1) {
        sprintf (str, "%0.6f,%0.6f,%0.6f,%d,%d,%d,%d\n", xaccel, zaccel, ymag, fdist, ldist, rdist, t.read_us());   //buffer data to be sent to computer
        Thread::wait(500);
    }
}        


int main() {
    //Test
    t.start();
    Thread left(left_thread);
    Thread right(right_thread);
    Thread front(front_thread);
    Thread IMU(IMU_thread);
    Thread send(send_thread);
    Ain1=Ain2=1;                //enabling PWM
    while(1) {
        PWMA.write(0.5f);       //writing 50% duty cycle
        PWMB.write(0.5f);       //to pwm A and B
    }
}