2011-03-19 07:20:11 -03:00
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
2011-02-24 01:56:59 -04:00
2011-07-17 07:34:05 -03:00
#if CLI_ENABLED == ENABLED
2010-12-19 12:40:33 -04:00
// These are function definitions so the Menu can be constructed before the functions
// are defined below. Order matters to the compiler.
2013-03-18 01:40:43 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
2012-08-21 23:19:50 -03:00
static int8_t test_baro(uint8_t argc, const Menu::arg *argv);
2011-12-26 04:12:05 -04:00
#endif
2013-05-31 03:15:47 -03:00
static int8_t test_compass(uint8_t argc, const Menu::arg *argv);
static int8_t test_gps(uint8_t argc, const Menu::arg *argv);
static int8_t test_ins(uint8_t argc, const Menu::arg *argv);
static int8_t test_logging(uint8_t argc, const Menu::arg *argv);
static int8_t test_motors(uint8_t argc, const Menu::arg *argv);
2014-01-18 04:00:43 -04:00
static int8_t test_motorsync(uint8_t argc, const Menu::arg *argv);
2013-05-31 03:15:47 -03:00
static int8_t test_optflow(uint8_t argc, const Menu::arg *argv);
static int8_t test_radio_pwm(uint8_t argc, const Menu::arg *argv);
static int8_t test_radio(uint8_t argc, const Menu::arg *argv);
static int8_t test_relay(uint8_t argc, const Menu::arg *argv);
2013-03-19 22:35:50 -03:00
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
2013-05-31 03:15:47 -03:00
static int8_t test_shell(uint8_t argc, const Menu::arg *argv);
#endif
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
static int8_t test_sonar(uint8_t argc, const Menu::arg *argv);
2013-03-19 22:35:50 -03:00
#endif
2010-12-19 12:40:33 -04:00
// Creates a constant array of structs representing menu options
// and stores them in Flash memory, not RAM.
// User enters the string in the console to call the functions on the right.
// See class Menu in AP_Coommon for implementation details
const struct Menu::command test_menu_commands[] PROGMEM = {
2013-03-18 01:40:43 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
2013-05-25 00:21:29 -03:00
{"baro", test_baro},
2011-12-26 04:12:05 -04:00
#endif
2013-05-31 03:15:47 -03:00
{"compass", test_compass},
{"gps", test_gps},
{"ins", test_ins},
2012-08-21 23:19:50 -03:00
{"logging", test_logging},
2013-05-31 03:15:47 -03:00
{"motors", test_motors},
2014-01-18 04:00:43 -04:00
{"motorsync", test_motorsync},
2013-05-31 03:15:47 -03:00
{"optflow", test_optflow},
{"pwm", test_radio_pwm},
{"radio", test_radio},
{"relay", test_relay},
2013-03-19 22:35:50 -03:00
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
{"shell", test_shell},
#endif
2013-05-31 03:15:47 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
{"sonar", test_sonar},
#endif
2010-12-19 12:40:33 -04:00
};
// A Macro to create the Menu
MENU(test_menu, "test", test_menu_commands);
2011-07-17 07:32:00 -03:00
static int8_t
2010-12-19 12:40:33 -04:00
test_mode(uint8_t argc, const Menu::arg *argv)
{
2012-08-21 23:19:50 -03:00
test_menu.run();
return 0;
2010-12-19 12:40:33 -04:00
}
2013-05-31 03:15:47 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
2010-12-19 12:40:33 -04:00
static int8_t
2013-05-31 03:15:47 -03:00
test_baro(uint8_t argc, const Menu::arg *argv)
2012-02-10 02:21:30 -04:00
{
2013-05-31 03:15:47 -03:00
int32_t alt;
2012-08-21 23:19:50 -03:00
print_hit_enter();
2014-01-15 10:18:23 -04:00
init_barometer(true);
2012-08-21 23:19:50 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
delay(100);
alt = read_barometer();
2012-08-21 23:19:50 -03:00
2013-05-31 03:15:47 -03:00
if (!barometer.healthy) {
cliSerial->println_P(PSTR("not healthy"));
} else {
cliSerial->printf_P(PSTR("Alt: %0.2fm, Raw: %f Temperature: %.1f\n"),
2013-09-21 08:30:54 -03:00
alt / 100.0,
barometer.get_pressure(),
barometer.get_temperature());
2013-05-31 03:15:47 -03:00
}
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2012-08-21 23:19:50 -03:00
return (0);
}
}
2013-05-31 03:15:47 -03:00
return 0;
2012-02-10 02:21:30 -04:00
}
2013-05-31 03:15:47 -03:00
#endif
2011-09-24 20:48:04 -03:00
2011-11-12 23:51:43 -04:00
static int8_t
2013-05-31 03:15:47 -03:00
test_compass(uint8_t argc, const Menu::arg *argv)
2011-11-12 23:51:43 -04:00
{
2013-05-31 03:15:47 -03:00
uint8_t delta_ms_fast_loop;
2013-10-11 08:40:20 -03:00
uint8_t medium_loopCounter = 0;
2013-05-31 03:15:47 -03:00
if (!g.compass_enabled) {
cliSerial->printf_P(PSTR("Compass: "));
print_enabled(false);
return (0);
}
if (!compass.init()) {
cliSerial->println_P(PSTR("Compass initialisation failed!"));
return 0;
}
2011-11-12 23:51:43 -04:00
2013-01-13 01:04:04 -04:00
ahrs.init();
2013-05-31 03:15:47 -03:00
ahrs.set_fly_forward(true);
ahrs.set_compass(&compass);
report_compass();
// we need the AHRS initialised for this test
2012-11-29 07:56:54 -04:00
ins.init(AP_InertialSensor::COLD_START,
2013-09-19 05:33:21 -03:00
ins_sample_rate);
2013-05-31 03:15:47 -03:00
ahrs.reset();
int16_t counter = 0;
float heading = 0;
2011-11-12 23:51:43 -04:00
2013-05-31 03:15:47 -03:00
print_hit_enter();
2011-11-12 23:51:43 -04:00
2012-08-21 23:19:50 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
delay(20);
if (millis() - fast_loopTimer > 19) {
delta_ms_fast_loop = millis() - fast_loopTimer;
G_Dt = (float)delta_ms_fast_loop / 1000.f; // used by DCM integrator
fast_loopTimer = millis();
2011-11-12 23:51:43 -04:00
2013-05-31 03:15:47 -03:00
// INS
// ---
ahrs.update();
2012-07-18 15:14:13 -03:00
2013-05-31 03:15:47 -03:00
medium_loopCounter++;
if(medium_loopCounter == 5) {
if (compass.read()) {
// Calculate heading
const Matrix3f &m = ahrs.get_dcm_matrix();
heading = compass.calculate_heading(m);
compass.null_offsets();
}
medium_loopCounter = 0;
}
counter++;
if (counter>20) {
2013-12-09 02:47:22 -04:00
if (compass.healthy()) {
2013-12-08 23:10:14 -04:00
const Vector3f &mag_ofs = compass.get_offsets();
const Vector3f &mag = compass.get_field();
cliSerial->printf_P(PSTR("Heading: %ld, XYZ: %.0f, %.0f, %.0f,\tXYZoff: %6.2f, %6.2f, %6.2f\n"),
(wrap_360_cd(ToDeg(heading) * 100)) /100,
mag.x,
mag.y,
mag.z,
mag_ofs.x,
mag_ofs.y,
mag_ofs.z);
2013-05-31 03:15:47 -03:00
} else {
cliSerial->println_P(PSTR("compass not healthy"));
}
counter=0;
}
}
if (cliSerial->available() > 0) {
break;
}
}
// save offsets. This allows you to get sane offset values using
// the CLI before you go flying.
cliSerial->println_P(PSTR("saving offsets"));
compass.save_offsets();
return (0);
}
2010-12-19 12:40:33 -04:00
static int8_t
test_gps(uint8_t argc, const Menu::arg *argv)
{
2012-08-21 23:19:50 -03:00
print_hit_enter();
delay(1000);
while(1) {
2013-03-21 08:45:07 -03:00
delay(100);
2012-08-21 23:19:50 -03:00
g_gps->update();
if (g_gps->new_data) {
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR("Lat: "));
2012-12-13 15:48:01 -04:00
print_latlon(cliSerial, g_gps->latitude);
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR(", Lon "));
2012-12-13 15:48:01 -04:00
print_latlon(cliSerial, g_gps->longitude);
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR(", Alt: %ldm, #sats: %d\n"),
2013-07-10 01:02:48 -03:00
g_gps->altitude_cm/100,
2012-08-21 23:19:50 -03:00
g_gps->num_sats);
g_gps->new_data = false;
}else{
2012-11-21 02:08:03 -04:00
cliSerial->print_P(PSTR("."));
2012-08-21 23:19:50 -03:00
}
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2012-08-21 23:19:50 -03:00
return (0);
}
}
return 0;
2010-12-19 12:40:33 -04:00
}
2011-04-17 20:08:16 -03:00
static int8_t
2013-05-31 03:15:47 -03:00
test_ins(uint8_t argc, const Menu::arg *argv)
2011-04-17 20:08:16 -03:00
{
2013-05-31 03:15:47 -03:00
Vector3f gyro, accel;
2012-08-21 23:19:50 -03:00
print_hit_enter();
2013-05-31 03:15:47 -03:00
cliSerial->printf_P(PSTR("INS\n"));
delay(1000);
ahrs.init();
ins.init(AP_InertialSensor::COLD_START,
2013-09-19 05:33:21 -03:00
ins_sample_rate);
2013-07-02 11:00:49 -03:00
cliSerial->printf_P(PSTR("...done\n"));
2013-05-31 03:15:47 -03:00
delay(50);
2012-08-21 23:19:50 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
ins.update();
gyro = ins.get_gyro();
accel = ins.get_accel();
float test = accel.length() / GRAVITY_MSS;
2013-07-02 11:00:49 -03:00
cliSerial->printf_P(PSTR("a %7.4f %7.4f %7.4f g %7.4f %7.4f %7.4f t %7.4f \n"),
2013-05-31 03:15:47 -03:00
accel.x, accel.y, accel.z,
gyro.x, gyro.y, gyro.z,
test);
2012-08-21 23:19:50 -03:00
2013-05-31 03:15:47 -03:00
delay(40);
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2012-08-21 23:19:50 -03:00
return (0);
}
}
2011-04-17 20:08:16 -03:00
}
2013-05-31 03:15:47 -03:00
/*
* test the dataflash is working
*/
2011-01-16 23:44:12 -04:00
static int8_t
2013-05-31 03:15:47 -03:00
test_logging(uint8_t argc, const Menu::arg *argv)
2011-01-16 23:44:12 -04:00
{
2013-05-31 03:15:47 -03:00
cliSerial->println_P(PSTR("Testing dataflash logging"));
DataFlash.ShowDeviceInfo(cliSerial);
return 0;
}
2013-05-16 04:32:00 -03:00
2013-05-31 03:15:47 -03:00
static int8_t
test_motors(uint8_t argc, const Menu::arg *argv)
{
cliSerial->printf_P(PSTR(
"Connect battery for this test.\n"
2013-09-24 09:31:17 -03:00
"Motors will spin by frame position order.\n"
2013-05-31 03:15:47 -03:00
"Front (& right of centerline) motor first, then in clockwise order around frame.\n"
"Remember to disconnect battery after this test.\n"
"Any key to exit.\n"));
2012-08-21 23:19:50 -03:00
2013-05-31 03:15:47 -03:00
// ensure all values have been sent to motors
motors.set_update_rate(g.rc_speed);
motors.set_frame_orientation(g.frame_orientation);
motors.set_min_throttle(g.throttle_min);
motors.set_mid_throttle(g.throttle_mid);
// enable motors
init_rc_out();
2012-08-21 23:19:50 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
delay(20);
2012-08-21 23:19:50 -03:00
read_radio();
2013-05-31 03:15:47 -03:00
motors.output_test();
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2013-05-31 03:15:47 -03:00
g.esc_calibrate.set_and_save(0);
return(0);
2012-08-21 23:19:50 -03:00
}
}
2011-01-16 23:44:12 -04:00
}
2014-01-18 04:00:43 -04:00
// test_motorsync - suddenly increases pwm output to motors to test if ESC loses sync
static int8_t
test_motorsync(uint8_t argc, const Menu::arg *argv)
{
bool test_complete = false;
bool spin_motors = false;
2014-01-25 22:08:34 -04:00
uint32_t spin_start_time = 0;
2014-01-18 04:00:43 -04:00
uint32_t last_run_time;
int16_t last_throttle = 0;
int16_t c;
// check if radio is calibration
pre_arm_rc_checks();
if (!ap.pre_arm_rc_check) {
cliSerial->print_P(PSTR("radio not calibrated\n"));
return 0;
}
// print warning that motors will spin
// ask user to raise throttle
// inform how to stop test
cliSerial->print_P(PSTR("This sends sudden outputs to the motors based on the pilot's throttle to test for ESC loss of sync. Motors will spin so mount props up-side-down!\n Hold throttle low, then raise throttle stick to desired level and press A. Motors will spin for 2 sec and then return to low.\nPress any key to exit.\n"));
// clear out user input
while (cliSerial->available()) {
cliSerial->read();
}
// disable throttle and battery failsafe
g.failsafe_throttle = FS_THR_DISABLED;
g.failsafe_battery_enabled = FS_BATT_DISABLED;
// read radio
read_radio();
// exit immediately if throttle is not zero
if( g.rc_3.control_in != 0 ) {
cliSerial->print_P(PSTR("throttle not zero\n"));
return 0;
}
// clear out any user input
while (cliSerial->available()) {
cliSerial->read();
}
// enable motors and pass through throttle
init_rc_out();
output_min();
motors.armed(true);
// initialise run time
last_run_time = millis();
// main run while the test is not complete
while(!test_complete) {
// 50hz loop
if( millis() - last_run_time > 20 ) {
last_run_time = millis();
// read radio input
read_radio();
// display throttle value
if (abs(g.rc_3.control_in-last_throttle) > 10) {
cliSerial->printf_P(PSTR("\nThr:%d"),g.rc_3.control_in);
last_throttle = g.rc_3.control_in;
}
// decode user input
if (cliSerial->available()) {
c = cliSerial->read();
if (c == 'a' || c == 'A') {
spin_motors = true;
spin_start_time = millis();
// display user's throttle level
cliSerial->printf_P(PSTR("\nSpin motors at:%d"),(int)g.rc_3.control_in);
// clear out any other use input queued up
while (cliSerial->available()) {
cliSerial->read();
}
}else{
// any other input ends the test
test_complete = true;
motors.armed(false);
}
}
// check if time to stop motors
if (spin_motors) {
if ((millis() - spin_start_time) > 2000) {
spin_motors = false;
cliSerial->printf_P(PSTR("\nMotors stopped"));
}
}
// output to motors
if (spin_motors) {
// pass pilot throttle through to motors
motors.throttle_pass_through();
}else{
// spin motors at minimum
output_min();
}
}
}
// stop motors
motors.output_min();
motors.armed(false);
// clear out any user input
while( cliSerial->available() ) {
cliSerial->read();
}
// display completion message
cliSerial->printf_P(PSTR("\nTest complete\n"));
return 0;
}
2010-12-19 12:40:33 -04:00
static int8_t
2013-05-31 03:15:47 -03:00
test_optflow(uint8_t argc, const Menu::arg *argv)
2010-12-19 12:40:33 -04:00
{
2013-05-31 03:15:47 -03:00
#if OPTFLOW == ENABLED
if(g.optflow_enabled) {
cliSerial->printf_P(PSTR("man id: %d\t"),optflow.read_register(ADNS3080_PRODUCT_ID));
print_hit_enter();
2011-02-17 05:36:33 -04:00
2013-05-31 03:15:47 -03:00
while(1) {
delay(200);
2013-10-23 08:42:03 -03:00
optflow.update();
2014-01-08 23:29:50 -04:00
cliSerial->printf_P(PSTR("dx:%d\t dy:%d\t squal:%d\n"),
2013-05-31 03:15:47 -03:00
optflow.dx,
optflow.dy,
optflow.surface_quality);
2011-02-17 05:36:33 -04:00
2013-05-31 03:15:47 -03:00
if(cliSerial->available() > 0) {
return (0);
}
}
} else {
cliSerial->printf_P(PSTR("OptFlow: "));
print_enabled(false);
}
2012-08-21 23:19:50 -03:00
return (0);
2013-05-31 03:15:47 -03:00
#else
return (0);
#endif // OPTFLOW == ENABLED
2010-12-19 12:40:33 -04:00
}
static int8_t
2013-05-31 03:15:47 -03:00
test_radio_pwm(uint8_t argc, const Menu::arg *argv)
2010-12-19 12:40:33 -04:00
{
2012-08-21 23:19:50 -03:00
print_hit_enter();
2013-05-31 03:15:47 -03:00
delay(1000);
2012-08-21 23:19:50 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
delay(20);
// Filters radio input - adjust filters in the radio.pde file
// ----------------------------------------------------------
read_radio();
// servo Yaw
//APM_RC.OutputCh(CH_7, g.rc_4.radio_out);
cliSerial->printf_P(PSTR("IN: 1: %d\t2: %d\t3: %d\t4: %d\t5: %d\t6: %d\t7: %d\t8: %d\n"),
g.rc_1.radio_in,
g.rc_2.radio_in,
g.rc_3.radio_in,
g.rc_4.radio_in,
g.rc_5.radio_in,
g.rc_6.radio_in,
g.rc_7.radio_in,
g.rc_8.radio_in);
2013-05-25 00:21:29 -03:00
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2012-08-21 23:19:50 -03:00
return (0);
}
}
2010-12-19 12:40:33 -04:00
}
2011-12-03 19:39:02 -04:00
2010-12-19 12:40:33 -04:00
static int8_t
2013-05-31 03:15:47 -03:00
test_radio(uint8_t argc, const Menu::arg *argv)
2011-04-08 16:13:31 -03:00
{
2013-05-31 03:15:47 -03:00
print_hit_enter();
delay(1000);
2012-08-21 23:19:50 -03:00
2013-05-31 03:15:47 -03:00
while(1) {
delay(20);
read_radio();
2013-04-27 11:33:04 -03:00
2013-05-31 03:15:47 -03:00
cliSerial->printf_P(PSTR("IN 1: %d\t2: %d\t3: %d\t4: %d\t5: %d\t6: %d\t7: %d\n"),
g.rc_1.control_in,
g.rc_2.control_in,
g.rc_3.control_in,
g.rc_4.control_in,
g.rc_5.control_in,
g.rc_6.control_in,
g.rc_7.control_in);
2013-04-27 11:33:04 -03:00
2013-05-31 03:15:47 -03:00
if(cliSerial->available() > 0) {
return (0);
}
}
}
2013-04-27 11:33:04 -03:00
2013-05-31 03:15:47 -03:00
static int8_t test_relay(uint8_t argc, const Menu::arg *argv)
{
2013-04-27 11:33:04 -03:00
print_hit_enter();
2013-05-31 03:15:47 -03:00
delay(1000);
2013-04-27 11:33:04 -03:00
while(1) {
2013-05-31 03:15:47 -03:00
cliSerial->printf_P(PSTR("Relay on\n"));
2014-01-19 23:54:21 -04:00
relay.on(0);
2013-05-31 03:15:47 -03:00
delay(3000);
if(cliSerial->available() > 0) {
return (0);
2013-04-27 11:33:04 -03:00
}
2013-05-31 03:15:47 -03:00
cliSerial->printf_P(PSTR("Relay off\n"));
2014-01-19 23:54:21 -04:00
relay.off(0);
2013-05-31 03:15:47 -03:00
delay(3000);
if(cliSerial->available() > 0) {
return (0);
2013-04-27 11:33:04 -03:00
}
}
2013-05-31 03:15:47 -03:00
}
2013-04-27 11:33:04 -03:00
2013-05-31 03:15:47 -03:00
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
/*
* run a debug shell
*/
static int8_t
test_shell(uint8_t argc, const Menu::arg *argv)
{
hal.util->run_debug_shell(cliSerial);
return 0;
2010-12-19 12:40:33 -04:00
}
2013-05-31 03:15:47 -03:00
#endif
2011-12-03 19:39:02 -04:00
2013-03-18 01:40:43 -03:00
#if HIL_MODE != HIL_MODE_ATTITUDE && HIL_MODE != HIL_MODE_SENSORS
2011-03-17 22:50:27 -03:00
/*
2012-08-21 23:19:50 -03:00
* test the sonar
2011-03-17 22:50:27 -03:00
*/
2011-06-03 15:49:13 -03:00
static int8_t
2011-03-17 22:50:27 -03:00
test_sonar(uint8_t argc, const Menu::arg *argv)
{
2013-03-18 01:40:43 -03:00
#if CONFIG_SONAR == ENABLED
2012-08-21 23:19:50 -03:00
if(g.sonar_enabled == false) {
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR("Sonar disabled\n"));
2012-08-21 23:19:50 -03:00
return (0);
}
2011-12-23 18:14:19 -04:00
2012-08-21 23:19:50 -03:00
// make sure sonar is initialised
init_sonar();
2011-06-16 14:03:26 -03:00
2012-08-21 23:19:50 -03:00
print_hit_enter();
while(1) {
delay(100);
2011-06-03 15:49:13 -03:00
2012-12-13 15:48:01 -04:00
cliSerial->printf_P(PSTR("Sonar: %d cm\n"), sonar->read());
2011-06-03 15:49:13 -03:00
2012-11-21 02:08:03 -04:00
if(cliSerial->available() > 0) {
2012-08-21 23:19:50 -03:00
return (0);
}
}
2013-03-18 01:40:43 -03:00
#endif
2012-08-21 23:19:50 -03:00
return (0);
2011-03-17 22:50:27 -03:00
}
2011-12-26 04:12:05 -04:00
#endif
2011-03-17 22:50:27 -03:00
2011-07-17 07:32:00 -03:00
static void print_hit_enter()
2010-12-19 12:40:33 -04:00
{
2012-11-21 02:08:03 -04:00
cliSerial->printf_P(PSTR("Hit Enter to exit.\n\n"));
2010-12-19 12:40:33 -04:00
}
2011-09-10 19:16:51 -03:00
2011-07-17 07:34:05 -03:00
#endif // CLI_ENABLED