blue mbed code for the BNO055 imu from adafruit
Dependencies: BNO055 MODSERIAL mbed
Fork of bmbed_lidar_belt by
main.cpp@11:c972bf9c24de, 2015-10-16 (annotated)
- Committer:
- baraki
- Date:
- Fri Oct 16 19:37:40 2015 +0000
- Revision:
- 11:c972bf9c24de
- Parent:
- 9:59d23ab8d73b
- Child:
- 12:6deb3b41c9e3
printfs off;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
baraki | 0:ce4f790399d9 | 1 | #include "mbed.h" |
baraki | 0:ce4f790399d9 | 2 | #include "MODSERIAL.h" |
baraki | 7:660e8ddb231e | 3 | #include <math.h> |
baraki | 7:660e8ddb231e | 4 | #include "BNO055.h" |
baraki | 0:ce4f790399d9 | 5 | |
baraki | 0:ce4f790399d9 | 6 | #define PC_BAUD 9600 |
baraki | 0:ce4f790399d9 | 7 | #define BT_BAUD 9600 |
baraki | 0:ce4f790399d9 | 8 | #define TX_PIN p13 |
baraki | 0:ce4f790399d9 | 9 | #define RX_PIN p14 |
baraki | 0:ce4f790399d9 | 10 | #define SDA_PIN p9 //SDA pin on LPC1768 |
baraki | 0:ce4f790399d9 | 11 | #define SCL_PIN p10 //SCL pin on LPC1768 |
baraki | 7:660e8ddb231e | 12 | #define IMU_SDA p28 |
baraki | 7:660e8ddb231e | 13 | #define IMU_SCL p27 |
baraki | 7:660e8ddb231e | 14 | #define PI 3.14159265 |
baraki | 0:ce4f790399d9 | 15 | |
baraki | 7:660e8ddb231e | 16 | BNO055 imu(p28,p27); |
baraki | 0:ce4f790399d9 | 17 | I2C sensor(SDA_PIN, SCL_PIN); //Define LIDAR Lite sensor 1 |
baraki | 0:ce4f790399d9 | 18 | MODSERIAL bt(TX_PIN, RX_PIN); |
baraki | 0:ce4f790399d9 | 19 | MODSERIAL pc(USBTX,USBRX); |
baraki | 0:ce4f790399d9 | 20 | |
baraki | 7:660e8ddb231e | 21 | //for calculating allowable change in height for the down lidar |
baraki | 7:660e8ddb231e | 22 | float allowHeight = 120; //120 mm |
baraki | 9:59d23ab8d73b | 23 | float downAngle = -5.6; |
baraki | 7:660e8ddb231e | 24 | //for calibrating IMU |
baraki | 9:59d23ab8d73b | 25 | //for IMU1: |
baraki | 9:59d23ab8d73b | 26 | //char cal_vals[22] = {255, 255, 220, 255, 13, 0, 83, 255, 36, 1, 80, 0, 253, 255, 0, 0, 1, 0, 232, 3, 235, 2}; |
baraki | 9:59d23ab8d73b | 27 | //for IMU2: |
baraki | 9:59d23ab8d73b | 28 | char cal_vals[22] = {231, 255, 253, 255, 8, 0, 43, 255, 31, 1, 221, 255, 0, 0, 254, 255, 2, 0, 232, 3, 210, 2}; |
baraki | 9:59d23ab8d73b | 29 | |
baraki | 7:660e8ddb231e | 30 | |
baraki | 7:660e8ddb231e | 31 | //for encoder: |
baraki | 7:660e8ddb231e | 32 | //need to set pins to DigitalIn with internal pullup resistors |
baraki | 7:660e8ddb231e | 33 | //DigitalIn mySwitch(p21); |
baraki | 7:660e8ddb231e | 34 | //mySwitch.mode(PullUp); |
baraki | 7:660e8ddb231e | 35 | |
baraki | 0:ce4f790399d9 | 36 | bool newline_detected = false; |
baraki | 0:ce4f790399d9 | 37 | bool newline_sent = false; |
baraki | 0:ce4f790399d9 | 38 | |
baraki | 7:660e8ddb231e | 39 | void setCal(){ |
baraki | 7:660e8ddb231e | 40 | imu.write_calibration_data(); |
baraki | 7:660e8ddb231e | 41 | } |
baraki | 7:660e8ddb231e | 42 | |
baraki | 0:ce4f790399d9 | 43 | // Called everytime a new character goes into |
baraki | 0:ce4f790399d9 | 44 | // the RX buffer. Test that character for \n |
baraki | 0:ce4f790399d9 | 45 | // Note, rxGetLastChar() gets the last char that |
baraki | 0:ce4f790399d9 | 46 | // we received but it does NOT remove it from |
baraki | 0:ce4f790399d9 | 47 | // the RX buffer. |
baraki | 0:ce4f790399d9 | 48 | void rxCallback(MODSERIAL_IRQ_INFO *q) |
baraki | 0:ce4f790399d9 | 49 | { |
baraki | 0:ce4f790399d9 | 50 | MODSERIAL *serial = q->serial; |
baraki | 0:ce4f790399d9 | 51 | if ( serial->rxGetLastChar() == '\n') { |
baraki | 0:ce4f790399d9 | 52 | newline_detected = true; |
baraki | 0:ce4f790399d9 | 53 | } |
baraki | 0:ce4f790399d9 | 54 | |
baraki | 0:ce4f790399d9 | 55 | } |
baraki | 0:ce4f790399d9 | 56 | |
baraki | 0:ce4f790399d9 | 57 | void txCallback(MODSERIAL_IRQ_INFO *q) |
baraki | 0:ce4f790399d9 | 58 | { |
baraki | 0:ce4f790399d9 | 59 | MODSERIAL *serial = q->serial; |
baraki | 0:ce4f790399d9 | 60 | if ( serial->txGetLastChar() == '\0') { |
baraki | 0:ce4f790399d9 | 61 | newline_sent = true; |
baraki | 0:ce4f790399d9 | 62 | } |
baraki | 0:ce4f790399d9 | 63 | } |
baraki | 0:ce4f790399d9 | 64 | |
baraki | 0:ce4f790399d9 | 65 | int main() |
baraki | 0:ce4f790399d9 | 66 | { |
baraki | 0:ce4f790399d9 | 67 | pc.baud(PC_BAUD); |
baraki | 0:ce4f790399d9 | 68 | bt.baud(BT_BAUD); |
baraki | 0:ce4f790399d9 | 69 | pc.attach(&rxCallback, MODSERIAL::RxIrq); |
baraki | 0:ce4f790399d9 | 70 | bt.attach(&txCallback, MODSERIAL::TxIrq); |
baraki | 0:ce4f790399d9 | 71 | |
baraki | 7:660e8ddb231e | 72 | //set up IMU |
baraki | 7:660e8ddb231e | 73 | imu.reset(); |
baraki | 7:660e8ddb231e | 74 | imu.setmode(OPERATION_MODE_NDOF); |
baraki | 7:660e8ddb231e | 75 | setCal(); |
baraki | 7:660e8ddb231e | 76 | imu.get_calib(); |
baraki | 7:660e8ddb231e | 77 | while (imu.calib == 0) |
baraki | 7:660e8ddb231e | 78 | { |
baraki | 7:660e8ddb231e | 79 | imu.get_calib(); |
baraki | 7:660e8ddb231e | 80 | } |
baraki | 7:660e8ddb231e | 81 | |
baraki | 0:ce4f790399d9 | 82 | sensor.frequency(100000); |
baraki | 0:ce4f790399d9 | 83 | |
baraki | 0:ce4f790399d9 | 84 | char sendData[1] = {0x00}; |
baraki | 0:ce4f790399d9 | 85 | |
baraki | 0:ce4f790399d9 | 86 | int addresses[7]; |
baraki | 0:ce4f790399d9 | 87 | addresses[0] = 0x60; //0x60 |
baraki | 0:ce4f790399d9 | 88 | addresses[1] = 0x64; //0x64 |
baraki | 0:ce4f790399d9 | 89 | addresses[2] = 0x68; //middle |
baraki | 0:ce4f790399d9 | 90 | addresses[3] = 0x6C; |
baraki | 0:ce4f790399d9 | 91 | addresses[4] = 0x70; |
baraki | 0:ce4f790399d9 | 92 | addresses[5] = 0x80; //up |
baraki | 0:ce4f790399d9 | 93 | addresses[6] = 0x84; //down |
baraki | 0:ce4f790399d9 | 94 | |
baraki | 0:ce4f790399d9 | 95 | uint8_t pulses[7] = {0}; |
baraki | 0:ce4f790399d9 | 96 | uint8_t intensity[7] = {0}; |
baraki | 0:ce4f790399d9 | 97 | |
baraki | 0:ce4f790399d9 | 98 | char btData[12] = {'a','b','c','d','e','f','g','\n','\0'}; |
baraki | 0:ce4f790399d9 | 99 | |
baraki | 0:ce4f790399d9 | 100 | //calibrate down sensor |
baraki | 0:ce4f790399d9 | 101 | int down_cal = 0; |
baraki | 8:2ddeec5d8f84 | 102 | float cospi = 0; //"cosine of initial pitch" |
baraki | 0:ce4f790399d9 | 103 | |
baraki | 0:ce4f790399d9 | 104 | unsigned int i = 0; |
baraki | 0:ce4f790399d9 | 105 | int count = 0; //for calibration |
baraki | 0:ce4f790399d9 | 106 | int count2 = 0;//for averaging |
baraki | 0:ce4f790399d9 | 107 | int differenceAvgSum = 0; |
baraki | 0:ce4f790399d9 | 108 | int moving_ave[5] = {0}; |
baraki | 0:ce4f790399d9 | 109 | while (1) { |
baraki | 0:ce4f790399d9 | 110 | for(int k=0; k<5; k++) { |
baraki | 0:ce4f790399d9 | 111 | char receiveData[3] = {0}; |
baraki | 0:ce4f790399d9 | 112 | if(sensor.write(addresses[k], sendData, 1)){ |
baraki | 1:5b1d88d69aa2 | 113 | //pc.printf("writing to sensor %d failed\n", k); |
baraki | 0:ce4f790399d9 | 114 | } |
baraki | 0:ce4f790399d9 | 115 | //write ---> 0 on success, 1 on failure |
baraki | 0:ce4f790399d9 | 116 | i = 0; |
baraki | 2:ec53792aef80 | 117 | while(sensor.read(addresses[k], receiveData, 3) && i < 10) { |
baraki | 0:ce4f790399d9 | 118 | i++; |
baraki | 1:5b1d88d69aa2 | 119 | //pc.printf("reading from sensor %d failed\n",k); |
baraki | 1:5b1d88d69aa2 | 120 | } |
baraki | 0:ce4f790399d9 | 121 | //while(!twi_master_transfer(addresses[k], sendData, 1, TWI_ISSUE_STOP)){;} |
baraki | 0:ce4f790399d9 | 122 | //while(!twi_master_transfer(addresses[k] + 1, receiveData, 3, TWI_ISSUE_STOP)){;} |
baraki | 0:ce4f790399d9 | 123 | int distance = ((int)receiveData[0]<<8 )+ (int)receiveData[1]; |
baraki | 0:ce4f790399d9 | 124 | if(distance == 0){ |
baraki | 0:ce4f790399d9 | 125 | pulses[k] = 1; |
baraki | 0:ce4f790399d9 | 126 | intensity[k] = 0; |
baraki | 0:ce4f790399d9 | 127 | } |
baraki | 0:ce4f790399d9 | 128 | if(distance > 0 && distance < 650) { |
baraki | 0:ce4f790399d9 | 129 | pulses[k] = 5; |
baraki | 0:ce4f790399d9 | 130 | intensity[k] = 7; |
baraki | 0:ce4f790399d9 | 131 | } else if(distance >= 650 && distance < 900) { |
baraki | 0:ce4f790399d9 | 132 | pulses[k] = 4; |
baraki | 0:ce4f790399d9 | 133 | intensity[k] = 6; |
baraki | 0:ce4f790399d9 | 134 | } else if(distance >= 900 && distance < 1350) { |
baraki | 0:ce4f790399d9 | 135 | pulses[k] = 3; |
baraki | 0:ce4f790399d9 | 136 | intensity[k] = 5; |
baraki | 0:ce4f790399d9 | 137 | } else if(distance >= 1350 && distance < 1850) { |
baraki | 0:ce4f790399d9 | 138 | pulses[k] = 2; |
baraki | 0:ce4f790399d9 | 139 | intensity[k] = 2; |
baraki | 0:ce4f790399d9 | 140 | } else if(distance >= 1800) { |
baraki | 0:ce4f790399d9 | 141 | pulses[k] = 1; |
baraki | 0:ce4f790399d9 | 142 | intensity[k] = 0; |
baraki | 0:ce4f790399d9 | 143 | } |
baraki | 3:a0ccaf565e8d | 144 | //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",k,pulses[k],intensity[k]); |
baraki | 0:ce4f790399d9 | 145 | } |
baraki | 0:ce4f790399d9 | 146 | |
baraki | 0:ce4f790399d9 | 147 | //find UP distance |
baraki | 0:ce4f790399d9 | 148 | char receiveData2[3] = {0}; |
baraki | 0:ce4f790399d9 | 149 | sensor.write(addresses[5], sendData, 1); |
baraki | 0:ce4f790399d9 | 150 | i = 0; |
baraki | 2:ec53792aef80 | 151 | while(sensor.read(addresses[5]+1, receiveData2, 3) && i < 10){ |
baraki | 0:ce4f790399d9 | 152 | i++;} |
baraki | 0:ce4f790399d9 | 153 | int distance2 = (receiveData2[0]<<8 )+ receiveData2[1]; |
baraki | 0:ce4f790399d9 | 154 | if(distance2 >= 500 && distance2 < 1000) { |
baraki | 0:ce4f790399d9 | 155 | pulses[5] = 5; |
baraki | 0:ce4f790399d9 | 156 | intensity[5] = 7; |
baraki | 0:ce4f790399d9 | 157 | } else { |
baraki | 0:ce4f790399d9 | 158 | pulses[5] = 1; |
baraki | 0:ce4f790399d9 | 159 | intensity[5] = 0; |
baraki | 0:ce4f790399d9 | 160 | } |
baraki | 0:ce4f790399d9 | 161 | |
baraki | 0:ce4f790399d9 | 162 | //find DOWN distance |
baraki | 0:ce4f790399d9 | 163 | char receiveData3[3] = {0}; |
baraki | 2:ec53792aef80 | 164 | i = 0; |
baraki | 0:ce4f790399d9 | 165 | sensor.write(addresses[6], sendData, 1); |
baraki | 2:ec53792aef80 | 166 | while(sensor.read(addresses[6]+1, receiveData3, 3) && i < 10){ |
baraki | 0:ce4f790399d9 | 167 | i++;} |
baraki | 0:ce4f790399d9 | 168 | int distance3 = (receiveData3[0]<<8 )+ receiveData3[1]; |
baraki | 8:2ddeec5d8f84 | 169 | if(count > 100) { //calibration over |
baraki | 8:2ddeec5d8f84 | 170 | //get allowableX and adjusted down_cal from IMU |
baraki | 8:2ddeec5d8f84 | 171 | imu.get_angles(); |
baraki | 8:2ddeec5d8f84 | 172 | float pitch = imu.euler.pitch; |
baraki | 8:2ddeec5d8f84 | 173 | float cosp = cos((pitch-downAngle) * PI/180.0); |
baraki | 8:2ddeec5d8f84 | 174 | float allowX = allowHeight/cosp; |
baraki | 8:2ddeec5d8f84 | 175 | float new_down_cal = ((float)down_cal)*cospi/cosp; |
baraki | 8:2ddeec5d8f84 | 176 | |
baraki | 8:2ddeec5d8f84 | 177 | //use moving average to find deltaX |
baraki | 8:2ddeec5d8f84 | 178 | int difference = abs(new_down_cal - distance3); |
baraki | 0:ce4f790399d9 | 179 | differenceAvgSum = differenceAvgSum - moving_ave[count2]; |
baraki | 0:ce4f790399d9 | 180 | moving_ave[count2] = difference; |
baraki | 0:ce4f790399d9 | 181 | differenceAvgSum = differenceAvgSum + difference; |
baraki | 0:ce4f790399d9 | 182 | count2 = count2 + 1; |
baraki | 0:ce4f790399d9 | 183 | int ave = (int)(differenceAvgSum/5); |
baraki | 7:660e8ddb231e | 184 | |
baraki | 11:c972bf9c24de | 185 | //pc.printf("distance: %d\tallowableX: %f\tdistance: %d\tpitch: %f\tdowncal: %d\tnewdowncal: %f\r\n",ave,allowX,distance3,pitch-downAngle,down_cal,new_down_cal); |
baraki | 7:660e8ddb231e | 186 | |
baraki | 3:a0ccaf565e8d | 187 | //pc.printf("down_cal: %d \t diff: %d \t distance: %d\n",down_cal, ave, distance3); |
baraki | 7:660e8ddb231e | 188 | if(ave >= allowX) { |
baraki | 0:ce4f790399d9 | 189 | pulses[6] = 5; |
baraki | 0:ce4f790399d9 | 190 | intensity[6] = 7; |
baraki | 0:ce4f790399d9 | 191 | } else { |
baraki | 0:ce4f790399d9 | 192 | pulses[6] = 1; |
baraki | 0:ce4f790399d9 | 193 | intensity[6] = 0; |
baraki | 0:ce4f790399d9 | 194 | } |
baraki | 0:ce4f790399d9 | 195 | |
baraki | 0:ce4f790399d9 | 196 | if(count2 >4) { |
baraki | 0:ce4f790399d9 | 197 | count2 = 0; |
baraki | 0:ce4f790399d9 | 198 | } |
baraki | 0:ce4f790399d9 | 199 | } else { |
baraki | 8:2ddeec5d8f84 | 200 | |
baraki | 0:ce4f790399d9 | 201 | down_cal = distance3; |
baraki | 8:2ddeec5d8f84 | 202 | imu.get_angles(); |
baraki | 8:2ddeec5d8f84 | 203 | float pitch = imu.euler.pitch; |
baraki | 8:2ddeec5d8f84 | 204 | cospi = cos((pitch-downAngle) * PI/180.0); |
baraki | 0:ce4f790399d9 | 205 | count = count+1; |
baraki | 0:ce4f790399d9 | 206 | } |
baraki | 3:a0ccaf565e8d | 207 | //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",6,pulses[6],intensity[6]); |
baraki | 0:ce4f790399d9 | 208 | |
baraki | 1:5b1d88d69aa2 | 209 | //pc.printf("about to send data\n"); |
baraki | 0:ce4f790399d9 | 210 | btData[0] = (pulses[0] << 5) | (intensity[0] << 2); |
baraki | 0:ce4f790399d9 | 211 | btData[1] = (pulses[1] << 4) | (intensity[1] << 1); |
baraki | 0:ce4f790399d9 | 212 | btData[2] = (pulses[2] << 3) | (intensity[2]); |
baraki | 0:ce4f790399d9 | 213 | btData[3] = (pulses[3] << 2) | (intensity[3] >> 1); |
baraki | 0:ce4f790399d9 | 214 | btData[4] = (intensity[3] << 7) | (pulses[4] << 1) | (intensity[4] >> 2); |
baraki | 0:ce4f790399d9 | 215 | btData[5] = (intensity[4] << 6) | (0x3); |
baraki | 0:ce4f790399d9 | 216 | btData[6] = (pulses[5] << 5) | (intensity[5] << 2); |
baraki | 0:ce4f790399d9 | 217 | btData[7] = (pulses[6] << 5) | (intensity[6] << 2); |
baraki | 0:ce4f790399d9 | 218 | btData[8] = '\0'; |
baraki | 0:ce4f790399d9 | 219 | for(int j=0;j<9;j++){ |
baraki | 1:5b1d88d69aa2 | 220 | if(bt.writeable()) |
baraki | 1:5b1d88d69aa2 | 221 | bt.putc(btData[j]); |
baraki | 1:5b1d88d69aa2 | 222 | //wait(0.001); |
baraki | 0:ce4f790399d9 | 223 | } |
baraki | 3:a0ccaf565e8d | 224 | wait(0.05); |
baraki | 1:5b1d88d69aa2 | 225 | //pc.printf("finished sending data\n"); |
baraki | 0:ce4f790399d9 | 226 | //ble_uart_c_write_string(&m_ble_uart_c, (uint8_t *)btData, 9); |
baraki | 0:ce4f790399d9 | 227 | } |
baraki | 0:ce4f790399d9 | 228 | } |