2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL_SITL.h"
|
2015-05-04 03:15:12 -03:00
|
|
|
#include "AP_HAL_SITL_Namespace.h"
|
|
|
|
#include "HAL_SITL_Class.h"
|
2012-12-14 21:54:26 -04:00
|
|
|
#include "UARTDriver.h"
|
|
|
|
#include "Scheduler.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2012-12-18 03:12:05 -04:00
|
|
|
#include <sys/select.h>
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2016-01-01 02:09:07 -04:00
|
|
|
#include <SITL/SIM_JSBSim.h>
|
2016-01-03 17:22:02 -04:00
|
|
|
#include <AP_HAL/utility/Socket.h>
|
2015-03-21 11:27:25 -03:00
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 18:08:42 -03:00
|
|
|
void SITL_State::_set_param_default(const char *parm)
|
2014-04-10 23:28:32 -03:00
|
|
|
{
|
2015-05-04 18:08:42 -03:00
|
|
|
char *pdup = strdup(parm);
|
|
|
|
char *p = strchr(pdup, '=');
|
2016-10-30 02:24:21 -03:00
|
|
|
if (p == nullptr) {
|
2014-04-10 23:28:32 -03:00
|
|
|
printf("Please specify parameter as NAME=VALUE");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
float value = strtof(p+1, nullptr);
|
2014-04-10 23:28:32 -03:00
|
|
|
*p = 0;
|
|
|
|
enum ap_var_type var_type;
|
2015-10-01 22:15:21 -03:00
|
|
|
AP_Param *vp = AP_Param::find(pdup, &var_type);
|
2016-10-30 02:24:21 -03:00
|
|
|
if (vp == nullptr) {
|
2015-10-01 22:15:21 -03:00
|
|
|
printf("Unknown parameter %s\n", pdup);
|
2015-05-04 21:59:07 -03:00
|
|
|
exit(1);
|
2014-04-10 23:28:32 -03:00
|
|
|
}
|
|
|
|
if (var_type == AP_PARAM_FLOAT) {
|
|
|
|
((AP_Float *)vp)->set_and_save(value);
|
|
|
|
} else if (var_type == AP_PARAM_INT32) {
|
|
|
|
((AP_Int32 *)vp)->set_and_save(value);
|
|
|
|
} else if (var_type == AP_PARAM_INT16) {
|
|
|
|
((AP_Int16 *)vp)->set_and_save(value);
|
|
|
|
} else if (var_type == AP_PARAM_INT8) {
|
|
|
|
((AP_Int8 *)vp)->set_and_save(value);
|
|
|
|
} else {
|
2015-10-01 22:15:21 -03:00
|
|
|
printf("Unable to set parameter %s\n", pdup);
|
2014-04-10 23:28:32 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-10-01 22:15:21 -03:00
|
|
|
printf("Set parameter %s to %f\n", pdup, value);
|
2015-05-04 18:08:42 -03:00
|
|
|
free(pdup);
|
2014-04-10 23:28:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
setup for SITL handling
|
|
|
|
*/
|
2015-11-18 21:22:47 -04:00
|
|
|
void SITL_State::_sitl_setup(const char *home_str)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2016-06-20 22:00:36 -03:00
|
|
|
_home_str = home_str;
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
#ifndef __CYGWIN__
|
2015-05-04 21:59:07 -03:00
|
|
|
_parent_pid = getppid();
|
2012-12-14 21:54:26 -04:00
|
|
|
#endif
|
2015-05-04 21:59:07 -03:00
|
|
|
_rcout_addr.sin_family = AF_INET;
|
|
|
|
_rcout_addr.sin_port = htons(_rcout_port);
|
|
|
|
inet_pton(AF_INET, _fdm_address, &_rcout_addr.sin_addr);
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2014-08-09 08:23:19 -03:00
|
|
|
#ifndef HIL_MODE
|
2015-05-04 21:59:07 -03:00
|
|
|
_setup_fdm();
|
2014-08-09 08:23:19 -03:00
|
|
|
#endif
|
2015-05-04 21:59:07 -03:00
|
|
|
fprintf(stdout, "Starting SITL input\n");
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
// find the barometer object if it exists
|
2015-10-22 10:04:42 -03:00
|
|
|
_sitl = (SITL::SITL *)AP_Param::find_object("SIM_");
|
2015-05-04 21:59:07 -03:00
|
|
|
_barometer = (AP_Baro *)AP_Param::find_object("GND_");
|
|
|
|
_ins = (AP_InertialSensor *)AP_Param::find_object("INS_");
|
|
|
|
_compass = (Compass *)AP_Param::find_object("COMPASS_");
|
2016-06-03 15:07:36 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2015-05-04 21:59:07 -03:00
|
|
|
_terrain = (AP_Terrain *)AP_Param::find_object("TERRAIN_");
|
2016-06-03 15:07:36 -03:00
|
|
|
#endif
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_sitl != nullptr) {
|
2012-12-14 21:54:26 -04:00
|
|
|
// setup some initial values
|
2014-08-09 08:23:19 -03:00
|
|
|
#ifndef HIL_MODE
|
2015-05-10 19:48:42 -03:00
|
|
|
_update_barometer(100);
|
|
|
|
_update_ins(0, 0, 0, 0, 0, 0, 0, 0, -9.8, 0, 100);
|
2012-12-14 21:54:26 -04:00
|
|
|
_update_compass(0, 0, 0);
|
2013-03-27 20:28:44 -03:00
|
|
|
_update_gps(0, 0, 0, 0, 0, 0, false);
|
2014-08-09 08:23:19 -03:00
|
|
|
#endif
|
2015-05-24 23:11:16 -03:00
|
|
|
if (enable_gimbal) {
|
2016-02-26 01:15:00 -04:00
|
|
|
gimbal = new SITL::Gimbal(_sitl->state);
|
2015-05-24 23:11:16 -03:00
|
|
|
}
|
2016-01-01 02:09:07 -04:00
|
|
|
|
2016-09-21 14:02:15 -03:00
|
|
|
if (_use_fg_view) {
|
2016-11-15 10:44:51 -04:00
|
|
|
fg_socket.connect("127.0.0.1", _fg_view_port);
|
2016-09-21 14:02:15 -03:00
|
|
|
}
|
|
|
|
|
2016-11-21 09:39:13 -04:00
|
|
|
fprintf(stdout, "Using Irlock at port : %d\n", _irlock_port);
|
2016-11-17 14:05:04 -04:00
|
|
|
_sitl->irlock_port = _irlock_port;
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
2015-03-21 11:27:25 -03:00
|
|
|
|
|
|
|
if (_synthetic_clock_mode) {
|
|
|
|
// start with non-zero clock
|
2015-04-30 04:15:40 -03:00
|
|
|
hal.scheduler->stop_clock(1);
|
2015-03-21 11:27:25 -03:00
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-09 08:23:19 -03:00
|
|
|
#ifndef HIL_MODE
|
2012-12-14 21:54:26 -04:00
|
|
|
/*
|
|
|
|
setup a SITL FDM listening UDP port
|
|
|
|
*/
|
|
|
|
void SITL_State::_setup_fdm(void)
|
|
|
|
{
|
2016-09-21 14:15:00 -03:00
|
|
|
if (!_sitl_rc_in.bind("0.0.0.0", _rcin_port)) {
|
2016-11-21 09:15:00 -04:00
|
|
|
fprintf(stderr, "SITL: socket bind failed on RC in port : %d - %s\n", _rcin_port, strerror(errno));
|
|
|
|
fprintf(stderr, "Abording launch...\n");
|
2015-05-04 21:59:07 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-01-03 17:22:02 -04:00
|
|
|
_sitl_rc_in.reuseaddress();
|
|
|
|
_sitl_rc_in.set_blocking(false);
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
2014-08-09 08:23:19 -03:00
|
|
|
#endif
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2015-05-02 08:41:13 -03:00
|
|
|
step the FDM by one time step
|
2012-12-14 21:54:26 -04:00
|
|
|
*/
|
2015-05-02 08:41:13 -03:00
|
|
|
void SITL_State::_fdm_input_step(void)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2015-05-02 08:41:13 -03:00
|
|
|
static uint32_t last_pwm_input = 0;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-11-22 22:21:33 -04:00
|
|
|
_fdm_input_local();
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-02 08:41:13 -03:00
|
|
|
/* make sure we die if our parent dies */
|
|
|
|
if (kill(_parent_pid, 0) != 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_scheduler->interrupts_are_blocked() || _sitl == nullptr) {
|
2015-05-02 08:41:13 -03:00
|
|
|
return;
|
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-02 08:41:13 -03:00
|
|
|
// simulate RC input at 50Hz
|
2015-11-19 23:11:17 -04:00
|
|
|
if (AP_HAL::millis() - last_pwm_input >= 20 && _sitl->rc_fail == 0) {
|
|
|
|
last_pwm_input = AP_HAL::millis();
|
2015-05-02 08:41:13 -03:00
|
|
|
new_rc_input = true;
|
|
|
|
}
|
2015-03-21 11:27:25 -03:00
|
|
|
|
2015-05-02 08:41:13 -03:00
|
|
|
_scheduler->sitl_begin_atomic();
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_update_count == 0 && _sitl != nullptr) {
|
2015-05-02 08:41:13 -03:00
|
|
|
_update_gps(0, 0, 0, 0, 0, 0, false);
|
|
|
|
_update_barometer(0);
|
2015-03-21 11:27:25 -03:00
|
|
|
_scheduler->timer_event();
|
|
|
|
_scheduler->sitl_end_atomic();
|
2015-05-02 08:41:13 -03:00
|
|
|
return;
|
|
|
|
}
|
2015-03-21 11:27:25 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_sitl != nullptr) {
|
2015-05-02 08:41:13 -03:00
|
|
|
_update_gps(_sitl->state.latitude, _sitl->state.longitude,
|
|
|
|
_sitl->state.altitude,
|
|
|
|
_sitl->state.speedN, _sitl->state.speedE, _sitl->state.speedD,
|
|
|
|
!_sitl->gps_disable);
|
|
|
|
_update_ins(_sitl->state.rollDeg, _sitl->state.pitchDeg, _sitl->state.yawDeg,
|
|
|
|
_sitl->state.rollRate, _sitl->state.pitchRate, _sitl->state.yawRate,
|
|
|
|
_sitl->state.xAccel, _sitl->state.yAccel, _sitl->state.zAccel,
|
|
|
|
_sitl->state.airspeed, _sitl->state.altitude);
|
|
|
|
_update_barometer(_sitl->state.altitude);
|
|
|
|
_update_compass(_sitl->state.rollDeg, _sitl->state.pitchDeg, _sitl->state.yawDeg);
|
2016-06-20 22:00:36 -03:00
|
|
|
|
|
|
|
if (_sitl->adsb_plane_count >= 0 &&
|
|
|
|
adsb == nullptr) {
|
|
|
|
adsb = new SITL::ADSB(_sitl->state, _home_str);
|
|
|
|
} else if (_sitl->adsb_plane_count == -1 &&
|
|
|
|
adsb != nullptr) {
|
|
|
|
delete adsb;
|
|
|
|
adsb = nullptr;
|
|
|
|
}
|
2015-05-02 08:41:13 -03:00
|
|
|
}
|
2015-05-04 21:59:07 -03:00
|
|
|
|
|
|
|
// trigger all APM timers.
|
2015-05-02 08:41:13 -03:00
|
|
|
_scheduler->timer_event();
|
|
|
|
_scheduler->sitl_end_atomic();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SITL_State::wait_clock(uint64_t wait_time_usec)
|
|
|
|
{
|
2015-11-19 23:11:17 -04:00
|
|
|
while (AP_HAL::micros64() < wait_time_usec) {
|
2015-05-02 08:41:13 -03:00
|
|
|
_fdm_input_step();
|
2015-03-21 11:27:25 -03:00
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
2014-08-09 08:23:19 -03:00
|
|
|
#ifndef HIL_MODE
|
2012-12-14 21:54:26 -04:00
|
|
|
/*
|
2016-09-21 14:15:00 -03:00
|
|
|
check for a SITL RC input packet
|
2012-12-14 21:54:26 -04:00
|
|
|
*/
|
2016-09-21 14:15:00 -03:00
|
|
|
void SITL_State::_check_rc_input(void)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
ssize_t size;
|
|
|
|
struct pwm_packet {
|
2016-01-03 17:22:02 -04:00
|
|
|
uint16_t pwm[16];
|
2015-11-22 22:21:33 -04:00
|
|
|
} pwm_pkt;
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2016-01-03 17:22:02 -04:00
|
|
|
size = _sitl_rc_in.recv(&pwm_pkt, sizeof(pwm_pkt), 0);
|
2015-11-22 22:21:33 -04:00
|
|
|
switch (size) {
|
2016-01-03 17:22:02 -04:00
|
|
|
case 8*2:
|
|
|
|
case 16*2: {
|
2015-05-04 21:59:07 -03:00
|
|
|
// a packet giving the receiver PWM inputs
|
|
|
|
uint8_t i;
|
2016-01-03 17:22:02 -04:00
|
|
|
for (i=0; i<size/2; i++) {
|
2016-01-04 09:21:54 -04:00
|
|
|
// setup the pwm input for the RC channel inputs
|
2016-05-03 23:50:02 -03:00
|
|
|
if (i < _sitl->state.rcin_chan_count) {
|
|
|
|
// we're using rc from simulator
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-22 22:21:33 -04:00
|
|
|
if (pwm_pkt.pwm[i] != 0) {
|
|
|
|
pwm_input[i] = pwm_pkt.pwm[i];
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
2015-05-02 07:27:33 -03:00
|
|
|
|
2016-01-01 02:09:07 -04:00
|
|
|
/*
|
|
|
|
output current state to flightgear
|
|
|
|
*/
|
|
|
|
void SITL_State::_output_to_flightgear(void)
|
|
|
|
{
|
|
|
|
SITL::FGNetFDM fdm {};
|
|
|
|
const SITL::sitl_fdm &sfdm = _sitl->state;
|
|
|
|
|
|
|
|
fdm.version = 0x18;
|
|
|
|
fdm.padding = 0;
|
|
|
|
fdm.longitude = radians(sfdm.longitude);
|
|
|
|
fdm.latitude = radians(sfdm.latitude);
|
|
|
|
fdm.altitude = sfdm.altitude;
|
|
|
|
fdm.agl = sfdm.altitude;
|
|
|
|
fdm.phi = radians(sfdm.rollDeg);
|
|
|
|
fdm.theta = radians(sfdm.pitchDeg);
|
|
|
|
fdm.psi = radians(sfdm.yawDeg);
|
2016-01-03 20:09:27 -04:00
|
|
|
if (_vehicle == ArduCopter) {
|
|
|
|
fdm.num_engines = 4;
|
|
|
|
for (uint8_t i=0; i<4; i++) {
|
|
|
|
fdm.rpm[i] = constrain_float((pwm_output[i]-1000), 0, 1000);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fdm.num_engines = 4;
|
|
|
|
fdm.rpm[0] = constrain_float((pwm_output[2]-1000)*3, 0, 3000);
|
|
|
|
// for quadplane
|
|
|
|
fdm.rpm[1] = constrain_float((pwm_output[5]-1000)*12, 0, 12000);
|
|
|
|
fdm.rpm[2] = constrain_float((pwm_output[6]-1000)*12, 0, 12000);
|
|
|
|
fdm.rpm[3] = constrain_float((pwm_output[7]-1000)*12, 0, 12000);
|
|
|
|
}
|
2016-01-01 02:09:07 -04:00
|
|
|
fdm.ByteSwap();
|
|
|
|
|
|
|
|
fg_socket.send(&fdm, sizeof(fdm));
|
|
|
|
}
|
2015-05-02 07:27:33 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
get FDM input from a local model
|
|
|
|
*/
|
|
|
|
void SITL_State::_fdm_input_local(void)
|
|
|
|
{
|
2016-02-26 01:15:00 -04:00
|
|
|
SITL::Aircraft::sitl_input input;
|
2015-05-02 07:27:33 -03:00
|
|
|
|
2015-05-02 08:54:19 -03:00
|
|
|
// check for direct RC input
|
2016-09-21 14:15:00 -03:00
|
|
|
_check_rc_input();
|
2015-05-02 08:54:19 -03:00
|
|
|
|
|
|
|
// construct servos structure for FDM
|
2015-05-02 07:27:33 -03:00
|
|
|
_simulator_servos(input);
|
2015-05-02 08:54:19 -03:00
|
|
|
|
|
|
|
// update the model
|
2015-05-02 07:27:33 -03:00
|
|
|
sitl_model->update(input);
|
2015-05-02 08:54:19 -03:00
|
|
|
|
|
|
|
// get FDM output from the model
|
2016-03-25 07:35:16 -03:00
|
|
|
if (_sitl) {
|
|
|
|
sitl_model->fill_fdm(_sitl->state);
|
|
|
|
_sitl->update_rate_hz = sitl_model->get_rate_hz();
|
2016-05-03 23:50:02 -03:00
|
|
|
|
2016-06-04 16:54:46 -03:00
|
|
|
if (_sitl->rc_fail == 0) {
|
|
|
|
for (uint8_t i=0; i< _sitl->state.rcin_chan_count; i++) {
|
|
|
|
pwm_input[i] = 1000 + _sitl->state.rcin[i]*1000;
|
|
|
|
}
|
2016-05-03 23:50:02 -03:00
|
|
|
}
|
2016-03-25 07:35:16 -03:00
|
|
|
}
|
2015-05-02 08:54:19 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (gimbal != nullptr) {
|
2015-05-24 23:11:16 -03:00
|
|
|
gimbal->update();
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
if (adsb != nullptr) {
|
2015-11-18 21:22:47 -04:00
|
|
|
adsb->update();
|
|
|
|
}
|
2015-05-24 23:11:16 -03:00
|
|
|
|
2016-09-21 14:02:15 -03:00
|
|
|
if (_sitl && _use_fg_view) {
|
2016-03-25 07:35:16 -03:00
|
|
|
_output_to_flightgear();
|
|
|
|
}
|
2016-01-01 02:09:07 -04:00
|
|
|
|
2015-05-02 08:54:19 -03:00
|
|
|
// update simulation time
|
2016-03-25 07:35:16 -03:00
|
|
|
if (_sitl) {
|
|
|
|
hal.scheduler->stop_clock(_sitl->state.timestamp_us);
|
|
|
|
} else {
|
|
|
|
hal.scheduler->stop_clock(AP_HAL::micros64()+100);
|
|
|
|
}
|
2015-05-02 08:54:19 -03:00
|
|
|
|
2016-11-19 02:21:54 -04:00
|
|
|
set_height_agl();
|
|
|
|
|
2015-05-02 07:27:33 -03:00
|
|
|
_synthetic_clock_mode = true;
|
|
|
|
_update_count++;
|
|
|
|
}
|
2014-08-09 08:23:19 -03:00
|
|
|
#endif
|
2012-12-14 21:54:26 -04:00
|
|
|
|
|
|
|
/*
|
2015-05-02 07:27:33 -03:00
|
|
|
create sitl_input structure for sending to FDM
|
2012-12-14 21:54:26 -04:00
|
|
|
*/
|
2016-02-26 01:15:00 -04:00
|
|
|
void SITL_State::_simulator_servos(SITL::Aircraft::sitl_input &input)
|
2012-12-14 21:54:26 -04:00
|
|
|
{
|
2015-05-02 07:27:33 -03:00
|
|
|
static uint32_t last_update_usec;
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
/* this maps the registers used for PWM outputs. The RC
|
|
|
|
* driver updates these whenever it wants the channel output
|
|
|
|
* to change */
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if (last_update_usec == 0) {
|
2015-06-29 19:55:02 -03:00
|
|
|
for (i=0; i<SITL_NUM_CHANNELS; i++) {
|
2015-05-04 21:59:07 -03:00
|
|
|
pwm_output[i] = 1000;
|
|
|
|
}
|
|
|
|
if (_vehicle == ArduPlane) {
|
|
|
|
pwm_output[0] = pwm_output[1] = pwm_output[3] = 1500;
|
|
|
|
}
|
|
|
|
if (_vehicle == APMrover2) {
|
|
|
|
pwm_output[0] = pwm_output[1] = pwm_output[2] = pwm_output[3] = 1500;
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
// output at chosen framerate
|
2015-11-19 23:11:17 -04:00
|
|
|
uint32_t now = AP_HAL::micros();
|
2015-05-04 21:59:07 -03:00
|
|
|
last_update_usec = now;
|
2013-09-15 20:16:52 -03:00
|
|
|
|
2015-05-22 22:04:13 -03:00
|
|
|
// pass wind into simulators, using a wind gradient below 60m
|
|
|
|
float altitude = _barometer?_barometer->get_altitude():0;
|
2016-07-15 22:36:59 -03:00
|
|
|
float wind_speed = 0;
|
|
|
|
float wind_direction = 0;
|
|
|
|
if (_sitl) {
|
|
|
|
// The EKF does not like step inputs so this LPF keeps it happy.
|
|
|
|
wind_speed = _sitl->wind_speed_active = (0.95f*_sitl->wind_speed_active) + (0.05f*_sitl->wind_speed);
|
|
|
|
wind_direction = _sitl->wind_direction_active = (0.95f*_sitl->wind_direction_active) + (0.05f*_sitl->wind_direction);
|
|
|
|
}
|
|
|
|
|
2015-05-22 22:04:13 -03:00
|
|
|
if (altitude < 0) {
|
|
|
|
altitude = 0;
|
|
|
|
}
|
|
|
|
if (altitude < 60) {
|
2016-04-20 04:26:16 -03:00
|
|
|
wind_speed *= sqrtf(MAX(altitude / 60, 0));
|
2015-05-22 22:04:13 -03:00
|
|
|
}
|
2016-07-15 22:36:59 -03:00
|
|
|
|
2015-05-22 22:04:13 -03:00
|
|
|
input.wind.speed = wind_speed;
|
2016-07-15 22:36:59 -03:00
|
|
|
input.wind.direction = wind_direction;
|
2016-03-25 07:35:16 -03:00
|
|
|
input.wind.turbulence = _sitl?_sitl->wind_turbulance:0;
|
2015-05-22 22:04:13 -03:00
|
|
|
|
2015-06-29 19:55:02 -03:00
|
|
|
for (i=0; i<SITL_NUM_CHANNELS; i++) {
|
2015-05-04 21:59:07 -03:00
|
|
|
if (pwm_output[i] == 0xFFFF) {
|
|
|
|
input.servos[i] = 0;
|
|
|
|
} else {
|
|
|
|
input.servos[i] = pwm_output[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 00:17:12 -03:00
|
|
|
float engine_mul = _sitl?_sitl->engine_mul.get():1;
|
2017-03-20 17:41:09 -03:00
|
|
|
uint8_t engine_fail = _sitl?_sitl->engine_fail.get():0;
|
2016-03-25 07:35:16 -03:00
|
|
|
bool motors_on = false;
|
|
|
|
|
2017-04-07 22:50:28 -03:00
|
|
|
if (engine_fail >= ARRAY_SIZE(input.servos)) {
|
2017-03-20 17:41:09 -03:00
|
|
|
engine_fail = 0;
|
|
|
|
}
|
2017-04-07 22:50:28 -03:00
|
|
|
// apply engine multiplier to motor defined by the SIM_ENGINE_FAIL parameter
|
|
|
|
input.servos[engine_fail] = ((input.servos[engine_fail]-1000) * engine_mul) + 1000;
|
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (_vehicle == ArduPlane) {
|
2016-03-25 07:35:16 -03:00
|
|
|
motors_on = ((input.servos[2]-1000)/1000.0f) > 0;
|
2015-05-04 21:59:07 -03:00
|
|
|
} else if (_vehicle == APMrover2) {
|
2016-03-25 07:35:16 -03:00
|
|
|
motors_on = ((input.servos[2]-1500)/500.0f) != 0;
|
2015-05-04 21:59:07 -03:00
|
|
|
} else {
|
2016-03-25 07:35:16 -03:00
|
|
|
motors_on = false;
|
2013-05-19 02:29:08 -03:00
|
|
|
// run checks on each motor
|
2015-05-04 21:59:07 -03:00
|
|
|
for (i=0; i<4; i++) {
|
2013-05-19 02:29:08 -03:00
|
|
|
// check motors do not exceed their limits
|
2015-05-02 07:27:33 -03:00
|
|
|
if (input.servos[i] > 2000) input.servos[i] = 2000;
|
|
|
|
if (input.servos[i] < 1000) input.servos[i] = 1000;
|
2013-05-19 02:29:08 -03:00
|
|
|
// update motor_on flag
|
2015-05-04 21:59:07 -03:00
|
|
|
if ((input.servos[i]-1000)/1000.0f > 0) {
|
2016-03-25 07:35:16 -03:00
|
|
|
motors_on = true;
|
2015-05-04 21:59:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 07:35:16 -03:00
|
|
|
if (_sitl) {
|
|
|
|
_sitl->motors_on = motors_on;
|
|
|
|
}
|
2012-12-14 21:54:26 -04:00
|
|
|
|
2016-03-25 07:35:16 -03:00
|
|
|
float voltage = 0;
|
|
|
|
_current = 0;
|
2015-11-22 22:31:29 -04:00
|
|
|
|
2016-03-25 07:35:16 -03:00
|
|
|
if (_sitl != nullptr) {
|
|
|
|
if (_sitl->state.battery_voltage <= 0) {
|
|
|
|
// simulate simple battery setup
|
|
|
|
float throttle = motors_on?(input.servos[2]-1000) / 1000.0f:0;
|
|
|
|
// lose 0.7V at full throttle
|
|
|
|
voltage = _sitl->batt_voltage - 0.7f*fabsf(throttle);
|
|
|
|
|
|
|
|
// assume 50A at full throttle
|
|
|
|
_current = 50.0f * fabsf(throttle);
|
|
|
|
} else {
|
|
|
|
// FDM provides voltage and current
|
|
|
|
voltage = _sitl->state.battery_voltage;
|
|
|
|
_current = _sitl->state.battery_current;
|
|
|
|
}
|
2015-11-22 22:31:29 -04:00
|
|
|
}
|
2015-05-02 07:27:33 -03:00
|
|
|
|
2013-09-30 02:34:47 -03:00
|
|
|
// assume 3DR power brick
|
2015-04-24 00:47:07 -03:00
|
|
|
voltage_pin_value = ((voltage / 10.1f) / 5.0f) * 1024;
|
|
|
|
current_pin_value = ((_current / 17.0f) / 5.0f) * 1024;
|
2015-05-02 07:27:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
// generate a random float between -1 and 1
|
|
|
|
float SITL_State::_rand_float(void)
|
|
|
|
{
|
|
|
|
return ((((unsigned)random()) % 2000000) - 1.0e6) / 1.0e6;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a random Vector3f of size 1
|
|
|
|
Vector3f SITL_State::_rand_vec3f(void)
|
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
Vector3f v = Vector3f(_rand_float(),
|
2012-12-14 21:54:26 -04:00
|
|
|
_rand_float(),
|
|
|
|
_rand_float());
|
2015-05-04 21:59:07 -03:00
|
|
|
if (v.length() != 0.0f) {
|
|
|
|
v.normalize();
|
|
|
|
}
|
|
|
|
return v;
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SITL_State::init(int argc, char * const argv[])
|
|
|
|
{
|
2012-12-17 23:56:21 -04:00
|
|
|
pwm_input[0] = pwm_input[1] = pwm_input[3] = 1500;
|
|
|
|
pwm_input[4] = pwm_input[7] = 1800;
|
|
|
|
pwm_input[2] = pwm_input[5] = pwm_input[6] = 1000;
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
_scheduler = Scheduler::from(hal.scheduler);
|
2015-05-04 21:59:07 -03:00
|
|
|
_parse_command_line(argc, argv);
|
2012-12-14 21:54:26 -04:00
|
|
|
}
|
|
|
|
|
2015-01-03 06:47:54 -04:00
|
|
|
/*
|
2016-11-19 02:21:54 -04:00
|
|
|
set height above the ground in meters
|
2015-01-03 06:47:54 -04:00
|
|
|
*/
|
2016-11-19 02:21:54 -04:00
|
|
|
void SITL_State::set_height_agl(void)
|
2015-01-03 06:47:54 -04:00
|
|
|
{
|
2015-05-04 21:59:07 -03:00
|
|
|
static float home_alt = -1;
|
2015-01-03 06:47:54 -04:00
|
|
|
|
2015-05-04 21:59:07 -03:00
|
|
|
if (home_alt == -1 && _sitl->state.altitude > 0) {
|
2015-01-03 06:47:54 -04:00
|
|
|
// remember home altitude as first non-zero altitude
|
2015-05-04 21:59:07 -03:00
|
|
|
home_alt = _sitl->state.altitude;
|
2015-01-03 06:47:54 -04:00
|
|
|
}
|
|
|
|
|
2016-06-03 15:07:36 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2015-01-03 06:47:54 -04:00
|
|
|
if (_terrain &&
|
2015-05-04 21:59:07 -03:00
|
|
|
_sitl->terrain_enable) {
|
2015-01-03 06:47:54 -04:00
|
|
|
// get height above terrain from AP_Terrain. This assumes
|
|
|
|
// AP_Terrain is working
|
|
|
|
float terrain_height_amsl;
|
|
|
|
struct Location location;
|
|
|
|
location.lat = _sitl->state.latitude*1.0e7;
|
|
|
|
location.lng = _sitl->state.longitude*1.0e7;
|
|
|
|
|
2016-04-18 08:42:48 -03:00
|
|
|
if (_terrain->height_amsl(location, terrain_height_amsl, false)) {
|
2016-11-19 02:21:54 -04:00
|
|
|
_sitl->height_agl = _sitl->state.altitude - terrain_height_amsl;
|
|
|
|
return;
|
2015-01-03 06:47:54 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 15:07:36 -03:00
|
|
|
#endif
|
2015-01-03 06:47:54 -04:00
|
|
|
|
|
|
|
// fall back to flat earth model
|
2016-11-19 02:21:54 -04:00
|
|
|
_sitl->height_agl = _sitl->state.altitude - home_alt;
|
2015-01-03 06:47:54 -04:00
|
|
|
}
|
|
|
|
|
2012-12-14 21:54:26 -04:00
|
|
|
#endif
|