Custom I2C Device Library Communications Issues

Hi everyone,

I’m having some issues with a custom library I am creating to add a custom I2C sensor (a magnetic encoder, AS 5600) to the existing code so I can make measurements during flight. I’m not sure if I ought to be posting this here or under the dev forum, but I don’t have posting privileges for the dev forum anyways.

The issue I am having is in actually communicating with the I2C device. I have successfully written and built the new library, added it to the waf, and uploaded it to the pixhawk4 mini which I am using for this project. The library is meant to get the angle measurement from the AS5600 sensor and then write it to the onboard flight log. I know that my code is running on the device because it is creating the log files, however it is recording a value of -1, which I programmed the software to do when the I2C data transfer is unsuccessful.

I have successfully tested the sensor using an Arduino, and checked my wire connections multiple times to ensure I haven’t gotten them wrong. I am using the uart/I2C port on the PH4-mini to supply the 5V and ground connections, as well as using the sda2 and scl2 pins for communications.

My first instinct was the bus number was wrong, as I was setting it manually, but I have now tried all the bus numbers from 0-3 (all available I2C bus numbers for the PH4-mini) with the same result.

I believe this to be a software problem and not a hardware issue, so I’ve attached my library files below. To use the library I simply create an instance of the AS_5600 class in the plane.h file and then created a function in Ardupilot.cpp which calls the .getRawAngle() function, and added it to the scheduler.

Thanks in advance for any help anyone can provide!

AS5600_AOA.cpp (7.6 KB) AS5600_AOA.h (1.9 KB)

We’re quite happy to help developers out on our Discord server - just pop something into #general if no other channel seems suitable.

I can’t immediately see the bug in your program.

I can see several things wrong with it, however :slight_smile:

Modelling it more after the existing i2c drivers might be the way forward; you’ve hopped in at the wrong layer by poking ChibiOS directly; should be going via the hal layer.

There are several other bits and pieces you really, really need to do before flying this sort of patch - creating a thread to do the regular i2c interactions, for example.

Several of the AP_Airspeed drivers are quite straight forward. I suggest you study one of those (the DLVR sensor, perhaps?)

One thing you could also do is to write a simulator for your AoA sensor; they’re getting easier to do over time. There are many already in libraries/SITL/. This will allow you to apply traditional debugging techniques to get your sensor working, without needing to flash it to hardware all the time.

Awesome thanks for the response, I’ll head over to the discord server :slightly_smiling_face:

Hi corem, I have the same requirement as you. But I have no idea how to add a new sensor to Ardupilot. Can you tell me the details about how to modify the code to enable a new sensor, or can you tell me where can I find the solutions? Thanks in advance for your help!