local poweredClimb = 0 local glideClimb = 0 local lastAltitude local lastVoiceState = -1 local engineSwitch local resetSwitch local altSensorIndex local energy local voiceSwitch local alarmStep local sensorIDs local sensorParams local sensorLabels local function toCapacity(climb) return 100 * climb / energy end local function onEngineSwitchChanged(value) engineSwitch = system.getInputsVal(value) ~= 0 and value or nil system.pSave("enginesw", engineSwitch) end local function onResetSwitchChanged(value) resetSwitch = system.getInputsVal(value) ~= 0 and value or nil system.pSave("resetsw", resetSwitch) end local function onAltSensorIndexChanged(value) altSensorIndex = value - 1 system.pSave("altidx", altSensorIndex) end local function onEnergyChanged(value) energy = value system.pSave("energy", energy) end local function onVoiceSwitchChanged(value) voiceSwitch = system.getInputsVal(value) ~= 0 and value or nil system.pSave("voicesw", voiceSwitch) end local function onAlarmStepChanged(value) alarmStep = value system.pSave("alarmstp", alarmStep) end local function initForm() form.addRow(2) form.addLabel({ label = "Engine" }) form.addInputbox(engineSwitch, false, onEngineSwitchChanged) form.addRow(2) form.addLabel({ label = "Altitude sensor" }) form.addSelectbox(sensorLabels, altSensorIndex + 1, true, onAltSensorIndexChanged) form.addRow(3) form.addLabel({ label = "100% battery = ", width = 180 }) form.addIntbox(energy, 1, 30000, 500, 0, 1, onEnergyChanged) form.addLabel({ label = "m", alignRight = true }) form.addRow(3) form.addLabel({ label = "Alarm step", width = 180 }) form.addIntbox(alarmStep, 1, 50, 10, 0, 1, onAlarmStepChanged) form.addLabel({ label = "%", alignRight = true }) form.addRow(2) form.addLabel({ label = "Voice announcement" }) form.addInputbox(voiceSwitch, false, onVoiceSwitchChanged) form.addRow(2) form.addLabel({ label = "Reset" }) form.addInputbox(resetSwitch, false, onResetSwitchChanged) end local function printTelemetry(width, height) local text1 = string.format("%.1fm powered", poweredClimb) local text2 = string.format("%.1fm thermal", glideClimb) lcd.drawText(width - 5 - lcd.getTextWidth(FONT_NORMAL, text1), 5, text1) lcd.drawText(width - 5 - lcd.getTextWidth(FONT_NORMAL, text2), 10 + lcd.getTextHeight(FONT_NORMAL), text2) end local function init() sensorIDs = {} sensorParams = {} sensorLabels = {"..."} for _,sensor in ipairs(system.getSensors()) do if sensor.param ~= 0 and sensor.type ~= 5 and sensor.type ~= 9 then sensorIDs[#sensorIDs+1] = sensor.id sensorParams[#sensorParams+1] = sensor.param sensorLabels[#sensorLabels+1] = string.format("%s: %s [%s]", sensor.sensorName, sensor.label, sensor.unit) end end engineSwitch = system.pLoad("enginesw") resetSwitch = system.pLoad("resetsw") altSensorIndex = system.pLoad("altidx", 0) energy = system.pLoad("energy", 500) alarmStep = system.pLoad("alarmstp", 10) voiceSwitch = system.pLoad("voicesw") if altSensorIndex > #sensorIDs then altSensorIndex = 0 end system.registerForm(1, MENU_APPS, "Calculated Capacity", initForm) system.registerTelemetry(2, "Calculated Capacity", 2, printTelemetry) end local function loop() local voicesw = voiceSwitch and system.getInputsVal(voiceSwitch) or -1 if voicesw == 1 and lastVoiceState ~= 1 then system.playNumber(100 - toCapacity(poweredClimb), 0, "%", "Capacity") end lastVoiceState = voicesw if system.getInputsVal(resetSwitch) == 1 then poweredClimb = 0 glideClimb = 0 end local altitude = altSensorIndex ~= 0 and system.getSensorValueByID(sensorIDs[altSensorIndex], sensorParams[altSensorIndex]) or nil if altitude and altitude.valid and lastAltitude then if system.getInputsVal(engineSwitch) == 1 and altitude.value > lastAltitude then poweredClimb = poweredClimb + altitude.value - lastAltitude if toCapacity(poweredClimb) // alarmStep > toCapacity(poweredClimb + lastAltitude - altitude.value) // alarmStep then -- next step -- ALARM system.playNumber((100 - toCapacity(poweredClimb + lastAltitude - altitude.value)) // alarmStep * alarmStep, 0, "%", "Capacity") end elseif system.getInputsVal(engineSwitch) == -1 and altitude.value > lastAltitude then glideClimb = glideClimb + altitude.value - lastAltitude end end lastAltitude = (altitude and altitude.valid) and altitude.value or nil end return { init = init, loop = loop, author = "LeonAir RC", version = "1.1.0", name = "Calculated Capacity" }