Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
nav_line.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Anton Kochevar, ENAC
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
28 #include "generated/airframe.h"
29 #include "modules/nav/nav_line.h"
31 
32 #if USE_MISSION
34 
35 static bool nav_line_mission(uint8_t nb, float *params, enum MissionRunFlag flag)
36 {
37  if (nb != 3) {
38  return false; // wrong number of parameters
39  }
40  if (flag == MissionInit) {
42  } else if (flag == MissionUpdate) {
43  return false; // nothing to do on update
44  }
45  uint8_t p1 = (uint8_t)(params[0]);
46  uint8_t p2 = (uint8_t)(params[1]);
47  float radius = params[2];
48  return nav_line_run(p1, p2, radius);
49 }
50 #endif
51 
54 static enum line_status line_status;
55 
56 void nav_line_init(void)
57 {
58 #if USE_MISSION
59  mission_register(nav_line_mission, "LINE");
60 #endif
61 }
62 
63 void nav_line_setup(void)
64 {
65  line_status = LR12;
66 }
67 
68 bool nav_line_run(uint8_t l1, uint8_t l2, float radius)
69 {
70  radius = fabs(radius);
71  float alt = waypoints[l1].a;
72  waypoints[l2].a = alt;
73 
74  float l2_l1_x = WaypointX(l1) - WaypointX(l2);
75  float l2_l1_y = WaypointY(l1) - WaypointY(l2);
76  float d = sqrt(l2_l1_x * l2_l1_x + l2_l1_y * l2_l1_y);
77 
78  /* Unit vector from l1 to l2 */
79  float u_x = l2_l1_x / d;
80  float u_y = l2_l1_y / d;
81 
82  /* The half circle centers and the other leg */
83  struct point l2_c1 = { WaypointX(l1) + radius * u_y,
84  WaypointY(l1) + radius * -u_x,
85  alt
86  };
87  struct point l2_c2 = { WaypointX(l1) + 1.732 * radius * u_x,
88  WaypointY(l1) + 1.732 * radius * u_y,
89  alt
90  };
91  struct point l2_c3 = { WaypointX(l1) + radius * -u_y,
92  WaypointY(l1) + radius * u_x,
93  alt
94  };
95 
96  struct point l1_c1 = { WaypointX(l2) + radius * -u_y,
97  WaypointY(l2) + radius * u_x,
98  alt
99  };
100  struct point l1_c2 = { WaypointX(l2) + 1.732 * radius * -u_x,
101  WaypointY(l2) + 1.732 * radius * -u_y,
102  alt
103  };
104  struct point l1_c3 = { WaypointX(l2) + radius * u_y,
105  WaypointY(l2) + radius * -u_x,
106  alt
107  };
108  float qdr_out_2_1 = M_PI / 3. - atan2(u_y, u_x);
109 
110  float qdr_out_2_2 = -M_PI / 3. - atan2(u_y, u_x);
111  float qdr_out_2_3 = M_PI - atan2(u_y, u_x);
112 
113  /* Vertical target */
114  NavVerticalAutoThrottleMode(0); /* No pitch */
116 
117  switch (line_status) {
118  case LR12: /* From wp l2 to wp l1 */
119  NavSegment(l2, l1);
120  if (NavApproachingFrom(l1, l2, CARROT)) {
121  line_status = LQC21;
122  nav_init_stage();
123  }
124  break;
125  case LQC21:
126  nav_circle_XY(l2_c1.x, l2_c1.y, radius);
127  if (NavQdrCloseTo(DegOfRad(qdr_out_2_1) - 10)) {
128  line_status = LTC2;
129  nav_init_stage();
130  }
131  break;
132  case LTC2:
133  nav_circle_XY(l2_c2.x, l2_c2.y, -radius);
134  if (NavQdrCloseTo(DegOfRad(qdr_out_2_2) + 10)) {
135  line_status = LQC22;
136  nav_init_stage();
137  }
138  break;
139  case LQC22:
140  nav_circle_XY(l2_c3.x, l2_c3.y, radius);
141  if (NavQdrCloseTo(DegOfRad(qdr_out_2_3) - 10)) {
142  line_status = LR21;
143  nav_init_stage();
144  }
145  break;
146  case LR21: /* From wp l1 to wp l2 */
147  NavSegment(l1, l2);
148  if (NavApproachingFrom(l2, l1, CARROT)) {
149  line_status = LQC12;
150  nav_init_stage();
151  }
152  break;
153  case LQC12:
154  nav_circle_XY(l1_c1.x, l1_c1.y, radius);
155  if (NavQdrCloseTo(DegOfRad(qdr_out_2_1 + M_PI) - 10)) {
156  line_status = LTC1;
157  nav_init_stage();
158  }
159  break;
160  case LTC1:
161  nav_circle_XY(l1_c2.x, l1_c2.y, -radius);
162  if (NavQdrCloseTo(DegOfRad(qdr_out_2_2 + M_PI) + 10)) {
163  line_status = LQC11;
164  nav_init_stage();
165  }
166  break;
167  case LQC11:
168  nav_circle_XY(l1_c3.x, l1_c3.y, radius);
169  if (NavQdrCloseTo(DegOfRad(qdr_out_2_3 + M_PI) - 10)) {
170  line_status = LR12;
171  nav_init_stage();
172  }
173  break;
174  default: /* Should not occur !!! End the pattern */
175  return false;
176  }
177  return true; /* This pattern never ends */
178 }
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:44
#define WaypointX(_wp)
Definition: common_nav.h:45
#define WaypointAlt(_wp)
waypoint altitude in m above MSL
Definition: common_nav.h:48
float y
Definition: common_nav.h:41
float a
Definition: common_nav.h:42
float x
Definition: common_nav.h:40
#define WaypointY(_wp)
Definition: common_nav.h:46
bool mission_register(mission_custom_cb cb, char *type)
Register a new navigation or action callback function.
mission planner library
MissionRunFlag
@ MissionInit
first exec
@ MissionUpdate
param update
void nav_init_stage(void)
needs to be implemented by fixedwing and rotorcraft seperately
Definition: nav.c:92
void nav_circle_XY(float x, float y, float radius)
Navigates around (x, y).
Definition: nav.c:108
Fixedwing Navigation library.
#define NavApproachingFrom(wp, from, time)
Definition: nav.h:172
#define NavQdrCloseTo(x)
True if x (in degrees) is close to the current QDR (less than 10 degrees)
Definition: nav.h:161
#define NavSegment(_start, _end)
Definition: nav.h:167
#define NavVerticalAltitudeMode(_alt, _pre_climb)
Set the vertical mode to altitude control with the specified altitude setpoint and climb pre-command.
Definition: nav.h:191
#define NavVerticalAutoThrottleMode(_pitch)
Set the climb control to auto-throttle with the specified pitch pre-command.
Definition: nav.h:177
void nav_line_setup(void)
Definition: nav_line.c:63
void nav_line_init(void)
Definition: nav_line.c:56
line_status
Status along the pattern.
Definition: nav_line.c:53
@ LQC21
Definition: nav_line.c:53
@ LQC22
Definition: nav_line.c:53
@ LR21
Definition: nav_line.c:53
@ LQC11
Definition: nav_line.c:53
@ LTC2
Definition: nav_line.c:53
@ LQC12
Definition: nav_line.c:53
@ LR12
Definition: nav_line.c:53
@ LTC1
Definition: nav_line.c:53
bool nav_line_run(uint8_t l1, uint8_t l2, float radius)
Definition: nav_line.c:68
Fixedwing navigation along a line with nice U-turns.
#define CARROT
default approaching_time for a wp
Definition: navigation.h:70
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98