39 #include "pprzlink/messages.h"
40 #include "pprzlink/dl_protocol.h"
41 #include "pprzlink/pprzlink_device.h"
42 #include "pprzlink/pprz_transport.h"
46 #warning "[hitl] Please define AP_DEV in your airframe file"
47 #define AP_DEV "/dev/ttyUSB0"
50 #define AP_BAUD B921600
51 PRINT_CONFIG_MSG_VALUE(
"[hitl] Using default baudrate for AP_DEV (B921600)",
AP_BAUD)
54 #define NPS_HITL_DEBUG 0
56 #define DEBUG_PRINT printf
58 #define DEBUG_PRINT(...) {}
67 #define PPRZLINK_BUFFER_SIZE 256
90 return space >= len ? space : 0;
109 }
while (ret < 1 && errno == EAGAIN);
111 DEBUG_PRINT(
"[hitl] put_byte: write failed [%d: %s]\n", ret, strerror(errno));
119 unsigned char c =
'B';
121 ret = read(d->
port->
fd, &c, 1);
137 int ret = poll(d->
fds, 1, 1000);
139 if (d->
fds[0].revents & POLLHUP) {
140 printf(
"[hitl] lost connection. Exiting\n");
143 if (d->
fds[0].revents & POLLIN) {
146 }
else if (ret == -1) {
172 printf(
"[hitl] Error opening %s code %d\n",
AP_DEV, ret);
179 dev.
fds[0].events = POLLIN;
185 struct timespec requestStart;
186 struct timespec requestEnd;
187 struct timespec waitFor;
188 long int period_ns = (1. / PERIODIC_FREQUENCY) * 1000000000L;
189 long int task_ns = 0;
201 float gx = (float)
sensors.gyro.value.x;
202 float gy = (
float)
sensors.gyro.value.y;
203 float gz = (float)
sensors.gyro.value.z;
204 float ax = (
float)
sensors.accel.value.x;
205 float ay = (float)
sensors.accel.value.y;
206 float az = (
float)
sensors.accel.value.z;
207 float mx = (float)
sensors.mag.value.x;
208 float my = (
float)
sensors.mag.value.y;
209 float mz = (float)
sensors.mag.value.z;
219 float gps_lat = (float)DegOfRad(
sensors.gps.lla_pos.lat);
220 float gps_lon = (float)DegOfRad(
sensors.gps.lla_pos.lon);
221 float gps_alt = (float)
sensors.gps.lla_pos.alt;
222 float gps_hmsl = (
float)
sensors.gps.hmsl;
223 float gps_vx = (float)
sensors.gps.ecef_vel.x;
224 float gps_vy = (
float)
sensors.gps.ecef_vel.y;
225 float gps_vz = (float)
sensors.gps.ecef_vel.z;
229 &gps_lat, &gps_lon, &gps_alt, &gps_hmsl,
230 &gps_vx, &gps_vy, &gps_vz,
231 &gps_time, &gps_fix, &
id);
236 float airspeed = -1.f;
238 float sideslip = 0.f;
240 SetBit(air_data_flag, 0);
241 baro = (float)
sensors.baro.value;
244 SetBit(air_data_flag, 1);
245 airspeed = (float)
sensors.airspeed.value;
248 SetBit(air_data_flag, 2);
249 aoa = (float)
sensors.aoa.value;
252 SetBit(air_data_flag, 3);
253 sideslip = (float)
sensors.sideslip.value;
255 if (air_data_flag != 0) {
257 &baro, &airspeed, &aoa, &sideslip, &air_data_flag, &
id);
266 task_ns = (requestEnd.tv_sec - requestStart.tv_sec) * 1000000000L + (requestEnd.tv_nsec - requestStart.tv_nsec);
269 if (task_ns < period_ns) {
271 waitFor.tv_nsec = period_ns - task_ns;
272 nanosleep(&waitFor, NULL);
276 printf(
"SENSORS: task took longer than one period, exactly %f [ms], but the period is %f [ms]\n",
277 (
double)task_ns / 1E6, (
double)period_ns / 1E6);
286 struct timespec waitFor;
289 bool msg_available =
false;
291 bool first_command =
true;
303 uint8_t sender_id = SenderIdOfPprzMsg(msg_buffer);
304 uint8_t msg_id = IdOfPprzMsg(msg_buffer);
306 if (sender_id != AC_ID) {
307 printf(
"[hitl] receiving message from wrong id (%d)\n", sender_id);
314 cmd_len = DL_COMMANDS_values_length(msg_buffer);
315 memcpy(&
cmd_buf, DL_COMMANDS_values(msg_buffer), cmd_len *
sizeof(
int16_t));
324 printf(
"[hitl] receiving COMMANDS message\n");
325 first_command =
false;
329 case DL_MOTOR_MIXING:
331 cmd_len = DL_MOTOR_MIXING_values_length(msg_buffer);
336 memcpy(&
cmd_buf, DL_MOTOR_MIXING_values(msg_buffer), cmd_len *
sizeof(
int16_t));
343 printf(
"[hitl] receiving MOTOR_MIXING message\n");
344 first_command =
false;
352 msg_available =
false;
357 waitFor.tv_nsec = 1000;
358 nanosleep(&waitFor, NULL);
struct NpsAutopilot nps_autopilot
#ifndef NPS_NO_MOTOR_MIXING #include "modules/actuators/motor_mixing.h"
double commands[NPS_COMMANDS_NB]
#define NPS_COMMANDS_NB
Number of commands sent to the FDM of NPS.
uint8_t rx_buf[PPRZLINK_BUFFER_SIZE]
Receive buffer.
static void put_buffer(struct linkdev *d, long fd, const uint8_t *data, uint16_t len)
void * nps_sensors_loop(void *data)
uint8_t tx_buf[PPRZLINK_BUFFER_SIZE]
Transmit buffer.
static void send_message(struct linkdev *d, long fd)
#define PPRZLINK_BUFFER_SIZE
struct SerialPort * port
Serial port.
static struct linkdev dev
struct link_device device
Generic device interface.
void * nps_ap_data_loop(void *data)
static int char_available(struct linkdev *d)
void nps_hitl_impl_init(void)
END pprzlink_dev.
static uint8_t getch(struct linkdev *d)
static int check_free_space(struct linkdev *d, long *fd, uint16_t len)
struct pprz_transport pprz_tp
transport
static void put_byte(struct linkdev *d, long fd, uint8_t data)
#define clock_get_current_time(_x)
pthread_mutex_t fdm_mutex
bool nps_sensors_airspeed_available(void)
bool nps_sensors_gps_available(void)
bool nps_sensors_sideslip_available(void)
bool nps_sensors_aoa_available(void)
bool nps_sensors_baro_available(void)
bool nps_sensors_gyro_available(void)
void serial_port_free(struct SerialPort *me)
int serial_port_open_raw(struct SerialPort *me, const char *device, speed_t speed)
struct SerialPort * serial_port_new(void)
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.