blue mbed code for the BNO055 imu from adafruit

Dependencies:   BNO055 MODSERIAL mbed

Fork of bmbed_lidar_belt by sensory_array

main.cpp

Committer:
rkk
Date:
2015-10-03
Revision:
5:d4ed744beea2
Parent:
4:c53761262e3f
Child:
6:9ae0e867efed

File content as of revision 5:d4ed744beea2:

#include "mbed.h"
#include "MODSERIAL.h"

//Initial LONG Range Settings
#define RANGE_01 750
#define RANGE_02 1100
#define RANGE_03 1700
#define RANGE_04 2750
#define UP_MIN 600
#define UP_MAX 1300
#define DOWN_DIFF 500

// Mid range settings
//#define RANGE_01 450
//#define RANGE_02 750
//#define RANGE_03 1150
//#define RANGE_04 1550
//#define UP_MIN 600
//#define UP_MAX 1300
//#define DOWN_DIFF 260

// Short range settings
//#define RANGE_01 400
//#define RANGE_02 650
//#define RANGE_03 1000
//#define RANGE_04 1350
//#define UP_MIN 600
//#define UP_MAX 1300
//#define DOWN_DIFF 260


#define PC_BAUD 9600
#define BT_BAUD 115200
#define TX_PIN p13
#define RX_PIN p14
#define SDA_PIN p9   //SDA pin on LPC1768
#define SCL_PIN p10  //SCL pin on LPC1768



I2C sensor(SDA_PIN, SCL_PIN); //Define LIDAR Lite sensor 1
MODSERIAL bt(TX_PIN, RX_PIN);
MODSERIAL pc(USBTX,USBRX);

bool newline_detected = false;
bool newline_sent = false;

// Called everytime a new character goes into
// the RX buffer. Test that character for \n
// Note, rxGetLastChar() gets the last char that
// we received but it does NOT remove it from
// the RX buffer.
void rxCallback(MODSERIAL_IRQ_INFO *q)
{
    MODSERIAL *serial = q->serial;
    if ( serial->rxGetLastChar() == '\n') {
        newline_detected = true;
    }

}

void txCallback(MODSERIAL_IRQ_INFO *q)
{
    MODSERIAL *serial = q->serial;
    if ( serial->txGetLastChar() == '\0') {
        newline_sent = true;
    }
}

int main()
{
    pc.baud(PC_BAUD);
    bt.baud(BT_BAUD);
    pc.attach(&rxCallback, MODSERIAL::RxIrq);
    bt.attach(&txCallback, MODSERIAL::TxIrq);
    
    sensor.frequency(100000);

    char sendData[1] = {0x00};

    int addresses[7];
    addresses[0] = 0x60; //0x60
    addresses[1] = 0x64; //0x64
    addresses[2] = 0x68; //middle
    addresses[3] = 0x6C;
    addresses[4] = 0x70;
    addresses[5] = 0x80; //up
    addresses[6] = 0x84; //down

    uint8_t pulses[7] = {0};
    uint8_t intensity[7] = {0};

    char btData[12] = {'a','b','c','d','e','f','g','\n','\0'};

    //calibrate down sensor
    int down_cal = 0;
    
    unsigned int i = 0;
    int count = 0; //for calibration
    int count2 = 0;//for averaging
    int differenceAvgSum = 0;
    int moving_ave[5] = {0};
    while (1) {
        for(int k=0; k<5; k++) {
            char receiveData[3] = {0};
            if(sensor.write(addresses[k], sendData, 1)){
                //pc.printf("writing to sensor %d failed\n", k);
                }
            //write ---> 0 on success, 1 on failure
            i = 0;
            while(sensor.read(addresses[k], receiveData, 3) && i < 10) {
                i++;
                //pc.printf("reading from sensor %d failed\n",k);
                }
                //while(!twi_master_transfer(addresses[k], sendData, 1, TWI_ISSUE_STOP)){;}
                //while(!twi_master_transfer(addresses[k] + 1, receiveData, 3, TWI_ISSUE_STOP)){;}
                int distance = ((int)receiveData[0]<<8 )+ (int)receiveData[1];
                if(distance == 0){
                  pulses[k] = 1;
                  intensity[k] = 0;
                }
                if(distance > 0 && distance < RANGE_01) {
                    pulses[k] = 5;
                    intensity[k] = 7;
                } else if(distance >= RANGE_01 && distance < RANGE_02) {
                    pulses[k] = 4;
                    intensity[k] = 6;
                } else if(distance >= RANGE_02 && distance < RANGE_03) {
                    pulses[k] = 3;
                    intensity[k] = 5;
                } else if(distance >= RANGE_03 && distance < RANGE_04) {
                    pulses[k] = 2;
                    intensity[k] = 2;
                } else if(distance >= RANGE_04) {
                    pulses[k] = 1;
                    intensity[k] = 0;
                }
            //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",k,pulses[k],intensity[k]);
        }

        //find UP distance
        char receiveData2[3] = {0};
        sensor.write(addresses[5], sendData, 1);
        i = 0;
        while(sensor.read(addresses[5]+1, receiveData2, 3) && i < 10){
            i++;}
        int distance2 = (receiveData2[0]<<8 )+ receiveData2[1];
        if(distance2 >= UP_MIN && distance2 < UP_MAX) {
            pulses[5] = 1; ///5
            intensity[5] = 0;   ///7
        } else {
            pulses[5] = 1;
            intensity[5] = 0;
        }

        //find DOWN distance
        char receiveData3[3] = {0};
        i = 0;
        sensor.write(addresses[6], sendData, 1);
        while(sensor.read(addresses[6]+1, receiveData3, 3) && i < 10){
            i++;}
        int distance3 = (receiveData3[0]<<8 )+ receiveData3[1];
        if(count > 200) {
            int difference = abs(down_cal - distance3);
            differenceAvgSum = differenceAvgSum - moving_ave[count2];
            moving_ave[count2] = difference;
            differenceAvgSum = differenceAvgSum + difference;
            count2 = count2 + 1;
            int ave = (int)(differenceAvgSum/5);
            //pc.printf("down_cal: %d \t diff: %d \t distance: %d\n",down_cal, ave, distance3);
            if(ave >= DOWN_DIFF) {
                pulses[6] = 1; //turned OFFFFFF
                intensity[6] = 0;
            } else {
                pulses[6] = 1; 
                intensity[6] = 0;
            }

            if(count2 >4) {
                count2 = 0;
            }
        } else {
            down_cal = distance3;
            count = count+1;
        }
        //pc.printf("num: %d \t pulses: %d \t intensity: %d \n",6,pulses[6],intensity[6]);

        //pc.printf("about to send data\n");
        btData[0] = (pulses[0] << 5) | (intensity[0] << 2);
        btData[1] = (pulses[1] << 4) | (intensity[1] << 1);
        btData[2] = (pulses[2] << 3) | (intensity[2]);
        btData[3] = (pulses[3] << 2) | (intensity[3] >> 1);
        btData[4] = (intensity[3] << 7) | (pulses[4] << 1) | (intensity[4] >> 2);
        btData[5] = (intensity[4] << 6) | (0x3);
        btData[6] = (pulses[5] << 5) | (intensity[5] << 2);
        btData[7] = (pulses[6] << 5) | (intensity[6] << 2);
        btData[8] = '\0';
        for(int j=0;j<9;j++){
            if(bt.writeable())
                bt.putc(btData[j]);
            //wait(0.001);
        }
        wait(0.05);
        //pc.printf("finished sending data\n");
        //ble_uart_c_write_string(&m_ble_uart_c, (uint8_t *)btData, 9);
    }
}