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

Dependencies:   mbed mbed-rtos LSM9DS1_Library_cal Motor

Committer:
wschon
Date:
Sun Apr 24 21:08:30 2016 +0000
Revision:
6:a51f4ac6f42b
Parent:
4:18e9f16cc53c
Child:
7:cbccf5c5da6d
Added timer and IMU, made distance variables ints ranging from ~10 - ~70 cm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wschon 0:8e17f11689f2 1 #include "mbed.h"
wschon 2:fcf6f5901ee6 2 #include "rtos.h"
wschon 3:70f624c5fe26 3 #include "LSM9DS1.h"
wschon 3:70f624c5fe26 4 #define DECLINATION -4.94
wschon 3:70f624c5fe26 5 #define PI 3.14159
wschon 3:70f624c5fe26 6
wschon 6:a51f4ac6f42b 7 // Timer example: https://developer.mbed.org/handbook/Timer
wschon 2:fcf6f5901ee6 8 AnalogIn infraredR(p20); //Right infrared distance sensor
wschon 2:fcf6f5901ee6 9 AnalogIn infraredL(p19); //Left infrared distance sensor
wschon 2:fcf6f5901ee6 10 AnalogIn infraredF(p17); //Front infrared distance sensor
wschon 3:70f624c5fe26 11 Serial pc(USBTX, USBRX);
wschon 6:a51f4ac6f42b 12 Timer t;
wschon 6:a51f4ac6f42b 13
wschon 2:fcf6f5901ee6 14 float leftDist, rightDist, frontDist; //Distances from robot to obstacles
wschon 3:70f624c5fe26 15 float xaccel, yaccel, zaccel; //Acceleration in the x, y, and z directions
wschon 3:70f624c5fe26 16 float xmag, ymag, zmag, head; //Magnetic readings
wschon 6:a51f4ac6f42b 17 int ldist, rdist, fdist;
wschon 3:70f624c5fe26 18
wschon 3:70f624c5fe26 19 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
wschon 3:70f624c5fe26 20 {
wschon 3:70f624c5fe26 21 float roll = atan2(ay, az);
wschon 3:70f624c5fe26 22 float pitch = atan2(-ax, sqrt(ay * ay + az * az));
wschon 3:70f624c5fe26 23 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
wschon 3:70f624c5fe26 24 mx = -mx;
wschon 3:70f624c5fe26 25 float heading;
wschon 3:70f624c5fe26 26 if (my == 0.0)
wschon 3:70f624c5fe26 27 heading = (mx < 0.0) ? 180.0 : 0.0;
wschon 3:70f624c5fe26 28 else
wschon 3:70f624c5fe26 29 heading = atan2(mx, my)*360.0/(2.0*PI);
wschon 3:70f624c5fe26 30 //pc.printf("heading atan=%f \n\r",heading);
wschon 3:70f624c5fe26 31 heading -= DECLINATION; //correct for geo location
wschon 3:70f624c5fe26 32 if(heading>180.0) heading = heading - 360.0;
wschon 3:70f624c5fe26 33 else if(heading<-180.0) heading = 360.0 + heading;
wschon 3:70f624c5fe26 34 else if(heading<0.0) heading = 360.0 + heading;
wschon 3:70f624c5fe26 35
wschon 3:70f624c5fe26 36
wschon 3:70f624c5fe26 37 // Convert everything from radians to degrees:
wschon 3:70f624c5fe26 38 //heading *= 180.0 / PI;
wschon 3:70f624c5fe26 39 pitch *= 180.0 / PI;
wschon 3:70f624c5fe26 40 roll *= 180.0 / PI;
wschon 3:70f624c5fe26 41
wschon 3:70f624c5fe26 42 //pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll);
wschon 3:70f624c5fe26 43 //pc.printf("Magnetic Heading: %f degress\n\r",heading);
wschon 3:70f624c5fe26 44 head = heading;
wschon 3:70f624c5fe26 45 }
wschon 3:70f624c5fe26 46
wschon 3:70f624c5fe26 47 void IMU_thread(void const *args) {
wschon 3:70f624c5fe26 48 LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
wschon 6:a51f4ac6f42b 49 IMU.begin();
wschon 6:a51f4ac6f42b 50 if (!IMU.begin()) {
wschon 6:a51f4ac6f42b 51 pc.printf("Failed to communicate with LSM9DS1.\n");
wschon 6:a51f4ac6f42b 52 }
wschon 3:70f624c5fe26 53 IMU.calibrate(1);
wschon 3:70f624c5fe26 54 IMU.calibrateMag(0);
wschon 3:70f624c5fe26 55 while(1) {
wschon 3:70f624c5fe26 56 while(!IMU.accelAvailable());
wschon 3:70f624c5fe26 57 IMU.readAccel();
wschon 3:70f624c5fe26 58 while(!IMU.magAvailable(X_AXIS));
wschon 3:70f624c5fe26 59 IMU.readMag();
wschon 3:70f624c5fe26 60 xaccel = IMU.calcAccel(IMU.ax);
wschon 3:70f624c5fe26 61 yaccel = IMU.calcAccel(IMU.ay);
wschon 3:70f624c5fe26 62 zaccel = IMU.calcAccel(IMU.az);
wschon 6:a51f4ac6f42b 63 pc.printf("%f\r\n",zaccel);
wschon 3:70f624c5fe26 64 xmag = IMU.calcMag(IMU.mx);
wschon 3:70f624c5fe26 65 ymag = IMU.calcMag(IMU.my);
wschon 3:70f624c5fe26 66 zmag = IMU.calcMag(IMU.mz);
wschon 4:18e9f16cc53c 67 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));
wschon 3:70f624c5fe26 68 Thread::wait(500); //Wait 1/2 second
wschon 3:70f624c5fe26 69 }
wschon 3:70f624c5fe26 70 }
wschon 0:8e17f11689f2 71
wschon 2:fcf6f5901ee6 72 void left_thread(void const *args) {
wschon 2:fcf6f5901ee6 73 while (1) {
wschon 6:a51f4ac6f42b 74 leftDist = infraredL * 80.0f;
wschon 6:a51f4ac6f42b 75 leftDist = 80.0f - leftDist;
wschon 6:a51f4ac6f42b 76 ldist = (int)leftDist;
wschon 2:fcf6f5901ee6 77 Thread::wait(500); //wait 1/2 second before updating
wschon 6:a51f4ac6f42b 78 //pc.printf("Left distance %d\r\n", ldist);
wschon 2:fcf6f5901ee6 79 }
wschon 2:fcf6f5901ee6 80 }
wschon 2:fcf6f5901ee6 81
wschon 2:fcf6f5901ee6 82 void right_thread(void const *args) {
wschon 2:fcf6f5901ee6 83 while (1) {
wschon 6:a51f4ac6f42b 84 rightDist = infraredR * 80.0f;
wschon 6:a51f4ac6f42b 85 rightDist = 80.0f - rightDist;
wschon 6:a51f4ac6f42b 86 rdist = (int)rightDist;
wschon 6:a51f4ac6f42b 87 rdist = (int)rightDist;
wschon 2:fcf6f5901ee6 88 Thread::wait(500); //wait 1/2 second before updating
wschon 6:a51f4ac6f42b 89 //pc.printf("Right distance %d\r\n", rdist);
wschon 2:fcf6f5901ee6 90 }
wschon 2:fcf6f5901ee6 91 }
wschon 2:fcf6f5901ee6 92
wschon 2:fcf6f5901ee6 93 void front_thread(void const *args) {
wschon 2:fcf6f5901ee6 94 while (1) {
wschon 6:a51f4ac6f42b 95 frontDist = infraredF * 80.0f;
wschon 6:a51f4ac6f42b 96 frontDist = 80.0f - frontDist;
wschon 6:a51f4ac6f42b 97 fdist = (int)frontDist;
wschon 2:fcf6f5901ee6 98 Thread::wait(500); //wait 1/2 second before updating
wschon 6:a51f4ac6f42b 99 pc.printf("Front distance %d\r\n", fdist);
wschon 2:fcf6f5901ee6 100 }
wschon 2:fcf6f5901ee6 101 }
wschon 0:8e17f11689f2 102
wschon 0:8e17f11689f2 103 int main() {
swilkins8 1:27c32ba9fe93 104 //Test
wschon 6:a51f4ac6f42b 105 t.begin();
wschon 2:fcf6f5901ee6 106 Thread left(left_thread);
wschon 2:fcf6f5901ee6 107 Thread right(right_thread);
wschon 2:fcf6f5901ee6 108 Thread front(front_thread);
wschon 6:a51f4ac6f42b 109 Thread IMU(IMU_thread);
wschon 0:8e17f11689f2 110 while(1) {
wschon 2:fcf6f5901ee6 111
wschon 0:8e17f11689f2 112 }
wschon 0:8e17f11689f2 113 }