#include "Sub.h"
#include "stdio.h"
namespace {
enum test_state {
STANDBY,
SETTLING,
THRUSTING,
DETECTING,
DONE
};
enum direction {
UP = 1,
DOWN = -1
};
static uint32_t settling_timer;
static uint32_t thrusting_timer;
static uint8_t md_state;
static uint8_t current_motor;
static int16_t current_direction;
}
bool ModeMotordetect::init(bool ignore_checks)
{
current_motor = 0;
md_state = STANDBY;
current_direction = UP;
return true;
}
void ModeMotordetect::run()
{
if (!motors.armed()) {
motors.set_desired_spool_state(AP_Motors::DesiredSpoolState::GROUND_IDLE);
for(uint8_t i=0; i < AP_MOTORS_MAX_NUM_MOTORS; i++) {
if (motors.motor_is_enabled(i)) {
motors.output_test_num(i, 1500);
}
}
md_state = STANDBY;
return;
}
switch(md_state) {
case STANDBY:
current_direction = UP;
current_motor = 0;
settling_timer = AP_HAL::millis();
md_state = SETTLING;
break;
case SETTLING:
for (uint8_t i=0; i <AP_MOTORS_MAX_NUM_MOTORS; i++) {
if (motors.motor_is_enabled(i)) {
motors.output_test_num(i, 1500);
}
}
if ((ahrs.get_gyro()*ahrs.get_gyro()) > 0.01) {
settling_timer = AP_HAL::millis();
}
if (AP_HAL::millis() > (settling_timer + 500)) {
md_state = THRUSTING;
thrusting_timer = AP_HAL::millis();
}
break;
case THRUSTING:
if (AP_HAL::millis() < (thrusting_timer + 500)) {
if (!motors.output_test_num(current_motor, 1500 + 300*current_direction)) {
md_state = DONE;
};
} else {
md_state = DETECTING;
}
break;
case DETECTING:
{
Vector3f gyro = ahrs.get_gyro();
bool roll_up = gyro.x > 0.4;
bool roll_down = gyro.x < -0.4;
int roll = (int(roll_up) - int(roll_down))*current_direction;
bool pitch_up = gyro.y > 0.4;
bool pitch_down = gyro.y < -0.4;
int pitch = (int(pitch_up) - int(pitch_down))*current_direction;
bool yaw_up = gyro.z > 0.5;
bool yaw_down = gyro.z < -0.5;
int yaw = (+int(yaw_up) - int(yaw_down))*current_direction;
Vector3f directions(roll, pitch, yaw);
if (directions == motors.get_motor_angular_factors(current_motor)) {
gcs().send_text(MAV_SEVERITY_INFO, "Thruster %d is ok!", current_motor + 1);
}
else if (-directions == motors.get_motor_angular_factors(current_motor)) {
gcs().send_text(MAV_SEVERITY_INFO, "Thruster %d is reversed! Saving it!", current_motor + 1);
motors.set_reversed(current_motor, true);
}
else {
gcs().send_text(MAV_SEVERITY_INFO, "Bad thrust read, trying to push the other way...");
if (current_direction == DOWN) {
gcs().send_text(MAV_SEVERITY_WARNING, "Failed! Please check Thruster %d and frame setup!", current_motor + 1);
md_state = DONE;
break;
}
current_direction = DOWN;
md_state = SETTLING;
break;
}
md_state = SETTLING;
current_motor++;
current_direction = UP;
if (!motors.motor_is_enabled(current_motor)) {
md_state = DONE;
gcs().send_text(MAV_SEVERITY_WARNING, "Motor direction detection is complete.");
}
break;
}
case DONE:
set_mode(sub.prev_control_mode, ModeReason::MISSION_END);
sub.arming.disarm(AP_Arming::Method::MOTORDETECTDONE);
break;
}
}