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:
Mon May 02 04:33:43 2016 +0000
Revision:
9:36807f7e58fb
Parent:
8:198568d5746b
Child:
11:805ef606a29d
f

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 8:198568d5746b 6 #include "Motor.h"
wschon 8:198568d5746b 7
wschon 3:70f624c5fe26 8
wschon 6:a51f4ac6f42b 9 // Timer example: https://developer.mbed.org/handbook/Timer
wschon 2:fcf6f5901ee6 10 AnalogIn infraredR(p20); //Right infrared distance sensor
wschon 2:fcf6f5901ee6 11 AnalogIn infraredL(p19); //Left infrared distance sensor
wschon 2:fcf6f5901ee6 12 AnalogIn infraredF(p17); //Front infrared distance sensor
wschon 3:70f624c5fe26 13 Serial pc(USBTX, USBRX);
wschon 6:a51f4ac6f42b 14 Timer t;
wschon 9:36807f7e58fb 15 char str[60]; //buffer for xbee messages
wschon 8:198568d5746b 16 float maxspeed = 0.4;
wschon 8:198568d5746b 17 float minspeed = -0.4;
wschon 8:198568d5746b 18 float leftDist, rightDist, frontDist; //Distances from robot to obstacles
wschon 8:198568d5746b 19 float xaccel, yaccel, zaccel; //Acceleration in the x, y, and z directions
wschon 8:198568d5746b 20 float xmag, ymag, zmag, head; //Magnetic readings
wschon 8:198568d5746b 21 int ldist, rdist, fdist;
wschon 8:198568d5746b 22 Serial xbee(p28,p27); //xbee over serial
wschon 9:36807f7e58fb 23 Mutex stdio_mutex;
wschon 9:36807f7e58fb 24 float heading;
wschon 7:cbccf5c5da6d 25 //dual H bridge Polulu
wschon 8:198568d5746b 26 Motor MotorRi(p21, p23, p24);
wschon 8:198568d5746b 27 Motor MotorLe(p22, p26, p25);
wschon 9:36807f7e58fb 28 float ti = 0, timelast = 0, deltat;
wschon 7:cbccf5c5da6d 29
wschon 9:36807f7e58fb 30 float imu[] = {0, 0, 0};
wschon 9:36807f7e58fb 31 float velX[] = {0, 0, 0};
wschon 9:36807f7e58fb 32 float velY[] = {0, 0, 0};
wschon 9:36807f7e58fb 33 float posX[] = {0, 0, 0};
wschon 9:36807f7e58fb 34 float posY[] = {0, 0, 0};
wschon 9:36807f7e58fb 35 float compass[] = {0, 0, 0};
wschon 7:cbccf5c5da6d 36
wschon 3:70f624c5fe26 37 void printAttitude(float ax, float ay, float az, float mx, float my, float mz)
wschon 3:70f624c5fe26 38 {
wschon 3:70f624c5fe26 39 float roll = atan2(ay, az);
wschon 3:70f624c5fe26 40 float pitch = atan2(-ax, sqrt(ay * ay + az * az));
wschon 3:70f624c5fe26 41 // touchy trig stuff to use arctan to get compass heading (scale is 0..360)
wschon 3:70f624c5fe26 42 mx = -mx;
wschon 3:70f624c5fe26 43 if (my == 0.0)
wschon 3:70f624c5fe26 44 heading = (mx < 0.0) ? 180.0 : 0.0;
wschon 3:70f624c5fe26 45 else
wschon 3:70f624c5fe26 46 heading = atan2(mx, my)*360.0/(2.0*PI);
wschon 3:70f624c5fe26 47 //pc.printf("heading atan=%f \n\r",heading);
wschon 3:70f624c5fe26 48 heading -= DECLINATION; //correct for geo location
wschon 3:70f624c5fe26 49 if(heading>180.0) heading = heading - 360.0;
wschon 3:70f624c5fe26 50 else if(heading<-180.0) heading = 360.0 + heading;
wschon 3:70f624c5fe26 51 else if(heading<0.0) heading = 360.0 + heading;
wschon 3:70f624c5fe26 52
wschon 3:70f624c5fe26 53
wschon 3:70f624c5fe26 54 // Convert everything from radians to degrees:
wschon 3:70f624c5fe26 55 //heading *= 180.0 / PI;
wschon 3:70f624c5fe26 56 pitch *= 180.0 / PI;
wschon 3:70f624c5fe26 57 roll *= 180.0 / PI;
wschon 3:70f624c5fe26 58
wschon 3:70f624c5fe26 59 //pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll);
wschon 3:70f624c5fe26 60 //pc.printf("Magnetic Heading: %f degress\n\r",heading);
wschon 9:36807f7e58fb 61 head = heading * PI / 180.0;
wschon 9:36807f7e58fb 62 }
wschon 9:36807f7e58fb 63
wschon 9:36807f7e58fb 64 void shiftArrays() {
wschon 9:36807f7e58fb 65 int i;
wschon 9:36807f7e58fb 66 for (i = 2; i > 0; i--) {
wschon 9:36807f7e58fb 67 posX[i] = posX[i - 1];
wschon 9:36807f7e58fb 68 posY[i] = posY[i - 1];
wschon 9:36807f7e58fb 69 velX[i] = velX[i - 1];
wschon 9:36807f7e58fb 70 velY[i] = velY[i - 1];
wschon 9:36807f7e58fb 71 imu[i] = imu[i - 1];
wschon 9:36807f7e58fb 72 compass[i] = compass[i-1];
wschon 9:36807f7e58fb 73 }
wschon 3:70f624c5fe26 74 }
wschon 3:70f624c5fe26 75
wschon 3:70f624c5fe26 76 void IMU_thread(void const *args) {
wschon 8:198568d5746b 77 LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
wschon 6:a51f4ac6f42b 78 IMU.begin();
wschon 6:a51f4ac6f42b 79 if (!IMU.begin()) {
wschon 6:a51f4ac6f42b 80 pc.printf("Failed to communicate with LSM9DS1.\n");
wschon 6:a51f4ac6f42b 81 }
wschon 3:70f624c5fe26 82 IMU.calibrate(1);
wschon 9:36807f7e58fb 83 //pc.printf("calibrated accel, now do mag\r\n");
wschon 3:70f624c5fe26 84 IMU.calibrateMag(0);
wschon 9:36807f7e58fb 85 //pc.printf("calibrated mag\r\n");
wschon 3:70f624c5fe26 86 while(1) {
wschon 9:36807f7e58fb 87 //pc.printf("check accelAvailable\r\n");
wschon 3:70f624c5fe26 88 while(!IMU.accelAvailable());
wschon 8:198568d5746b 89 //pc.printf("got accel\r\n");
wschon 3:70f624c5fe26 90 IMU.readAccel();
wschon 3:70f624c5fe26 91 while(!IMU.magAvailable(X_AXIS));
wschon 8:198568d5746b 92 //pc.printf("got mag\r\n");
wschon 3:70f624c5fe26 93 IMU.readMag();
wschon 3:70f624c5fe26 94 xaccel = IMU.calcAccel(IMU.ax);
wschon 9:36807f7e58fb 95 if (abs(xaccel) < 0.13)
wschon 9:36807f7e58fb 96 xaccel = 0.0;
wschon 3:70f624c5fe26 97 yaccel = IMU.calcAccel(IMU.ay);
wschon 9:36807f7e58fb 98 if (abs(yaccel) < 0.13)
wschon 9:36807f7e58fb 99 yaccel = 0.0;
wschon 3:70f624c5fe26 100 zaccel = IMU.calcAccel(IMU.az);
wschon 9:36807f7e58fb 101 if (abs(zaccel) < 0.13)
wschon 9:36807f7e58fb 102 zaccel = 0.0;
wschon 8:198568d5746b 103 //pc.printf("%f\r\n",zaccel);
wschon 3:70f624c5fe26 104 xmag = IMU.calcMag(IMU.mx);
wschon 3:70f624c5fe26 105 ymag = IMU.calcMag(IMU.my);
wschon 3:70f624c5fe26 106 zmag = IMU.calcMag(IMU.mz);
wschon 9:36807f7e58fb 107 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 9:36807f7e58fb 108 shiftArrays();
wschon 9:36807f7e58fb 109 imu[0] = xaccel;
wschon 9:36807f7e58fb 110 compass[0] = head;
wschon 9:36807f7e58fb 111 velX[0] += imu[0]*cos(compass[0]) * deltat;
wschon 9:36807f7e58fb 112 velY[0] += imu[0]*sin(compass[0]) * deltat;
wschon 9:36807f7e58fb 113 posX[0] += velX[0] * deltat;
wschon 9:36807f7e58fb 114 posY[0] += velY[0] * deltat;
wschon 9:36807f7e58fb 115 // Thread::wait(50); //Wait
wschon 3:70f624c5fe26 116 }
wschon 3:70f624c5fe26 117 }
wschon 0:8e17f11689f2 118
wschon 2:fcf6f5901ee6 119 void left_thread(void const *args) {
wschon 2:fcf6f5901ee6 120 while (1) {
wschon 6:a51f4ac6f42b 121 leftDist = infraredL * 80.0f;
wschon 6:a51f4ac6f42b 122 leftDist = 80.0f - leftDist;
wschon 6:a51f4ac6f42b 123 ldist = (int)leftDist;
wschon 2:fcf6f5901ee6 124 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 125 }
wschon 2:fcf6f5901ee6 126 }
wschon 2:fcf6f5901ee6 127
wschon 2:fcf6f5901ee6 128 void right_thread(void const *args) {
wschon 2:fcf6f5901ee6 129 while (1) {
wschon 6:a51f4ac6f42b 130 rightDist = infraredR * 80.0f;
wschon 6:a51f4ac6f42b 131 rightDist = 80.0f - rightDist;
wschon 6:a51f4ac6f42b 132 rdist = (int)rightDist;
wschon 2:fcf6f5901ee6 133 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 134 }
wschon 2:fcf6f5901ee6 135 }
wschon 2:fcf6f5901ee6 136
wschon 2:fcf6f5901ee6 137 void front_thread(void const *args) {
wschon 2:fcf6f5901ee6 138 while (1) {
wschon 6:a51f4ac6f42b 139 frontDist = infraredF * 80.0f;
wschon 6:a51f4ac6f42b 140 frontDist = 80.0f - frontDist;
wschon 6:a51f4ac6f42b 141 fdist = (int)frontDist;
wschon 2:fcf6f5901ee6 142 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 143 }
wschon 2:fcf6f5901ee6 144 }
wschon 0:8e17f11689f2 145
wschon 7:cbccf5c5da6d 146 void send_thread(void const *args) {
wschon 8:198568d5746b 147 xbee.baud(9600);
wschon 7:cbccf5c5da6d 148 while (1) {
wschon 9:36807f7e58fb 149 ti = t.read();
wschon 9:36807f7e58fb 150 deltat = ti-timelast;
wschon 9:36807f7e58fb 151 stdio_mutex.lock();
wschon 9:36807f7e58fb 152 sprintf (str, "%0.6f,%0.6f,%0.6f,%d,%d,%d,%0.6f\n", posX[0], posY[0], heading, fdist, ldist, rdist, deltat); //buffer data to be sent to computer
wschon 8:198568d5746b 153 pc.printf(str);
wschon 9:36807f7e58fb 154 //pc.printf("%0.6f\r\n", zmag*9.8);
wschon 8:198568d5746b 155 xbee.printf(str);
wschon 9:36807f7e58fb 156 stdio_mutex.unlock();
wschon 9:36807f7e58fb 157 timelast = ti;
wschon 7:cbccf5c5da6d 158 Thread::wait(500);
wschon 7:cbccf5c5da6d 159 }
wschon 7:cbccf5c5da6d 160 }
wschon 7:cbccf5c5da6d 161
wschon 8:198568d5746b 162 void motor_thread(void const *args) {
wschon 8:198568d5746b 163 Thread::wait(5000); //allow gyro and accel to calibrate on level surface
wschon 8:198568d5746b 164 MotorRi.speed(0.4);
wschon 8:198568d5746b 165 MotorLe.speed(-0.4);
wschon 8:198568d5746b 166 Thread::wait(3000); //allow mag to calibrate by spinning
wschon 8:198568d5746b 167 while(1) {
wschon 9:36807f7e58fb 168 if (fdist > 40) { //if there's room to go forward
wschon 8:198568d5746b 169 if (ldist < 20) {
wschon 8:198568d5746b 170 MotorRi.speed(0.1);
wschon 9:36807f7e58fb 171 MotorLe.speed(0.3);
wschon 9:36807f7e58fb 172 Thread::wait(40);
wschon 8:198568d5746b 173 }
wschon 8:198568d5746b 174 else if (rdist < 20) {
wschon 9:36807f7e58fb 175 MotorRi.speed(0.3);
wschon 8:198568d5746b 176 MotorLe.speed(0.1);
wschon 9:36807f7e58fb 177 Thread::wait(40);
wschon 8:198568d5746b 178 }
wschon 8:198568d5746b 179 else {
wschon 9:36807f7e58fb 180 MotorRi.speed(0.3);
wschon 9:36807f7e58fb 181 MotorLe.speed(0.3);
wschon 8:198568d5746b 182 }
wschon 8:198568d5746b 183 }
wschon 8:198568d5746b 184 else if (rdist < ldist) { //hard turn to the left
wschon 9:36807f7e58fb 185 MotorRi.speed(0.3);
wschon 9:36807f7e58fb 186 MotorLe.speed(-0.3);
wschon 9:36807f7e58fb 187 Thread::wait(100);
wschon 8:198568d5746b 188 }
wschon 8:198568d5746b 189 else if (ldist < rdist) { //hard turn to the right
wschon 8:198568d5746b 190 MotorRi.speed(-0.4);
wschon 8:198568d5746b 191 MotorLe.speed(0.4);
wschon 9:36807f7e58fb 192 Thread::wait(100);
wschon 8:198568d5746b 193 }
wschon 8:198568d5746b 194 }
wschon 8:198568d5746b 195 }
wschon 7:cbccf5c5da6d 196
wschon 0:8e17f11689f2 197 int main() {
swilkins8 1:27c32ba9fe93 198 //Test
wschon 7:cbccf5c5da6d 199 t.start();
wschon 2:fcf6f5901ee6 200 Thread left(left_thread);
wschon 2:fcf6f5901ee6 201 Thread right(right_thread);
wschon 2:fcf6f5901ee6 202 Thread front(front_thread);
wschon 6:a51f4ac6f42b 203 Thread IMU(IMU_thread);
wschon 8:198568d5746b 204 Thread Mot(motor_thread);
wschon 7:cbccf5c5da6d 205 Thread send(send_thread);
wschon 8:198568d5746b 206 while(1){
wschon 8:198568d5746b 207 //do stuff
wschon 8:198568d5746b 208 }
wschon 0:8e17f11689f2 209 }