Incorporating external sensors to Pixhawk via I2C

Hi guys.
I want to connect some extra sensors to the Pixhawk for monitoring some gases.
Doing some research I´ve found this post http://diydrones.com/forum/topics/incporating-sensor-data-into-the-apm-log-files.
That post is a little old, so I´m try to adapt that drivers provided by James Overington to the pixhawk convention with the information found in http://ardupilot.org/dev/docs/code-overview-sensor-drivers.html. But I keep finding problems with the communication on the I2C. I´m attaching my drivers and example programs for the Pixhawk and the ArduinoSlave.
Any help will be great.

I2C_Sensor.zip (5.8 KB)

I have gotten an Arduino to talk via I2C to a Pixhawk before. I don’t know what’s specifically wrong with yours, but here’s some things that (for some reason) I did differently.

Your requestEvent has:

void requestEvent()
{
Wire.write((uint8_t *)&data,sizeof(data)); // respond with message of 24 bytes
} // as expected by master

In mine, I instead used a memcpy():

void request_handler() {
byte i2c_data[8];
memcpy(i2c_data, data, 8);
Wire.write(i2c_data, 2*sizeof(float));
}

I was passing 2 floats (8 bytes) whereas you’re passing 1 byte, but the memcpy might still be necessary? I don’t remember.

Another thing I did differently was NOT use hex-notation in my Arduino code. I think it shouldn’t matter, but try changing your line

Wire.begin(0x48);
to be instead the decimal equivalent
Wire.begin(72);

Finally, I don’t know enough to know if James Overington’s drivers will work or not… I don’t understand the hal i2c_mgr and semaphores and such well enough myself. It might take a lot of work, but if I were you, I’d copy-and-modify something currently in the ArduPlane source code.

Maybe someone else can be more useful on helping with that?

Thanks, it´s working now.
My problem was not using memcpy in the requestEvent, with that change it start working perfect.

I keep using hex-notation in my Arduino code and it works perfect.

Thanks and Merry Christmas !

1 Like