2020-09-12 16:04:38 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && defined(HAL_BUILD_AP_PERIPH)
|
|
|
|
|
|
|
|
#include "AP_HAL_SITL.h"
|
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
|
|
|
#include "UARTDriver.h"
|
|
|
|
#include "Scheduler.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
#include <SITL/SIM_JSBSim.h>
|
2023-12-25 22:21:11 -04:00
|
|
|
#include <AP_HAL/utility/Socket_native.h>
|
2020-12-28 10:33:56 -04:00
|
|
|
#include <AP_HAL/utility/getopt_cpp.h>
|
2023-08-17 17:18:36 -03:00
|
|
|
#include <SITL/SITL.h>
|
2020-09-12 16:04:38 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
using namespace HALSITL;
|
|
|
|
|
2023-07-06 01:25:48 -03:00
|
|
|
enum long_options {
|
|
|
|
CMDLINE_SERIAL0=1,
|
|
|
|
CMDLINE_SERIAL1,
|
|
|
|
CMDLINE_SERIAL2,
|
|
|
|
CMDLINE_SERIAL3,
|
|
|
|
CMDLINE_SERIAL4,
|
|
|
|
CMDLINE_SERIAL5,
|
|
|
|
CMDLINE_SERIAL6,
|
|
|
|
CMDLINE_SERIAL7,
|
|
|
|
CMDLINE_SERIAL8,
|
|
|
|
CMDLINE_SERIAL9,
|
2023-08-17 22:55:32 -03:00
|
|
|
CMDLINE_DEFAULTS,
|
2023-07-06 01:25:48 -03:00
|
|
|
};
|
|
|
|
|
2020-09-12 16:04:38 -03:00
|
|
|
void SITL_State::init(int argc, char * const argv[]) {
|
2020-12-28 10:33:56 -04:00
|
|
|
int opt;
|
|
|
|
const struct GetOptLong::option options[] = {
|
|
|
|
{"help", false, 0, 'h'},
|
|
|
|
{"instance", true, 0, 'I'},
|
2022-04-29 08:48:37 -03:00
|
|
|
{"maintenance", false, 0, 'M'},
|
2023-07-06 01:25:48 -03:00
|
|
|
{"serial0", true, 0, CMDLINE_SERIAL0},
|
|
|
|
{"serial1", true, 0, CMDLINE_SERIAL1},
|
|
|
|
{"serial2", true, 0, CMDLINE_SERIAL2},
|
|
|
|
{"serial3", true, 0, CMDLINE_SERIAL3},
|
|
|
|
{"serial4", true, 0, CMDLINE_SERIAL4},
|
|
|
|
{"serial5", true, 0, CMDLINE_SERIAL5},
|
|
|
|
{"serial6", true, 0, CMDLINE_SERIAL6},
|
|
|
|
{"serial7", true, 0, CMDLINE_SERIAL7},
|
|
|
|
{"serial8", true, 0, CMDLINE_SERIAL8},
|
|
|
|
{"serial9", true, 0, CMDLINE_SERIAL9},
|
2023-08-17 22:55:32 -03:00
|
|
|
{"defaults", true, 0, CMDLINE_DEFAULTS},
|
2023-07-03 05:27:21 -03:00
|
|
|
{0, false, 0, 0}
|
2020-12-28 10:33:56 -04:00
|
|
|
};
|
2020-09-12 16:04:38 -03:00
|
|
|
|
2020-12-28 10:33:56 -04:00
|
|
|
setvbuf(stdout, (char *)0, _IONBF, 0);
|
|
|
|
setvbuf(stderr, (char *)0, _IONBF, 0);
|
|
|
|
|
2022-04-29 08:48:37 -03:00
|
|
|
GetOptLong gopt(argc, argv, "hI:M",
|
2020-12-28 10:33:56 -04:00
|
|
|
options);
|
|
|
|
|
|
|
|
while((opt = gopt.getoption()) != -1) {
|
|
|
|
switch (opt) {
|
2023-07-06 01:25:48 -03:00
|
|
|
case 'I':
|
|
|
|
_instance = atoi(gopt.optarg);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
printf("Running in Maintenance Mode\n");
|
|
|
|
_maintenance = true;
|
|
|
|
break;
|
|
|
|
case CMDLINE_SERIAL0:
|
|
|
|
case CMDLINE_SERIAL1:
|
|
|
|
case CMDLINE_SERIAL2:
|
|
|
|
case CMDLINE_SERIAL3:
|
|
|
|
case CMDLINE_SERIAL4:
|
|
|
|
case CMDLINE_SERIAL5:
|
|
|
|
case CMDLINE_SERIAL6:
|
|
|
|
case CMDLINE_SERIAL7:
|
|
|
|
case CMDLINE_SERIAL8:
|
2023-12-10 23:55:21 -04:00
|
|
|
case CMDLINE_SERIAL9:
|
|
|
|
_serial_path[opt - CMDLINE_SERIAL0] = gopt.optarg;
|
2023-07-06 01:25:48 -03:00
|
|
|
break;
|
2023-08-17 22:55:32 -03:00
|
|
|
case CMDLINE_DEFAULTS:
|
|
|
|
defaults_path = strdup(gopt.optarg);
|
|
|
|
break;
|
2023-07-06 01:25:48 -03:00
|
|
|
default:
|
|
|
|
printf("Options:\n"
|
|
|
|
"\t--help|-h display this help information\n"
|
|
|
|
"\t--instance|-I N set instance of SITL Periph\n"
|
|
|
|
"\t--maintenance|-M run in maintenance mode\n"
|
2023-08-17 22:55:32 -03:00
|
|
|
"\t--defaults path set param defaults file\n"
|
2023-07-06 01:25:48 -03:00
|
|
|
"\t--serial0 device set device string for SERIAL0\n"
|
|
|
|
"\t--serial1 device set device string for SERIAL1\n"
|
|
|
|
"\t--serial2 device set device string for SERIAL2\n"
|
|
|
|
"\t--serial3 device set device string for SERIAL3\n"
|
|
|
|
"\t--serial4 device set device string for SERIAL4\n"
|
|
|
|
"\t--serial5 device set device string for SERIAL5\n"
|
|
|
|
"\t--serial6 device set device string for SERIAL6\n"
|
|
|
|
"\t--serial7 device set device string for SERIAL7\n"
|
|
|
|
"\t--serial8 device set device string for SERIAL8\n"
|
|
|
|
"\t--serial9 device set device string for SERIAL9\n"
|
|
|
|
);
|
|
|
|
exit(1);
|
2020-12-28 10:33:56 -04:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 00:10:40 -03:00
|
|
|
|
2020-12-28 10:33:56 -04:00
|
|
|
printf("Running Instance: %d\n", _instance);
|
2023-08-17 17:18:36 -03:00
|
|
|
|
2024-05-26 22:24:12 -03:00
|
|
|
sitl_model = NEW_NOTHROW SimMCast("");
|
2023-08-17 17:18:36 -03:00
|
|
|
|
|
|
|
_sitl = AP::sitl();
|
2023-09-27 20:34:22 -03:00
|
|
|
|
|
|
|
_sitl->i2c_sim.init();
|
|
|
|
sitl_model->set_i2c(&_sitl->i2c_sim);
|
2020-09-12 16:04:38 -03:00
|
|
|
}
|
|
|
|
|
2023-08-17 17:18:36 -03:00
|
|
|
void SITL_State::wait_clock(uint64_t wait_time_usec)
|
|
|
|
{
|
2023-08-21 22:41:17 -03:00
|
|
|
while (AP_HAL::micros64() < wait_time_usec) {
|
2023-08-17 17:18:36 -03:00
|
|
|
struct sitl_input input {};
|
2024-07-13 14:00:11 -03:00
|
|
|
sitl_model->update(input); // delays up to 1 millisecond
|
2023-08-17 17:18:36 -03:00
|
|
|
sim_update();
|
2023-08-17 18:55:20 -03:00
|
|
|
update_voltage_current(input, 0);
|
2020-09-12 16:04:38 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 17:18:36 -03:00
|
|
|
/*
|
|
|
|
open multicast input from main simulator
|
|
|
|
*/
|
|
|
|
void SimMCast::multicast_open(void)
|
|
|
|
{
|
2023-11-23 01:56:12 -04:00
|
|
|
if (!sock.connect(SITL_MCAST_IP, SITL_MCAST_PORT)) {
|
|
|
|
fprintf(stderr, "multicast socket failed - %s\n", strerror(errno));
|
2023-08-17 17:18:36 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2023-11-23 01:56:12 -04:00
|
|
|
servo_sock.set_blocking(false);
|
2023-08-19 18:33:30 -03:00
|
|
|
::printf("multicast receiver initialised\n");
|
2023-08-17 17:18:36 -03:00
|
|
|
}
|
|
|
|
|
2023-08-19 03:09:50 -03:00
|
|
|
/*
|
|
|
|
open UDP socket back to master for servo output
|
|
|
|
*/
|
|
|
|
void SimMCast::servo_fd_open(void)
|
|
|
|
{
|
2023-11-23 01:56:12 -04:00
|
|
|
const char *in_addr = nullptr;
|
|
|
|
uint16_t port;
|
|
|
|
sock.last_recv_address(in_addr, port);
|
|
|
|
if (in_addr == nullptr) {
|
|
|
|
return;
|
2023-08-19 03:09:50 -03:00
|
|
|
}
|
2023-11-23 01:56:12 -04:00
|
|
|
if (!servo_sock.connect(in_addr, SITL_SERVO_PORT)) {
|
|
|
|
fprintf(stderr, "servo socket failed - %s\n", strerror(errno));
|
2023-08-19 03:09:50 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2023-11-23 01:56:12 -04:00
|
|
|
servo_sock.set_blocking(false);
|
2023-08-19 03:09:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
send servo outputs back to master
|
|
|
|
*/
|
|
|
|
void SimMCast::servo_send(void)
|
|
|
|
{
|
2023-08-20 21:50:37 -03:00
|
|
|
const auto *_sitl = AP::sitl();
|
|
|
|
if (_sitl == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-19 03:09:50 -03:00
|
|
|
uint16_t out[SITL_NUM_CHANNELS] {};
|
|
|
|
hal.rcout->read(out, SITL_NUM_CHANNELS);
|
2023-08-20 21:50:37 -03:00
|
|
|
|
|
|
|
float out_float[SITL_NUM_CHANNELS];
|
|
|
|
const uint32_t mask = uint32_t(_sitl->can_servo_mask.get());
|
|
|
|
for (uint8_t i=0; i<SITL_NUM_CHANNELS; i++) {
|
|
|
|
out_float[i] = (mask & (1U<<i)) ? out[i] : nanf("");
|
|
|
|
}
|
2023-11-23 01:56:12 -04:00
|
|
|
servo_sock.send((void*)out_float, sizeof(out_float));
|
2023-08-19 03:09:50 -03:00
|
|
|
}
|
|
|
|
|
2023-08-17 17:18:36 -03:00
|
|
|
/*
|
|
|
|
read state from multicast
|
|
|
|
*/
|
|
|
|
void SimMCast::multicast_read(void)
|
|
|
|
{
|
|
|
|
auto *_sitl = AP::sitl();
|
|
|
|
if (_sitl == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-18 22:03:34 -03:00
|
|
|
if (_sitl->state.timestamp_us == 0) {
|
|
|
|
printf("Waiting for multicast state\n");
|
|
|
|
}
|
2023-08-17 17:18:36 -03:00
|
|
|
struct SITL::sitl_fdm state;
|
2024-07-13 14:00:11 -03:00
|
|
|
while (sock.recv((void*)&state, sizeof(state), 1) != sizeof(state)) {
|
2023-08-19 03:37:16 -03:00
|
|
|
// nop
|
|
|
|
}
|
2023-08-19 18:33:30 -03:00
|
|
|
if (_sitl->state.timestamp_us == 0) {
|
|
|
|
printf("Got multicast state input\n");
|
|
|
|
}
|
2023-08-19 03:37:16 -03:00
|
|
|
if (state.timestamp_us < _sitl->state.timestamp_us) {
|
2023-08-19 18:33:30 -03:00
|
|
|
printf("multicast state time reset\n");
|
2023-08-19 03:37:16 -03:00
|
|
|
// main process has rebooted
|
|
|
|
base_time_us += (_sitl->state.timestamp_us - state.timestamp_us);
|
|
|
|
}
|
|
|
|
_sitl->state = state;
|
2023-09-29 01:11:22 -03:00
|
|
|
location.lat = state.latitude*1.0e7;
|
|
|
|
location.lng = state.longitude*1.0e7;
|
|
|
|
location.alt = state.altitude*1.0e2;
|
|
|
|
if (home.is_zero()) {
|
|
|
|
home = location;
|
|
|
|
}
|
2023-08-19 03:37:16 -03:00
|
|
|
hal.scheduler->stop_clock(_sitl->state.timestamp_us + base_time_us);
|
|
|
|
HALSITL::Scheduler::timer_event();
|
2023-11-23 01:56:12 -04:00
|
|
|
if (!servo_sock.is_connected()) {
|
2023-08-19 03:37:16 -03:00
|
|
|
servo_fd_open();
|
|
|
|
} else {
|
|
|
|
servo_send();
|
2023-08-17 17:18:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SimMCast::SimMCast(const char *frame_str) :
|
|
|
|
Aircraft(frame_str)
|
|
|
|
{
|
|
|
|
multicast_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimMCast::update(const struct sitl_input &input)
|
|
|
|
{
|
|
|
|
multicast_read();
|
2023-10-31 05:09:11 -03:00
|
|
|
update_home();
|
2023-09-27 20:34:22 -03:00
|
|
|
update_external_payload(input);
|
|
|
|
|
|
|
|
auto *_sitl = AP::sitl();
|
|
|
|
if (_sitl != nullptr) {
|
|
|
|
battery_voltage = _sitl->batt_voltage;
|
|
|
|
}
|
2023-08-17 17:18:36 -03:00
|
|
|
}
|
2020-09-12 16:04:38 -03:00
|
|
|
|
|
|
|
#endif //CONFIG_HAL_BOARD == HAL_BOARD_SITL && defined(HAL_BUILD_AP_PERIPH)
|