2019-08-15 05:22:38 -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/>.
|
|
|
|
*/
|
2020-08-06 08:06:12 -03:00
|
|
|
/*
|
2019-05-20 19:51:42 -03:00
|
|
|
Simulator connector for Airsim: https://github.com/Microsoft/AirSim
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-10-11 01:59:19 -03:00
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
|
|
|
|
#ifndef HAL_SIM_AIRSIM_ENABLED
|
|
|
|
#define HAL_SIM_AIRSIM_ENABLED (CONFIG_HAL_BOARD == HAL_BOARD_SITL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAL_SIM_AIRSIM_ENABLED
|
|
|
|
|
2023-12-25 22:21:11 -04:00
|
|
|
#include <AP_HAL/utility/Socket_native.h>
|
2019-05-20 19:51:42 -03:00
|
|
|
#include "SIM_Aircraft.h"
|
|
|
|
|
|
|
|
namespace SITL {
|
|
|
|
|
2020-08-06 08:06:12 -03:00
|
|
|
/*
|
2019-05-20 19:51:42 -03:00
|
|
|
Airsim Simulator
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AirSim : public Aircraft {
|
|
|
|
public:
|
2019-08-15 01:01:24 -03:00
|
|
|
AirSim(const char *frame_str);
|
2019-05-20 19:51:42 -03:00
|
|
|
|
|
|
|
/* update model by one time step */
|
|
|
|
void update(const struct sitl_input &input) override;
|
|
|
|
|
|
|
|
/* static object creator */
|
2019-08-15 01:01:24 -03:00
|
|
|
static Aircraft *create(const char *frame_str) {
|
2024-05-26 22:24:16 -03:00
|
|
|
return NEW_NOTHROW AirSim(frame_str);
|
2019-05-20 19:51:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and set in/out socket for Airsim simulator */
|
|
|
|
void set_interface_ports(const char* address, const int port_in, const int port_out) override;
|
|
|
|
|
|
|
|
private:
|
2019-07-31 04:13:40 -03:00
|
|
|
enum class OutputType {
|
|
|
|
Copter = 1,
|
|
|
|
Rover = 2
|
|
|
|
} output_type;
|
|
|
|
|
|
|
|
// Control packet for Rover
|
|
|
|
struct rover_packet {
|
|
|
|
float throttle; // -1 to 1
|
|
|
|
float steering; // -1 to 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// rotor control packet sent by Ardupilot
|
|
|
|
static const int kArduCopterRotorControlCount = 11;
|
2019-05-20 19:51:42 -03:00
|
|
|
|
2019-07-31 04:13:40 -03:00
|
|
|
struct servo_packet {
|
2019-05-20 19:51:42 -03:00
|
|
|
uint16_t pwm[kArduCopterRotorControlCount];
|
|
|
|
};
|
|
|
|
|
|
|
|
// default connection_info_.ip_address
|
|
|
|
const char *airsim_ip = "127.0.0.1";
|
|
|
|
|
|
|
|
// connection_info_.ip_port
|
|
|
|
uint16_t airsim_sensor_port = 9003;
|
|
|
|
|
|
|
|
// connection_info_.sitl_ip_port
|
|
|
|
uint16_t airsim_control_port = 9002;
|
|
|
|
|
2023-12-25 22:21:11 -04:00
|
|
|
SocketAPM_native sock;
|
2019-05-20 19:51:42 -03:00
|
|
|
|
|
|
|
double average_frame_time;
|
|
|
|
uint64_t frame_counter;
|
|
|
|
uint64_t last_frame_count;
|
2019-08-28 10:53:02 -03:00
|
|
|
uint64_t last_timestamp;
|
2019-05-20 19:51:42 -03:00
|
|
|
|
2020-08-06 08:06:12 -03:00
|
|
|
void output_copter(const sitl_input& input);
|
|
|
|
void output_rover(const sitl_input& input);
|
|
|
|
// Wrapper function over the above 2 output methods
|
|
|
|
void output_servos(const sitl_input& input);
|
|
|
|
|
|
|
|
void recv_fdm(const sitl_input& input);
|
2019-05-20 19:51:42 -03:00
|
|
|
void report_FPS(void);
|
|
|
|
|
|
|
|
bool parse_sensors(const char *json);
|
|
|
|
|
|
|
|
// buffer for parsing pose data in JSON format
|
2019-07-17 13:27:14 -03:00
|
|
|
uint8_t sensor_buffer[65000];
|
2019-05-20 19:51:42 -03:00
|
|
|
uint32_t sensor_buffer_len;
|
|
|
|
|
|
|
|
enum data_type {
|
|
|
|
DATA_UINT64,
|
|
|
|
DATA_FLOAT,
|
|
|
|
DATA_DOUBLE,
|
|
|
|
DATA_VECTOR3F,
|
2019-07-17 13:27:14 -03:00
|
|
|
DATA_VECTOR3F_ARRAY,
|
2019-07-25 04:33:16 -03:00
|
|
|
DATA_FLOAT_ARRAY,
|
2019-05-20 19:51:42 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint64_t timestamp;
|
|
|
|
struct {
|
|
|
|
Vector3f angular_velocity;
|
|
|
|
Vector3f linear_acceleration;
|
|
|
|
} imu;
|
|
|
|
struct {
|
2019-12-16 17:49:17 -04:00
|
|
|
double lat, lon, alt;
|
2019-05-20 19:51:42 -03:00
|
|
|
} gps;
|
|
|
|
struct {
|
|
|
|
float roll, pitch, yaw;
|
|
|
|
} pose;
|
|
|
|
struct {
|
|
|
|
Vector3f world_linear_velocity;
|
|
|
|
} velocity;
|
2019-07-17 13:27:14 -03:00
|
|
|
struct {
|
|
|
|
struct vector3f_array points;
|
|
|
|
} lidar;
|
2019-07-25 04:33:16 -03:00
|
|
|
struct {
|
|
|
|
struct float_array rc_channels;
|
|
|
|
} rc;
|
2020-08-07 17:32:06 -03:00
|
|
|
struct {
|
|
|
|
struct float_array rng_distances;
|
|
|
|
} rng;
|
2019-08-28 10:53:02 -03:00
|
|
|
} state;
|
2019-05-20 19:51:42 -03:00
|
|
|
|
|
|
|
// table to aid parsing of JSON sensor data
|
|
|
|
struct keytable {
|
|
|
|
const char *section;
|
|
|
|
const char *key;
|
|
|
|
void *ptr;
|
|
|
|
enum data_type type;
|
2020-08-07 17:32:06 -03:00
|
|
|
} keytable[13] = {
|
2019-05-20 19:51:42 -03:00
|
|
|
{ "", "timestamp", &state.timestamp, DATA_UINT64 },
|
|
|
|
{ "imu", "angular_velocity", &state.imu.angular_velocity, DATA_VECTOR3F },
|
|
|
|
{ "imu", "linear_acceleration", &state.imu.linear_acceleration, DATA_VECTOR3F },
|
2019-12-16 17:49:17 -04:00
|
|
|
{ "gps", "lat", &state.gps.lat, DATA_DOUBLE },
|
|
|
|
{ "gps", "lon", &state.gps.lon, DATA_DOUBLE },
|
|
|
|
{ "gps", "alt", &state.gps.alt, DATA_DOUBLE },
|
2019-05-20 19:51:42 -03:00
|
|
|
{ "pose", "roll", &state.pose.roll, DATA_FLOAT },
|
|
|
|
{ "pose", "pitch", &state.pose.pitch, DATA_FLOAT },
|
|
|
|
{ "pose", "yaw", &state.pose.yaw, DATA_FLOAT },
|
|
|
|
{ "velocity", "world_linear_velocity", &state.velocity.world_linear_velocity, DATA_VECTOR3F },
|
2019-07-17 13:27:14 -03:00
|
|
|
{ "lidar", "point_cloud", &state.lidar.points, DATA_VECTOR3F_ARRAY },
|
2019-07-25 04:33:16 -03:00
|
|
|
{ "rc", "channels", &state.rc.rc_channels, DATA_FLOAT_ARRAY },
|
2020-08-07 17:32:06 -03:00
|
|
|
{ "rng", "distances", &state.rng.rng_distances, DATA_FLOAT_ARRAY },
|
2019-05-20 19:51:42 -03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-10-11 01:59:19 -03:00
|
|
|
|
|
|
|
#endif // HAL_SIM_AIRSIM_ENABLED
|