55 #include <px4_platform_common/px4_config.h> 56 #include <px4_platform_common/defines.h> 57 #include <px4_platform_common/posix.h> 58 #include <px4_platform_common/tasks.h> 68 #define GEOFENCE_CHECK_INTERVAL 200000 78 ModuleParams(nullptr),
138 bool have_geofence_position_data =
false;
152 px4_pollfd_struct_t fds[2] {};
155 fds[0].fd = _local_pos_sub;
156 fds[0].events = POLLIN;
158 fds[1].events = POLLIN;
165 while (!should_exit()) {
168 int pret =
px4_poll(&fds[0], (
sizeof(fds) /
sizeof(fds[0])), 1000);
173 }
else if (pret < 0) {
175 PX4_ERR(
"poll error %d, %d", pret, errno);
180 if (fds[0].revents & POLLIN) {
195 have_geofence_position_data =
true;
204 have_geofence_position_data =
true;
226 if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_GO_AROUND) {
232 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_REPOSITION) {
245 rep->
current.
type = position_setpoint_s::SETPOINT_TYPE_LOITER;
248 if (cmd.param1 <= 0 || !PX4_ISFINITE(cmd.param1)) {
259 if (PX4_ISFINITE(cmd.param4)) {
268 if (PX4_ISFINITE(cmd.param5) && PX4_ISFINITE(cmd.param6)) {
271 rep->
current.
lat = (cmd.param5 < 1000) ? cmd.param5 : cmd.param5 / (
double)1e7;
272 rep->
current.
lon = (cmd.param6 < 1000) ? cmd.param6 : cmd.param6 / (
double)1e7;
274 if (PX4_ISFINITE(cmd.param7)) {
281 }
else if (PX4_ISFINITE(cmd.param7) && curr->
current.
valid 303 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_NAV_TAKEOFF) {
314 rep->
current.
type = position_setpoint_s::SETPOINT_TYPE_TAKEOFF;
325 if (PX4_ISFINITE(cmd.param5) && PX4_ISFINITE(cmd.param6)) {
326 rep->
current.
lat = (cmd.param5 < 1000) ? cmd.param5 : cmd.param5 / (
double)1e7;
327 rep->
current.
lon = (cmd.param6 < 1000) ? cmd.param6 : cmd.param6 / (
double)1e7;
342 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_LAND_START) {
349 vcmd.
command = vehicle_command_s::VEHICLE_CMD_MISSION_START;
354 PX4_WARN(
"planned mission landing not available");
359 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_MISSION_START) {
362 PX4_WARN(
"CMD_MISSION_START failed");
368 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_CHANGE_SPEED) {
388 }
else if (cmd.command == vehicle_command_s::VEHICLE_CMD_DO_SET_ROI
389 || cmd.command == vehicle_command_s::VEHICLE_CMD_NAV_ROI
390 || cmd.command == vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_LOCATION
391 || cmd.command == vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_WPNEXT_OFFSET
392 || cmd.command == vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_NONE) {
395 switch (cmd.command) {
396 case vehicle_command_s::VEHICLE_CMD_DO_SET_ROI:
397 case vehicle_command_s::VEHICLE_CMD_NAV_ROI:
401 case vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_LOCATION:
402 _vroi.
mode = vehicle_command_s::VEHICLE_ROI_LOCATION;
408 case vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_WPNEXT_OFFSET:
409 _vroi.
mode = vehicle_command_s::VEHICLE_ROI_WPNEXT;
415 case vehicle_command_s::VEHICLE_CMD_DO_SET_ROI_NONE:
416 _vroi.
mode = vehicle_command_s::VEHICLE_ROI_NONE;
420 _vroi.
mode = vehicle_command_s::VEHICLE_ROI_NONE;
436 if (have_geofence_position_data &&
443 have_geofence_position_data =
false;
474 case vehicle_status_s::NAVIGATION_STATE_AUTO_MISSION:
482 case vehicle_status_s::NAVIGATION_STATE_AUTO_LOITER:
484 navigation_mode_new = &
_loiter;
487 case vehicle_status_s::NAVIGATION_STATE_AUTO_RCRECOVER:
489 navigation_mode_new = &
_rcLoss;
492 case vehicle_status_s::NAVIGATION_STATE_AUTO_RTL: {
495 const bool rtl_activated =
_previous_nav_state != vehicle_status_s::NAVIGATION_STATE_AUTO_RTL;
516 navigation_mode_new = &
_rtl;
572 navigation_mode_new = &
_rtl;
583 navigation_mode_new = &
_rtl;
591 case vehicle_status_s::NAVIGATION_STATE_AUTO_TAKEOFF:
596 case vehicle_status_s::NAVIGATION_STATE_AUTO_LAND:
598 navigation_mode_new = &
_land;
601 case vehicle_status_s::NAVIGATION_STATE_AUTO_PRECLAND:
607 case vehicle_status_s::NAVIGATION_STATE_AUTO_RTGS:
612 case vehicle_status_s::NAVIGATION_STATE_AUTO_LANDENGFAIL:
617 case vehicle_status_s::NAVIGATION_STATE_AUTO_LANDGPSFAIL:
622 case vehicle_status_s::NAVIGATION_STATE_AUTO_FOLLOW_TARGET:
627 case vehicle_status_s::NAVIGATION_STATE_MANUAL:
628 case vehicle_status_s::NAVIGATION_STATE_ACRO:
629 case vehicle_status_s::NAVIGATION_STATE_ALTCTL:
630 case vehicle_status_s::NAVIGATION_STATE_POSCTL:
631 case vehicle_status_s::NAVIGATION_STATE_DESCEND:
632 case vehicle_status_s::NAVIGATION_STATE_TERMINATION:
633 case vehicle_status_s::NAVIGATION_STATE_OFFBOARD:
634 case vehicle_status_s::NAVIGATION_STATE_STAB:
636 navigation_mode_new =
nullptr;
653 navigation_mode_new == &
_loiter)) {
699 _task_id = px4_task_spawn_cmd(
"navigator",
701 SCHED_PRIORITY_NAVIGATION,
703 (px4_main_t)&run_trampoline,
704 (
char *
const *)argv);
718 if (instance ==
nullptr) {
719 PX4_ERR(
"alloc failed");
745 return _param_nav_acc_rad.get();
758 return _param_nav_fw_alt_rad.get();
760 }
else if (
get_vstatus()->vehicle_type == vehicle_status_s::VEHICLE_TYPE_ROVER) {
764 float alt_acceptance_radius = _param_nav_mc_alt_rad.get();
773 return alt_acceptance_radius;
783 if (next_sp.
type == position_setpoint_s::SETPOINT_TYPE_LAND && next_sp.
valid) {
785 return _param_nav_fw_altl_rad.get();
856 float radius = mission_item_radius;
875 float yaw = mission_item_yaw;
899 float altitude_diff,
float hor_velocity,
float ver_velocity)
912 tr.icao_address = 1234;
915 tr.altitude_type = 0;
917 tr.heading = traffic_heading;
918 tr.hor_velocity = hor_velocity;
919 tr.ver_velocity = ver_velocity;
920 strncpy(&tr.callsign[0], callsign,
sizeof(tr.callsign) - 1);
921 tr.callsign[
sizeof(tr.callsign) - 1] = 0;
924 tr.flags = transponder_report_s::PX4_ADSB_FLAGS_VALID_COORDS | transponder_report_s::PX4_ADSB_FLAGS_VALID_HEADING |
925 transponder_report_s::PX4_ADSB_FLAGS_VALID_VELOCITY |
926 transponder_report_s::PX4_ADSB_FLAGS_VALID_ALTITUDE |
927 transponder_report_s::PX4_ADSB_FLAGS_VALID_CALLSIGN;
948 float horizontal_separation = 500;
949 float vertical_separation = 500;
956 uint16_t required_flags = transponder_report_s::PX4_ADSB_FLAGS_VALID_COORDS |
957 transponder_report_s::PX4_ADSB_FLAGS_VALID_HEADING |
958 transponder_report_s::PX4_ADSB_FLAGS_VALID_VELOCITY | transponder_report_s::PX4_ADSB_FLAGS_VALID_ALTITUDE;
960 if ((tr.flags & required_flags) != required_flags) {
967 tr.lat, tr.lon, tr.altitude, &d_hor, &d_vert);
971 float end_alt = tr.altitude + (d_vert / tr.hor_velocity) * tr.ver_velocity;
974 float prediction_distance = d_hor + 1000.0f;
986 if ((fabsf(alt - tr.altitude) < vertical_separation) || ((end_alt - horizontal_separation) < alt)) {
988 double end_lat, end_lon;
1000 switch (_param_nav_traff_avoid.get()) {
1004 PX4_WARN(
"TRAFFIC %s, hdg: %d", tr.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_CALLSIGN ? tr.callsign :
1012 tr.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_CALLSIGN ? tr.callsign :
"unknown",
1019 tr.flags & transponder_report_s::PX4_ADSB_FLAGS_VALID_CALLSIGN ? tr.callsign :
"unknown",
1027 vcmd.
command = vehicle_command_s::VEHICLE_CMD_NAV_RETURN_TO_LAUNCH;
1044 bool should_abort =
false;
1061 return should_abort;
1069 && _param_nav_force_vt.get();
1079 if (!strcmp(argv[0],
"fencefile")) {
1083 }
else if (!strcmp(argv[0],
"fake_traffic")) {
1084 get_instance()->fake_traffic(
"LX007", 500, 1.0
f, -1.0
f, 100.0
f, 90.0
f, 0.001
f);
1085 get_instance()->fake_traffic(
"LX55", 1000, 0, 0, 100.0
f, 90.0
f, 0.001
f);
1086 get_instance()->fake_traffic(
"LX20", 15000, 1.0
f, -1.0
f, 280.0
f, 90.0
f, 0.001
f);
1163 command_ack.
result = result;
1173 PX4_WARN(
"%s\n", reason);
1176 PRINT_MODULE_DESCRIPTION(
1179 Module that is responsible for autonomous flight modes. This includes missions (read from dataman), 1181 It is also responsible for geofence violation checking. 1184 The different internal modes are implemented as separate classes that inherit from a common base class `NavigatorMode`. 1185 The member `_navigation_mode` contains the current active mode. 1187 Navigator publishes position setpoint triplets (`position_setpoint_triplet_s`), which are then used by the position 1192 PRINT_MODULE_USAGE_NAME("navigator",
"controller");
1193 PRINT_MODULE_USAGE_COMMAND(
"start");
1194 PRINT_MODULE_USAGE_COMMAND_DESCR(
"fencefile",
"load a geofence file from SD card, stored at etc/geofence.txt");
1195 PRINT_MODULE_USAGE_COMMAND_DESCR(
"fake_traffic",
"publishes 3 fake transponder_report_s uORB messages");
1196 PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
#define VEHICLE_TYPE_FIXED_WING
position_setpoint_triplet_s _pos_sp_triplet
triplet of position setpoints
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
geofence_result_s _geofence_result
uORB::Subscription _home_pos_sub
home position subscription
uORB::SubscriptionData< position_controller_status_s > _position_controller_status_sub
bool home_position_valid()
uORB::PublicationQueued< vehicle_command_s > _vehicle_cmd_pub
float get_distance_to_point_global_wgs84(double lat_now, double lon_now, float alt_now, double lat_next, double lon_next, float alt_next, float *dist_xy, float *dist_z)
#define mavlink_log_critical(_pub, _text,...)
Send a mavlink critical message and print to console.
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
uORB::Subscription _vehicle_command_sub
vehicle commands (onboard and offboard)
void publish_position_setpoint_triplet()
Publish a new position setpoint triplet for position controllers.
Land _land
class for handling land commands
orb_advert_t _mavlink_log_pub
the uORB advert to send messages over mavlink
float get_default_altitude_acceptance_radius()
Get the default altitude acceptance radius (i.e.
vehicle_gps_position_s _gps_pos
gps position
measure the time elapsed performing an event
Helper class to access missions.
#define GEOFENCE_CHECK_INTERVAL
NavigatorMode * _navigation_mode_array[NAVIGATOR_MODE_ARRAY_SIZE]
array of navigation modes
float get_acceptance_radius()
Get the acceptance radius.
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition of geo / math functions to perform geodesic calculations.
#define NAVIGATOR_MODE_ARRAY_SIZE
Number of navigation modes that need on_active/on_inactive calls.
bool is_planned_mission() const
vehicle_local_position_s _local_pos
local vehicle position
static int custom_command(int argc, char *argv[])
bool _geofence_violation_warning_sent
prevents spaming to mavlink
vehicle_global_position_s _global_pos
global vehicle position
int main(int argc, char **argv)
float get_cruising_throttle()
Get the target throttle.
struct position_setpoint_s next
uORB::Subscription _global_pos_sub
global position subscription
int orb_set_interval(int handle, unsigned interval)
param_t _handle_back_trans_dec_mss
bool publish(const T &data)
Publish the struct.
int print_status() override
__EXPORT int navigator_main(int argc, char *argv[])
navigator app start / stop handling function
NavigatorMode * _navigation_mode
abstract pointer to current navigation mode class
struct position_setpoint_triplet_s * get_takeoff_triplet()
bool _pos_sp_triplet_updated
flags if position SP triplet needs to be published
struct position_setpoint_s previous
float _param_reverse_delay
uORB::Subscription _pos_ctrl_landing_status_sub
position controller landing status subscription
#define GEOFENCE_FILENAME
struct vehicle_land_detected_s * get_land_detected()
home_position_s _home_pos
home position for RTL
uint16_t item_do_jump_remaining
constexpr T degrees(T radians)
void printStatus()
print Geofence status to the console
float altitude_acceptance
High-resolution timer with callouts and timekeeping.
vehicle_roi_s _vroi
vehicle ROI
mission_result_s _mission_result
GpsFailure _gpsFailure
class that handles the OBC gpsfailure loss mode
Geofence _geofence
class that handles the geofence
#define mavlink_and_console_log_info(_pub, _text,...)
Send a mavlink emergency message and print to console.
vehicle_land_detected_s _land_detected
vehicle land_detected
perf_counter_t _loop_perf
loop performance counter
void set_return_alt_min(bool min)
int _vehicle_status_sub
local position subscription
int orb_subscribe(const struct orb_metadata *meta)
uORB::Publication< mission_result_s > _mission_result_pub
void reset_cruising_speed()
Reset cruising speed to default values.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
bool item_do_jump_changed
struct position_setpoint_s current
uint16_t get_land_start_index() const
bool publish(const T &data)
Publish the struct.
void check_traffic()
Check nearby traffic for potential collisions.
void publish_vehicle_command_ack(const vehicle_command_s &cmd, uint8_t result)
void reset_triplets()
Set triplets to invalid.
bool _mission_result_updated
flags if mission result has seen an update
bool get_mission_changed() const
PrecLand _precland
class for handling precision land commands
struct vehicle_global_position_s * get_global_position()
uORB::Publication< vehicle_roi_s > _vehicle_roi_pub
void waypoint_from_heading_and_distance(double lat_start, double lon_start, float bearing, float dist, double *lat_target, double *lon_target)
Creates a waypoint from given waypoint, distance and bearing see http://www.movable-type.co.uk/scripts/latlong.html.
bool get_mission_finished() const
float get_cruising_speed()
Get the cruising speed.
bool set_current_mission_index(uint16_t index)
bool check(const vehicle_global_position_s &global_position, const vehicle_gps_position_s &gps_position, const home_position_s home_pos, bool home_position_set)
Return whether the system obeys the geofence.
uORB::Subscription _gps_pos_sub
gps position subscription
int orb_unsubscribe(int handle)
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
uint8_t _previous_nav_state
nav_state of the previous iteration
static int print_usage(const char *reason=nullptr)
EngineFailure _engineFailure
class that handles the engine failure mode (FW only!)
void perf_end(perf_counter_t handle)
End a performance event.
static Navigator * instantiate(int argc, char *argv[])
vehicle_status_s _vstatus
vehicle status
void set_closest_item_as_current()
void set_execution_mode(const uint8_t mode)
Set a new mission mode and handle the switching between the different modes.
bool updated()
Check if there is a new update.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
RCLoss _rcLoss
class that handles RTL according to OBC rules (rc loss mode)
bool get_land_start_available() const
Mission _mission
class that handles the missions
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
FollowTarget _follow_target
uORB::Subscription _parameter_update_sub
param update subscription
uORB::Subscription _traffic_sub
traffic subscription
struct position_setpoint_triplet_s * get_position_setpoint_triplet()
uint16_t item_changed_index
uORB::PublicationQueued< vehicle_command_ack_s > _vehicle_cmd_ack_pub
DataLinkLoss _dataLinkLoss
class that handles the OBC datalink loss mode
param_t _handle_reverse_delay
float _mission_cruising_speed_fw
void set_cruising_throttle(float throttle=-1.0f)
Set the target throttle.
static int task_spawn(int argc, char *argv[])
bool start_mission_landing()
void load_fence_from_file(const char *filename)
Load fence from file.
void publish_vehicle_cmd(vehicle_command_s *vcmd)
float get_loiter_radius()
Takeoff _takeoff
class for handling takeoff commands
float get_default_acceptance_radius()
Returns the default acceptance radius defined by the parameter.
float get_yaw_acceptance(float mission_item_yaw)
Get the yaw acceptance given the current mission item.
int loadFromFile(const char *filename)
Load a single inclusion polygon, replacing any already existing polygons.
struct position_setpoint_triplet_s * get_reposition_triplet()
RTL _rtl
class that handles RTL
bool on_mission_landing()
bool _pos_sp_triplet_published_invalid_once
flags if position SP triplet has been published once to UORB
orb_advert_t * get_mavlink_log_pub()
bool get_mission_waypoints_changed() const
bool update(void *dst)
Update the struct.
uORB::Subscription _land_detected_sub
vehicle land detected subscription
struct vehicle_status_s * get_vstatus()
void set_mission_result_updated()
uORB::Publication< geofence_result_s > _geofence_result_pub
int get_distance_to_line(struct crosstrack_error_s *crosstrack_error, double lat_now, double lon_now, double lat_start, double lon_start, double lat_end, double lon_end)
float _param_back_trans_dec_mss
Loiter _loiter
class that handles loiter
float _mission_cruising_speed_mc
bool copy(void *dst)
Copy the struct.
void set_mission_failure(const char *reason)
void fake_traffic(const char *callsign, float distance, float direction, float traffic_heading, float altitude_diff, float hor_velocity, float ver_velocity)
Generate an artificial traffic indication.
void publish_mission_result()
Publish the mission result so commander and mavlink know what is going on.
void perf_begin(perf_counter_t handle)
Begin a performance event.
bool _can_loiter_at_sp
flags if current position SP can be used to loiter
float get_altitude_acceptance_radius()
Get the altitude acceptance radius.
uORB::Publication< position_setpoint_triplet_s > _pos_sp_triplet_pub
void set_mode(PrecLandMode mode)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
struct vehicle_local_position_s * get_local_position()
void set_cruising_speed(float speed=-1.0f)
Set the cruising speed.