Browse Source

Merge pull request #169 from PX4/home_position

Home position
sbg
px4dev 12 years ago
parent
commit
7eb7836d2d
  1. 126
      apps/commander/commander.c
  2. 16
      apps/mavlink/orb_listener.c
  3. 2
      apps/mavlink/orb_topics.h
  4. 3
      apps/uORB/objects_common.cpp
  5. 77
      apps/uORB/topics/home_position.h

126
apps/commander/commander.c

@ -72,8 +72,10 @@ @@ -72,8 +72,10 @@
#include <uORB/topics/battery_status.h>
#include <uORB/topics/manual_control_setpoint.h>
#include <uORB/topics/offboard_control_setpoint.h>
#include <uORB/topics/home_position.h>
#include <uORB/topics/vehicle_global_position.h>
#include <uORB/topics/vehicle_local_position.h>
#include <uORB/topics/vehicle_gps_position.h>
#include <uORB/topics/vehicle_command.h>
#include <uORB/topics/subsystem_info.h>
#include <uORB/topics/actuator_controls.h>
@ -1251,6 +1253,7 @@ int commander_thread_main(int argc, char *argv[]) @@ -1251,6 +1253,7 @@ int commander_thread_main(int argc, char *argv[])
{
/* not yet initialized */
commander_initialized = false;
bool home_position_set = false;
/* set parameters */
failsafe_lowlevel_timeout_ms = 0;
@ -1302,6 +1305,11 @@ int commander_thread_main(int argc, char *argv[]) @@ -1302,6 +1305,11 @@ int commander_thread_main(int argc, char *argv[])
/* publish current state machine */
state_machine_publish(stat_pub, &current_status, mavlink_fd);
/* home position */
orb_advert_t home_pub = -1;
struct home_position_s home;
memset(&home, 0, sizeof(home));
if (stat_pub < 0) {
warnx("ERROR: orb_advertise for topic vehicle_status failed.\n");
exit(ERROR);
@ -1332,10 +1340,6 @@ int commander_thread_main(int argc, char *argv[]) @@ -1332,10 +1340,6 @@ int commander_thread_main(int argc, char *argv[])
uint16_t stick_off_counter = 0;
uint16_t stick_on_counter = 0;
float hdop = 65535.0f;
int gps_quality_good_counter = 0;
/* Subscribe to manual control data */
int sp_man_sub = orb_subscribe(ORB_ID(manual_control_setpoint));
struct manual_control_setpoint_s sp_man;
@ -1356,6 +1360,15 @@ int commander_thread_main(int argc, char *argv[]) @@ -1356,6 +1360,15 @@ int commander_thread_main(int argc, char *argv[])
memset(&local_position, 0, sizeof(local_position));
uint64_t last_local_position_time = 0;
/*
* The home position is set based on GPS only, to prevent a dependency between
* position estimator and commander. RAW GPS is more than good enough for a
* non-flying vehicle.
*/
int gps_sub = orb_subscribe(ORB_ID(vehicle_gps_position));
struct vehicle_gps_position_s gps_position;
memset(&gps_position, 0, sizeof(gps_position));
int sensor_sub = orb_subscribe(ORB_ID(sensor_combined));
struct sensor_combined_s sensors;
memset(&sensors, 0, sizeof(sensors));
@ -1660,65 +1673,57 @@ int commander_thread_main(int argc, char *argv[]) @@ -1660,65 +1673,57 @@ int commander_thread_main(int argc, char *argv[])
state_changed = true;
}
if (orb_check(ORB_ID(vehicle_gps_position), &new_data)) {
/* Check if last transition deserved an audio event */
// #warning This code depends on state that is no longer? maintained
// #if 0
// trigger_audio_alarm(vehicle_mode_previous, vehicle_state_previous, current_status.mode, current_status.state_machine);
// #endif
orb_copy(ORB_ID(vehicle_gps_position), gps_sub, &gps_position);
/* only check gps fix if we are outdoor */
// if (flight_env == PX4_FLIGHT_ENVIRONMENT_OUTDOOR) {
//
// hdop = (float)(gps.eph) / 100.0f;
//
// /* check if gps fix is ok */
// if (gps.fix_type == GPS_FIX_TYPE_3D) { //TODO: is 2d-fix ok? //see http://en.wikipedia.org/wiki/Dilution_of_precision_%28GPS%29
//
// if (gotfix_counter >= GPS_GOTFIX_COUNTER_REQUIRED) { //TODO: add also a required time?
// update_state_machine_got_position_fix(stat_pub, &current_status);
// gotfix_counter = 0;
// } else {
// gotfix_counter++;
// }
// nofix_counter = 0;
//
// if (hdop < 5.0f) { //TODO: this should be a parameter
// if (gps_quality_good_counter > GPS_QUALITY_GOOD_COUNTER_LIMIT) {
// current_status.gps_valid = true;//--> position estimator can use the gps measurements
// }
//
// gps_quality_good_counter++;
//
//
//// if(counter%10 == 0)//for testing only
//// warnx("gps_quality_good_counter = %u\n", gps_quality_good_counter);//for testing only
//
// } else {
// gps_quality_good_counter = 0;
// current_status.gps_valid = false;//--> position estimator can not use the gps measurements
// }
//
// } else {
// gps_quality_good_counter = 0;
// current_status.gps_valid = false;//--> position estimator can not use the gps measurements
//
// if (nofix_counter > GPS_NOFIX_COUNTER_LIMIT) { //TODO: add also a timer limit?
// update_state_machine_no_position_fix(stat_pub, &current_status);
// nofix_counter = 0;
// } else {
// nofix_counter++;
// }
// gotfix_counter = 0;
// }
//
// }
//
//
// if (flight_env == PX4_FLIGHT_ENVIRONMENT_TESTING) //simulate position fix for quick indoor tests
//update_state_machine_got_position_fix(stat_pub, &current_status, mavlink_fd);
/* end: check gps */
/* check for first, long-term and valid GPS lock -> set home position */
float hdop_m = gps_position.eph / 100.0f;
float vdop_m = gps_position.epv / 100.0f;
/* check if gps fix is ok */
// XXX magic number
float dop_threshold_m = 2.0f;
/*
* If horizontal dilution of precision (hdop / eph)
* and vertical diluation of precision (vdop / epv)
* are below a certain threshold (e.g. 4 m), AND
* home position is not yet set AND the last GPS
* GPS measurement is not older than two seconds AND
* the system is currently not armed, set home
* position to the current position.
*/
if (gps_position.fix_type == GPS_FIX_TYPE_3D && (hdop < dop_threshold_m)
&& (vdop_m < dop_threshold_m)
&& !home_position_set
&& (hrt_absolute_time() - gps_position.timestamp < 2000000)
&& !current_status.flag_system_armed) {
warnx("setting home position");
/* copy position data to uORB home message, store it locally as well */
home.lat = gps_position.lat;
home.lon = gps_position.lon;
home.alt = gps_position.alt;
home.eph = gps_position.eph;
home.epv = gps_position.epv;
home.s_variance = gps_position.s_variance;
home.p_variance = gps_position.p_variance;
/* announce new home position */
if (home_pub > 0) {
orb_publish(ORB_ID(home_position), home_pub, &home);
} else {
home_pub = orb_advertise(ORB_ID(home_position), &home);
}
/* mark home position as set */
home_position_set = true;
tune_confirm();
}
}
/* ignore RC signals if in offboard control mode */
if (!current_status.offboard_control_signal_found_once && sp_man.timestamp != 0) {
@ -2041,4 +2046,3 @@ int commander_thread_main(int argc, char *argv[]) @@ -2041,4 +2046,3 @@ int commander_thread_main(int argc, char *argv[])
return 0;
}

16
apps/mavlink/orb_listener.c

@ -114,6 +114,7 @@ static void l_vehicle_attitude_controls(struct listener *l); @@ -114,6 +114,7 @@ static void l_vehicle_attitude_controls(struct listener *l);
static void l_debug_key_value(struct listener *l);
static void l_optical_flow(struct listener *l);
static void l_vehicle_rates_setpoint(struct listener *l);
static void l_home(struct listener *l);
struct listener listeners[] = {
{l_sensor_combined, &mavlink_subs.sensor_sub, 0},
@ -137,6 +138,7 @@ struct listener listeners[] = { @@ -137,6 +138,7 @@ struct listener listeners[] = {
{l_debug_key_value, &mavlink_subs.debug_key_value, 0},
{l_optical_flow, &mavlink_subs.optical_flow, 0},
{l_vehicle_rates_setpoint, &mavlink_subs.rates_setpoint_sub, 0},
{l_home, &mavlink_subs.home_sub, 0},
};
static const unsigned n_listeners = sizeof(listeners) / sizeof(listeners[0]);
@ -621,6 +623,16 @@ l_optical_flow(struct listener *l) @@ -621,6 +623,16 @@ l_optical_flow(struct listener *l)
flow.flow_comp_x_m, flow.flow_comp_y_m, flow.quality, flow.ground_distance_m);
}
void
l_home(struct listener *l)
{
struct home_position_s home;
orb_copy(ORB_ID(home_position), mavlink_subs.home_sub, &home);
mavlink_msg_gps_global_origin_send(MAVLINK_COMM_0, home.lat, home.lon, home.alt);
}
static void *
uorb_receive_thread(void *arg)
{
@ -688,6 +700,10 @@ uorb_receive_start(void) @@ -688,6 +700,10 @@ uorb_receive_start(void)
mavlink_subs.gps_sub = orb_subscribe(ORB_ID(vehicle_gps_position));
orb_set_interval(mavlink_subs.gps_sub, 1000); /* 1Hz updates */
/* --- HOME POSITION --- */
mavlink_subs.home_sub = orb_subscribe(ORB_ID(home_position));
orb_set_interval(mavlink_subs.home_sub, 1000); /* 1Hz updates */
/* --- SYSTEM STATE --- */
status_sub = orb_subscribe(ORB_ID(vehicle_status));
orb_set_interval(status_sub, 300); /* max 3.33 Hz updates */

2
apps/mavlink/orb_topics.h

@ -45,6 +45,7 @@ @@ -45,6 +45,7 @@
#include <uORB/topics/vehicle_attitude.h>
#include <uORB/topics/vehicle_gps_position.h>
#include <uORB/topics/vehicle_global_position.h>
#include <uORB/topics/home_position.h>
#include <uORB/topics/vehicle_status.h>
#include <uORB/topics/offboard_control_setpoint.h>
#include <uORB/topics/vehicle_command.h>
@ -81,6 +82,7 @@ struct mavlink_subscriptions { @@ -81,6 +82,7 @@ struct mavlink_subscriptions {
int input_rc_sub;
int optical_flow;
int rates_setpoint_sub;
int home_sub;
};
extern struct mavlink_subscriptions mavlink_subs;

3
apps/uORB/objects_common.cpp

@ -68,6 +68,9 @@ ORB_DEFINE(sensor_combined, struct sensor_combined_s); @@ -68,6 +68,9 @@ ORB_DEFINE(sensor_combined, struct sensor_combined_s);
#include "topics/vehicle_gps_position.h"
ORB_DEFINE(vehicle_gps_position, struct vehicle_gps_position_s);
#include "topics/home_position.h"
ORB_DEFINE(home_position, struct home_position_s);
#include "topics/vehicle_status.h"
ORB_DEFINE(vehicle_status, struct vehicle_status_s);

77
apps/uORB/topics/home_position.h

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
/****************************************************************************
*
* Copyright (C) 2012-2013 PX4 Development Team. All rights reserved.
* Author: Lorenz Meier <lm@inf.ethz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file home_position.h
* Definition of the GPS home position uORB topic.
*
* @author Lorenz Meier <lm@inf.ethz.ch>
*/
#ifndef TOPIC_HOME_POSITION_H_
#define TOPIC_HOME_POSITION_H_
#include <stdint.h>
#include "../uORB.h"
/**
* @addtogroup topics
* @{
*/
/**
* GPS home position in WGS84 coordinates.
*/
struct home_position_s
{
uint64_t timestamp; /**< Timestamp (microseconds since system boot) */
uint64_t time_gps_usec; /**< Timestamp (microseconds in GPS format), this is the timestamp from the gps module */
int32_t lat; /**< Latitude in 1E7 degrees */
int32_t lon; /**< Longitude in 1E7 degrees */
int32_t alt; /**< Altitude in 1E3 meters (millimeters) above MSL */
uint16_t eph; /**< GPS HDOP horizontal dilution of position in cm (m*100). If unknown, set to: 65535 */
uint16_t epv; /**< GPS VDOP horizontal dilution of position in cm (m*100). If unknown, set to: 65535 */
float s_variance; /**< speed accuracy estimate cm/s */
float p_variance; /**< position accuracy estimate cm */
};
/**
* @}
*/
/* register this as object request broker structure */
ORB_DECLARE(home_position);
#endif
Loading…
Cancel
Save