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
datalink.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Pascal Brisset, Antoine Drouin
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 #define DATALINK_C
29 
30 #define MODULES_DATALINK_C
31 
32 #include <inttypes.h>
33 #include <string.h>
35 
36 #include "generated/modules.h"
37 
38 #ifdef TRAFFIC_INFO
40 #endif // TRAFFIC_INFO
41 
42 #if defined NAV || defined WIND_INFO
43 #include "state.h"
45 #endif
46 
47 #ifdef HITL
48 #include "subsystems/gps.h"
49 #endif
50 
51 
53 #include "generated/settings.h"
55 
56 #include "mcu_periph/uart.h"
58 
59 
60 #if USE_JOYSTICK
62 #include "autopilot.h"
64 #define JoystickHandeDatalink(_roll_int8, _pitch_int8, _throttle_int8) { \
65  if (pprz_mode == PPRZ_MODE_AUTO2 && nav_block == joystick_block) { \
66  h_ctl_roll_setpoint = _roll_int8 * (AUTO1_MAX_ROLL / 0x7f); \
67  h_ctl_pitch_setpoint = _pitch_int8 * (AUTO1_MAX_PITCH / 0x7f); \
68  v_ctl_throttle_setpoint = (MAX_PPRZ/0x7f) * _throttle_int8; \
69  } \
70  }
71 #endif
72 
73 #if defined RADIO_CONTROL && defined RADIO_CONTROL_TYPE_DATALINK
75 #endif
76 
77 #define MOfCm(_x) (((float)(_x))/100.)
78 #define MOfMM(_x) (((float)(_x))/1000.)
79 
80 #define SenderIdOfMsg(x) (x[0])
81 #define IdOfMsg(x) (x[1])
82 
83 #if USE_NPS
84 bool_t datalink_enabled = TRUE;
85 #endif
86 
87 
88 void dl_parse_msg(void)
89 {
90  uint8_t msg_id = IdOfMsg(dl_buffer);
91 
92 #if 0 // not ready yet
93  uint8_t sender_id = SenderIdOfMsg(dl_buffer);
94 
95  /* parse telemetry messages coming from other AC */
96  if (sender_id != 0) {
97  switch (msg_id) {
98 #ifdef TCAS
99  case DL_TCAS_RA: {
100  if (DL_TCAS_RESOLVE_ac_id(dl_buffer) == AC_ID && SenderIdOfMsg(dl_buffer) != AC_ID) {
101  uint8_t ac_id_conflict = SenderIdOfMsg(dl_buffer);
102  tcas_acs_status[the_acs_id[ac_id_conflict]].resolve = DL_TCAS_RA_resolve(dl_buffer);
103  }
104  }
105 #endif
106  }
107  return;
108  }
109 #endif
110 
111  switch (msg_id) {
112 
113  case DL_PING: {
114  DOWNLINK_SEND_PONG(DefaultChannel, DefaultDevice);
115  }
116  break;
117 
118 #ifdef TRAFFIC_INFO
119  case DL_ACINFO: {
120  if (DL_ACINFO_ac_id(dl_buffer) == AC_ID) { break; }
121  uint8_t id = DL_ACINFO_ac_id(dl_buffer);
122  float ux = MOfCm(DL_ACINFO_utm_east(dl_buffer));
123  float uy = MOfCm(DL_ACINFO_utm_north(dl_buffer));
124  float a = MOfCm(DL_ACINFO_alt(dl_buffer));
125  float c = RadOfDeg(((float)DL_ACINFO_course(dl_buffer)) / 10.);
126  float s = MOfCm(DL_ACINFO_speed(dl_buffer));
127  float cl = MOfCm(DL_ACINFO_climb(dl_buffer));
128  uint32_t t = DL_ACINFO_itow(dl_buffer);
129  SetAcInfo(id, ux, uy, c, a, s, cl, t);
130  }
131  break;
132 #endif
133 
134 #ifdef NAV
135  case DL_MOVE_WP: {
136  if (DL_MOVE_WP_ac_id(dl_buffer) != AC_ID) { break; }
137  uint8_t wp_id = DL_MOVE_WP_wp_id(dl_buffer);
138  float a = MOfMM(DL_MOVE_WP_alt(dl_buffer));
139 
140  /* Computes from (lat, long) in the referenced UTM zone */
141  struct LlaCoor_f lla;
142  lla.lat = RadOfDeg((float)(DL_MOVE_WP_lat(dl_buffer) / 1e7));
143  lla.lon = RadOfDeg((float)(DL_MOVE_WP_lon(dl_buffer) / 1e7));
144  struct UtmCoor_f utm;
145  utm.zone = nav_utm_zone0;
146  utm_of_lla_f(&utm, &lla);
147  nav_move_waypoint(wp_id, utm.east, utm.north, a);
148 
149  /* Waypoint range is limited. Computes the UTM pos back from the relative
150  coordinates */
151  utm.east = waypoints[wp_id].x + nav_utm_east0;
152  utm.north = waypoints[wp_id].y + nav_utm_north0;
153  DOWNLINK_SEND_WP_MOVED(DefaultChannel, DefaultDevice, &wp_id, &utm.east, &utm.north, &a, &nav_utm_zone0);
154  }
155  break;
156 
157  case DL_BLOCK: {
158  if (DL_BLOCK_ac_id(dl_buffer) != AC_ID) { break; }
159  nav_goto_block(DL_BLOCK_block_id(dl_buffer));
160  SEND_NAVIGATION(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
161  }
162  break;
163 #endif
166 #ifdef WIND_INFO
167  case DL_WIND_INFO: {
168  if (DL_WIND_INFO_ac_id(dl_buffer) != AC_ID) { break; }
169  struct FloatVect2 wind;
170  wind.x = DL_WIND_INFO_north(dl_buffer);
171  wind.y = DL_WIND_INFO_east(dl_buffer);
173 #if !USE_AIRSPEED
174  stateSetAirspeed_f(DL_WIND_INFO_airspeed(dl_buffer));
175 #endif
176 #ifdef WIND_INFO_RET
177  float airspeed = stateGetAirspeed_f();
178  DOWNLINK_SEND_WIND_INFO_RET(DefaultChannel, DefaultDevice, &wind.y, &wind.x, &airspeed);
179 #endif
180  }
181  break;
182 #endif
184 #ifdef HITL
185 
186  case DL_HITL_INFRARED: {
188  infrared.roll = DL_HITL_INFRARED_roll(dl_buffer);
189  infrared.pitch = DL_HITL_INFRARED_pitch(dl_buffer);
190  infrared.top = DL_HITL_INFRARED_top(dl_buffer);
191  }
192  break;
193 
194  case DL_HITL_UBX: {
196  if (gps_msg_received) {
197  gps_nb_ovrn++;
198  } else {
199  ubx_class = DL_HITL_UBX_class(dl_buffer);
200  ubx_id = DL_HITL_UBX_id(dl_buffer);
201  uint8_t l = DL_HITL_UBX_ubx_payload_length(dl_buffer);
202  uint8_t *ubx_payload = DL_HITL_UBX_ubx_payload(dl_buffer);
203  memcpy(ubx_msg_buf, ubx_payload, l);
204  gps_msg_received = TRUE;
205  }
206  }
207  break;
208 #endif /* HITL */
209 
210 #ifdef DlSetting
211  case DL_SETTING: {
212  if (DL_SETTING_ac_id(dl_buffer) != AC_ID) { break; }
213  uint8_t i = DL_SETTING_index(dl_buffer);
214  float val = DL_SETTING_value(dl_buffer);
215  DlSetting(i, val);
216  DOWNLINK_SEND_DL_VALUE(DefaultChannel, DefaultDevice, &i, &val);
217  }
218  break;
219 
220  case DL_GET_SETTING: {
221  if (DL_GET_SETTING_ac_id(dl_buffer) != AC_ID) { break; }
222  uint8_t i = DL_GET_SETTING_index(dl_buffer);
223  float val = settings_get_value(i);
224  DOWNLINK_SEND_DL_VALUE(DefaultChannel, DefaultDevice, &i, &val);
225  }
226  break;
227 #endif
229 #if USE_JOYSTICK
230  case DL_JOYSTICK_RAW: {
231  if (DL_JOYSTICK_RAW_ac_id(dl_buffer) == AC_ID) {
232  JoystickHandeDatalink(DL_JOYSTICK_RAW_roll(dl_buffer),
233  DL_JOYSTICK_RAW_pitch(dl_buffer),
234  DL_JOYSTICK_RAW_throttle(dl_buffer));
235  }
236  }
237  break;
238 #endif // USE_JOYSTICK
239 
240 #if defined RADIO_CONTROL && defined RADIO_CONTROL_TYPE_DATALINK
241  case DL_RC_3CH: {
242  //if (DL_RC_3CH_ac_id(dl_buffer) != TX_ID) { break; }
243 #ifdef RADIO_CONTROL_DATALINK_LED
244  LED_TOGGLE(RADIO_CONTROL_DATALINK_LED);
245 #endif
246  parse_rc_3ch_datalink(DL_RC_3CH_throttle_mode(dl_buffer),
247  DL_RC_3CH_roll(dl_buffer),
248  DL_RC_3CH_pitch(dl_buffer));
249  }
250  break;
251 
252  case DL_RC_4CH: {
253  if (DL_RC_4CH_ac_id(dl_buffer) == AC_ID) {
254 #ifdef RADIO_CONTROL_DATALINK_LED
255  LED_TOGGLE(RADIO_CONTROL_DATALINK_LED);
256 #endif
257  parse_rc_4ch_datalink(DL_RC_4CH_mode(dl_buffer),
258  DL_RC_4CH_throttle(dl_buffer),
259  DL_RC_4CH_roll(dl_buffer),
260  DL_RC_4CH_pitch(dl_buffer),
261  DL_RC_4CH_yaw(dl_buffer));
262  }
263  }
264  break;
265 #endif // RC_DATALINK
266 
267  default:
268  break;
269  }
270 
271  /* Parse modules datalink */
272  modules_parse_datalink(msg_id);
273 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
struct Infrared infrared
Definition: infrared.c:30
float x
Definition: common_nav.h:40
float east
in meters
enum tcas_resolve resolve
Definition: tcas.h:48
float north
in meters
uint8_t the_acs_id[NB_ACS_ID]
Definition: traffic_info.c:34
Periodic telemetry system header (includes downlink utility and generated code).
int16_t top
Definition: infrared.h:131
uint8_t gps_nb_ovrn
Definition: sim_ap.c:40
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition: state.h:1310
uint8_t nav_utm_zone0
Definition: common_nav.c:44
void nav_move_waypoint(uint8_t wp_id, float ux, float uy, float alt)
Move a waypoint to given UTM coordinates.
Definition: common_nav.c:135
position in UTM coordinates Units: meters
#define LED_TOGGLE(i)
Definition: led_hw.h:44
Fixed wing horizontal control.
struct tcas_ac_status tcas_acs_status[NB_ACS]
Definition: tcas.c:44
Paparazzi floating point math for geodetic calculations.
vector in Latitude, Longitude and Altitude
#define TRUE
Definition: std.h:4
float y
Definition: common_nav.h:41
int32_t nav_utm_north0
Definition: common_nav.c:43
uint8_t joystick_block
Device independent GPS code (interface)
uint16_t val[TCOUPLE_NB]
unsigned long uint32_t
Definition: types.h:18
uint8_t zone
UTM zone number.
#define SetAcInfo(_id, _utm_x, _utm_y, _course, _alt, _gspeed, _climb, _itow)
Definition: traffic_info.h:52
int16_t roll
Definition: infrared.h:129
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
int32_t nav_utm_east0
Definition: common_nav.c:42
struct point waypoints[NB_WAYPOINT]
size == nb_waypoint, waypoint 0 is a dummy waypoint
Definition: common_nav.c:38
static void stateSetAirspeed_f(float airspeed)
Set airspeed (float).
Definition: state.h:1254
float lon
in radians
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:64
float lat
in radians
int16_t pitch
Definition: infrared.h:130
void nav_goto_block(uint8_t b)
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)
Information relative to the other aircrafts.
static void stateSetHorizontalWindspeed_f(struct FloatVect2 *h_windspeed)
Set horizontal windspeed (float).
Definition: state.h:1245