forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_guided.pde
91 lines (81 loc) · 3.34 KB
/
control_guided.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
* control_guided.pde - init and run calls for guided flight mode
*/
#ifndef GUIDED_LOOK_AT_TARGET_MIN_DISTANCE_CM
# define GUIDED_LOOK_AT_TARGET_MIN_DISTANCE_CM 500 // point nose at target if it is more than 5m away
#endif
static bool guided_pilot_yaw_override_yaw = false;
// guided_init - initialise guided controller
static bool guided_init(bool ignore_checks)
{
if (GPS_ok() || ignore_checks) {
// initialise wpnav to stopping point at current altitude
// To-Do: set to current location if disarmed?
// To-Do: set to stopping point altitude?
Vector3f stopping_point;
stopping_point.z = inertial_nav.get_altitude();
wp_nav.get_wp_stopping_point_xy(stopping_point);
wp_nav.set_wp_destination(stopping_point);
guided_pilot_yaw_override_yaw = false;
// initialise yaw to hold at current heading (reset to point at waypoint in guided_set_destination)
set_auto_yaw_mode(AUTO_YAW_HOLD);
return true;
}else{
return false;
}
}
// guided_set_destination - sets guided mode's target destination
static void guided_set_destination(const Vector3f& destination)
{
if (control_mode == GUIDED) {
wp_nav.set_wp_destination(destination);
if (!guided_pilot_yaw_override_yaw) {
// get default yaw mode
set_auto_yaw_mode(get_default_auto_yaw_mode(false));
// point nose at next waypoint if it is more than 10m away
if (auto_yaw_mode == AUTO_YAW_LOOK_AT_NEXT_WP) {
// get distance to new location
wp_distance = wp_nav.get_wp_distance_to_destination();
// set original_wp_bearing to point at next waypoint
if (wp_distance >= GUIDED_LOOK_AT_TARGET_MIN_DISTANCE_CM) {
original_wp_bearing = wp_bearing;
}
}
}
}
}
// guided_run - runs the guided controller
// should be called at 100hz or more
static void guided_run()
{
// if not auto armed set throttle to zero and exit immediately
if(!ap.auto_armed) {
// To-Do: reset waypoint controller?
attitude_control.init_targets();
attitude_control.set_throttle_out(0, false);
// To-Do: handle take-offs - these may not only be immediately after auto_armed becomes true
return;
}
// process pilot's yaw input
float target_yaw_rate = 0;
if (!failsafe.radio) {
// get pilot's desired yaw rate
target_yaw_rate = get_pilot_desired_yaw_rate(g.rc_4.control_in);
if (target_yaw_rate != 0) {
set_auto_yaw_mode(AUTO_YAW_HOLD);
}
}
// run waypoint controller
wp_nav.update_wpnav();
// call z-axis position controller (wpnav should have already updated it's alt target)
pos_control.update_z_controller();
// call attitude controller
if (auto_yaw_mode == AUTO_YAW_HOLD) {
// roll & pitch from waypoint controller, yaw rate from pilot
attitude_control.angle_ef_roll_pitch_rate_ef_yaw(wp_nav.get_roll(), wp_nav.get_pitch(), target_yaw_rate);
}else{
// roll, pitch from waypoint controller, yaw heading from auto_heading()
attitude_control.angle_ef_roll_pitch_yaw(wp_nav.get_roll(), wp_nav.get_pitch(), get_auto_heading(),true);
}
}