35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
49 #include "generated/airframe.h"
50 #include "generated/flight_plan.h"
52 #ifndef NPS_CRRCSIM_HOST_IP
53 #define NPS_CRRCSIM_HOST_IP "127.0.0.1"
56 #ifndef NPS_CRRCSIM_HOST_PORT
57 #define NPS_CRRCSIM_HOST_PORT 9002
60 #ifndef NPS_CRRCSIM_ROLL_NEUTRAL
61 #define NPS_CRRCSIM_ROLL_NEUTRAL 0.
64 #ifndef NPS_CRRCSIM_PITCH_NEUTRAL
65 #define NPS_CRRCSIM_PITCH_NEUTRAL 0.
69 #define UDP_BLOCKING 0
70 #define UDP_NONBLOCKING 1
73 #define word unsigned short
74 #define byte unsigned char
77 #define IMU_PACKET_LENGTH 51
78 #define GPS_PACKET_LENGTH 86
79 #define AHRS_PACKET_LENGTH 93
80 #define FULL_PACKET_SIZE 93
86 #define INPUT_BUFFER_SIZE (3*FULL_PACKET_SIZE)
108 static void open_udp(
char *host,
int port,
int blocking);
135 printf(
"Starting to connect to CRRCsim server.\n");
139 printf(
"Connection to CRRCsim failed\n");
142 printf(
"Connection to CRRCsim succed\n");
146 double zero[] = { 0., 0., 0. };
195 printf(
"invalid data packet...!\n");
201 double dir __attribute__((unused)))
206 double wind_east __attribute__((unused)),
207 double wind_down __attribute__((unused)))
212 int turbulence_severity __attribute__((unused)))
220 static void open_udp(
char *host,
int port,
int blocking)
269 llh_nav0.
lat = RadOfDeg((
double)NAV_LAT0 / 1e7);
270 llh_nav0.
lon = RadOfDeg((
double)NAV_LON0 / 1e7);
272 llh_nav0.
alt = (NAV_ALT0 + NAV_MSL0) / 1000.;
284 #pragma message "Using magnetic field as defined in airframe file."
338 if (packet_len > 0 && c->
length < packet_len) {
341 if (packet_len > 0) {
343 word rcvchecksum = 0;
346 for (i = 2, ib = c->
start + (
byte)2; i < packet_len - 2; i++, ib++) {
349 rcvchecksum = c->
buf[ib++] << 8;
350 rcvchecksum = rcvchecksum | c->
buf[ib++];
352 if (rcvchecksum != sum) {
354 printf(
"checksum error\n");
358 if (packet_len > 0) {
359 for (i = 0; i < packet_len; i++) {
375 #define LongOfBuf(_buf,_idx) (int32_t)(((uint32_t)_buf[_idx+3]<<24)|((uint32_t)_buf[_idx+2]<<16)|((uint32_t)_buf[_idx+1]<<8)|((uint32_t)_buf[_idx]))
377 #define UShortOfBuf(_buf,_idx) (uint16_t)(((uint16_t)_buf[_idx+1]<<8)|((uint16_t)_buf[_idx]))
379 #define ShortOfBuf(_buf,_idx) (int16_t)(((uint16_t)_buf[_idx]<<8)|((uint16_t)_buf[_idx+1]))
388 vel.
x = (double)
LongOfBuf(buffer, 3) * 1.0e-2;
389 vel.
y = (double)
LongOfBuf(buffer, 7) * 1.0e-2;
390 vel.
z = (double)
LongOfBuf(buffer, 11) * 1.0e-2;
413 fdm->
hmsl = pos.
alt - NAV_MSL0 / 1000.;
425 #if NPS_CRRCSIM_DEBUG
426 printf(
"decode gps | pos %f %f %f | vel %f %f %f | time %f\n",
448 #if NPS_CRRCSIM_DEBUG
449 printf(
"decode ahrs %f %f %f\n",
491 #if NPS_CRRCSIM_DEBUG
492 printf(
"decode imu | accel %f %f %f | gyro %f %f %f\n",
504 #define MSG_NOSIGNAL SO_NOSIGPIPE
514 byte data[24] = {0,};
518 word roll = (
word)((65535 / 4) * commands[NPS_CRRCSIM_COMMAND_ROLL] + (65536 / 2));
519 word pitch = (
word)((65535 / 4) * commands[NPS_CRRCSIM_COMMAND_PITCH] + (65536 / 2));
523 cnt_cmd[2] = (
word)(65535 * commands[NPS_CRRCSIM_COMMAND_THROTTLE]);
525 #if NPS_CRRCSIM_DEBUG
526 printf(
"send servo %f %f %f | %d %d %d | %d %d\n",
543 data[4] = (
byte)(cnt_cmd[0] >> 8);
544 data[5] = (
byte)cnt_cmd[0];
546 data[6] = (
byte)(cnt_cmd[1] >> 8);
547 data[7] = (
byte)cnt_cmd[1];
549 data[8] = (
byte)(cnt_cmd[2] >> 8);
550 data[9] = (
byte)cnt_cmd[2];
554 for (i = 4; i < 22; i++) { sum += data[i]; }
556 data[22] = (
byte)(sum >> 8);
557 data[23] = (
byte)sum;
561 send(io->
socket, (
char *)data, 24, MSG_NOSIGNAL);
static int get_msg(struct _crrcsim *io, byte *data_buffer)
double total_pressure
total atmospheric pressure in Pascal
vector in North East Down coordinates Units: meters
static float pprz_isa_pressure_of_altitude(float altitude)
Get pressure in Pa from absolute altitude (using simplified equation).
static struct LtpDef_d ltpdef
struct DoubleRates body_ecef_rotvel
static void send_servo_cmd(struct _crrcsim *io, double *commands)
void nps_fdm_set_wind(double speed, double dir)
#define INPUT_BUFFER_SIZE
#define UShortOfBuf(_buf, _idx)
static void open_udp(char *host, int port, int blocking)
void nps_fdm_set_turbulence(double wind_speed, int turbulence_severity)
void ecef_of_ned_vect_d(struct EcefCoor_d *ecef, struct LtpDef_d *def, struct NedCoor_d *ned)
#define NPS_CRRCSIM_HOST_PORT
#define VECT3_COPY(_a, _b)
void nps_fdm_init(double dt)
double alt
in meters above WGS84 reference ellipsoid
#define NPS_CRRCSIM_ROLL_NEUTRAL
static void inputbuf_init(struct inputbuf *c)
struct LlaCoor_d lla
origin of local frame in LLA
struct LlaCoor_d lla_pos_pprz
vector in Latitude, Longitude and Altitude
struct DoubleRates body_inertial_rotvel
static void init_ltp(void)
struct DoubleEulers ltp_to_body_eulers
static void decode_gpspacket(struct NpsFdm *fdm, byte *buffer)
struct NedCoor_d ltp_ecef_vel
velocity in LTP frame, wrt ECEF frame
double pressure_sl
pressure at sea level in Pascal
struct NedCoor_d ltpprz_pos
byte buf[INPUT_BUFFER_SIZE]
void nps_fdm_run_step(bool launch, double *commands, int commands_nb)
Paparazzi double-precision floating point math for geodetic calculations.
#define NPS_CRRCSIM_HOST_IP
Paparazzi floating point math for geodetic calculations.
struct EcefCoor_d ecef_ecef_vel
velocity in ECEF frame, wrt ECEF frame
double airspeed
equivalent airspeed in m/s
static void read_into_buffer(struct _crrcsim *io)
struct DoubleVect3 body_accel
acceleration in body frame as measured by an accelerometer (incl.
double pressure
current (static) atmospheric pressure in Pascal
Paparazzi floating point algebra.
#define NPS_CRRCSIM_PITCH_NEUTRAL
static void decode_ahrspacket(struct NpsFdm *fdm, byte *buffer)
void double_quat_of_eulers(struct DoubleQuat *q, struct DoubleEulers *e)
Paparazzi generic algebra macros.
Paparazzi atmospheric pressure conversion utilities.
definition of the local (flat earth) coordinate system
double dynamic_pressure
dynamic pressure in Pascal
static double double_vect3_norm(struct DoubleVect3 *v)
vector in EarthCenteredEarthFixed coordinates
byte data_buffer[FULL_PACKET_SIZE]
void ltp_def_from_ecef_d(struct LtpDef_d *def, struct EcefCoor_d *ecef)
#define PPRZ_ISA_SEA_LEVEL_PRESSURE
ISA sea level standard atmospheric pressure in Pascal.
Paparazzi generic macros for geodetic calculations.
double temperature
current temperature in degrees Celcius
static void decode_imupacket(struct NpsFdm *fdm, byte *buffer)
void ned_of_ecef_point_d(struct NedCoor_d *ned, struct LtpDef_d *def, struct EcefCoor_d *ecef)
pprz_t commands[COMMANDS_NB]
Storage of intermediate command values.
#define LongOfBuf(_buf, _idx)
#define AHRS_PACKET_LENGTH
static struct _crrcsim crrcsim
struct EcefCoor_d ecef_pos
struct DoubleVect3 body_ecef_accel
acceleration in body frame, wrt ECEF frame
struct NedCoor_d ltpprz_ecef_vel
velocity in ltppprz frame, wrt ECEF frame
void ecef_of_lla_d(struct EcefCoor_d *ecef, struct LlaCoor_d *lla)
#define IMU_PACKET_LENGTH
struct DoubleQuat ltp_to_body_quat
void nps_fdm_set_wind_ned(double wind_north, double wind_east, double wind_down)
void ned_of_ecef_vect_d(struct NedCoor_d *ned, struct LtpDef_d *def, struct EcefCoor_d *ecef)
#define GPS_PACKET_LENGTH
#define ShortOfBuf(_buf, _idx)