blue mbed code for the BNO055 imu from adafruit

Dependencies:   BNO055 MODSERIAL mbed

Fork of bmbed_lidar_belt by sensory_array

Committer:
rkk
Date:
Fri Oct 16 19:42:06 2015 +0000
Revision:
12:6deb3b41c9e3
Parent:
11:c972bf9c24de
Parent:
6:9ae0e867efed
Child:
13:b22fd9c4fbb4
merged;

Who changed what in which revision?

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