Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nps_autopilot_fixedwing.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2013 The Paparazzi Team
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #include "nps_autopilot.h"
24 
25 #ifdef FBW
27 #define Fbw(f) f ## _fbw()
28 #else
29 #define Fbw(f)
30 #endif
31 
32 #ifdef AP
34 #define Ap(f) f ## _ap()
35 #else
36 #define Ap(f)
37 #endif
38 
39 #include "nps_sensors.h"
40 #include "nps_radio_control.h"
41 #include "nps_electrical.h"
42 #include "nps_fdm.h"
43 
45 #include "subsystems/imu.h"
46 #include "mcu_periph/sys_time.h"
47 #include "state.h"
48 #include "subsystems/commands.h"
49 
50 #include "subsystems/abi.h"
51 
52 // for launch
53 #include "autopilot.h"
54 
55 // for datalink_time hack
57 
58 #if USE_SONAR
59 // for sonar/lidar agl
61 #endif
62 
66 
67 #ifndef NPS_BYPASS_AHRS
68 #define NPS_BYPASS_AHRS FALSE
69 #endif
70 
71 #ifndef NPS_BYPASS_INS
72 #define NPS_BYPASS_INS FALSE
73 #endif
74 
75 
76 #if !defined (FBW) || !defined (AP)
77 #error NPS does not currently support dual processor simulation for FBW and AP on fixedwing!
78 #endif
79 
80 void nps_autopilot_init(enum NpsRadioControlType type_rc, int num_rc_script, char *rc_dev)
81 {
82 
84 
85  if (rc_dev != NULL) {
86  nps_radio_control_init(type_rc, num_rc_script, rc_dev);
87  }
89 
92 
93  Fbw(init);
94  Ap(init);
95 
96 }
97 
99 {
101 }
102 
103 #include <stdio.h>
104 #include "subsystems/gps.h"
105 
106 void nps_autopilot_run_step(double time)
107 {
108 
110 
111 #if RADIO_CONTROL && !RADIO_CONTROL_TYPE_DATALINK
112  if (nps_radio_control_available(time)) {
113  radio_control_feed();
114  Fbw(event_task);
115  }
116 #endif
117 
120  Fbw(event_task);
121  Ap(event_task);
122  }
123 
125  imu_feed_mag();
126  Fbw(event_task);
127  Ap(event_task);
128  }
129 
131  uint32_t now_ts = get_sys_time_usec();
132  float pressure = (float) sensors.baro.value;
133  AbiSendMsgBARO_ABS(BARO_SIM_SENDER_ID, now_ts, pressure);
134  Fbw(event_task);
135  Ap(event_task);
136  }
137 
139  AbiSendMsgTEMPERATURE(BARO_SIM_SENDER_ID, (float)sensors.temp.value);
140  }
141 
142 #if USE_AIRSPEED || USE_NPS_AIRSPEED
144  AbiSendMsgAIRSPEED(AIRSPEED_NPS_ID, (float)sensors.airspeed.value);
145  Fbw(event_task);
146  Ap(event_task);
147  }
148 #endif
149 
151  gps_feed_value();
152  Fbw(event_task);
153  Ap(event_task);
154  }
155 
156 #if USE_SONAR
158  uint32_t now_ts = get_sys_time_usec();
159  float dist = (float) sensors.sonar.value;
160  AbiSendMsgAGL(AGL_SONAR_NPS_ID, now_ts, dist);
161 
162 #ifdef SENSOR_SYNC_SEND_SONAR
163  uint16_t foo = 0;
164  DOWNLINK_SEND_SONAR(DefaultChannel, DefaultDevice, &foo, &dist);
165 #endif
166 
167  Fbw(event_task);
168  Ap(event_task);
169  }
170 #endif
171 
172  // standalone angle of attack
173 #if USE_NPS_AOA && !NPS_SYNC_INCIDENCE
175  AbiSendMsgINCIDENCE(INCIDENCE_NPS_ID, 1, (float)sensors.aoa.value, 0.f);
176  Fbw(event_task);
177  Ap(event_task);
178  }
179 #endif
180 
181  // standalone sideslip
182 #if USE_NPS_SIDESLIP && !NPS_SYNC_INCIDENCE
184  AbiSendMsgINCIDENCE(INCIDENCE_NPS_ID, 2, 0.f, (float)sensors.sideslip.value);
185  Fbw(event_task);
186  Ap(event_task);
187  }
188 #endif
189 
190  // synchronized angle of attack and sideslip
191 #if NPS_SYNC_INCIDENCE && USE_NPS_AOA && USE_NPS_SIDESLIP
192  static uint8_t flag = 0;
194  SetBit(flag, 0);
195  }
197  SetBit(flag, 1);
198  }
199  if (flag == 3) {
200  // both sensors are updated
201  AbiSendMsgINCIDENCE(INCIDENCE_NPS_ID, 3, (float)sensors.aoa.value, (float)sensors.sideslip.value);
202  Fbw(event_task);
203  Ap(event_task);
204  flag = 0;
205  }
206 #endif
207 
208 
209 
210  if (nps_bypass_ahrs) {
212  }
213 
214  if (nps_bypass_ins) {
216  }
217 
220 
221  /* scale final motor commands to 0-1 for feeding the fdm */
222 #ifdef NPS_ACTUATOR_NAMES
223  PRINT_CONFIG_MSG("actuators for JSBSim explicitly set.")
224  PRINT_CONFIG_VAR(NPS_COMMANDS_NB)
225  //PRINT_CONFIG_VAR(NPS_ACTUATOR_NAMES)
226 
227  for (uint8_t i = 0; i < NPS_COMMANDS_NB; i++) {
228  nps_autopilot.commands[i] = (double)commands[i] / MAX_PPRZ;
229  }
230  // hack: invert pitch to fit most JSBSim models
231  nps_autopilot.commands[COMMAND_PITCH] = -(double)commands[COMMAND_PITCH] / MAX_PPRZ;
232 #else
233  PRINT_CONFIG_MSG("Using throttle, roll, pitch, yaw commands instead of explicit actuators.")
234  PRINT_CONFIG_VAR(COMMAND_THROTTLE)
235  PRINT_CONFIG_VAR(COMMAND_ROLL)
236  PRINT_CONFIG_VAR(COMMAND_PITCH)
237 
238  nps_autopilot.commands[COMMAND_THROTTLE] = (double)commands[COMMAND_THROTTLE] / MAX_PPRZ;
239  nps_autopilot.commands[COMMAND_ROLL] = (double)commands[COMMAND_ROLL] / MAX_PPRZ;
240  // hack: invert pitch to fit most JSBSim models
241  nps_autopilot.commands[COMMAND_PITCH] = -(double)commands[COMMAND_PITCH] / MAX_PPRZ;
242 #ifdef COMMAND_YAW
243  PRINT_CONFIG_VAR(COMMAND_YAW)
244  nps_autopilot.commands[COMMAND_YAW] = (double)commands[COMMAND_YAW] / MAX_PPRZ;
245 #endif /* COMMAND_YAW */
246 #endif /* NPS_ACTUATOR_NAMES */
247 
248  // do the launch when clicking launch in GCS
250  if (!autopilot.launch) {
251  nps_autopilot.commands[COMMAND_THROTTLE] = 0;
252  }
253 }
254 
256 {
257 
258  struct FloatQuat quat_f;
259  QUAT_COPY(quat_f, fdm.ltp_to_body_quat);
260  stateSetNedToBodyQuat_f(&quat_f);
261 
262  struct FloatRates rates_f;
263  RATES_COPY(rates_f, fdm.body_ecef_rotvel);
264  stateSetBodyRates_f(&rates_f);
265 
266 }
267 
269 {
270 
272  struct NedCoor_f ltp_pos;
273  VECT3_COPY(ltp_pos, fdm.ltpprz_pos);
274  stateSetPositionNed_f(&ltp_pos);
275  }
276  else if (state.utm_initialized_f) {
277  struct LlaCoor_f lla;
278  LLA_COPY(lla, fdm.lla_pos);
279  struct UtmCoor_f utm;
280  utm.zone = (lla.lon / 1e7 + 180) / 6 + 1;
281  utm_of_lla_f(&utm, &lla);
282  stateSetPositionUtm_f(&utm);
283  }
284 
285  struct NedCoor_f ltp_speed;
286  VECT3_COPY(ltp_speed, fdm.ltpprz_ecef_vel);
287  stateSetSpeedNed_f(&ltp_speed);
288 
289  struct NedCoor_f ltp_accel;
290  VECT3_COPY(ltp_accel, fdm.ltpprz_ecef_accel);
291  stateSetAccelNed_f(&ltp_accel);
292 
293 }
unsigned short uint16_t
Definition: types.h:16
bool launch
request launch
Definition: autopilot.h:71
bool utm_initialized_f
True if utm origin (float) coordinate frame is initialsed.
Definition: state.h:236
#define NPS_BYPASS_AHRS
struct NedCoor_d ltpprz_ecef_accel
accel in ltppprz frame, wrt ECEF frame
Definition: nps_fdm.h:81
bool nps_sensors_gps_available(void)
Definition: nps_sensors.c:70
bool nps_sensors_aoa_available(void)
Definition: nps_sensors.c:106
void nps_autopilot_run_systime_step(void)
struct DoubleRates body_ecef_rotvel
Definition: nps_fdm.h:97
struct NpsFdm fdm
Holds all necessary NPS FDM state information.
void nps_electrical_run_step(double time)
bool nps_sensors_mag_available(void)
Definition: nps_sensors.c:52
#define NPS_COMMANDS_NB
Number of commands sent to the FDM of NPS.
Definition: nps_autopilot.h:42
#define NPS_BYPASS_INS
#define AIRSPEED_NPS_ID
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
bool ned_initialized_f
True if local float coordinate frame is initialsed.
Definition: state.h:223
bool nps_sensors_baro_available(void)
Definition: nps_sensors.c:61
Main include for ABI (AirBorneInterface).
double value
airspeed reading in meters/second
struct NpsSensorBaro baro
Definition: nps_sensors.h:21
static void stateSetNedToBodyQuat_f(struct FloatQuat *ned_to_body_quat)
Set vehicle body attitude from quaternion (float).
Definition: state.h:1093
NpsRadioControlType
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main_ap.c:202
position in UTM coordinates Units: meters
struct NpsSensorSonar sonar
Definition: nps_sensors.h:23
struct pprz_autopilot autopilot
Global autopilot structure.
Definition: autopilot.c:50
#define FALSE
Definition: std.h:5
struct NedCoor_d ltpprz_pos
Definition: nps_fdm.h:54
Roation quaternion.
#define AGL_SONAR_NPS_ID
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:596
static void stateSetPositionUtm_f(struct UtmCoor_f *utm_pos)
Set position from UTM coordinates (float).
Definition: state.h:582
vector in Latitude, Longitude and Altitude
static void stateSetSpeedNed_f(struct NedCoor_f *ned_speed)
Set ground speed in local NED coordinates (float).
Definition: state.h:809
#define INCIDENCE_NPS_ID
void imu_feed_mag(void)
Definition: imu_nps.c:53
static void sys_tick_handler(void)
double value
temperature in degrees Celcius
vector in North East Down coordinates Units: meters
bool nps_sensors_sideslip_available(void)
Definition: nps_sensors.c:115
struct NpsSensorAngleOfAttack aoa
Definition: nps_sensors.h:26
Architecture independent timing functions.
#define Fbw(f)
void nps_electrical_init(void)
struct NpsAutopilot nps_autopilot
Device independent GPS code (interface)
void nps_radio_control_init(enum NpsRadioControlType type, int num_script, char *js_dev)
unsigned long uint32_t
Definition: types.h:18
double value
sideslip reading in radian
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void sim_overwrite_ahrs(void)
uint16_t foo
Definition: main_demo5.c:59
Hardware independent code for commands handling.
struct NpsSensors sensors
Definition: nps_sensors.c:6
double value
sonar reading in meters
Inertial Measurement Unit interface.
uint8_t zone
UTM zone number.
static void stateSetPositionNed_f(struct NedCoor_f *ned_pos)
Set position from local NED coordinates (float).
Definition: state.h:598
#define LLA_COPY(_pos1, _pos2)
Definition: pprz_geodetic.h:58
bool ned_initialized_i
true if local int coordinate frame is initialsed
Definition: state.h:171
void gps_feed_value(void)
Definition: gps_sim_nps.c:31
void sim_overwrite_ins(void)
Core autopilot interface common to all firmwares.
struct LlaCoor_d lla_pos
Definition: nps_fdm.h:55
void imu_feed_gyro_accel(void)
Definition: imu_nps.c:40
void nps_autopilot_init(enum NpsRadioControlType type_rc, int num_rc_script, char *rc_dev)
bool nps_sensors_temperature_available(void)
Definition: nps_sensors.c:97
#define BARO_SIM_SENDER_ID
unsigned char uint8_t
Definition: types.h:14
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
Definition: commands.c:30
bool nps_bypass_ins
API to get/set the generic vehicle states.
struct NpsSensorSideSlip sideslip
Definition: nps_sensors.h:27
bool nps_sensors_airspeed_available(void)
Definition: nps_sensors.c:88
bool nps_radio_control_available(double time)
Electrical status (bat voltage) for NPS.
void nps_autopilot_run_step(double time)
bool nps_bypass_ahrs
bool nps_sensors_gyro_available(void)
Definition: nps_sensors.c:43
double value
pressure in Pascal
float lon
in radians
struct NpsSensorAirspeed airspeed
Definition: nps_sensors.h:24
struct NedCoor_d ltpprz_ecef_vel
velocity in ltppprz frame, wrt ECEF frame
Definition: nps_fdm.h:79
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
#define MAX_PPRZ
Definition: paparazzi.h:8
#define Ap(f)
FBW ( FlyByWire ) process API.
double value
angle of attack reading in radian
bool kill_throttle
allow autopilot to use throttle
Definition: autopilot.h:69
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
static void stateSetBodyRates_f(struct FloatRates *body_rate)
Set vehicle body angular rate (float).
Definition: state.h:1181
bool nps_sensors_sonar_available(void)
Definition: nps_sensors.c:79
static void stateSetAccelNed_f(struct NedCoor_f *ned_accel)
Set acceleration in NED coordinates (float).
Definition: state.h:1002
struct DoubleQuat ltp_to_body_quat
Definition: nps_fdm.h:91
double commands[NPS_COMMANDS_NB]
Definition: nps_autopilot.h:47
AP ( AutoPilot ) process API.
angular rates
struct NpsSensorTemperature temp
Definition: nps_sensors.h:25
struct State state
Definition: state.c:36
void utm_of_lla_f(struct UtmCoor_f *utm, struct LlaCoor_f *lla)