2014-06-06 17:17:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2014 PX4 Development Team. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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 mission_block.cpp
|
|
|
|
|
*
|
|
|
|
|
* Helper class to use mission items
|
|
|
|
|
*
|
|
|
|
|
* @author Julian Oes <julian@oes.ch>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <systemlib/err.h>
|
|
|
|
|
#include <geo/geo.h>
|
2014-06-28 00:54:27 +02:00
|
|
|
#include <mavlink/mavlink_log.h>
|
2014-06-06 17:17:41 +02:00
|
|
|
|
|
|
|
|
#include <uORB/uORB.h>
|
|
|
|
|
|
|
|
|
|
#include "navigator.h"
|
|
|
|
|
#include "mission_block.h"
|
|
|
|
|
|
|
|
|
|
|
2014-06-27 11:34:19 +02:00
|
|
|
MissionBlock::MissionBlock(Navigator *navigator, const char *name) :
|
|
|
|
|
NavigatorMode(navigator, name),
|
2014-06-06 17:17:41 +02:00
|
|
|
_waypoint_position_reached(false),
|
|
|
|
|
_waypoint_yaw_reached(false),
|
|
|
|
|
_time_first_inside_orbit(0),
|
2014-06-29 14:09:22 +02:00
|
|
|
_mission_item({0})
|
2014-06-06 17:17:41 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MissionBlock::~MissionBlock()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
MissionBlock::is_mission_item_reached()
|
|
|
|
|
{
|
2014-06-29 14:09:22 +02:00
|
|
|
if (_mission_item.nav_cmd == NAV_CMD_IDLE) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 11:37:33 +02:00
|
|
|
if (_mission_item.nav_cmd == NAV_CMD_LAND) {
|
2014-06-27 11:34:19 +02:00
|
|
|
return _navigator->get_vstatus()->condition_landed;
|
2014-06-09 11:37:33 +02:00
|
|
|
}
|
2014-06-27 00:27:08 +02:00
|
|
|
|
2014-06-06 17:17:41 +02:00
|
|
|
/* TODO: count turns */
|
2014-06-27 00:29:00 +02:00
|
|
|
if ((/*_mission_item.nav_cmd == NAV_CMD_LOITER_TURN_COUNT ||*/
|
|
|
|
|
_mission_item.nav_cmd == NAV_CMD_LOITER_UNLIMITED)) {
|
2014-06-06 17:17:41 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hrt_abstime now = hrt_absolute_time();
|
|
|
|
|
|
|
|
|
|
if (!_waypoint_position_reached) {
|
|
|
|
|
float dist = -1.0f;
|
|
|
|
|
float dist_xy = -1.0f;
|
|
|
|
|
float dist_z = -1.0f;
|
|
|
|
|
|
|
|
|
|
float altitude_amsl = _mission_item.altitude_is_relative
|
2014-06-27 11:34:19 +02:00
|
|
|
? _mission_item.altitude + _navigator->get_home_position()->alt
|
2014-06-06 17:17:41 +02:00
|
|
|
: _mission_item.altitude;
|
|
|
|
|
|
|
|
|
|
dist = get_distance_to_point_global_wgs84(_mission_item.lat, _mission_item.lon, altitude_amsl,
|
2014-06-27 11:34:19 +02:00
|
|
|
_navigator->get_global_position()->lat,
|
|
|
|
|
_navigator->get_global_position()->lon,
|
|
|
|
|
_navigator->get_global_position()->alt,
|
2014-06-06 17:17:41 +02:00
|
|
|
&dist_xy, &dist_z);
|
|
|
|
|
|
2014-06-27 11:34:19 +02:00
|
|
|
if (_mission_item.nav_cmd == NAV_CMD_TAKEOFF && _navigator->get_vstatus()->is_rotary_wing) {
|
2014-06-10 17:24:04 +02:00
|
|
|
/* require only altitude for takeoff for multicopter */
|
2014-06-27 11:34:19 +02:00
|
|
|
if (_navigator->get_global_position()->alt >
|
|
|
|
|
altitude_amsl - _navigator->get_acceptance_radius()) {
|
2014-06-10 17:24:04 +02:00
|
|
|
_waypoint_position_reached = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (_mission_item.nav_cmd == NAV_CMD_TAKEOFF) {
|
|
|
|
|
/* for takeoff mission items use the parameter for the takeoff acceptance radius */
|
2014-06-27 11:34:19 +02:00
|
|
|
if (dist >= 0.0f && dist <= _navigator->get_acceptance_radius()) {
|
2014-06-06 17:17:41 +02:00
|
|
|
_waypoint_position_reached = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-06-10 17:24:04 +02:00
|
|
|
/* for normal mission items used their acceptance radius */
|
2014-06-06 17:17:41 +02:00
|
|
|
if (dist >= 0.0f && dist <= _mission_item.acceptance_radius) {
|
|
|
|
|
_waypoint_position_reached = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_waypoint_position_reached && !_waypoint_yaw_reached) {
|
|
|
|
|
|
|
|
|
|
/* TODO: removed takeoff, why? */
|
2014-06-27 11:34:19 +02:00
|
|
|
if (_navigator->get_vstatus()->is_rotary_wing && isfinite(_mission_item.yaw)) {
|
2014-06-06 17:17:41 +02:00
|
|
|
|
|
|
|
|
/* check yaw if defined only for rotary wing except takeoff */
|
2014-06-27 11:34:19 +02:00
|
|
|
float yaw_err = _wrap_pi(_mission_item.yaw - _navigator->get_global_position()->yaw);
|
2014-06-06 17:17:41 +02:00
|
|
|
|
|
|
|
|
if (fabsf(yaw_err) < 0.2f) { /* TODO: get rid of magic number */
|
|
|
|
|
_waypoint_yaw_reached = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
_waypoint_yaw_reached = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if the current waypoint was reached */
|
|
|
|
|
if (_waypoint_position_reached && _waypoint_yaw_reached) {
|
|
|
|
|
|
|
|
|
|
if (_time_first_inside_orbit == 0) {
|
|
|
|
|
_time_first_inside_orbit = now;
|
|
|
|
|
|
|
|
|
|
// if (_mission_item.time_inside > 0.01f) {
|
|
|
|
|
// mavlink_log_info(_mavlink_fd, "#audio: waypoint reached, wait for %.1fs",
|
|
|
|
|
// (double)_mission_item.time_inside);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if the MAV was long enough inside the waypoint orbit */
|
|
|
|
|
if (now - _time_first_inside_orbit >= (hrt_abstime)_mission_item.time_inside * 1e6f) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MissionBlock::reset_mission_item_reached()
|
|
|
|
|
{
|
|
|
|
|
_waypoint_position_reached = false;
|
|
|
|
|
_waypoint_yaw_reached = false;
|
|
|
|
|
_time_first_inside_orbit = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MissionBlock::mission_item_to_position_setpoint(const struct mission_item_s *item, struct position_setpoint_s *sp)
|
|
|
|
|
{
|
|
|
|
|
sp->valid = true;
|
|
|
|
|
sp->lat = item->lat;
|
|
|
|
|
sp->lon = item->lon;
|
2014-06-27 11:34:19 +02:00
|
|
|
sp->alt = item->altitude_is_relative ? item->altitude + _navigator->get_home_position()->alt : item->altitude;
|
2014-06-06 17:17:41 +02:00
|
|
|
sp->yaw = item->yaw;
|
|
|
|
|
sp->loiter_radius = item->loiter_radius;
|
|
|
|
|
sp->loiter_direction = item->loiter_direction;
|
|
|
|
|
sp->pitch_min = item->pitch_min;
|
|
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
switch (item->nav_cmd) {
|
|
|
|
|
case NAV_CMD_IDLE:
|
|
|
|
|
sp->type = SETPOINT_TYPE_IDLE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NAV_CMD_TAKEOFF:
|
2014-06-06 17:17:41 +02:00
|
|
|
sp->type = SETPOINT_TYPE_TAKEOFF;
|
2014-06-27 00:27:08 +02:00
|
|
|
break;
|
2014-06-06 17:17:41 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case NAV_CMD_LAND:
|
2014-06-06 17:17:41 +02:00
|
|
|
sp->type = SETPOINT_TYPE_LAND;
|
2014-06-27 00:27:08 +02:00
|
|
|
break;
|
2014-06-06 17:17:41 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
case NAV_CMD_LOITER_TIME_LIMIT:
|
|
|
|
|
case NAV_CMD_LOITER_TURN_COUNT:
|
|
|
|
|
case NAV_CMD_LOITER_UNLIMITED:
|
2014-06-06 17:17:41 +02:00
|
|
|
sp->type = SETPOINT_TYPE_LOITER;
|
2014-06-27 00:27:08 +02:00
|
|
|
break;
|
2014-06-06 17:17:41 +02:00
|
|
|
|
2014-06-27 00:27:08 +02:00
|
|
|
default:
|
2014-06-06 17:17:41 +02:00
|
|
|
sp->type = SETPOINT_TYPE_POSITION;
|
2014-06-27 00:27:08 +02:00
|
|
|
break;
|
2014-06-06 17:17:41 +02:00
|
|
|
}
|
2014-06-25 17:13:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MissionBlock::set_previous_pos_setpoint(struct position_setpoint_triplet_s *pos_sp_triplet)
|
|
|
|
|
{
|
|
|
|
|
/* reuse current setpoint as previous setpoint */
|
|
|
|
|
if (pos_sp_triplet->current.valid) {
|
|
|
|
|
memcpy(&pos_sp_triplet->previous, &pos_sp_triplet->current, sizeof(struct position_setpoint_s));
|
|
|
|
|
}
|
2014-06-06 17:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-28 00:54:27 +02:00
|
|
|
void
|
2014-06-29 14:09:22 +02:00
|
|
|
MissionBlock::set_loiter_item(struct mission_item_s *item)
|
2014-06-06 17:17:41 +02:00
|
|
|
{
|
2014-06-28 00:54:27 +02:00
|
|
|
if (_navigator->get_vstatus()->condition_landed) {
|
|
|
|
|
/* landed, don't takeoff, but switch to IDLE mode */
|
2014-06-29 14:09:22 +02:00
|
|
|
item->nav_cmd = NAV_CMD_IDLE;
|
2014-06-28 00:54:27 +02:00
|
|
|
|
|
|
|
|
} else {
|
2014-06-29 14:09:22 +02:00
|
|
|
item->nav_cmd = NAV_CMD_LOITER_UNLIMITED;
|
|
|
|
|
|
|
|
|
|
struct position_setpoint_triplet_s *pos_sp_triplet = _navigator->get_position_setpoint_triplet();
|
2014-06-06 17:17:41 +02:00
|
|
|
|
2014-06-28 00:54:27 +02:00
|
|
|
if (_navigator->get_can_loiter_at_sp() && pos_sp_triplet->current.valid) {
|
|
|
|
|
/* use current position setpoint */
|
2014-06-29 14:09:22 +02:00
|
|
|
item->lat = pos_sp_triplet->current.lat;
|
|
|
|
|
item->lon = pos_sp_triplet->current.lon;
|
|
|
|
|
item->altitude = pos_sp_triplet->current.alt;
|
2014-06-28 00:54:27 +02:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
/* use current position */
|
2014-06-29 14:09:22 +02:00
|
|
|
item->lat = _navigator->get_global_position()->lat;
|
|
|
|
|
item->lon = _navigator->get_global_position()->lon;
|
|
|
|
|
item->altitude = _navigator->get_global_position()->alt;
|
2014-06-28 00:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-29 14:09:22 +02:00
|
|
|
item->altitude_is_relative = false;
|
|
|
|
|
item->yaw = NAN;
|
|
|
|
|
item->loiter_radius = _navigator->get_loiter_radius();
|
|
|
|
|
item->loiter_direction = 1;
|
|
|
|
|
item->acceptance_radius = _navigator->get_acceptance_radius();
|
|
|
|
|
item->time_inside = 0.0f;
|
|
|
|
|
item->pitch_min = 0.0f;
|
|
|
|
|
item->autocontinue = false;
|
|
|
|
|
item->origin = ORIGIN_ONBOARD;
|
2014-06-28 00:54:27 +02:00
|
|
|
}
|
2014-06-06 17:17:41 +02:00
|
|
|
}
|