Dustan Giles
New Member
- Messages
- 1
- Reaction score
- 4
I made a altitude holding PID controler for kFS.
I have never really done control systems programing so this was a bit of an adventure. Currently I have a vertical speed limiting if statement which seems to prevent excessive overshooting. It works okay on earth but not as well as on the moon. Thank you devs for making such a cool 'game'!
demo video

I have never really done control systems programing so this was a bit of an adventure. Currently I have a vertical speed limiting if statement which seems to prevent excessive overshooting. It works okay on earth but not as well as on the moon. Thank you devs for making such a cool 'game'!
demo video

Code
#include <kfs.h>
#include <vehicle.h>
#pragma kfs tick_hz 10
#pragma kfs subscribe MSG_FLIGHT_DATA flight
#pragma kfs task fly rate_hz=10
#define TARGET_ALTITUDE_M (4.0f)
kfs_msg_flight_data_t flight;
float throttle;
float altitude_error;
float kP = 0.21;
float kD = 0.8f;
float kI = 0.006f;
float p_term;
float i_term;
float d_term;
void fly(void)
{
// kfs_hold_heading(0.0f, 90.0f * DEG2RAD, KFS_ROLL_UP);
kfs_engines_on();
altitude_error = TARGET_ALTITUDE_M - flight.radar_altitude_m;
p_term = altitude_error;
i_term = i_term + (altitude_error*0.1f);
d_term = -1*flight.vertical_speed_mps;
if (i_term > 20.0f)
i_term = 20.0f;
if (i_term < -20.0f)
i_term = -20.0f;
if (flight.vertical_speed_mps < 10)
throttle = p_term*kP+d_term*kD+i_term*kI;
else
throttle = 0;
if (throttle > 1.0f) throttle = 1.0f;
if (throttle < 0.0f) throttle = 0.0f;
kfs_set_throttle(throttle);
kfs_logf("altitude %.1f m, altitude error %.3f m, i_term %.3f, vertical speed %.1f m/s throttle %.3f", flight.radar_altitude_m, altitude_error, i_term, flight.vertical_speed_mps, throttle);
}
Last edited: