2015-11-19 02:11:13 -04: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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simulator connector for FlightAxis
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SIM_FlightAxis.h"
|
|
|
|
|
2021-10-11 01:59:19 -03:00
|
|
|
#if HAL_SIM_FLIGHTAXIS_ENABLED
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-09-15 21:00:47 -03:00
|
|
|
#include <sys/time.h>
|
2015-11-19 02:11:13 -04:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-01-18 00:23:42 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2015-11-19 02:11:13 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2017-10-20 21:57:59 -03:00
|
|
|
using namespace SITL;
|
2015-11-19 02:11:13 -04:00
|
|
|
|
2021-09-11 23:23:06 -03:00
|
|
|
/*
|
|
|
|
we use a thread for socket creation to reduce the impact of socket
|
|
|
|
creation latency. These condition variables are used to synchronise
|
|
|
|
the thread
|
|
|
|
*/
|
|
|
|
static pthread_cond_t sockcond1 = PTHREAD_COND_INITIALIZER;
|
|
|
|
static pthread_cond_t sockcond2 = PTHREAD_COND_INITIALIZER;
|
|
|
|
static pthread_mutex_t sockmtx = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
// the asprintf() calls are not worth checking for SITL
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-result"
|
|
|
|
|
2017-11-19 03:24:08 -04:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
float value;
|
2018-07-12 20:47:27 -03:00
|
|
|
bool save;
|
2017-11-19 03:24:08 -04:00
|
|
|
} sim_defaults[] = {
|
2020-04-21 21:10:39 -03:00
|
|
|
{ "BRD_OPTIONS", 0},
|
2017-11-19 03:24:08 -04:00
|
|
|
{ "AHRS_EKF_TYPE", 10 },
|
|
|
|
{ "INS_GYR_CAL", 0 },
|
2020-07-30 06:05:08 -03:00
|
|
|
{ "BATT_MONITOR", 4 },
|
2018-07-12 20:47:27 -03:00
|
|
|
{ "RC1_MIN", 1000, true },
|
|
|
|
{ "RC1_MAX", 2000, true },
|
|
|
|
{ "RC2_MIN", 1000, true },
|
|
|
|
{ "RC2_MAX", 2000, true },
|
|
|
|
{ "RC3_MIN", 1000, true },
|
|
|
|
{ "RC3_MAX", 2000, true },
|
|
|
|
{ "RC4_MIN", 1000, true },
|
|
|
|
{ "RC4_MAX", 2000, true },
|
|
|
|
{ "RC2_REVERSED", 1 }, // interlink has reversed rc2
|
2017-11-19 03:24:08 -04:00
|
|
|
{ "SERVO1_MIN", 1000 },
|
|
|
|
{ "SERVO1_MAX", 2000 },
|
|
|
|
{ "SERVO2_MIN", 1000 },
|
|
|
|
{ "SERVO2_MAX", 2000 },
|
|
|
|
{ "SERVO3_MIN", 1000 },
|
|
|
|
{ "SERVO3_MAX", 2000 },
|
|
|
|
{ "SERVO4_MIN", 1000 },
|
|
|
|
{ "SERVO4_MAX", 2000 },
|
|
|
|
{ "SERVO5_MIN", 1000 },
|
|
|
|
{ "SERVO5_MAX", 2000 },
|
|
|
|
{ "SERVO6_MIN", 1000 },
|
|
|
|
{ "SERVO6_MAX", 2000 },
|
|
|
|
{ "SERVO6_MIN", 1000 },
|
|
|
|
{ "SERVO6_MAX", 2000 },
|
|
|
|
{ "INS_ACC2OFFS_X", 0.001 },
|
|
|
|
{ "INS_ACC2OFFS_Y", 0.001 },
|
|
|
|
{ "INS_ACC2OFFS_Z", 0.001 },
|
|
|
|
{ "INS_ACC2SCAL_X", 1.001 },
|
|
|
|
{ "INS_ACC2SCAL_Y", 1.001 },
|
|
|
|
{ "INS_ACC2SCAL_Z", 1.001 },
|
|
|
|
{ "INS_ACCOFFS_X", 0.001 },
|
|
|
|
{ "INS_ACCOFFS_Y", 0.001 },
|
|
|
|
{ "INS_ACCOFFS_Z", 0.001 },
|
|
|
|
{ "INS_ACCSCAL_X", 1.001 },
|
|
|
|
{ "INS_ACCSCAL_Y", 1.001 },
|
|
|
|
{ "INS_ACCSCAL_Z", 1.001 },
|
2021-08-10 08:59:10 -03:00
|
|
|
{ "RPM1_TYPE", 10 },
|
2017-11-19 03:24:08 -04:00
|
|
|
};
|
|
|
|
|
2021-09-15 21:00:47 -03:00
|
|
|
/*
|
|
|
|
get system timestamp in seconds
|
|
|
|
*/
|
|
|
|
static double timestamp_sec()
|
|
|
|
{
|
|
|
|
struct timeval tval;
|
|
|
|
gettimeofday(&tval,NULL);
|
|
|
|
return tval.tv_sec + (tval.tv_usec*1.0e-6);
|
|
|
|
}
|
2017-11-19 03:24:08 -04:00
|
|
|
|
2019-08-15 01:01:24 -03:00
|
|
|
FlightAxis::FlightAxis(const char *frame_str) :
|
|
|
|
Aircraft(frame_str)
|
2015-11-19 02:11:13 -04:00
|
|
|
{
|
|
|
|
use_time_sync = false;
|
2019-09-27 16:56:15 -03:00
|
|
|
num_motors = 2;
|
2015-11-19 02:11:13 -04:00
|
|
|
rate_hz = 250 / target_speedup;
|
2016-10-30 02:24:21 -03:00
|
|
|
heli_demix = strstr(frame_str, "helidemix") != nullptr;
|
|
|
|
rev4_servos = strstr(frame_str, "rev4") != nullptr;
|
2016-04-28 20:03:48 -03:00
|
|
|
const char *colon = strchr(frame_str, ':');
|
|
|
|
if (colon) {
|
|
|
|
controller_ip = colon+1;
|
|
|
|
}
|
2017-11-19 03:24:08 -04:00
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(sim_defaults); i++) {
|
|
|
|
AP_Param::set_default_by_name(sim_defaults[i].name, sim_defaults[i].value);
|
2018-07-12 20:47:27 -03:00
|
|
|
if (sim_defaults[i].save) {
|
|
|
|
enum ap_var_type ptype;
|
|
|
|
AP_Param *p = AP_Param::find(sim_defaults[i].name, &ptype);
|
|
|
|
if (!p->configured()) {
|
|
|
|
p->save();
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 03:24:08 -04:00
|
|
|
}
|
2021-09-11 23:23:06 -03:00
|
|
|
|
|
|
|
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&FlightAxis::socket_creator, void), "SocketCreator", 8192,
|
|
|
|
AP_HAL::Scheduler::PRIORITY_BOOST, 0)) {
|
|
|
|
printf("Failed to create socket_creator thread\n");
|
|
|
|
}
|
2017-10-20 21:57:59 -03:00
|
|
|
}
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
/*
|
|
|
|
extremely primitive SOAP parser that assumes the format used by FlightAxis
|
|
|
|
*/
|
|
|
|
void FlightAxis::parse_reply(const char *reply)
|
|
|
|
{
|
|
|
|
const char *reply0 = reply;
|
|
|
|
for (uint16_t i=0; i<num_keys; i++) {
|
|
|
|
const char *p = strstr(reply, keytable[i].key);
|
|
|
|
if (p == nullptr) {
|
|
|
|
p = strstr(reply0, keytable[i].key);
|
|
|
|
}
|
|
|
|
if (p == nullptr) {
|
|
|
|
printf("Failed to find key %s\n", keytable[i].key);
|
2016-06-03 23:07:03 -03:00
|
|
|
controller_started = false;
|
2015-11-19 02:11:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p += strlen(keytable[i].key) + 1;
|
2016-05-29 06:17:29 -03:00
|
|
|
double v;
|
|
|
|
if (strncmp(p, "true", 4) == 0) {
|
|
|
|
v = 1;
|
|
|
|
} else if (strncmp(p, "false", 5) == 0) {
|
|
|
|
v = 0;
|
|
|
|
} else {
|
|
|
|
v = atof(p);
|
|
|
|
}
|
|
|
|
keytable[i].ref = v;
|
2016-05-03 23:49:42 -03:00
|
|
|
// this assumes key order and allows us to decode arrays
|
|
|
|
p = strchr(p, '>');
|
|
|
|
if (p != nullptr) {
|
|
|
|
reply = p;
|
|
|
|
}
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
make a SOAP request, returning body of reply
|
|
|
|
*/
|
2020-12-16 23:02:06 -04:00
|
|
|
bool FlightAxis::soap_request_start(const char *action, const char *fmt, ...)
|
2015-11-19 02:11:13 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *req1;
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2021-09-11 23:23:06 -03:00
|
|
|
if (sock) {
|
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vasprintf(&req1, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2021-09-11 23:23:06 -03:00
|
|
|
pthread_mutex_lock(&sockmtx);
|
|
|
|
while (socknext == nullptr) {
|
|
|
|
pthread_cond_wait(&sockcond1, &sockmtx);
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
2021-09-11 23:23:06 -03:00
|
|
|
sock = socknext;
|
|
|
|
socknext = nullptr;
|
|
|
|
pthread_cond_broadcast(&sockcond2);
|
|
|
|
pthread_mutex_unlock(&sockmtx);
|
2015-11-19 02:11:13 -04:00
|
|
|
|
|
|
|
char *req;
|
2016-09-27 11:46:23 -03:00
|
|
|
asprintf(&req, R"(POST / HTTP/1.1
|
|
|
|
soapaction: '%s'
|
|
|
|
content-length: %u
|
2015-11-19 02:11:13 -04:00
|
|
|
content-type: text/xml;charset='UTF-8'
|
2016-09-27 11:46:23 -03:00
|
|
|
Connection: Keep-Alive
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
%s)",
|
|
|
|
action,
|
|
|
|
(unsigned)strlen(req1), req1);
|
2020-12-16 23:02:06 -04:00
|
|
|
sock->send(req, strlen(req));
|
2015-11-19 02:11:13 -04:00
|
|
|
free(req1);
|
|
|
|
free(req);
|
2020-12-16 23:02:06 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *FlightAxis::soap_request_end(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
if (!sock) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!sock->pollin(timeout_ms)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
sock->set_blocking(true);
|
|
|
|
ssize_t ret = sock->recv(replybuf, sizeof(replybuf)-1, 1000);
|
2015-11-19 02:11:13 -04:00
|
|
|
if (ret <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-12-16 23:02:06 -04:00
|
|
|
replybuf[ret] = 0;
|
|
|
|
|
|
|
|
char *p = strstr(replybuf, "Content-Length: ");
|
2015-11-19 02:11:13 -04:00
|
|
|
if (p == nullptr) {
|
2020-12-16 23:02:06 -04:00
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
2015-11-19 02:11:13 -04:00
|
|
|
printf("No Content-Length\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the content length
|
2016-10-30 02:24:21 -03:00
|
|
|
uint32_t content_length = strtoul(p+16, nullptr, 10);
|
2015-11-19 02:11:13 -04:00
|
|
|
char *body = strstr(p, "\r\n\r\n");
|
|
|
|
if (body == nullptr) {
|
|
|
|
printf("No body\n");
|
2020-12-16 23:02:06 -04:00
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
2015-11-19 02:11:13 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
body += 4;
|
|
|
|
|
|
|
|
// get the rest of the body
|
2020-12-16 23:02:06 -04:00
|
|
|
int32_t expected_length = content_length + (body - replybuf);
|
|
|
|
if (expected_length >= (int32_t)sizeof(replybuf)) {
|
2016-02-23 13:01:23 -04:00
|
|
|
printf("Reply too large %i\n", expected_length);
|
2020-12-16 23:02:06 -04:00
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
2015-11-19 02:11:13 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
while (ret < expected_length) {
|
2020-12-16 23:02:06 -04:00
|
|
|
ssize_t ret2 = sock->recv(&replybuf[ret], sizeof(replybuf)-(1+ret), 1000);
|
2015-11-19 02:11:13 -04:00
|
|
|
if (ret2 <= 0) {
|
2020-12-16 23:02:06 -04:00
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
2015-11-19 02:11:13 -04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-26 03:37:17 -03:00
|
|
|
// nul terminate
|
2020-12-16 23:02:06 -04:00
|
|
|
replybuf[ret+ret2] = 0;
|
2015-11-19 02:11:13 -04:00
|
|
|
ret += ret2;
|
|
|
|
}
|
2020-12-16 23:02:06 -04:00
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
2021-09-11 23:23:06 -03:00
|
|
|
|
2020-12-16 23:02:06 -04:00
|
|
|
return strdup(replybuf);
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
void FlightAxis::exchange_data(const struct sitl_input &input)
|
|
|
|
{
|
2020-12-16 23:02:06 -04:00
|
|
|
if (!sock &&
|
|
|
|
(!controller_started ||
|
|
|
|
is_zero(state.m_flightAxisControllerIsActive) ||
|
|
|
|
!is_zero(state.m_resetButtonHasBeenPressed))) {
|
2016-04-28 20:03:48 -03:00
|
|
|
printf("Starting controller at %s\n", controller_ip);
|
2015-11-19 02:11:13 -04:00
|
|
|
// call a restore first. This allows us to connect after the aircraft is changed in RealFlight
|
2020-12-16 23:02:06 -04:00
|
|
|
soap_request_start("RestoreOriginalControllerDevice", R"(<?xml version='1.0' encoding='UTF-8'?>
|
2015-11-19 02:11:13 -04:00
|
|
|
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
|
|
|
|
<soap:Body>
|
|
|
|
<RestoreOriginalControllerDevice><a>1</a><b>2</b></RestoreOriginalControllerDevice>
|
|
|
|
</soap:Body>
|
|
|
|
</soap:Envelope>)");
|
2020-12-16 23:02:06 -04:00
|
|
|
soap_request_end(1000);
|
|
|
|
soap_request_start("InjectUAVControllerInterface", R"(<?xml version='1.0' encoding='UTF-8'?>
|
2015-11-19 02:11:13 -04:00
|
|
|
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
|
|
|
|
<soap:Body>
|
|
|
|
<InjectUAVControllerInterface><a>1</a><b>2</b></InjectUAVControllerInterface>
|
|
|
|
</soap:Body>
|
|
|
|
</soap:Envelope>)");
|
2020-12-16 23:02:06 -04:00
|
|
|
soap_request_end(1000);
|
2015-11-19 02:11:13 -04:00
|
|
|
activation_frame_counter = frame_counter;
|
|
|
|
controller_started = true;
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:03:07 -04:00
|
|
|
// maximum number of servos to send is 12 with new FlightAxis
|
|
|
|
float scaled_servos[12];
|
|
|
|
for (uint8_t i=0; i<ARRAY_SIZE(scaled_servos); i++) {
|
2015-11-19 02:11:13 -04:00
|
|
|
scaled_servos[i] = (input.servos[i] - 1000) / 1000.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rev4_servos) {
|
|
|
|
// swap first 4 and last 4 servos, for quadplane testing
|
|
|
|
float saved[4];
|
|
|
|
memcpy(saved, &scaled_servos[0], sizeof(saved));
|
|
|
|
memcpy(&scaled_servos[0], &scaled_servos[4], sizeof(saved));
|
|
|
|
memcpy(&scaled_servos[4], saved, sizeof(saved));
|
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
if (heli_demix) {
|
|
|
|
// FlightAxis expects "roll/pitch/collective/yaw" input
|
|
|
|
float swash1 = scaled_servos[0];
|
|
|
|
float swash2 = scaled_servos[1];
|
|
|
|
float swash3 = scaled_servos[2];
|
|
|
|
|
|
|
|
float roll_rate = swash1 - swash2;
|
|
|
|
float pitch_rate = -((swash1+swash2) / 2.0f - swash3);
|
|
|
|
|
|
|
|
scaled_servos[0] = constrain_float(roll_rate + 0.5, 0, 1);
|
|
|
|
scaled_servos[1] = constrain_float(pitch_rate + 0.5, 0, 1);
|
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2021-08-14 22:15:33 -03:00
|
|
|
const uint16_t channels = hal.scheduler->is_system_initialized()?4095:0;
|
2020-12-16 23:02:06 -04:00
|
|
|
if (!sock) {
|
|
|
|
soap_request_start("ExchangeData", R"(<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
|
2015-11-19 02:11:13 -04:00
|
|
|
<soap:Body>
|
|
|
|
<ExchangeData>
|
|
|
|
<pControlInputs>
|
2021-08-14 22:15:33 -03:00
|
|
|
<m-selectedChannels>%u</m-selectedChannels>
|
2015-11-19 02:11:13 -04:00
|
|
|
<m-channelValues-0to1>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
2018-11-26 22:03:07 -04:00
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
|
|
|
<item>%.4f</item>
|
2015-11-19 02:11:13 -04:00
|
|
|
</m-channelValues-0to1>
|
|
|
|
</pControlInputs>
|
|
|
|
</ExchangeData>
|
|
|
|
</soap:Body>
|
|
|
|
</soap:Envelope>)",
|
2021-08-14 22:15:33 -03:00
|
|
|
channels,
|
|
|
|
scaled_servos[0],
|
|
|
|
scaled_servos[1],
|
|
|
|
scaled_servos[2],
|
|
|
|
scaled_servos[3],
|
|
|
|
scaled_servos[4],
|
|
|
|
scaled_servos[5],
|
|
|
|
scaled_servos[6],
|
|
|
|
scaled_servos[7],
|
|
|
|
scaled_servos[8],
|
|
|
|
scaled_servos[9],
|
|
|
|
scaled_servos[10],
|
|
|
|
scaled_servos[11]);
|
2020-12-16 23:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *reply = nullptr;
|
|
|
|
if (sock) {
|
|
|
|
reply = soap_request_end(0);
|
2021-09-15 21:00:47 -03:00
|
|
|
if (reply == nullptr) {
|
|
|
|
sock_error_count++;
|
|
|
|
if (sock_error_count >= 10000 && timestamp_sec() - last_recv_sec > 1) {
|
|
|
|
printf("socket timeout\n");
|
|
|
|
delete sock;
|
|
|
|
sock = nullptr;
|
|
|
|
sock_error_count = 0;
|
|
|
|
last_recv_sec = timestamp_sec();
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 23:02:06 -04:00
|
|
|
}
|
2015-11-19 02:11:13 -04:00
|
|
|
|
|
|
|
if (reply) {
|
2021-09-15 21:00:47 -03:00
|
|
|
sock_error_count = 0;
|
|
|
|
last_recv_sec = timestamp_sec();
|
2017-10-20 21:57:59 -03:00
|
|
|
double lastt_s = state.m_currentPhysicsTime_SEC;
|
2015-11-19 02:11:13 -04:00
|
|
|
parse_reply(reply);
|
2017-10-20 21:57:59 -03:00
|
|
|
double dt = state.m_currentPhysicsTime_SEC - lastt_s;
|
|
|
|
if (dt > 0 && dt < 0.1) {
|
|
|
|
if (average_frame_time_s < 1.0e-6) {
|
|
|
|
average_frame_time_s = dt;
|
|
|
|
}
|
|
|
|
average_frame_time_s = average_frame_time_s * 0.98 + dt * 0.02;
|
|
|
|
}
|
|
|
|
socket_frame_counter++;
|
2015-11-19 02:11:13 -04:00
|
|
|
free(reply);
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
/*
|
|
|
|
update the FlightAxis simulation by one time step
|
|
|
|
*/
|
|
|
|
void FlightAxis::update(const struct sitl_input &input)
|
|
|
|
{
|
2017-10-20 21:57:59 -03:00
|
|
|
last_input = input;
|
2020-12-16 22:19:19 -04:00
|
|
|
exchange_data(input);
|
|
|
|
|
2016-06-03 22:34:57 -03:00
|
|
|
double dt_seconds = state.m_currentPhysicsTime_SEC - last_time_s;
|
2016-06-03 23:07:03 -03:00
|
|
|
if (dt_seconds < 0) {
|
|
|
|
// cope with restarting RealFlight while connected
|
|
|
|
initial_time_s = time_now_us * 1.0e-6f;
|
|
|
|
last_time_s = state.m_currentPhysicsTime_SEC;
|
|
|
|
position_offset.zero();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-20 21:57:59 -03:00
|
|
|
if (dt_seconds < 0.00001f) {
|
|
|
|
float delta_time = 0.001;
|
|
|
|
// don't go past the next expected frame
|
|
|
|
if (delta_time + extrapolated_s > average_frame_time_s) {
|
|
|
|
delta_time = average_frame_time_s - extrapolated_s;
|
|
|
|
}
|
|
|
|
if (delta_time <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
time_now_us += delta_time * 1.0e6;
|
|
|
|
extrapolate_sensors(delta_time);
|
|
|
|
update_position();
|
|
|
|
update_mag_field_bf();
|
|
|
|
extrapolated_s += delta_time;
|
|
|
|
report_FPS();
|
2015-11-19 02:11:13 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-10-20 21:57:59 -03:00
|
|
|
|
|
|
|
extrapolated_s = 0;
|
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
if (initial_time_s <= 0) {
|
|
|
|
dt_seconds = 0.001f;
|
|
|
|
initial_time_s = state.m_currentPhysicsTime_SEC - dt_seconds;
|
|
|
|
}
|
2016-06-03 22:03:32 -03:00
|
|
|
|
|
|
|
/*
|
2019-12-10 15:03:51 -04:00
|
|
|
the quaternion convention in realflight seems to have Z negative
|
2016-06-03 22:03:32 -03:00
|
|
|
*/
|
2015-11-19 02:11:13 -04:00
|
|
|
Quaternion quat(state.m_orientationQuaternion_W,
|
2016-06-03 22:03:32 -03:00
|
|
|
state.m_orientationQuaternion_Y,
|
|
|
|
state.m_orientationQuaternion_X,
|
2015-11-19 02:11:13 -04:00
|
|
|
-state.m_orientationQuaternion_Z);
|
|
|
|
quat.rotation_matrix(dcm);
|
2017-02-10 01:28:09 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
gyro = Vector3f(radians(constrain_float(state.m_rollRate_DEGpSEC, -2000, 2000)),
|
2017-02-13 05:45:38 -04:00
|
|
|
radians(constrain_float(state.m_pitchRate_DEGpSEC, -2000, 2000)),
|
2015-11-19 02:11:13 -04:00
|
|
|
-radians(constrain_float(state.m_yawRate_DEGpSEC, -2000, 2000))) * target_speedup;
|
2017-02-10 01:28:09 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
velocity_ef = Vector3f(state.m_velocityWorldU_MPS,
|
|
|
|
state.m_velocityWorldV_MPS,
|
|
|
|
state.m_velocityWorldW_MPS);
|
2021-06-20 23:59:02 -03:00
|
|
|
position = Vector3d(state.m_aircraftPositionY_MTR,
|
2015-11-19 02:11:13 -04:00
|
|
|
state.m_aircraftPositionX_MTR,
|
2016-06-04 01:20:55 -03:00
|
|
|
-state.m_altitudeASL_MTR - home.alt*0.01);
|
2021-07-10 03:10:54 -03:00
|
|
|
position.xy() += origin.get_distance_NE_double(home);
|
2016-06-04 01:20:55 -03:00
|
|
|
|
2020-06-04 02:54:29 -03:00
|
|
|
accel_body = {
|
|
|
|
float(state.m_accelerationBodyAX_MPS2),
|
|
|
|
float(state.m_accelerationBodyAY_MPS2),
|
|
|
|
float(state.m_accelerationBodyAZ_MPS2)
|
|
|
|
};
|
2017-02-10 01:28:09 -04:00
|
|
|
|
2016-06-04 02:52:00 -03:00
|
|
|
// accel on the ground is nasty in realflight, and prevents helicopter disarm
|
2018-08-14 23:37:10 -03:00
|
|
|
if (!is_zero(state.m_isTouchingGround)) {
|
2016-06-04 02:52:00 -03:00
|
|
|
Vector3f accel_ef = (velocity_ef - last_velocity_ef) / dt_seconds;
|
|
|
|
accel_ef.z -= GRAVITY_MSS;
|
|
|
|
accel_body = dcm.transposed() * accel_ef;
|
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2016-06-04 01:20:55 -03:00
|
|
|
// limit to 16G to match pixhawk
|
|
|
|
float a_limit = GRAVITY_MSS*16;
|
|
|
|
accel_body.x = constrain_float(accel_body.x, -a_limit, a_limit);
|
|
|
|
accel_body.y = constrain_float(accel_body.y, -a_limit, a_limit);
|
|
|
|
accel_body.z = constrain_float(accel_body.z, -a_limit, a_limit);
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
// offset based on first position to account for offset in RF world
|
2018-08-14 23:37:10 -03:00
|
|
|
if (position_offset.is_zero() || !is_zero(state.m_resetButtonHasBeenPressed)) {
|
2015-11-19 02:11:13 -04:00
|
|
|
position_offset = position;
|
|
|
|
}
|
|
|
|
position -= position_offset;
|
|
|
|
|
|
|
|
airspeed = state.m_airspeed_MPS;
|
2019-04-14 17:36:07 -03:00
|
|
|
|
|
|
|
/* for pitot airspeed we need the airspeed along the X axis. We
|
2019-12-10 15:03:51 -04:00
|
|
|
can't get that from m_airspeed_MPS, so instead we calculate it
|
2019-04-14 17:36:07 -03:00
|
|
|
from wind vector and ground speed
|
|
|
|
*/
|
2022-07-29 22:45:27 -03:00
|
|
|
wind_ef = Vector3f(state.m_windY_MPS,state.m_windX_MPS,state.m_windZ_MPS);
|
|
|
|
Vector3f airspeed_3d_ef = velocity_ef - wind_ef;
|
2019-04-14 17:36:07 -03:00
|
|
|
Vector3f airspeed3d = dcm.mul_transpose(airspeed_3d_ef);
|
|
|
|
|
2019-12-11 20:11:03 -04:00
|
|
|
if (last_imu_rotation != ROTATION_NONE) {
|
2022-09-12 19:40:39 -03:00
|
|
|
airspeed3d = sitl->ahrs_rotation * airspeed3d;
|
2019-12-10 15:03:51 -04:00
|
|
|
}
|
2019-04-14 17:36:07 -03:00
|
|
|
airspeed_pitot = MAX(airspeed3d.x,0);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("WIND: %.1f %.1f %.1f AS3D %.1f %.1f %.1f\n",
|
|
|
|
state.m_windX_MPS,
|
|
|
|
state.m_windY_MPS,
|
|
|
|
state.m_windZ_MPS,
|
|
|
|
airspeed3d.x,
|
|
|
|
airspeed3d.y,
|
|
|
|
airspeed3d.z);
|
|
|
|
#endif
|
2015-11-19 02:11:13 -04:00
|
|
|
|
2021-01-17 18:18:48 -04:00
|
|
|
battery_voltage = MAX(state.m_batteryVoltage_VOLTS, 0);
|
|
|
|
battery_current = MAX(state.m_batteryCurrentDraw_AMPS, 0);
|
2019-09-27 16:56:15 -03:00
|
|
|
rpm[0] = state.m_heliMainRotorRPM;
|
|
|
|
rpm[1] = state.m_propRPM;
|
2016-05-03 23:49:42 -03:00
|
|
|
|
|
|
|
/*
|
2020-07-25 13:04:49 -03:00
|
|
|
the interlink interface supports 12 input channels
|
2016-05-03 23:49:42 -03:00
|
|
|
*/
|
2020-07-25 13:04:49 -03:00
|
|
|
rcin_chan_count = 12;
|
2016-05-04 00:05:24 -03:00
|
|
|
for (uint8_t i=0; i<rcin_chan_count; i++) {
|
2016-05-03 23:49:42 -03:00
|
|
|
rcin[i] = state.rcin[i];
|
|
|
|
}
|
2016-06-04 01:20:55 -03:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
update_position();
|
2017-03-03 06:23:40 -04:00
|
|
|
time_advance();
|
2017-10-20 21:57:59 -03:00
|
|
|
uint64_t new_time_us = (state.m_currentPhysicsTime_SEC - initial_time_s)*1.0e6;
|
|
|
|
if (new_time_us < time_now_us) {
|
|
|
|
uint64_t dt_us = time_now_us - new_time_us;
|
|
|
|
if (dt_us > 500000) {
|
|
|
|
// time going backwards
|
|
|
|
time_now_us = new_time_us;
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
2017-10-20 21:57:59 -03:00
|
|
|
} else {
|
|
|
|
time_now_us = new_time_us;
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
2017-02-13 05:45:38 -04:00
|
|
|
|
2015-11-19 02:11:13 -04:00
|
|
|
last_time_s = state.m_currentPhysicsTime_SEC;
|
2016-06-04 02:52:00 -03:00
|
|
|
|
|
|
|
last_velocity_ef = velocity_ef;
|
2016-06-17 00:46:12 -03:00
|
|
|
|
|
|
|
// update magnetic field
|
|
|
|
update_mag_field_bf();
|
2017-10-20 21:57:59 -03:00
|
|
|
|
|
|
|
report_FPS();
|
2015-11-19 02:11:13 -04:00
|
|
|
}
|
|
|
|
|
2017-10-20 21:57:59 -03:00
|
|
|
/*
|
|
|
|
report frame rates
|
|
|
|
*/
|
|
|
|
void FlightAxis::report_FPS(void)
|
|
|
|
{
|
|
|
|
if (frame_counter++ % 1000 == 0) {
|
2018-08-14 23:37:10 -03:00
|
|
|
if (!is_zero(last_frame_count_s)) {
|
2017-10-20 21:57:59 -03:00
|
|
|
uint64_t frames = socket_frame_counter - last_socket_frame_counter;
|
|
|
|
last_socket_frame_counter = socket_frame_counter;
|
|
|
|
double dt = state.m_currentPhysicsTime_SEC - last_frame_count_s;
|
|
|
|
printf("%.2f/%.2f FPS avg=%.2f\n",
|
|
|
|
frames / dt, 1000 / dt, 1.0/average_frame_time_s);
|
|
|
|
} else {
|
|
|
|
printf("Initial position %f %f %f\n", position.x, position.y, position.z);
|
|
|
|
}
|
|
|
|
last_frame_count_s = state.m_currentPhysicsTime_SEC;
|
|
|
|
}
|
|
|
|
}
|
2021-09-11 23:23:06 -03:00
|
|
|
|
|
|
|
void FlightAxis::socket_creator(void)
|
|
|
|
{
|
|
|
|
socket_pid = getpid();
|
|
|
|
while (true) {
|
|
|
|
pthread_mutex_lock(&sockmtx);
|
|
|
|
while (socknext != nullptr) {
|
|
|
|
pthread_cond_wait(&sockcond2, &sockmtx);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&sockmtx);
|
|
|
|
auto *sck = new SocketAPM(false);
|
|
|
|
if (sck == nullptr) {
|
|
|
|
usleep(500);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!sck->connect(controller_ip, controller_port)) {
|
|
|
|
::printf("connect failed\n");
|
|
|
|
delete sck;
|
|
|
|
usleep(5000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sck->set_blocking(false);
|
|
|
|
socknext = sck;
|
|
|
|
pthread_mutex_lock(&sockmtx);
|
|
|
|
pthread_cond_broadcast(&sockcond1);
|
|
|
|
pthread_mutex_unlock(&sockmtx);
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 01:59:19 -03:00
|
|
|
|
|
|
|
#endif // HAL_SIM_FLIGHTAXIS_ENABLED
|