collectgarbage() local startStopSwitch local startStopSwitchState=0 local valueIndexLeft,valueIndexRight local altitudeSensorId,altitudeSensorParam local distanceSensorId,distanceSensorParam local distanceUnit local valueNames={"Duration","Sink Rate","Airspeed","Glide Ratio"} local valueUnits={"s","m/s","m/s",":1"} local sensorLabels,sensorIds,sensorParams local distanceUnits={"[m]","[km]"} local duration,sinkRate,airspeed,glideRatio={},{},{},{} local timeStart,altitudeStart,distanceStart local function printColumnBox(x,valueIndex) local value="-" if #duration>0 then if valueIndex==4 then if glideRatio[1]>=0 then value=string.format("%.1f",glideRatio[1]) end elseif valueIndex==3 then if airspeed[1]>=0 then value=string.format("%.1f",airspeed[1]) end elseif valueIndex==2 then if sinkRate[1]>=0 then value=string.format("%.2f",sinkRate[1]) end else value=string.format("%d",duration[1]) end end lcd.drawText(x-lcd.getTextWidth(FONT_MINI,valueNames[valueIndex])/2,3,valueNames[valueIndex],FONT_MINI) lcd.drawText(x-lcd.getTextWidth(FONT_MINI,"["..valueUnits[valueIndex].."]")/2,15,"["..valueUnits[valueIndex].."]",FONT_MINI) lcd.drawText(x-lcd.getTextWidth(FONT_MAXI,value)/2,28,value,FONT_MAXI) end local function printBox() printColumnBox(38,valueIndexLeft) printColumnBox(113,valueIndexRight) end local function printCellTable(x,y,w,value,format) lcd.drawText(x+(w+1-lcd.getTextWidth(format,value))/2,y,value,format) return x+w end local function printRowTable(row,values,format) local x,y=-1,-1+row*18 x=printCellTable(x,y,80,values[1],format) x=printCellTable(x,y,80,values[2],format) x=printCellTable(x,y,80,values[3],format) x=printCellTable(x,y,80,values[4],format) lcd.drawLine(0,y,320,y) end local function printTable() printRowTable(0,valueNames,FONT_BOLD) for i=1,#duration do local values={"-","-","-","-"} if duration[i]>=0 then values[1]=string.format("%d ",duration[i])..valueUnits[1] end if sinkRate[i]>=0 then values[2]=string.format("%.2f ",sinkRate[i])..valueUnits[2] end if airspeed[i]>=0 then values[3]=string.format("%.1f ",airspeed[i])..valueUnits[3] end if glideRatio[i]>=0 then values[4]=string.format("%.1f ",glideRatio[i])..valueUnits[4] end printRowTable(i,values,FONT_NORMAL) end end local function initSensors() local sensors=system.getSensors() sensorLabels={"..."} sensorIds={0} sensorParams={0} for i,sensor in ipairs(sensors) do if sensor.param>0 then table.insert(sensorLabels,sensor.sensorName..": "..sensor.label.." ["..sensor.unit.."]") table.insert(sensorIds,sensor.id) table.insert(sensorParams,sensor.param) end end end local function createForm(formID) initSensors() local altitudeSensorIndex,distanceSensorIndex=1,1 for i=2,#sensorLabels do if altitudeSensorId==sensorIds[i] and altitudeSensorParam==sensorParams[i] then altitudeSensorIndex=i end if distanceSensorId==sensorIds[i] and distanceSensorParam==sensorParams[i] then distanceSensorIndex=i end end form.addRow(2) form.addLabel({label="Start/Stop Switch"}) form.addInputbox(startStopSwitch,false,function(input) startStopSwitch=input system.pSave("startStopSwitch",startStopSwitch) end) form.addRow(2) form.addLabel({label="Box Value Left",width=120}) form.addSelectbox(valueNames,valueIndexLeft,false,function(index) valueIndexLeft=index system.pSave("valueIndexLeft",valueIndexLeft) end,{width=192}) form.addRow(2) form.addLabel({label="Box Value Right",width=120}) form.addSelectbox(valueNames,valueIndexRight,false,function(index) valueIndexRight=index system.pSave("valueIndexRight",valueIndexRight) end,{width=192}) form.addLabel() form.addLabel({label="Sensors",font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Altitude [m]",width=135}) form.addSelectbox(sensorLabels,altitudeSensorIndex,true,function(index) altitudeSensorId=sensorIds[index] system.pSave("altitudeSensorId",altitudeSensorId) altitudeSensorParam=sensorParams[index] system.pSave("altitudeSensorParam",altitudeSensorParam) end,{width=177}) form.addRow(3) form.addLabel({label="Distance",width=72}) form.addSelectbox(distanceUnits,distanceUnit,false,function(index) distanceUnit=index system.pSave("distanceUnit",distanceUnit) end,{width=63}) form.addSelectbox(sensorLabels,distanceSensorIndex,true,function(index) distanceSensorId=sensorIds[index] system.pSave("distanceSensorId",distanceSensorId) distanceSensorParam=sensorParams[index] system.pSave("distanceSensorParam",distanceSensorParam) end,{width=177}) form.addRow(3) end local function init(code) startStopSwitch=system.pLoad("startStopSwitch") valueIndexLeft=system.pLoad("valueIndexLeft",1) valueIndexRight=system.pLoad("valueIndexRight",2) altitudeSensorId=system.pLoad("altitudeSensorId",0) altitudeSensorParam=system.pLoad("altitudeSensorParam",0) distanceSensorId=system.pLoad("distanceSensorId",0) distanceSensorParam=system.pLoad("distanceSensorParam",0) distanceUnit=system.pLoad("distanceUnit",1) system.registerTelemetry(1,"Glide Performance Box",2,printBox) system.registerTelemetry(2,"Glide Performance Table",4,printTable) system.registerForm(1,MENU_APPS,"Glide Performance",createForm) collectgarbage() end local function loop() if not startStopSwitch then return end local startStopSwitchStateOld=startStopSwitchState startStopSwitchState=system.getInputsVal(startStopSwitch) if startStopSwitchState>0 then local timeNow=system.getTimeCounter() local altitudeSensor=system.getSensorValueByID(altitudeSensorId,altitudeSensorParam) local distanceSensor=system.getSensorValueByID(distanceSensorId,distanceSensorParam) if startStopSwitchStateOld<=0 then timeStart=timeNow if altitudeSensor then altitudeStart=altitudeSensor.value end if distanceSensor then distanceStart=distanceSensor.value end table.insert(duration,1,-1) table.insert(sinkRate,1,-1) table.insert(airspeed,1,-1) table.insert(glideRatio,1,-1) if #duration==9 then table.remove(duration,9) table.remove(sinkRate,9) table.remove(airspeed,9) table.remove(glideRatio,9) end else duration[1]=(timeNow-timeStart)/1000 if altitudeSensor then sinkRate[1]=(altitudeStart-altitudeSensor.value)/duration[1] end if distanceSensor then airspeed[1]=(distanceSensor.value-distanceStart)/duration[1] if distanceUnit==2 then airspeed[1]=airspeed[1]*1000 end end if altitudeSensor and distanceSensor then glideRatio[1]=(distanceSensor.value-distanceStart)/(altitudeStart-altitudeSensor.value) if distanceUnit==2 then glideRatio[1]=glideRatio[1]*1000 end end end end collectgarbage() end collectgarbage() return {init=init,loop=loop,author="LD",version="2.1",name="Glide Performance"}