August 20, 200520 yr Moderator Here's one way to get "Vertical Speed Change" working in a C autopilot. VS is selected via the "knob" to descend or ascend. Clicking on the knob's center will "cancel" SVS (Selected Vertical Speed) and return the AP HOLD (current altitude.For brevity, I've only included the relevant parts of the C source code:/* The two FLOAT64 values may be simple "float" if you wish. I have to use FLOAT64 because of the need to convert to WCHAR for a GDI+ string */FLOAT64 vspeed = 0 ; // This variable is used for display of the AUTOPILOT_VERTICAL_HOLD_VARFLOAT64 dvspeed = 0 ; // This variable is needed because the AUTOPILOT_VERTICAL_HOLD_VAR cannot be "changed" when in ALT HOLD modeMODULE_VAR AUTOPILOT_ALTITUDE_LOCKvar = {AUTOPILOT_ALTITUDE_LOCK};MODULE_VAR AUTOPILOT_ACTIVEvar = {AUTOPILOT_ACTIVE};MODULE_VAR AUTOPILOT_HEADING_LOCKvar = {AUTOPILOT_HEADING_LOCK};MODULE_VAR AUTOPILOT_NAV1_LOCKvar = {AUTOPILOT_NAV1_LOCK};MODULE_VAR GPS_GROUND_TRACKvar = {GPS_GROUND_TRACK};MODULE_VAR AUTOPILOT_VERTICAL_HOLD_VARvar = {AUTOPILOT_VERTICAL_HOLD_VAR};MODULE_VAR AUTOPILOT_VERTICAL_HOLDvar = {AUTOPILOT_VERTICAL_HOLD};MODULE_VAR AUTOPILOT_ATTITUDE_HOLDvar = { AUTOPILOT_ATTITUDE_HOLD };case PANEL_SERVICE_PRE_UPDATE: lookup_var(&AUTOPILOT_ALTITUDE_LOCKvar); lookup_var(&AUTOPILOT_ACTIVEvar); lookup_var(&AUTOPILOT_NAV1_LOCKvar); lookup_var(&AUTOPILOT_VERTICAL_HOLD_VARvar); lookup_var(&AUTOPILOT_ATTITUDE_HOLDvar); vspeed = (AUTOPILOT_VERTICAL_HOLD_VARvar.var_value.n); lookup_var(&AUTOPILOT_HEADING_LOCKvar); lookup_var(&AUTOPILOT_HEADING_LOCK_DIRvar); hdg = (AUTOPILOT_HEADING_LOCK_DIRvar.var_value.n); lookup_var(&HSI_OBI_NEEDLEvar); crs = (HSI_OBI_NEEDLEvar.var_value.n); lookup_var(&GPS_GROUND_TRACKvar); dtk = RAD_TO_DEG(GPS_GROUND_TRACKvar.var_value.n);break;case PANEL_SERVICE_PRE_DRAW: { /* Update Routines */ /* The dvspeed variable is set via the "knob turn" in the mouse callback. This line ensures that the "selected altitude" is always different (plus or minus) than current altitude! */ trigger_key_event(KEY_AP_ALT_VAR_SET_ENGLISH, (ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n + dvspeed) ); }break;/* This is the DECREASE mousepoint */BOOL FSAPI DF_mouse_cb2( PPIXPOINT relative_point, FLAGS32 mouse_flags){ if (mode == 3 || mode == 5) { dvspeed = dvspeed - 100 ; trigger_key_event(KEY_AP_VS_VAR_SET_ENGLISH, dvspeed); trigger_key_event(KEY_AP_WING_LEVELER_OFF,0); } return FALSE;}/* This is the INCREASE mousepoint */BOOL FSAPI DF_mouse_cb3( PPIXPOINT relative_point, FLAGS32 mouse_flags){ if (mode == 3 || mode == 5) { dvspeed = dvspeed + 100 ; trigger_key_event(KEY_AP_VS_VAR_SET_ENGLISH, dvspeed); trigger_key_event(KEY_AP_WING_LEVELER_OFF,0); } return FALSE;}/* This is the CANCEL mousepoint */BOOL FSAPI DF_mouse_cb4( PPIXPOINT relative_point, FLAGS32 mouse_flags){ if (mode == 3 || mode == 5 ) { dvspeed = 0 ; trigger_key_event(KEY_AP_VS_VAR_SET_ENGLISH, dvspeed); trigger_key_event(KEY_AP_ALT_VAR_SET_ENGLISH, (ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n) ); trigger_key_event(KEY_AP_WING_LEVELER_OFF,0); trigger_key_event(KEY_AP_PANEL_VS_OFF, 0); } Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
August 21, 200520 yr Hi Bill, there's a major problem with this code. First, please use send_key_event instead of trigger_key_event. send_key_event doesn't flood FS with key presses. And second, avoid unconditional calls to send/trigger_key_event in element or gauge callbacks - they kill the frame rate and stop shortcuts like Shift+E+2 from working.Do it like this:case PANEL_SERVICE_PRE_DRAW: if ((ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n != oldAlt) || (dvspeed != olddvspeed)){ send_key_event(KEY_AP_ALT_VAR_SET_ENGLISH, ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n + dvspeed) ); oldAlt = ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n; olddvspeed = dvspeed;}break;BTW: there's no need for brackets {} in a case branch.
August 21, 200520 yr Author Moderator >BTW: there's no need for brackets {} in a case branch.Thank you. I am aware of that. I omitted all the rest of the code that is contained within those {} braces though for the sake of clarity... :)Thanks for the suggestion to use send_key_event, as well as the reality check in the PRE_DRAW update routine. Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
August 21, 200520 yr Author Moderator After removing the extraneous ), the code is perfect. Thanks again!ALT_FROM_BAROMETRIC_PRESSUREvar.var_value.n + dvspeed) ); Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
August 21, 200520 yr Author Moderator >Oops. I didn't see that when I typed the message. Nobody's>perfect ;-)Too true! Pobody's nerfect, that for sure! Thanks again... Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
Create an account or sign in to comment