Hello,
I am trying to establish I2C commnication between APM 2.5 with firmware Arducopter 3.1.5 and Arduino Pro Mini. I am taking sonar readings at the Pro Mini side however I just can’t send the distance data successfuly to APM. I have defined each distance variable to be of type short(2 bytes) and send these distance data to APM as a packet of 8 bytes(4 distance readings). Some of the bits are lost after transmission and I am not getting meaningful data. I cannot manage to control when the I2C starts and stops reading at the APM side. How can I write the code at the APM to successfully read distance data?
I am sharing the code below:
Code:
//
// SONAR DATA ACQUISITION FROM ARDUPRO MINI
//
uint8_t stat = hal.i2c->read(address, 8, data);
if (stat == 0){
//SONAR#1
if (data[0] == 0)
distance1 = (int)data[1];
else if (data[0] == 1)
distance1 = (int)data[1] + 255;
else if (data[0] == 2)
distance1 = (int)data[1] + 255 + 255;
else distance1 = 0;
//SONAR#2
if (data[2] == 0)
distance2 = (int)data[3];
else if (data[2] == 1)
distance2 = (int)data[3] + 255;
else if (data[2] == 2)
distance2 = (int)data[3] + 255 + 255;
else distance2 = 0;
//SONAR#3
if (data[4] == 0)
distance3 = (int)data[5];
else if (data[4] == 1)
distance3 = (int)data[5] + 255;
else if (data[4] == 2)
distance3 = (int)data[5] + 255 + 255;
else distance3 = 0;
//SONAR#4
if (data[6] == 0)
distance4 = (int)data[7];
else if (data[6] == 1)
distance4 = (int)data[7] + 255;
else if (data[6] == 2)
distance4 = (int)data[7] + 255 + 255;
else distance4 = 0;
hal.console->printf_P(PSTR(" DISTANCE1: %d DISTANCE2: %d DISTANCE3: %d DISTANCE4: %d\n"), distance1, distance2, distance3, distance4);
Thank you