Update on this… after spending a good deal of time with the code, I understand the servo mapping now. However, I can’t seem to access other servo data outside of the first 5 channels.
I have created a tuned ArduPlane parameter file that successfully flies the full-size C172P in JSBSim in auto mission mode with a modified electric motor. In the parameter file, I have mapped SERVO5_FUNCTION
to the auto flaps function and SERVO6_FUNCTION
to ground steering. I then added the following code to JSBSim::send_servos
method:
// NOTE channel mapping begins at 1
char *buf = nullptr;
float aileron = filtered_servo_angle(input, 0); // SERVO1_FUNCTION (ch1)
float elevator = filtered_servo_angle(input, 1); // SERVO2_FUNCTION (ch2)
float throttle = filtered_servo_range(input, 2); // SERVO3_FUNCTION (ch3)
float rudder = filtered_servo_angle(input, 3); // SERVO4_FUNCTION (ch4)
//printf("Input ail: %f elv %f thr %f rud %f\n", aileron, elevator, throttle, rudder);
float auto_flaps = filtered_servo_range(input, 4); // SERVO5_FUNCTION (ch5)
printf("Input flaps: %f \n", auto_flaps);
float auto_steer = filtered_servo_angle(input, 5); // SERVO6_FUNCTION (ch6)
printf("Input steer: %f \n", auto_steer);
The new extracted values are auto_flaps
and auto_steer
. The flaps servo is at offset 4 within the sitl_input
struct and it appears to correctly output a flaps setting while flying based the LAND_FLAP_PERCNT
, TKOFF_FLAP_PERCNT
, FLAP_X_PERCNT
, FLAP_X_SPEED
ArduPlane parameter settings.
The problem I have is that the auto_steer
call to filtered_servo_angle
generates an error – PANIC: Attempt to filter invalid servo at offset 5
. It appears that only the first five channels exist in the sitl_input
struct and there is no channel 6 (offset 5). The JSBSim::send_servos
method is called from JSBSim::update
which takes the sitl_input
struct from its caller. This struct looks like this:
struct sitl_input {
uint16_t servos[32];
struct {
float speed; // m/s
float direction; // degrees 0..360
float turbulence;
float dir_z; //degrees -90..90
} wind;
This struct is supposed to contain 32 values in the servos
array. So it would seem that floats should exist at offsets 4 (ch5) and 5 (ch6).
I am not sure who calls JSBSim::update
outside of the SIM_JSBSim.cpp
file or where the sitl_input
is populated. I am also not sure why the error says the servo value is “invalid” as opposed to some kind of access violation. What am I missing? Any help would be appreciated.