Robot using IMU and IR sensor data to drive autonomously and create a map
Dependencies: mbed mbed-rtos LSM9DS1_Library_cal Motor
main.cpp@8:198568d5746b, 2016-05-01 (annotated)
- Committer:
- wschon
- Date:
- Sun May 01 23:56:41 2016 +0000
- Revision:
- 8:198568d5746b
- Parent:
- 7:cbccf5c5da6d
- Child:
- 9:36807f7e58fb
added
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:198568d5746b | 15 | char str[40]; //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 | 3:70f624c5fe26 | 23 | |
wschon | 7:cbccf5c5da6d | 24 | //dual H bridge Polulu |
wschon | 8:198568d5746b | 25 | Motor MotorRi(p21, p23, p24); |
wschon | 8:198568d5746b | 26 | Motor MotorLe(p22, p26, p25); |
wschon | 7:cbccf5c5da6d | 27 | |
wschon | 7:cbccf5c5da6d | 28 | |
wschon | 3:70f624c5fe26 | 29 | void printAttitude(float ax, float ay, float az, float mx, float my, float mz) |
wschon | 3:70f624c5fe26 | 30 | { |
wschon | 3:70f624c5fe26 | 31 | float roll = atan2(ay, az); |
wschon | 3:70f624c5fe26 | 32 | float pitch = atan2(-ax, sqrt(ay * ay + az * az)); |
wschon | 3:70f624c5fe26 | 33 | // touchy trig stuff to use arctan to get compass heading (scale is 0..360) |
wschon | 3:70f624c5fe26 | 34 | mx = -mx; |
wschon | 3:70f624c5fe26 | 35 | float heading; |
wschon | 3:70f624c5fe26 | 36 | if (my == 0.0) |
wschon | 3:70f624c5fe26 | 37 | heading = (mx < 0.0) ? 180.0 : 0.0; |
wschon | 3:70f624c5fe26 | 38 | else |
wschon | 3:70f624c5fe26 | 39 | heading = atan2(mx, my)*360.0/(2.0*PI); |
wschon | 3:70f624c5fe26 | 40 | //pc.printf("heading atan=%f \n\r",heading); |
wschon | 3:70f624c5fe26 | 41 | heading -= DECLINATION; //correct for geo location |
wschon | 3:70f624c5fe26 | 42 | if(heading>180.0) heading = heading - 360.0; |
wschon | 3:70f624c5fe26 | 43 | else if(heading<-180.0) heading = 360.0 + heading; |
wschon | 3:70f624c5fe26 | 44 | else if(heading<0.0) heading = 360.0 + heading; |
wschon | 3:70f624c5fe26 | 45 | |
wschon | 3:70f624c5fe26 | 46 | |
wschon | 3:70f624c5fe26 | 47 | // Convert everything from radians to degrees: |
wschon | 3:70f624c5fe26 | 48 | //heading *= 180.0 / PI; |
wschon | 3:70f624c5fe26 | 49 | pitch *= 180.0 / PI; |
wschon | 3:70f624c5fe26 | 50 | roll *= 180.0 / PI; |
wschon | 3:70f624c5fe26 | 51 | |
wschon | 3:70f624c5fe26 | 52 | //pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll); |
wschon | 3:70f624c5fe26 | 53 | //pc.printf("Magnetic Heading: %f degress\n\r",heading); |
wschon | 3:70f624c5fe26 | 54 | head = heading; |
wschon | 3:70f624c5fe26 | 55 | } |
wschon | 3:70f624c5fe26 | 56 | |
wschon | 3:70f624c5fe26 | 57 | void IMU_thread(void const *args) { |
wschon | 8:198568d5746b | 58 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
wschon | 6:a51f4ac6f42b | 59 | IMU.begin(); |
wschon | 6:a51f4ac6f42b | 60 | if (!IMU.begin()) { |
wschon | 6:a51f4ac6f42b | 61 | pc.printf("Failed to communicate with LSM9DS1.\n"); |
wschon | 6:a51f4ac6f42b | 62 | } |
wschon | 3:70f624c5fe26 | 63 | IMU.calibrate(1); |
wschon | 8:198568d5746b | 64 | pc.printf("calibrated accel, now do mag\r\n"); |
wschon | 3:70f624c5fe26 | 65 | IMU.calibrateMag(0); |
wschon | 8:198568d5746b | 66 | pc.printf("calibrated mag\r\n"); |
wschon | 3:70f624c5fe26 | 67 | while(1) { |
wschon | 8:198568d5746b | 68 | pc.printf("check accelAvailable\r\n"); |
wschon | 3:70f624c5fe26 | 69 | while(!IMU.accelAvailable()); |
wschon | 8:198568d5746b | 70 | //pc.printf("got accel\r\n"); |
wschon | 3:70f624c5fe26 | 71 | IMU.readAccel(); |
wschon | 3:70f624c5fe26 | 72 | while(!IMU.magAvailable(X_AXIS)); |
wschon | 8:198568d5746b | 73 | //pc.printf("got mag\r\n"); |
wschon | 3:70f624c5fe26 | 74 | IMU.readMag(); |
wschon | 3:70f624c5fe26 | 75 | xaccel = IMU.calcAccel(IMU.ax); |
wschon | 3:70f624c5fe26 | 76 | yaccel = IMU.calcAccel(IMU.ay); |
wschon | 3:70f624c5fe26 | 77 | zaccel = IMU.calcAccel(IMU.az); |
wschon | 8:198568d5746b | 78 | //pc.printf("%f\r\n",zaccel); |
wschon | 3:70f624c5fe26 | 79 | xmag = IMU.calcMag(IMU.mx); |
wschon | 3:70f624c5fe26 | 80 | ymag = IMU.calcMag(IMU.my); |
wschon | 3:70f624c5fe26 | 81 | zmag = IMU.calcMag(IMU.mz); |
wschon | 8:198568d5746b | 82 | //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 | 83 | Thread::wait(500); //Wait 1/2 second |
wschon | 3:70f624c5fe26 | 84 | } |
wschon | 3:70f624c5fe26 | 85 | } |
wschon | 0:8e17f11689f2 | 86 | |
wschon | 2:fcf6f5901ee6 | 87 | void left_thread(void const *args) { |
wschon | 2:fcf6f5901ee6 | 88 | while (1) { |
wschon | 6:a51f4ac6f42b | 89 | leftDist = infraredL * 80.0f; |
wschon | 6:a51f4ac6f42b | 90 | leftDist = 80.0f - leftDist; |
wschon | 6:a51f4ac6f42b | 91 | ldist = (int)leftDist; |
wschon | 2:fcf6f5901ee6 | 92 | Thread::wait(500); //wait 1/2 second before updating |
wschon | 2:fcf6f5901ee6 | 93 | } |
wschon | 2:fcf6f5901ee6 | 94 | } |
wschon | 2:fcf6f5901ee6 | 95 | |
wschon | 2:fcf6f5901ee6 | 96 | void right_thread(void const *args) { |
wschon | 2:fcf6f5901ee6 | 97 | while (1) { |
wschon | 6:a51f4ac6f42b | 98 | rightDist = infraredR * 80.0f; |
wschon | 6:a51f4ac6f42b | 99 | rightDist = 80.0f - rightDist; |
wschon | 6:a51f4ac6f42b | 100 | rdist = (int)rightDist; |
wschon | 2:fcf6f5901ee6 | 101 | Thread::wait(500); //wait 1/2 second before updating |
wschon | 2:fcf6f5901ee6 | 102 | } |
wschon | 2:fcf6f5901ee6 | 103 | } |
wschon | 2:fcf6f5901ee6 | 104 | |
wschon | 2:fcf6f5901ee6 | 105 | void front_thread(void const *args) { |
wschon | 2:fcf6f5901ee6 | 106 | while (1) { |
wschon | 6:a51f4ac6f42b | 107 | frontDist = infraredF * 80.0f; |
wschon | 6:a51f4ac6f42b | 108 | frontDist = 80.0f - frontDist; |
wschon | 6:a51f4ac6f42b | 109 | fdist = (int)frontDist; |
wschon | 2:fcf6f5901ee6 | 110 | Thread::wait(500); //wait 1/2 second before updating |
wschon | 2:fcf6f5901ee6 | 111 | } |
wschon | 2:fcf6f5901ee6 | 112 | } |
wschon | 0:8e17f11689f2 | 113 | |
wschon | 7:cbccf5c5da6d | 114 | void send_thread(void const *args) { |
wschon | 8:198568d5746b | 115 | xbee.baud(9600); |
wschon | 7:cbccf5c5da6d | 116 | while (1) { |
wschon | 8:198568d5746b | 117 | //stdio_mutex.lock(); |
wschon | 7:cbccf5c5da6d | 118 | 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 |
wschon | 8:198568d5746b | 119 | pc.printf(str); |
wschon | 8:198568d5746b | 120 | //pc.printf("%0.6f\r\n", zmag); |
wschon | 8:198568d5746b | 121 | xbee.printf(str); |
wschon | 8:198568d5746b | 122 | //stdio_mutex.unlock(); |
wschon | 7:cbccf5c5da6d | 123 | Thread::wait(500); |
wschon | 7:cbccf5c5da6d | 124 | } |
wschon | 7:cbccf5c5da6d | 125 | } |
wschon | 7:cbccf5c5da6d | 126 | |
wschon | 8:198568d5746b | 127 | void motor_thread(void const *args) { |
wschon | 8:198568d5746b | 128 | Thread::wait(5000); //allow gyro and accel to calibrate on level surface |
wschon | 8:198568d5746b | 129 | MotorRi.speed(0.4); |
wschon | 8:198568d5746b | 130 | MotorLe.speed(-0.4); |
wschon | 8:198568d5746b | 131 | Thread::wait(3000); //allow mag to calibrate by spinning |
wschon | 8:198568d5746b | 132 | while(1) { |
wschon | 8:198568d5746b | 133 | if (fdist > 30) { //if there's room to go forward |
wschon | 8:198568d5746b | 134 | if (ldist < 20) { |
wschon | 8:198568d5746b | 135 | MotorRi.speed(0.1); |
wschon | 8:198568d5746b | 136 | MotorLe.speed(0.4); |
wschon | 8:198568d5746b | 137 | Thread::wait(80); |
wschon | 8:198568d5746b | 138 | } |
wschon | 8:198568d5746b | 139 | else if (rdist < 20) { |
wschon | 8:198568d5746b | 140 | MotorRi.speed(0.4); |
wschon | 8:198568d5746b | 141 | MotorLe.speed(0.1); |
wschon | 8:198568d5746b | 142 | Thread::wait(80); |
wschon | 8:198568d5746b | 143 | } |
wschon | 8:198568d5746b | 144 | else { |
wschon | 8:198568d5746b | 145 | MotorRi.speed(0.4); |
wschon | 8:198568d5746b | 146 | MotorLe.speed(0.4); |
wschon | 8:198568d5746b | 147 | } |
wschon | 8:198568d5746b | 148 | } |
wschon | 8:198568d5746b | 149 | else if (rdist < ldist) { //hard turn to the left |
wschon | 8:198568d5746b | 150 | MotorRi.speed(0.4); |
wschon | 8:198568d5746b | 151 | MotorLe.speed(-0.4); |
wschon | 8:198568d5746b | 152 | Thread::wait(200); |
wschon | 8:198568d5746b | 153 | } |
wschon | 8:198568d5746b | 154 | else if (ldist < rdist) { //hard turn to the right |
wschon | 8:198568d5746b | 155 | MotorRi.speed(-0.4); |
wschon | 8:198568d5746b | 156 | MotorLe.speed(0.4); |
wschon | 8:198568d5746b | 157 | Thread::wait(200); |
wschon | 8:198568d5746b | 158 | } |
wschon | 8:198568d5746b | 159 | } |
wschon | 8:198568d5746b | 160 | } |
wschon | 7:cbccf5c5da6d | 161 | |
wschon | 0:8e17f11689f2 | 162 | int main() { |
swilkins8 | 1:27c32ba9fe93 | 163 | //Test |
wschon | 7:cbccf5c5da6d | 164 | t.start(); |
wschon | 2:fcf6f5901ee6 | 165 | Thread left(left_thread); |
wschon | 2:fcf6f5901ee6 | 166 | Thread right(right_thread); |
wschon | 2:fcf6f5901ee6 | 167 | Thread front(front_thread); |
wschon | 6:a51f4ac6f42b | 168 | Thread IMU(IMU_thread); |
wschon | 8:198568d5746b | 169 | Thread Mot(motor_thread); |
wschon | 7:cbccf5c5da6d | 170 | Thread send(send_thread); |
wschon | 8:198568d5746b | 171 | while(1){ |
wschon | 8:198568d5746b | 172 | //do stuff |
wschon | 8:198568d5746b | 173 | } |
wschon | 0:8e17f11689f2 | 174 | } |