-- ############################################################################# -- # Calculated Capacity V0.9 - Lua application tested with DS16 -- # -- # Copyright (c) 2017, Walter Loetscher -- # All rights reserved. -- # -- # License: Share alike -- # Can be used and changed non commercial -- # -- # V0.9 - Initial release (Beta), -- ############################################################################# -------------------------------------------------------------------------------- collectgarbage() -- Locals local appName = "Calculated Capacity V0.9" local actualFuel=1000 fuel=1000 maxFuel=1000 dispFuel=100 local count = 0 addThrottle = 0 avgThrottle = 0 throttle = 0 local actualTime = 0 oneSecond = false acType = 1 local acOptions = {"Electro", "Jet", "Jetstart", "Gas"} local starting = false local alarm50 = true alarm25 = true alarm20 = true local alarm15 = true alarm10 = true local alarmknockoff = true alarmbingo = true local fullThrust, thrustControl, cellDec, charLine, minThrust, safetySwitch, resetSwitch local knockoff, bingo local engineState = 0 -- Engine Status: (0=stop, 1=start, 3=running) local sw1, rs1, tr1, gf1, delay local corrFuel = 1 fuelFlow = 0 -------------------------------------------------------------------------------- local function printTelem() lcd.drawText(10,5,string.format("%s%%",dispFuel),FONT_MAXI) lcd.drawNumber(10,50,fullThrust,FONT_NORMAL) lcd.drawText(50,50,"Sekunden") end -------------------------------------------------------------------------------- local function atChanged(value) acType=value system.pSave("acType",value) end local function tcChanged(value) thrustControl=value system.pSave("thrustControl",value) end local function ftChanged(value) fullThrust=value system.pSave("fullThrust",value) end local function cdChanged(value) cellDec=value system.pSave("cellDec",value) end local function clChanged(value) charLine=value system.pSave("charLine",value) end local function mtChanged(value) minThrust=value system.pSave("minThrust",value) end local function koChanged(value) knockoff=value system.pSave("knockoff",value) end local function biChanged(value) bingo=value system.pSave("bingo",value) end local function swChanged(value) safetySwitch=value system.pSave("safetySwitch",value) end local function rsChanged(value) resetSwitch=value system.pSave("resetSwitch",value) end local function gfChanged(value) getFuel=value system.pSave("getFuel",value) end -------------------------------------------------------------------------------- local function calcAvgThrottle() if system.getInputsVal(thrustControl) then throttle = ((system.getInputsVal(thrustControl))+1)/2 addThrottle = (addThrottle + throttle) end end -------------------------------------------------------------------------------- local function calcOneSecond() oneSecond = false if system.getTimeCounter() >= actualTime +1000 then oneSecond = true actualTime = system.getTimeCounter() end end -------------------------------------------------------------------------------- local function adjustFuel() corrFuel = 1-((1-actualFuel/maxFuel)*cellDec/100) fuelFlow = avgThrottle^(charLine/100)*(corrFuel-minThrust/1000)+minThrust/1000 actualFuel = actualFuel-fuelFlow*maxFuel/fullThrust fuel = math.floor(actualFuel) dispFuel = string.format("%.1f", fuel/10) end -------------------------------------------------------------------------------- local function resetAlarm() alarm50 = true alarm25 = true alarm20 = true alarm15 = true alarm10 = true alarmknockoff = true alarmbingo = true end -------------------------------------------------------------------------------- local function alarm() if (bingo ~= 0) or (knockoff ~= 0) then if fuel <= 500 and alarm50 then system.playNumber(50, 0, "%") alarm50 = false end if fuel <= (knockoff * 10) and alarmknockoff then system.playFile("CC_Knockoff.wav", AUDIO_BACKGROUND) alarmknockoff = false end if fuel <= (bingo * 10) and alarmbingo then system.playFile("CC_Bingo.wav", AUDIO_BACKGROUND) alarmbingo = false end else if fuel <= 500 and alarm50 then system.playNumber(50, 0, "%") alarm50 = false end if fuel <= 250 and alarm25 then system.playNumber(25, 0, "%") alarm25 = false end if fuel <= 200 and alarm20 then system.playNumber(20, 0, "%") alarm20 = false end if fuel <= 150 and alarm15 then system.playNumber(15, 0, "%") alarm15 = false end if fuel <= 100 and alarm10 then system.playNumber(10, 0, "%") alarm10 = false end end end -------------------------------------------------------------------------------- local function startup() if (sw1 >= 0) and (engineState == 0) then engineState = 1 end if (sw1 >= 0) and (engineState == 1) and (tr1 > 0.9) then starting = true end if (starting == true) and (tr1 < 0.1) then starting = false engineState = 3 end if (sw1 == -1) and engineState == 3 then engineState = 0 end end -------------------------------------------------------------------------------- local function state() if sw1 >= 0 then engineState = 3 end if (acType ~= 1) and (sw1 == -1) then engineState = 0 end end -------------------------------------------------------------------------------- local function check() sw1 = system.getInputsVal(safetySwitch) rs1 = system.getInputsVal(resetSwitch) tr1 = system.getInputsVal(thrustControl) gf1 = system.getInputsVal(getFuel) if sw1 == nil then sw1 = 0 end if rs1 == nil then rs1 = 0 end if tr1 == nil then tr1 = 0 end if gf1 == nil then gf1 = 0 end end -------------------------------------------------------------------------------- local function report() if delay == 1 then system.playNumber(fuel/10, 0, "%") end if delay == 100 then delay = 0 end end -------------------------------------------------------------------------------- -- Loop local function loop() print("fuel", fuel, "avgThrottle", avgThrottle, "actualFuel", actualFuel) print("count", count, "Get Fuel", gf1) check() if rs1 == 1 then actualFuel = 1000 fuel = 1000 rs1 = 0 resetAlarm() end if acType == 3 then startup() else state() end local txTel = system.getTxTelemetry() --if txTel.rx1Voltage > 3 then count = count + 1 calcAvgThrottle() calcOneSecond() if oneSecond then avgThrottle = addThrottle / count if sw1 == -1 then avgThrottle = 0 end if engineState >= 2 then adjustFuel() end addThrottle = 0 count = 0 end --end alarm() if gf1 == 1 then delay = delay + 1 report() else delay = 0 end end -------------------------------------------------------------------------------- -- Form initialization local function initForm(subform) form.addRow(2) form.addLabel({label="Aircraft Type", font=FONT_BOLD}) form.addSelectbox(acOptions, acType, false, atChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Throttle Control", font=FONT_BOLD}) form.addInputbox(thrustControl,true,tcChanged, {font=FONT_BOLD}) form.addLabel({label=" --> Set Proportional!"}) form.addRow(2) form.addLabel({label="Seconds Fullthrust", font=FONT_BOLD}) form.addIntbox(fullThrust,1,3600,0,0,1,ftChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Voltage Drop %", font=FONT_BOLD}) form.addIntbox(cellDec,0,100,20,0,1,cdChanged, {font=FONT_BOLD}) form.addLabel({label=" (Electro 20, Gas + Jet 0)"}) form.addRow(2) form.addLabel({label="Thrust Linearity", font=FONT_BOLD}) form.addIntbox(charLine,1,500,140,2,1,clChanged, {font=FONT_BOLD}) form.addLabel({label=" (Example: Electro 1.4, Jet 1.0)"}) form.addRow(2) form.addLabel({label="Idle Thrust", font=FONT_BOLD}) form.addIntbox(minThrust,0,1000,5,1,1,mtChanged, {font=FONT_BOLD}) form.addLabel({label=" (Idle Consumption Per Cent)"}) form.addRow(2) form.addLabel({label="Prewarning", font=FONT_BOLD}) form.addIntbox(knockoff,0,100,0,0,1,koChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Warning (Bingo)", font=FONT_BOLD}) form.addIntbox(bingo,0,100,0,0,1,biChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Safety Switch", font=FONT_BOLD}) form.addInputbox(safetySwitch,true,swChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Reset Switch", font=FONT_BOLD}) form.addInputbox(resetSwitch,true,rsChanged, {font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Report Actual Fuel", font=FONT_BOLD}) form.addInputbox(getFuel,true,gfChanged, {font=FONT_BOLD}) end -------------------------------------------------------------------------------- local function init() system.registerForm(1,MENU_MAIN,appName,initForm, nil,printForm) system.registerTelemetry(1,"Calc Capacity 0.9",2,printTelem) -- Saved Items acType = system.pLoad("acType",1) -- Which AC Type thrustControl = system.pLoad("thrustControl") -- Throttle (P4 for Mode 2), use Proportional! fullThrust = system.pLoad("fullThrust",180) -- Flight Time Full Thrust cellDec = system.pLoad("cellDec",20) -- Voltage Drop, ca. 20 for LiPo charLine = system.pLoad("charLine",140) -- Controller Characteristic minThrust = system.pLoad("minThrust",5) -- Idle Consumption in Thousandth (Promille) of Max Thrust knockoff = system.pLoad("knockoff",0) -- Prewarning bingo = system.pLoad("bingo",0) -- Final Warning safetySwitch = system.pLoad("safetySwitch") -- Engine Safety Switch resetSwitch = system.pLoad("resetSwitch") -- Reset capacity to 100% (Gas Engines) getFuel = system.pLoad("getFuel") -- Report actual Capacity collectgarbage() end -------------------------------------------------------------------------------- collectgarbage() return {init=init, loop=loop, author="Walter Loetscher", version="0.9", name = appName}