blue mbed code for the BNO055 imu from adafruit

Dependencies:   BNO055 MODSERIAL mbed

Fork of bmbed_lidar_belt by sensory_array

Committer:
baraki
Date:
Sat Oct 17 19:02:41 2015 +0000
Revision:
14:8620dceb3ba2
Parent:
13:b22fd9c4fbb4
Child:
15:d6edd100d7fe
added potentiometer and ellipse and scaling functions

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