2020-05-19 22:01:39 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-11 01:59:19 -03:00
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
|
|
|
|
#ifndef HAL_SIM_JSON_ENABLED
|
|
|
|
#define HAL_SIM_JSON_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_SIM_JSON_ENABLED
|
|
|
|
|
2023-12-25 22:21:11 -04:00
|
|
|
#include <AP_HAL/utility/Socket_native.h>
|
2020-05-19 22:01:39 -03:00
|
|
|
#include "SIM_Aircraft.h"
|
|
|
|
|
|
|
|
namespace SITL {
|
|
|
|
|
|
|
|
class JSON : public Aircraft {
|
|
|
|
public:
|
|
|
|
JSON(const char *frame_str);
|
|
|
|
|
|
|
|
/* update model by one time step */
|
|
|
|
void update(const struct sitl_input &input) override;
|
|
|
|
|
|
|
|
/* static object creator */
|
|
|
|
static Aircraft *create(const char *frame_str) {
|
2024-05-26 22:24:16 -03:00
|
|
|
return NEW_NOTHROW JSON(frame_str);
|
2020-05-19 22:01:39 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and set in/out socket for JSON generic simulator */
|
|
|
|
void set_interface_ports(const char* address, const int port_in, const int port_out) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-03-16 14:49:29 -03:00
|
|
|
struct servo_packet_16 {
|
2020-05-31 19:10:41 -03:00
|
|
|
uint16_t magic = 18458; // constant magic value
|
|
|
|
uint16_t frame_rate;
|
2020-05-19 22:01:39 -03:00
|
|
|
uint32_t frame_count;
|
|
|
|
uint16_t pwm[16];
|
|
|
|
};
|
|
|
|
|
2023-03-16 14:49:29 -03:00
|
|
|
struct servo_packet_32 {
|
|
|
|
uint16_t magic = 29569; // constant magic value
|
|
|
|
uint16_t frame_rate;
|
|
|
|
uint32_t frame_count;
|
|
|
|
uint16_t pwm[32];
|
|
|
|
};
|
|
|
|
|
2020-05-19 22:01:39 -03:00
|
|
|
// default connection_info_.ip_address
|
|
|
|
const char *target_ip = "127.0.0.1";
|
|
|
|
|
|
|
|
// default connection_info_.sitl_ip_port
|
|
|
|
uint16_t control_port = 9002;
|
|
|
|
|
2023-12-25 22:21:11 -04:00
|
|
|
SocketAPM_native sock;
|
2020-05-19 22:01:39 -03:00
|
|
|
|
|
|
|
uint32_t frame_counter;
|
2020-05-31 19:10:41 -03:00
|
|
|
double last_timestamp_s;
|
2020-05-19 22:01:39 -03:00
|
|
|
|
|
|
|
void output_servos(const struct sitl_input &input);
|
|
|
|
void recv_fdm(const struct sitl_input &input);
|
|
|
|
|
2021-04-24 21:32:27 -03:00
|
|
|
uint32_t parse_sensors(const char *json);
|
2020-05-19 22:01:39 -03:00
|
|
|
|
|
|
|
// buffer for parsing pose data in JSON format
|
|
|
|
uint8_t sensor_buffer[65000];
|
|
|
|
uint32_t sensor_buffer_len;
|
|
|
|
|
|
|
|
enum data_type {
|
|
|
|
DATA_UINT64,
|
|
|
|
DATA_FLOAT,
|
|
|
|
DATA_DOUBLE,
|
|
|
|
DATA_VECTOR3F,
|
2021-06-20 23:59:02 -03:00
|
|
|
DATA_VECTOR3D,
|
2020-06-30 15:06:50 -03:00
|
|
|
QUATERNION,
|
2021-04-24 21:32:27 -03:00
|
|
|
BOOLEAN,
|
2020-05-19 22:01:39 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
2020-05-31 19:10:41 -03:00
|
|
|
double timestamp_s;
|
2020-05-19 22:01:39 -03:00
|
|
|
struct {
|
|
|
|
Vector3f gyro;
|
|
|
|
Vector3f accel_body;
|
|
|
|
} imu;
|
2021-06-20 23:59:02 -03:00
|
|
|
Vector3d position;
|
2020-05-19 22:01:39 -03:00
|
|
|
Vector3f attitude;
|
2020-06-30 15:06:50 -03:00
|
|
|
Quaternion quaternion;
|
2020-05-19 22:01:39 -03:00
|
|
|
Vector3f velocity;
|
2020-07-17 20:38:07 -03:00
|
|
|
float rng[6];
|
2020-08-15 12:40:51 -03:00
|
|
|
struct {
|
|
|
|
float direction;
|
|
|
|
float speed;
|
|
|
|
} wind_vane_apparent;
|
2021-02-02 10:29:58 -04:00
|
|
|
float airspeed;
|
2021-04-24 21:32:27 -03:00
|
|
|
bool no_time_sync;
|
2020-05-19 22:01:39 -03:00
|
|
|
} state;
|
|
|
|
|
|
|
|
// table to aid parsing of JSON sensor data
|
|
|
|
struct keytable {
|
|
|
|
const char *section;
|
|
|
|
const char *key;
|
|
|
|
void *ptr;
|
|
|
|
enum data_type type;
|
2020-06-30 15:06:50 -03:00
|
|
|
bool required;
|
2021-04-24 21:32:27 -03:00
|
|
|
} keytable[17] = {
|
2020-06-30 15:06:50 -03:00
|
|
|
{ "", "timestamp", &state.timestamp_s, DATA_DOUBLE, true },
|
|
|
|
{ "imu", "gyro", &state.imu.gyro, DATA_VECTOR3F, true },
|
|
|
|
{ "imu", "accel_body", &state.imu.accel_body, DATA_VECTOR3F, true },
|
2021-06-20 23:59:02 -03:00
|
|
|
{ "", "position", &state.position, DATA_VECTOR3D, true },
|
2020-06-30 15:06:50 -03:00
|
|
|
{ "", "attitude", &state.attitude, DATA_VECTOR3F, false },
|
|
|
|
{ "", "quaternion", &state.quaternion, QUATERNION, false },
|
|
|
|
{ "", "velocity", &state.velocity, DATA_VECTOR3F, true },
|
2020-07-17 20:38:07 -03:00
|
|
|
{ "", "rng_1", &state.rng[0], DATA_FLOAT, false },
|
|
|
|
{ "", "rng_2", &state.rng[1], DATA_FLOAT, false },
|
|
|
|
{ "", "rng_3", &state.rng[2], DATA_FLOAT, false },
|
|
|
|
{ "", "rng_4", &state.rng[3], DATA_FLOAT, false },
|
|
|
|
{ "", "rng_5", &state.rng[4], DATA_FLOAT, false },
|
|
|
|
{ "", "rng_6", &state.rng[5], DATA_FLOAT, false },
|
2020-08-15 12:40:51 -03:00
|
|
|
{"windvane","direction", &state.wind_vane_apparent.direction, DATA_FLOAT, false},
|
|
|
|
{"windvane","speed", &state.wind_vane_apparent.speed, DATA_FLOAT, false},
|
2021-02-02 10:29:58 -04:00
|
|
|
{"", "airspeed", &state.airspeed, DATA_FLOAT, false},
|
2021-04-24 21:32:27 -03:00
|
|
|
{"", "no_time_sync", &state.no_time_sync, BOOLEAN, false},
|
2020-06-30 15:06:50 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Enum coresponding to the ordering of keys in the keytable.
|
|
|
|
enum DataKey {
|
|
|
|
TIMESTAMP = 1U << 0,
|
|
|
|
GYRO = 1U << 1,
|
|
|
|
ACCEL_BODY = 1U << 2,
|
|
|
|
POSITION = 1U << 3,
|
|
|
|
EULER_ATT = 1U << 4,
|
|
|
|
QUAT_ATT = 1U << 5,
|
|
|
|
VELOCITY = 1U << 6,
|
2020-07-17 20:38:07 -03:00
|
|
|
RNG_1 = 1U << 7,
|
|
|
|
RNG_2 = 1U << 8,
|
|
|
|
RNG_3 = 1U << 9,
|
|
|
|
RNG_4 = 1U << 10,
|
|
|
|
RNG_5 = 1U << 11,
|
|
|
|
RNG_6 = 1U << 12,
|
2020-08-15 12:40:51 -03:00
|
|
|
WIND_DIR = 1U << 13,
|
|
|
|
WIND_SPD = 1U << 14,
|
2021-02-02 10:29:58 -04:00
|
|
|
AIRSPEED = 1U << 15,
|
2021-04-24 21:32:27 -03:00
|
|
|
TIME_SYNC = 1U << 16,
|
2020-05-19 22:01:39 -03:00
|
|
|
};
|
2021-04-24 21:32:27 -03:00
|
|
|
uint32_t last_received_bitmask;
|
2020-05-19 22:01:39 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-10-11 01:59:19 -03:00
|
|
|
|
|
|
|
#endif // HAL_SIM_JSON_ENABLED
|