2014-02-07 01:31:34 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
/*
|
|
|
|
compass/motor interference calibration
|
|
|
|
*/
|
|
|
|
|
|
|
|
// setup_compassmot - sets compass's motor interference parameters
|
2015-05-29 23:12:49 -03:00
|
|
|
uint8_t Copter::mavlink_compassmot(mavlink_channel_t chan)
|
2014-02-07 01:31:34 -04:00
|
|
|
{
|
2015-07-15 03:08:13 -03:00
|
|
|
#if FRAME_CONFIG == HELI_FRAME
|
|
|
|
// compassmot not implemented for tradheli
|
|
|
|
return 1;
|
|
|
|
#else
|
2014-02-07 01:31:34 -04:00
|
|
|
int8_t comp_type; // throttle or current based compensation
|
2014-07-22 04:31:45 -03:00
|
|
|
Vector3f compass_base[COMPASS_MAX_INSTANCES]; // compass vector when throttle is zero
|
|
|
|
Vector3f motor_impact[COMPASS_MAX_INSTANCES]; // impact of motors on compass vector
|
|
|
|
Vector3f motor_impact_scaled[COMPASS_MAX_INSTANCES]; // impact of motors on compass vector scaled with throttle
|
|
|
|
Vector3f motor_compensation[COMPASS_MAX_INSTANCES]; // final compensation to be stored to eeprom
|
2014-02-07 01:31:34 -04:00
|
|
|
float throttle_pct; // throttle as a percentage 0.0 ~ 1.0
|
|
|
|
float throttle_pct_max = 0.0f; // maximum throttle reached (as a percentage 0~1.0)
|
|
|
|
float current_amps_max = 0.0f; // maximum current reached
|
2014-07-22 04:31:45 -03:00
|
|
|
float interference_pct[COMPASS_MAX_INSTANCES]; // interference as a percentage of total mag field (for reporting purposes only)
|
2014-02-07 01:31:34 -04:00
|
|
|
uint32_t last_run_time;
|
|
|
|
uint32_t last_send_time;
|
|
|
|
bool updated = false; // have we updated the compensation vector at least once
|
|
|
|
uint8_t command_ack_start = command_ack_counter;
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// exit immediately if we are already in compassmot
|
|
|
|
if (ap.compass_mot) {
|
|
|
|
// ignore restart messages
|
|
|
|
return 1;
|
2014-02-07 01:31:34 -04:00
|
|
|
}else{
|
2014-02-19 00:29:36 -04:00
|
|
|
ap.compass_mot = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check compass is enabled
|
|
|
|
if (!g.compass_enabled) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("compass disabled\n"));
|
2014-02-19 00:29:36 -04:00
|
|
|
ap.compass_mot = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check compass health
|
|
|
|
compass.read();
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
if (!compass.healthy(i)) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("check compass"));
|
2014-07-22 04:31:45 -03:00
|
|
|
ap.compass_mot = false;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-07 01:31:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if radio is calibrated
|
|
|
|
pre_arm_rc_checks();
|
|
|
|
if (!ap.pre_arm_rc_check) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("RC not calibrated"));
|
2014-02-19 00:29:36 -04:00
|
|
|
ap.compass_mot = false;
|
2014-02-07 01:31:34 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// check throttle is at zero
|
|
|
|
read_radio();
|
2015-05-11 23:24:13 -03:00
|
|
|
if (channel_throttle->control_in != 0) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("thr not zero"));
|
2014-02-19 00:29:36 -04:00
|
|
|
ap.compass_mot = false;
|
2014-02-07 01:31:34 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// check we are landed
|
|
|
|
if (!ap.land_complete) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("Not landed"));
|
2014-02-19 00:29:36 -04:00
|
|
|
ap.compass_mot = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable cpu failsafe
|
|
|
|
failsafe_disable();
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
// initialise compass
|
|
|
|
init_compass();
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// default compensation type to use current if possible
|
2014-12-03 01:32:44 -04:00
|
|
|
if (battery.has_current()) {
|
2014-02-19 00:29:36 -04:00
|
|
|
comp_type = AP_COMPASS_MOT_COMP_CURRENT;
|
|
|
|
}else{
|
|
|
|
comp_type = AP_COMPASS_MOT_COMP_THROTTLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send back initial ACK
|
|
|
|
mavlink_msg_command_ack_send(chan, MAV_CMD_PREFLIGHT_CALIBRATION,0);
|
|
|
|
|
|
|
|
// flash leds
|
|
|
|
AP_Notify::flags.esc_calibration = true;
|
2014-02-07 01:31:34 -04:00
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// warn user we are starting calibration
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("STARTING CALIBRATION"));
|
2014-02-07 01:31:34 -04:00
|
|
|
|
|
|
|
// inform what type of compensation we are attempting
|
2014-02-19 00:29:36 -04:00
|
|
|
if (comp_type == AP_COMPASS_MOT_COMP_CURRENT) {
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("CURRENT"));
|
2014-02-07 01:31:34 -04:00
|
|
|
} else{
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("THROTTLE"));
|
2014-02-07 01:31:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// disable throttle and battery failsafe
|
|
|
|
g.failsafe_throttle = FS_THR_DISABLED;
|
|
|
|
g.failsafe_battery_enabled = FS_BATT_DISABLED;
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// disable motor compensation
|
|
|
|
compass.motor_compensation_type(AP_COMPASS_MOT_COMP_DISABLED);
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
compass.set_motor_compensation(i, Vector3f(0,0,0));
|
|
|
|
}
|
2014-02-07 01:31:34 -04:00
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// get initial compass readings
|
2014-02-07 01:31:34 -04:00
|
|
|
last_run_time = millis();
|
2014-02-19 00:29:36 -04:00
|
|
|
while ( millis() - last_run_time < 500 ) {
|
2014-02-07 01:31:34 -04:00
|
|
|
compass.accumulate();
|
|
|
|
}
|
|
|
|
compass.read();
|
|
|
|
|
|
|
|
// store initial x,y,z compass values
|
2014-07-22 04:31:45 -03:00
|
|
|
// initialise interference percentage
|
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
2015-09-28 16:57:03 -03:00
|
|
|
compass_base[i] = compass.get_field(i);
|
2014-07-22 04:31:45 -03:00
|
|
|
interference_pct[i] = 0.0f;
|
|
|
|
}
|
2014-02-07 01:31:34 -04:00
|
|
|
|
|
|
|
// enable motors and pass through throttle
|
|
|
|
init_rc_out();
|
2015-04-22 15:59:34 -03:00
|
|
|
enable_motor_output();
|
2014-02-07 01:31:34 -04:00
|
|
|
motors.armed(true);
|
|
|
|
|
|
|
|
// initialise run time
|
|
|
|
last_run_time = millis();
|
|
|
|
last_send_time = millis();
|
|
|
|
|
|
|
|
// main run while there is no user input and the compass is healthy
|
2014-07-22 04:31:45 -03:00
|
|
|
while (command_ack_start == command_ack_counter && compass.healthy(compass.get_primary()) && motors.armed()) {
|
2014-02-07 01:31:34 -04:00
|
|
|
// 50hz loop
|
2014-02-19 00:29:36 -04:00
|
|
|
if (millis() - last_run_time < 20) {
|
2014-02-07 01:31:34 -04:00
|
|
|
// grab some compass values
|
|
|
|
compass.accumulate();
|
|
|
|
hal.scheduler->delay(5);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
last_run_time = millis();
|
|
|
|
|
|
|
|
// read radio input
|
|
|
|
read_radio();
|
|
|
|
|
|
|
|
// pass through throttle to motors
|
2015-05-11 23:24:13 -03:00
|
|
|
motors.throttle_pass_through(channel_throttle->radio_in);
|
2014-02-07 01:31:34 -04:00
|
|
|
|
|
|
|
// read some compass values
|
|
|
|
compass.read();
|
|
|
|
|
|
|
|
// read current
|
|
|
|
read_battery();
|
|
|
|
|
|
|
|
// calculate scaling for throttle
|
2015-05-11 23:24:13 -03:00
|
|
|
throttle_pct = (float)channel_throttle->control_in / 1000.0f;
|
2014-02-07 01:31:34 -04:00
|
|
|
throttle_pct = constrain_float(throttle_pct,0.0f,1.0f);
|
2014-07-22 04:31:45 -03:00
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// if throttle is near zero, update base x,y,z values
|
|
|
|
if (throttle_pct <= 0.0f) {
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
2015-09-28 16:57:03 -03:00
|
|
|
compass_base[i] = compass_base[i] * 0.99f + compass.get_field(i) * 0.01f;
|
2014-07-22 04:31:45 -03:00
|
|
|
}
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
// causing printing to happen as soon as throttle is lifted
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// calculate diff from compass base and scale with throttle
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
2015-09-28 16:57:03 -03:00
|
|
|
motor_impact[i] = compass.get_field(i) - compass_base[i];
|
2014-07-22 04:31:45 -03:00
|
|
|
}
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
// throttle based compensation
|
2014-02-19 00:29:36 -04:00
|
|
|
if (comp_type == AP_COMPASS_MOT_COMP_THROTTLE) {
|
2014-07-22 04:31:45 -03:00
|
|
|
// for each compass
|
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
// scale by throttle
|
|
|
|
motor_impact_scaled[i] = motor_impact[i] / throttle_pct;
|
|
|
|
// adjust the motor compensation to negate the impact
|
|
|
|
motor_compensation[i] = motor_compensation[i] * 0.99f - motor_impact_scaled[i] * 0.01f;
|
|
|
|
}
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
updated = true;
|
|
|
|
} else {
|
2014-07-22 04:31:45 -03:00
|
|
|
// for each compass
|
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
// current based compensation if more than 3amps being drawn
|
|
|
|
motor_impact_scaled[i] = motor_impact[i] / battery.current_amps();
|
2014-02-07 01:31:34 -04:00
|
|
|
|
2014-07-22 04:31:45 -03:00
|
|
|
// adjust the motor compensation to negate the impact if drawing over 3amps
|
|
|
|
if (battery.current_amps() >= 3.0f) {
|
|
|
|
motor_compensation[i] = motor_compensation[i] * 0.99f - motor_impact_scaled[i] * 0.01f;
|
|
|
|
updated = true;
|
|
|
|
}
|
2014-02-07 01:31:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// calculate interference percentage at full throttle as % of total mag field
|
|
|
|
if (comp_type == AP_COMPASS_MOT_COMP_THROTTLE) {
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
// interference is impact@fullthrottle / mag field * 100
|
|
|
|
interference_pct[i] = motor_compensation[i].length() / (float)COMPASS_MAGFIELD_EXPECTED * 100.0f;
|
|
|
|
}
|
2014-02-19 00:29:36 -04:00
|
|
|
}else{
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
// interference is impact/amp * (max current seen / max throttle seen) / mag field * 100
|
|
|
|
interference_pct[i] = motor_compensation[i].length() * (current_amps_max/throttle_pct_max) / (float)COMPASS_MAGFIELD_EXPECTED * 100.0f;
|
|
|
|
}
|
2014-02-19 00:29:36 -04:00
|
|
|
}
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
// record maximum throttle and current
|
|
|
|
throttle_pct_max = max(throttle_pct_max, throttle_pct);
|
|
|
|
current_amps_max = max(current_amps_max, battery.current_amps());
|
|
|
|
|
|
|
|
}
|
|
|
|
if (hal.scheduler->millis() - last_send_time > 500) {
|
|
|
|
last_send_time = hal.scheduler->millis();
|
|
|
|
mavlink_msg_compassmot_status_send(chan,
|
2015-05-11 23:24:13 -03:00
|
|
|
channel_throttle->control_in,
|
2014-02-07 01:31:34 -04:00
|
|
|
battery.current_amps(),
|
2014-07-22 04:31:45 -03:00
|
|
|
interference_pct[compass.get_primary()],
|
|
|
|
motor_compensation[compass.get_primary()].x,
|
|
|
|
motor_compensation[compass.get_primary()].y,
|
|
|
|
motor_compensation[compass.get_primary()].z);
|
2014-02-07 01:31:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop motors
|
|
|
|
motors.output_min();
|
|
|
|
motors.armed(false);
|
|
|
|
|
|
|
|
// set and save motor compensation
|
2014-02-19 00:29:36 -04:00
|
|
|
if (updated) {
|
2014-02-07 01:31:34 -04:00
|
|
|
compass.motor_compensation_type(comp_type);
|
2014-07-22 04:31:45 -03:00
|
|
|
for (uint8_t i=0; i<compass.get_count(); i++) {
|
|
|
|
compass.set_motor_compensation(i, motor_compensation[i]);
|
|
|
|
}
|
2014-02-07 01:31:34 -04:00
|
|
|
compass.save_motor_compensation();
|
2014-02-19 00:29:36 -04:00
|
|
|
// display success message
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("Calibration Successful!"));
|
2014-02-07 01:31:34 -04:00
|
|
|
} else {
|
|
|
|
// compensation vector never updated, report failure
|
2015-08-11 20:50:34 -03:00
|
|
|
gcs[chan-MAVLINK_COMM_0].send_text_P(MAV_SEVERITY_CRITICAL, PSTR("Failed!"));
|
2014-02-07 01:31:34 -04:00
|
|
|
compass.motor_compensation_type(AP_COMPASS_MOT_COMP_DISABLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
// display new motor offsets and save
|
|
|
|
report_compass();
|
|
|
|
|
2014-02-19 00:29:36 -04:00
|
|
|
// turn off notify leds
|
|
|
|
AP_Notify::flags.esc_calibration = false;
|
|
|
|
|
|
|
|
// re-enable cpu failsafe
|
|
|
|
failsafe_enable();
|
|
|
|
|
|
|
|
// re-enable failsafes
|
|
|
|
g.failsafe_throttle.load();
|
|
|
|
g.failsafe_battery_enabled.load();
|
|
|
|
|
|
|
|
// flag we have completed
|
|
|
|
ap.compass_mot = false;
|
|
|
|
|
2014-02-07 01:31:34 -04:00
|
|
|
return 0;
|
2015-07-15 03:08:13 -03:00
|
|
|
#endif // FRAME_CONFIG != HELI_FRAME
|
2014-02-07 01:31:34 -04:00
|
|
|
}
|
|
|
|
|