I am continuing my mavlink message journey and wanted to learn how the pixhawk or QGC does the calibration. After seeing this post here and @EliotBR 's response, I tried to calibrate the pixhawk by sending a command long message with the MAV_CMD_ACCELCAL_VEHICLE_POS command from the ardupilot dialect according to the mavlink website here.
But, when I tried this with my pixhawk, I couldn’t get the calibration to start. I looked at the command_ack messages to see what was being sent and I am getting a Mav_result response of 4, meaning the command is correct, but it failed nonetheless. What could be the reason for this?
Given the code can be confusing to navigate, I’ll try to lay out my own steps when finding the functionality. Hopefully that can serve as an example of the reasoning process for anyone looking to find other similar information in the source
Accelerometer Calibration
The MAV_CMD_ACCELCAL_VEHICLE_POS command says it’s “Used when doing accelerometer calibration.” That’s a bit ambiguous, but given it’s not working for you then the “when doing” likely implies the calibration has to be started with a different message, and the vehicle pos command is just for stepping through the positions during the calibration.
From the QGC source file that was linked to in the other post,
Looking at the nearby commands in the message set comes up with the MAV_CMD_DO_START_MAG_CAL, which is presumably used to start magnetometer (compass) calibration (so we have something to look for).
The calibrateCompass method (from step 1) hands off logic to the _mavCommandResult method in the same file,
where you can see that MAV_CMD_DO_START_MAG_CAL is indeed used if onboard calibration is supported,
APMCompassCal.cc defines the function we’re after, as well as the various calculations that are used to perform a compass calibration externally.
The code there is quite convoluted, so I’m not certain how it tracks the calibration state (if it even does - it seems like it might just communicate via string/text messages).
Focusing back on the more promising approach, to confirm whether the MAG_CAL messages work I tried following the QGC approach from step 3. - sending a CANCEL_MAG_CAL command and seeing what kind of MAV_RESULT the command is acknowledged with (as per the Command Protocol):
On ArduSub 4.0.3 (the latest stable) that worked fine, so onboard calibration should be available, and you shouldn’t need to worry about implementing external calibration
That said, unless you’re trying to make a full QGC replacement it’s likely easier to calibrate sensors via QGC instead of directly over MAVLink, because it already has a useful interface, and related functionality in the same place (e.g. the “autopilot rotation” when about to calibrate accelerometer/compass).
As an aside, the software team have suggested an alternative and potentially simpler approach to finding out this information (and its direct usage in Pymavlink) would be to look at mavproxy’s implementation instead. This file was the first result in a search for “calibration”.