blue mbed code for the BNO055 imu from adafruit
Dependencies: BNO055 MODSERIAL mbed
Fork of bmbed_lidar_belt by
main.cpp@4:c53761262e3f, 2015-09-25 (annotated)
- Committer:
- rkk
- Date:
- Fri Sep 25 20:19:50 2015 +0000
- Revision:
- 4:c53761262e3f
- Parent:
- 3:a0ccaf565e8d
- Child:
- 5:d4ed744beea2
changed baud rate
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 | 0:ce4f790399d9 | 3 | |
baraki | 0:ce4f790399d9 | 4 | #define PC_BAUD 9600 |
rkk | 4:c53761262e3f | 5 | #define BT_BAUD 115200 |
baraki | 0:ce4f790399d9 | 6 | #define TX_PIN p13 |
baraki | 0:ce4f790399d9 | 7 | #define RX_PIN p14 |
baraki | 0:ce4f790399d9 | 8 | #define SDA_PIN p9 //SDA pin on LPC1768 |
baraki | 0:ce4f790399d9 | 9 | #define SCL_PIN p10 //SCL pin on LPC1768 |
baraki | 0:ce4f790399d9 | 10 | |
baraki | 0:ce4f790399d9 | 11 | I2C sensor(SDA_PIN, SCL_PIN); //Define LIDAR Lite sensor 1 |
baraki | 0:ce4f790399d9 | 12 | MODSERIAL bt(TX_PIN, RX_PIN); |
baraki | 0:ce4f790399d9 | 13 | MODSERIAL pc(USBTX,USBRX); |
baraki | 0:ce4f790399d9 | 14 | |
baraki | 0:ce4f790399d9 | 15 | bool newline_detected = false; |
baraki | 0:ce4f790399d9 | 16 | bool newline_sent = false; |
baraki | 0:ce4f790399d9 | 17 | |
baraki | 0:ce4f790399d9 | 18 | // Called everytime a new character goes into |
baraki | 0:ce4f790399d9 | 19 | // the RX buffer. Test that character for \n |
baraki | 0:ce4f790399d9 | 20 | // Note, rxGetLastChar() gets the last char that |
baraki | 0:ce4f790399d9 | 21 | // we received but it does NOT remove it from |
baraki | 0:ce4f790399d9 | 22 | // the RX buffer. |
baraki | 0:ce4f790399d9 | 23 | void rxCallback(MODSERIAL_IRQ_INFO *q) |
baraki | 0:ce4f790399d9 | 24 | { |
baraki | 0:ce4f790399d9 | 25 | MODSERIAL *serial = q->serial; |
baraki | 0:ce4f790399d9 | 26 | if ( serial->rxGetLastChar() == '\n') { |
baraki | 0:ce4f790399d9 | 27 | newline_detected = true; |
baraki | 0:ce4f790399d9 | 28 | } |
baraki | 0:ce4f790399d9 | 29 | |
baraki | 0:ce4f790399d9 | 30 | } |
baraki | 0:ce4f790399d9 | 31 | |
baraki | 0:ce4f790399d9 | 32 | void txCallback(MODSERIAL_IRQ_INFO *q) |
baraki | 0:ce4f790399d9 | 33 | { |
baraki | 0:ce4f790399d9 | 34 | MODSERIAL *serial = q->serial; |
baraki | 0:ce4f790399d9 | 35 | if ( serial->txGetLastChar() == '\0') { |
baraki | 0:ce4f790399d9 | 36 | newline_sent = true; |
baraki | 0:ce4f790399d9 | 37 | } |
baraki | 0:ce4f790399d9 | 38 | } |
baraki | 0:ce4f790399d9 | 39 | |
baraki | 0:ce4f790399d9 | 40 | int main() |
baraki | 0:ce4f790399d9 | 41 | { |
baraki | 0:ce4f790399d9 | 42 | pc.baud(PC_BAUD); |
baraki | 0:ce4f790399d9 | 43 | bt.baud(BT_BAUD); |
baraki | 0:ce4f790399d9 | 44 | pc.attach(&rxCallback, MODSERIAL::RxIrq); |
baraki | 0:ce4f790399d9 | 45 | bt.attach(&txCallback, MODSERIAL::TxIrq); |
baraki | 0:ce4f790399d9 | 46 | |
baraki | 0:ce4f790399d9 | 47 | sensor.frequency(100000); |
baraki | 0:ce4f790399d9 | 48 | |
baraki | 0:ce4f790399d9 | 49 | char sendData[1] = {0x00}; |
baraki | 0:ce4f790399d9 | 50 | |
baraki | 0:ce4f790399d9 | 51 | int addresses[7]; |
baraki | 0:ce4f790399d9 | 52 | addresses[0] = 0x60; //0x60 |
baraki | 0:ce4f790399d9 | 53 | addresses[1] = 0x64; //0x64 |
baraki | 0:ce4f790399d9 | 54 | addresses[2] = 0x68; //middle |
baraki | 0:ce4f790399d9 | 55 | addresses[3] = 0x6C; |
baraki | 0:ce4f790399d9 | 56 | addresses[4] = 0x70; |
baraki | 0:ce4f790399d9 | 57 | addresses[5] = 0x80; //up |
baraki | 0:ce4f790399d9 | 58 | addresses[6] = 0x84; //down |
baraki | 0:ce4f790399d9 | 59 | |
baraki | 0:ce4f790399d9 | 60 | uint8_t pulses[7] = {0}; |
baraki | 0:ce4f790399d9 | 61 | uint8_t intensity[7] = {0}; |
baraki | 0:ce4f790399d9 | 62 | |
baraki | 0:ce4f790399d9 | 63 | char btData[12] = {'a','b','c','d','e','f','g','\n','\0'}; |
baraki | 0:ce4f790399d9 | 64 | |
baraki | 0:ce4f790399d9 | 65 | //calibrate down sensor |
baraki | 0:ce4f790399d9 | 66 | int down_cal = 0; |
baraki | 0:ce4f790399d9 | 67 | |
baraki | 0:ce4f790399d9 | 68 | unsigned int i = 0; |
baraki | 0:ce4f790399d9 | 69 | int count = 0; //for calibration |
baraki | 0:ce4f790399d9 | 70 | int count2 = 0;//for averaging |
baraki | 0:ce4f790399d9 | 71 | int differenceAvgSum = 0; |
baraki | 0:ce4f790399d9 | 72 | int moving_ave[5] = {0}; |
baraki | 0:ce4f790399d9 | 73 | while (1) { |
baraki | 0:ce4f790399d9 | 74 | for(int k=0; k<5; k++) { |
baraki | 0:ce4f790399d9 | 75 | char receiveData[3] = {0}; |
baraki | 0:ce4f790399d9 | 76 | if(sensor.write(addresses[k], sendData, 1)){ |
baraki | 1:5b1d88d69aa2 | 77 | //pc.printf("writing to sensor %d failed\n", k); |
baraki | 0:ce4f790399d9 | 78 | } |
baraki | 0:ce4f790399d9 | 79 | //write ---> 0 on success, 1 on failure |
baraki | 0:ce4f790399d9 | 80 | i = 0; |
baraki | 2:ec53792aef80 | 81 | while(sensor.read(addresses[k], receiveData, 3) && i < 10) { |
baraki | 0:ce4f790399d9 | 82 | i++; |
baraki | 1:5b1d88d69aa2 | 83 | //pc.printf("reading from sensor %d failed\n",k); |
baraki | 1:5b1d88d69aa2 | 84 | } |
baraki | 0:ce4f790399d9 | 85 | //while(!twi_master_transfer(addresses[k], sendData, 1, TWI_ISSUE_STOP)){;} |
baraki | 0:ce4f790399d9 | 86 | //while(!twi_master_transfer(addresses[k] + 1, receiveData, 3, TWI_ISSUE_STOP)){;} |
baraki | 0:ce4f790399d9 | 87 | int distance = ((int)receiveData[0]<<8 )+ (int)receiveData[1]; |
baraki | 0:ce4f790399d9 | 88 | if(distance == 0){ |
baraki | 0:ce4f790399d9 | 89 | pulses[k] = 1; |
baraki | 0:ce4f790399d9 | 90 | intensity[k] = 0; |
baraki | 0:ce4f790399d9 | 91 | } |
baraki | 0:ce4f790399d9 | 92 | if(distance > 0 && distance < 650) { |
baraki | 0:ce4f790399d9 | 93 | pulses[k] = 5; |
baraki | 0:ce4f790399d9 | 94 | intensity[k] = 7; |
baraki | 0:ce4f790399d9 | 95 | } else if(distance >= 650 && distance < 900) { |
baraki | 0:ce4f790399d9 | 96 | pulses[k] = 4; |
baraki | 0:ce4f790399d9 | 97 | intensity[k] = 6; |
baraki | 0:ce4f790399d9 | 98 | } else if(distance >= 900 && distance < 1350) { |
baraki | 0:ce4f790399d9 | 99 | pulses[k] = 3; |
baraki | 0:ce4f790399d9 | 100 | intensity[k] = 5; |
baraki | 0:ce4f790399d9 | 101 | } else if(distance >= 1350 && distance < 1850) { |
baraki | 0:ce4f790399d9 | 102 | pulses[k] = 2; |
baraki | 0:ce4f790399d9 | 103 | intensity[k] = 2; |
baraki | 0:ce4f790399d9 | 104 | } else if(distance >= 1800) { |
baraki | 0:ce4f790399d9 | 105 | pulses[k] = 1; |
baraki | 0:ce4f790399d9 | 106 | intensity[k] = 0; |
baraki | 0:ce4f790399d9 | 107 | } |
baraki | 3:a0ccaf565e8d | 108 | //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",k,pulses[k],intensity[k]); |
baraki | 0:ce4f790399d9 | 109 | } |
baraki | 0:ce4f790399d9 | 110 | |
baraki | 0:ce4f790399d9 | 111 | //find UP distance |
baraki | 0:ce4f790399d9 | 112 | char receiveData2[3] = {0}; |
baraki | 0:ce4f790399d9 | 113 | sensor.write(addresses[5], sendData, 1); |
baraki | 0:ce4f790399d9 | 114 | i = 0; |
baraki | 2:ec53792aef80 | 115 | while(sensor.read(addresses[5]+1, receiveData2, 3) && i < 10){ |
baraki | 0:ce4f790399d9 | 116 | i++;} |
baraki | 0:ce4f790399d9 | 117 | int distance2 = (receiveData2[0]<<8 )+ receiveData2[1]; |
baraki | 0:ce4f790399d9 | 118 | if(distance2 >= 500 && distance2 < 1000) { |
baraki | 0:ce4f790399d9 | 119 | pulses[5] = 5; |
baraki | 0:ce4f790399d9 | 120 | intensity[5] = 7; |
baraki | 0:ce4f790399d9 | 121 | } else { |
baraki | 0:ce4f790399d9 | 122 | pulses[5] = 1; |
baraki | 0:ce4f790399d9 | 123 | intensity[5] = 0; |
baraki | 0:ce4f790399d9 | 124 | } |
baraki | 0:ce4f790399d9 | 125 | |
baraki | 0:ce4f790399d9 | 126 | //find DOWN distance |
baraki | 0:ce4f790399d9 | 127 | char receiveData3[3] = {0}; |
baraki | 2:ec53792aef80 | 128 | i = 0; |
baraki | 0:ce4f790399d9 | 129 | sensor.write(addresses[6], sendData, 1); |
baraki | 2:ec53792aef80 | 130 | while(sensor.read(addresses[6]+1, receiveData3, 3) && i < 10){ |
baraki | 0:ce4f790399d9 | 131 | i++;} |
baraki | 0:ce4f790399d9 | 132 | int distance3 = (receiveData3[0]<<8 )+ receiveData3[1]; |
baraki | 3:a0ccaf565e8d | 133 | if(count > 200) { |
baraki | 0:ce4f790399d9 | 134 | int difference = abs(down_cal - distance3); |
baraki | 0:ce4f790399d9 | 135 | differenceAvgSum = differenceAvgSum - moving_ave[count2]; |
baraki | 0:ce4f790399d9 | 136 | moving_ave[count2] = difference; |
baraki | 0:ce4f790399d9 | 137 | differenceAvgSum = differenceAvgSum + difference; |
baraki | 0:ce4f790399d9 | 138 | count2 = count2 + 1; |
baraki | 0:ce4f790399d9 | 139 | int ave = (int)(differenceAvgSum/5); |
baraki | 3:a0ccaf565e8d | 140 | //pc.printf("down_cal: %d \t diff: %d \t distance: %d\n",down_cal, ave, distance3); |
baraki | 0:ce4f790399d9 | 141 | if(ave >= 160) { |
baraki | 0:ce4f790399d9 | 142 | pulses[6] = 5; |
baraki | 0:ce4f790399d9 | 143 | intensity[6] = 7; |
baraki | 0:ce4f790399d9 | 144 | } else { |
baraki | 0:ce4f790399d9 | 145 | pulses[6] = 1; |
baraki | 0:ce4f790399d9 | 146 | intensity[6] = 0; |
baraki | 0:ce4f790399d9 | 147 | } |
baraki | 0:ce4f790399d9 | 148 | |
baraki | 0:ce4f790399d9 | 149 | if(count2 >4) { |
baraki | 0:ce4f790399d9 | 150 | count2 = 0; |
baraki | 0:ce4f790399d9 | 151 | } |
baraki | 0:ce4f790399d9 | 152 | } else { |
baraki | 0:ce4f790399d9 | 153 | down_cal = distance3; |
baraki | 0:ce4f790399d9 | 154 | count = count+1; |
baraki | 0:ce4f790399d9 | 155 | } |
baraki | 3:a0ccaf565e8d | 156 | //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",6,pulses[6],intensity[6]); |
baraki | 0:ce4f790399d9 | 157 | |
baraki | 1:5b1d88d69aa2 | 158 | //pc.printf("about to send data\n"); |
baraki | 0:ce4f790399d9 | 159 | btData[0] = (pulses[0] << 5) | (intensity[0] << 2); |
baraki | 0:ce4f790399d9 | 160 | btData[1] = (pulses[1] << 4) | (intensity[1] << 1); |
baraki | 0:ce4f790399d9 | 161 | btData[2] = (pulses[2] << 3) | (intensity[2]); |
baraki | 0:ce4f790399d9 | 162 | btData[3] = (pulses[3] << 2) | (intensity[3] >> 1); |
baraki | 0:ce4f790399d9 | 163 | btData[4] = (intensity[3] << 7) | (pulses[4] << 1) | (intensity[4] >> 2); |
baraki | 0:ce4f790399d9 | 164 | btData[5] = (intensity[4] << 6) | (0x3); |
baraki | 0:ce4f790399d9 | 165 | btData[6] = (pulses[5] << 5) | (intensity[5] << 2); |
baraki | 0:ce4f790399d9 | 166 | btData[7] = (pulses[6] << 5) | (intensity[6] << 2); |
baraki | 0:ce4f790399d9 | 167 | btData[8] = '\0'; |
baraki | 0:ce4f790399d9 | 168 | for(int j=0;j<9;j++){ |
baraki | 1:5b1d88d69aa2 | 169 | if(bt.writeable()) |
baraki | 1:5b1d88d69aa2 | 170 | bt.putc(btData[j]); |
baraki | 1:5b1d88d69aa2 | 171 | //wait(0.001); |
baraki | 0:ce4f790399d9 | 172 | } |
baraki | 3:a0ccaf565e8d | 173 | wait(0.05); |
baraki | 1:5b1d88d69aa2 | 174 | //pc.printf("finished sending data\n"); |
baraki | 0:ce4f790399d9 | 175 | //ble_uart_c_write_string(&m_ble_uart_c, (uint8_t *)btData, 9); |
baraki | 0:ce4f790399d9 | 176 | } |
baraki | 0:ce4f790399d9 | 177 | } |