Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tcas.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 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  */
22 
28 #include "multi/tcas.h"
29 #include "generated/airframe.h"
30 #include "state.h"
32 #include "subsystems/gps.h"
33 #include "generated/flight_plan.h"
34 
35 #include "messages.h"
37 
40 
45 
46 #ifndef TCAS_TAU_TA // Traffic Advisory
47 #define TCAS_TAU_TA 2*CARROT
48 #endif
49 
50 #ifndef TCAS_TAU_RA // Resolution Advisory
51 #define TCAS_TAU_RA CARROT
52 #endif
53 
54 #ifndef TCAS_DMOD // Distance Modification
55 #define TCAS_DMOD 10.
56 #endif
57 
58 #ifndef TCAS_ALIM // Altitude Limit
59 #define TCAS_ALIM 15.
60 #endif
61 
62 #ifndef TCAS_DT_MAX // ms (lost com and timeout)
63 #define TCAS_DT_MAX 1500
64 #endif
65 
66 #define TCAS_HUGE_TAU 100*TCAS_TAU_TA
67 
68 /* AC is inside the horizontol dmod area and twice the vertical alim separation */
69 #define TCAS_IsInside() ( (ddh < Square(tcas_dmod) && ddv < Square(2*tcas_alim)) ? 1 : 0 )
70 
71 void tcas_init(void)
72 {
73  tcas_alt_setpoint = GROUND_ALT + SECURITY_HEIGHT;
80  tcas_ac_RA = AC_ID;
81  uint8_t i;
82  for (i = 0; i < NB_ACS; i++) {
85  }
86 }
87 
89 {
90  struct ac_info_ * ac = get_ac_info(id);
91  float dz = ac->alt - stateGetPositionUtm_f()->alt;
92  if (dz > tcas_alim / 2) { return RA_DESCEND; }
93  else if (dz < -tcas_alim / 2) { return RA_CLIMB; }
94  else { // AC with the smallest ID descend
95  if (AC_ID < id) { return RA_DESCEND; }
96  else { return RA_CLIMB; }
97  }
98 }
99 
100 
101 /* conflicts detection and monitoring */
103 {
104  // no TCAS under security_height
105  if (stateGetPositionUtm_f()->alt < GROUND_ALT + SECURITY_HEIGHT) {
106  uint8_t i;
107  for (i = 0; i < NB_ACS; i++) { tcas_acs_status[i].status = TCAS_NO_ALARM; }
108  return;
109  }
110  // test possible conflicts
111  float tau_min = tcas_tau_ta;
112  uint8_t ac_id_close = AC_ID;
113  uint8_t i;
116  for (i = 2; i < NB_ACS; i++) {
117  if (the_acs[i].ac_id == 0) { continue; } // no AC data
118  uint32_t dt = gps.tow - the_acs[i].itow;
119  if (dt > 3 * TCAS_DT_MAX) {
120  tcas_acs_status[i].status = TCAS_NO_ALARM; // timeout, reset status
121  continue;
122  }
123  if (dt > TCAS_DT_MAX) { continue; } // lost com but keep current status
124  float dx = the_acs[i].east - stateGetPositionEnu_f()->x;
125  float dy = the_acs[i].north - stateGetPositionEnu_f()->y;
126  float dz = the_acs[i].alt - stateGetPositionUtm_f()->alt;
127  float dvx = vx - the_acs[i].gspeed * sinf(the_acs[i].course);
128  float dvy = vy - the_acs[i].gspeed * cosf(the_acs[i].course);
129  float dvz = stateGetSpeedEnu_f()->z - the_acs[i].climb;
130  float scal = dvx * dx + dvy * dy + dvz * dz;
131  float ddh = dx * dx + dy * dy;
132  float ddv = dz * dz;
133  float tau = TCAS_HUGE_TAU;
134  if (scal > 0.) { tau = (ddh + ddv) / scal; }
135  // monitor conflicts
136  uint8_t inside = TCAS_IsInside();
137  //enum tcas_resolve test_dir = RA_NONE;
138  switch (tcas_acs_status[i].status) {
139  case TCAS_RA:
140  if (tau >= TCAS_HUGE_TAU && !inside) {
141  tcas_acs_status[i].status = TCAS_NO_ALARM; // conflict is now resolved
143  DOWNLINK_SEND_TCAS_RESOLVED(DefaultChannel, DefaultDevice, &(the_acs[i].ac_id));
144  }
145  break;
146  case TCAS_TA:
147  if (tau < tcas_tau_ra || inside) {
148  tcas_acs_status[i].status = TCAS_RA; // TA -> RA
149  // Downlink alert
150  //test_dir = tcas_test_direction(the_acs[i].ac_id);
151  //DOWNLINK_SEND_TCAS_RA(DefaultChannel, DefaultDevice,&(the_acs[i].ac_id),&test_dir);// FIXME only one closest AC ???
152  break;
153  }
154  if (tau > tcas_tau_ta && !inside) {
155  tcas_acs_status[i].status = TCAS_NO_ALARM; // conflict is now resolved
156  }
158  DOWNLINK_SEND_TCAS_RESOLVED(DefaultChannel, DefaultDevice, &(the_acs[i].ac_id));
159  break;
160  case TCAS_NO_ALARM:
161  if (tau < tcas_tau_ta || inside) {
162  tcas_acs_status[i].status = TCAS_TA; // NO_ALARM -> TA
163  // Downlink warning
164  DOWNLINK_SEND_TCAS_TA(DefaultChannel, DefaultDevice, &(the_acs[i].ac_id));
165  }
166  if (tau < tcas_tau_ra || inside) {
167  tcas_acs_status[i].status = TCAS_RA; // NO_ALARM -> RA = big problem ?
168  // Downlink alert
169  //test_dir = tcas_test_direction(the_acs[i].ac_id);
170  //DOWNLINK_SEND_TCAS_RA(DefaultChannel, DefaultDevice,&(the_acs[i].ac_id),&test_dir);
171  }
172  break;
173  }
174  // store closest AC
175  if (tau < tau_min) {
176  tau_min = tau;
177  ac_id_close = the_acs[i].ac_id;
178 
179  }
180  }
181  // set current conflict mode
183  ac_id_close = tcas_ac_RA; // keep RA until resolved
184  }
186  // at least one in conflict, deal with closest one
187  if (tcas_status == TCAS_RA) {
188  tcas_ac_RA = ac_id_close;
191  if (ac_resolve != RA_NONE) { // first resolution, no message received
192  if (ac_resolve == tcas_resolve) { // same direction, lowest id go down
193  if (AC_ID < tcas_ac_RA) { tcas_resolve = RA_DESCEND; }
194  else { tcas_resolve = RA_CLIMB; }
195  }
196  tcas_acs_status[the_acs_id[tcas_ac_RA]].resolve = RA_LEVEL; // assuming level flight for now
197  } else { // second resolution or message received
198  if (ac_resolve != RA_LEVEL) { // message received
199  if (ac_resolve == tcas_resolve) { // same direction, lowest id go down
200  if (AC_ID < tcas_ac_RA) { tcas_resolve = RA_DESCEND; }
201  else { tcas_resolve = RA_CLIMB; }
202  }
203  } else { // no message
204  if (tcas_resolve == RA_CLIMB && the_acs[the_acs_id[tcas_ac_RA]].climb > 1.0) { tcas_resolve = RA_DESCEND; } // revert resolve
205  else if (tcas_resolve == RA_DESCEND && the_acs[the_acs_id[tcas_ac_RA]].climb < -1.0) { tcas_resolve = RA_CLIMB; } // revert resolve
206  }
207  }
208  // Downlink alert
209  uint8_t resolve = tcas_resolve;
210  DOWNLINK_SEND_TCAS_RA(DefaultChannel, DefaultDevice, &tcas_ac_RA, &resolve);
211  } else { tcas_ac_RA = AC_ID; } // no conflict
212 #ifdef TCAS_DEBUG
213  if (tcas_status == TCAS_RA) { DOWNLINK_SEND_TCAS_DEBUG(DefaultChannel, DefaultDevice, &ac_id_close, &tau_min); }
214 #endif
215 }
216 
217 
218 /* altitude control loop */
220 {
221  // set alt setpoint
222  if (stateGetPositionUtm_f()->alt > GROUND_ALT + SECURITY_HEIGHT && tcas_status == TCAS_RA) {
223  struct ac_info_ * ac = get_ac_info(tcas_ac_RA);
224  switch (tcas_resolve) {
225  case RA_CLIMB :
227  break;
228  case RA_DESCEND :
230  break;
231  case RA_LEVEL :
232  case RA_NONE :
234  break;
235  }
236  // Bound alt
237  tcas_alt_setpoint = Max(GROUND_ALT + SECURITY_HEIGHT, tcas_alt_setpoint);
238  } else {
241  }
242 }
uint8_t ac_id
Definition: sim_ap.c:47
status
Definition: anemotaxis.c:10
static enum tcas_resolve tcas_test_direction(uint8_t id)
Definition: tcas.c:88
float tcas_alt_setpoint
Definition: tcas.c:38
static float stateGetHorizontalSpeedNorm_f(void)
Get norm of horizontal ground speed (float).
Definition: state.h:912
#define TCAS_DMOD
Definition: tcas.c:55
#define Min(x, y)
enum tcas_resolve resolve
Definition: tcas.h:48
uint8_t the_acs_id[NB_ACS_ID]
Definition: traffic_info.c:34
struct ac_info_ the_acs[NB_ACS]
Definition: traffic_info.c:35
float alt
in meters above WGS84 reference ellipsoid
uint8_t ac_id
Definition: traffic_info.h:36
float gspeed
Definition: traffic_info.h:41
Definition: tcas.h:40
float course
Definition: traffic_info.h:39
float north
Definition: traffic_info.h:38
uint8_t tcas_ac_RA
Definition: tcas.c:43
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:702
struct ac_info_ * get_ac_info(uint8_t id)
Definition: traffic_info.c:45
#define TCAS_RA
Definition: tcas.h:39
enum tcas_resolve tcas_resolve
Definition: tcas.c:42
float tcas_alim
Definition: tcas.c:39
float alt
Definition: traffic_info.h:40
struct tcas_ac_status tcas_acs_status[NB_ACS]
Definition: tcas.c:44
#define TCAS_TAU_TA
Definition: tcas.c:47
uint8_t status
Definition: tcas.h:47
float tcas_dmod
Definition: tcas.c:39
#define Max(x, y)
float east
Definition: traffic_info.h:37
float x
in meters
#define TCAS_NO_ALARM
Definition: tcas.h:37
uint32_t tow
GPS time of week in ms.
Definition: gps.h:84
uint32_t itow
Definition: traffic_info.h:43
Device independent GPS code (interface)
unsigned long uint32_t
Definition: types.h:18
Definition: tcas.h:40
float tcas_tau_ta
Definition: tcas.c:39
Definition: tcas.h:40
static float stateGetHorizontalSpeedDir_f(void)
Get dir of horizontal ground speed (float).
Definition: state.h:921
static struct UtmCoor_f * stateGetPositionUtm_f(void)
Get position in UTM coordinates (float).
Definition: state.h:675
#define NB_ACS
Definition: traffic_info.h:33
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:894
float z
in meters
float climb
Definition: traffic_info.h:42
Collision avoidance library.
#define TCAS_TA
Definition: tcas.h:38
void tcas_periodic_task_1Hz(void)
Definition: tcas.c:102
#define TCAS_ALIM
Definition: tcas.c:59
#define TCAS_HUGE_TAU
Definition: tcas.c:66
#define TCAS_TAU_RA
Definition: tcas.c:51
#define TCAS_IsInside()
Definition: tcas.c:69
#define TCAS_DT_MAX
Definition: tcas.c:63
uint8_t tcas_status
Definition: tcas.c:41
float tcas_tau_ra
Definition: tcas.c:39
float y
in meters
void tcas_periodic_task_4Hz(void)
Definition: tcas.c:219
struct GpsState gps
global GPS state
Definition: gps.c:41
void tcas_init(void)
Definition: tcas.c:71
tcas_resolve
Definition: tcas.h:40