--[[ Driver for INF_Inject EFI system https://innoflighttechnology.com/efi/ --]] local PARAM_TABLE_KEY = 43 local PARAM_TABLE_PREFIX = "EFI_INF_" local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} local CMD_CDI1 = 1 local CMD_CDI2 = 2 local CMD_OIL_PUMP = 3 -- local CMD_SHUTDOWN = 4 -- local CMD_PRE_INJECTION = 5 local CMD_THROTTLE = 6 local K_THROTTLE = 70 -- bind a parameter to a variable given local function bind_param(name) local p = Parameter() assert(p:init(name), string.format('could not find %s parameter', name)) return p end -- add a parameter and bind it to a variable local function bind_add_param(name, idx, default_value) assert(param:add_param(PARAM_TABLE_KEY, idx, name, default_value), string.format('could not add param %s', name)) return bind_param(PARAM_TABLE_PREFIX .. name) end -- setup script specific parameters assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 8), 'could not add param table') --[[ // @Param: EFI_INF_ENABLE // @DisplayName: EFI INF-Inject enable // @Description: Enable EFI INF-Inject driver // @Values: 0:Disabled,1:Enabled // @User: Standard --]] EFI_INF_ENABLE = bind_add_param("ENABLE", 1, 1) --[[ // @Param: EFI_INF_OPTIONS // @DisplayName: EFI INF-Inject options // @Description: EFI INF driver options // @Bitmask: 0:EnableLogging // @User: Standard --]] EFI_INF_OPTIONS = bind_add_param("OPTIONS", 2, 0) --[[ // @Param: EFI_INF_THR_HZ // @DisplayName: EFI INF-Inject throttle rate // @Description: EFI INF throttle output rate // @Range: 0 50 // @Units: Hz // @User: Standard --]] EFI_INF_THR_HZ = bind_add_param("THR_HZ", 3, 0) --[[ // @Param: EFI_INF_IGN_AUX // @DisplayName: EFI INF-Inject ignition aux function // @Description: EFI INF throttle ignition aux function // @User: Standard --]] EFI_INF_IGN_AUX = bind_add_param("IGN_AUX", 4, 300) local OPTION_LOGGING = (1<<0) --[[ return true if an option is enabled --]] local function option_enabled(option) return (EFI_INF_OPTIONS:get() & option) ~= 0 end if EFI_INF_ENABLE:get() ~= 1 then return end local EFI_FUEL_DENS = bind_param("EFI_FUEL_DENS") local SCR_VM_I_COUNT = bind_param("SCR_VM_I_COUNT") local uart = serial:find_serial(0) -- first scripting serial if not uart then gcs:send_text(MAV_SEVERITY.ERROR, "EFI_INF: unable to find scripting serial") return end uart:begin(9600) local efi_backend = efi:get_backend(0) if not efi_backend then gcs:send_text(MAV_SEVERITY.ERROR, "EFI_INF: unable to find EFI backend") return end --[[ we need a bit more time in this driver --]] if SCR_VM_I_COUNT:get() < 50000 then gcs:send_text(MAV_SEVERITY.INFO, "EFI_INF: raising SCR_VM_I_COUNT to 50000") SCR_VM_I_COUNT:set_and_save(50000) end local state = {} state.last_read_us = uint32_t(0) state.chk0 = 0 state.chk1 = 0 state.total_fuel_g = 0.0 local last_throttle_send_ms = uint32_t(0) local last_ignition_send_ms = uint32_t(0) local last_ign_sw_pos = -1 local file_handle = nil local efi_device_id = nil --[[ log a set of bytes --]] local function log_bytes(s) if not file_handle then file_handle = io.open("INF_Inject.log", "w") end if file_handle then local magic = 0x7fe53b04 local now_ms = millis():toint() local hdr = string.pack("= packet_size and not header_ok do state.chk0 = 0 state.chk1 = 0 local header0 = string.unpack("= 1 then command = 1 end if sw_pos ~= last_ign_sw_pos then onoff = "OFF" if command == 1 then onoff = "ON" end gcs:send_text(MAV_SEVERITY.INFO, string.format("EFI_INF: ignition %s", onoff)) end last_ign_sw_pos = sw_pos send_packet(CMD_CDI1, command) send_packet(CMD_CDI2, command) send_packet(CMD_OIL_PUMP, command) end --[[ main update function --]] local function update() if check_input() then update_EFI() end if efi_device_id then update_throttle() update_ignition() end return update, 10 end gcs:send_text(MAV_SEVERITY.INFO, "EFI_INF: loaded") return update()