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 05:19:35 2016 +0000
Revision:
12:dfa473dd9311
Parent:
11:805ef606a29d
Child:
13:f8e7af7ad87a
sldfkj;

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 11:805ef606a29d 108
wschon 11:805ef606a29d 109 // Thread::wait(50); //Wait
wschon 11:805ef606a29d 110 }
wschon 11:805ef606a29d 111 }
wschon 11:805ef606a29d 112 void pos_thread(void const *args) {
wschon 11:805ef606a29d 113 while(1) {
wschon 12:dfa473dd9311 114
wschon 9:36807f7e58fb 115 shiftArrays();
wschon 9:36807f7e58fb 116 imu[0] = xaccel;
wschon 9:36807f7e58fb 117 compass[0] = head;
wschon 9:36807f7e58fb 118 velX[0] += imu[0]*cos(compass[0]) * deltat;
wschon 9:36807f7e58fb 119 velY[0] += imu[0]*sin(compass[0]) * deltat;
wschon 12:dfa473dd9311 120 posX[0] += velX[0] * .1;
wschon 12:dfa473dd9311 121 posY[0] += velY[0] * .1;
wschon 12:dfa473dd9311 122 wait(.1);
wschon 3:70f624c5fe26 123 }
wschon 11:805ef606a29d 124 }
wschon 2:fcf6f5901ee6 125 void left_thread(void const *args) {
wschon 2:fcf6f5901ee6 126 while (1) {
wschon 6:a51f4ac6f42b 127 leftDist = infraredL * 80.0f;
wschon 6:a51f4ac6f42b 128 leftDist = 80.0f - leftDist;
wschon 6:a51f4ac6f42b 129 ldist = (int)leftDist;
wschon 2:fcf6f5901ee6 130 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 131 }
wschon 2:fcf6f5901ee6 132 }
wschon 2:fcf6f5901ee6 133
wschon 2:fcf6f5901ee6 134 void right_thread(void const *args) {
wschon 2:fcf6f5901ee6 135 while (1) {
wschon 6:a51f4ac6f42b 136 rightDist = infraredR * 80.0f;
wschon 6:a51f4ac6f42b 137 rightDist = 80.0f - rightDist;
wschon 6:a51f4ac6f42b 138 rdist = (int)rightDist;
wschon 2:fcf6f5901ee6 139 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 140 }
wschon 2:fcf6f5901ee6 141 }
wschon 2:fcf6f5901ee6 142
wschon 2:fcf6f5901ee6 143 void front_thread(void const *args) {
wschon 2:fcf6f5901ee6 144 while (1) {
wschon 6:a51f4ac6f42b 145 frontDist = infraredF * 80.0f;
wschon 6:a51f4ac6f42b 146 frontDist = 80.0f - frontDist;
wschon 6:a51f4ac6f42b 147 fdist = (int)frontDist;
wschon 2:fcf6f5901ee6 148 Thread::wait(500); //wait 1/2 second before updating
wschon 2:fcf6f5901ee6 149 }
wschon 2:fcf6f5901ee6 150 }
wschon 0:8e17f11689f2 151
wschon 7:cbccf5c5da6d 152 void send_thread(void const *args) {
wschon 8:198568d5746b 153 xbee.baud(9600);
wschon 7:cbccf5c5da6d 154 while (1) {
wschon 9:36807f7e58fb 155 ti = t.read();
wschon 9:36807f7e58fb 156 deltat = ti-timelast;
wschon 9:36807f7e58fb 157 stdio_mutex.lock();
wschon 9:36807f7e58fb 158 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 12:dfa473dd9311 159 // pc.printf("%0.6f,%0.6f,%0.6f\r\n",xaccel,yaccel,deltat);
wschon 8:198568d5746b 160 pc.printf(str);
wschon 12:dfa473dd9311 161 // pc.printf("%0.6f\r\n", zmag*9.8);
wschon 8:198568d5746b 162 xbee.printf(str);
wschon 9:36807f7e58fb 163 stdio_mutex.unlock();
wschon 9:36807f7e58fb 164 timelast = ti;
wschon 7:cbccf5c5da6d 165 Thread::wait(500);
wschon 7:cbccf5c5da6d 166 }
wschon 7:cbccf5c5da6d 167 }
wschon 7:cbccf5c5da6d 168
wschon 8:198568d5746b 169 void motor_thread(void const *args) {
wschon 8:198568d5746b 170 Thread::wait(5000); //allow gyro and accel to calibrate on level surface
wschon 8:198568d5746b 171 MotorRi.speed(0.4);
wschon 8:198568d5746b 172 MotorLe.speed(-0.4);
wschon 8:198568d5746b 173 Thread::wait(3000); //allow mag to calibrate by spinning
wschon 8:198568d5746b 174 while(1) {
wschon 9:36807f7e58fb 175 if (fdist > 40) { //if there's room to go forward
wschon 8:198568d5746b 176 if (ldist < 20) {
wschon 8:198568d5746b 177 MotorRi.speed(0.1);
wschon 9:36807f7e58fb 178 MotorLe.speed(0.3);
wschon 9:36807f7e58fb 179 Thread::wait(40);
wschon 8:198568d5746b 180 }
wschon 8:198568d5746b 181 else if (rdist < 20) {
wschon 9:36807f7e58fb 182 MotorRi.speed(0.3);
wschon 8:198568d5746b 183 MotorLe.speed(0.1);
wschon 9:36807f7e58fb 184 Thread::wait(40);
wschon 8:198568d5746b 185 }
wschon 8:198568d5746b 186 else {
wschon 9:36807f7e58fb 187 MotorRi.speed(0.3);
wschon 9:36807f7e58fb 188 MotorLe.speed(0.3);
wschon 8:198568d5746b 189 }
wschon 8:198568d5746b 190 }
wschon 8:198568d5746b 191 else if (rdist < ldist) { //hard turn to the left
wschon 9:36807f7e58fb 192 MotorRi.speed(0.3);
wschon 9:36807f7e58fb 193 MotorLe.speed(-0.3);
wschon 9:36807f7e58fb 194 Thread::wait(100);
wschon 8:198568d5746b 195 }
wschon 8:198568d5746b 196 else if (ldist < rdist) { //hard turn to the right
wschon 8:198568d5746b 197 MotorRi.speed(-0.4);
wschon 8:198568d5746b 198 MotorLe.speed(0.4);
wschon 9:36807f7e58fb 199 Thread::wait(100);
wschon 8:198568d5746b 200 }
wschon 8:198568d5746b 201 }
wschon 8:198568d5746b 202 }
wschon 7:cbccf5c5da6d 203
wschon 0:8e17f11689f2 204 int main() {
swilkins8 1:27c32ba9fe93 205 //Test
wschon 7:cbccf5c5da6d 206 t.start();
wschon 11:805ef606a29d 207 Thread posi(pos_thread);
wschon 2:fcf6f5901ee6 208 Thread left(left_thread);
wschon 2:fcf6f5901ee6 209 Thread right(right_thread);
wschon 2:fcf6f5901ee6 210 Thread front(front_thread);
wschon 6:a51f4ac6f42b 211 Thread IMU(IMU_thread);
wschon 8:198568d5746b 212 Thread Mot(motor_thread);
wschon 7:cbccf5c5da6d 213 Thread send(send_thread);
wschon 8:198568d5746b 214 while(1){
wschon 8:198568d5746b 215 //do stuff
wschon 8:198568d5746b 216 }
wschon 0:8e17f11689f2 217 }