collectgarbage() local startStopSwitch local startStopSwitchState=0 local windowValue1,windowValue2 local altitudeSensorID,altitudeSensorParam local distanceSensorID,distanceSensorParam local distanceUnit local windowValues={"Duration","Sink Rate","Airspeed","Glide Ratio"} local windowValueUnits={"[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 printRowWindow(y,windowValue) local value="-" if #duration>0 then if windowValue==4 then if glideRatio[1]>=0 then value=string.format("%.1f",glideRatio[1]) end elseif windowValue==3 then if airspeed[1]>=0 then value=string.format("%.1f",airspeed[1]) end elseif windowValue==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(5,y+7,windowValues[windowValue],FONT_MINI) lcd.drawText(5,y+19,windowValueUnits[windowValue],FONT_MINI) lcd.drawText(145-lcd.getTextWidth(FONT_MAXI,value),y,value,FONT_MAXI) end local function printWindow() printRowWindow(-3,windowValue1) printRowWindow(30,windowValue2) end local function printCellTable(x,y,w,text,format) lcd.drawText(x+(w+1-lcd.getTextWidth(format,text))/2,y,text,format) --lcd.drawRectangle(x,y,w+1) return x+w end local function printRowTable(row,text,format) local x,y=-1,-1+row*18 x=printCellTable(x,y,80,text[1],format) x=printCellTable(x,y,80,text[2],format) x=printCellTable(x,y,80,text[3],format) x=printCellTable(x,y,80,text[4],format) lcd.drawLine(0,y,320,y) end local function printTable() printRowTable(0,windowValues,FONT_BOLD) for i=1,#duration do local text={"-","-","-","-"} if duration[i]>=0 then text[1]=string.format("%d s",duration[i]) end if sinkRate[i]>=0 then text[2]=string.format("%.2f m/s",sinkRate[i]) end if airspeed[i]>=0 then text[3]=string.format("%.1f m/s",airspeed[i]) end if glideRatio[i]>=0 then text[4]=string.format("%.1f:1",glideRatio[i]) end printRowTable(i,text,FONT_NORMAL) end end local function initSensors() local telSensors=system.getSensors() sensorLabels={"..."} sensorIDs={nil} sensorParams={nil} for i,sensor in ipairs(telSensors) do table.insert(sensorIDs,sensor.ID) table.insert(sensorParams,sensor.param) if sensor.param==0 then table.insert(sensorLabels,sensor.label) else table.insert(sensorLabels,sensor.label.." ["..sensor.unit.."]") end end end local function createForm(formID) initSensors() 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="Window Value 1",width=120}) form.addSelectbox(windowValues,windowValue1,false,function(index) windowValue1=index system.pSave("windowValue1",windowValue1) end,{width=192}) form.addRow(2) form.addLabel({label="Window Value 2",width=120}) form.addSelectbox(windowValues,windowValue2,false,function(index) windowValue2=index system.pSave("windowValue2",windowValue2) end,{width=192}) form.addLabel() form.addLabel({label="Sensors",font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Altitude [m]",width=134}) form.addSelectbox(sensorLabels,1,true,function(index) altitudeSensorID=sensorIDs[index] system.pSave("altitudeSensorID",altitudeSensorID) altitudeSensorParam=sensorParams[index] system.pSave("altitudeSensorParam",altitudeSensorParam) end,{width=178}) form.addRow(3) form.addLabel({label="Distance",width=69}) form.addSelectbox(distanceUnits,distanceUnit,false,function(index) distanceUnit=index system.pSave("distanceUnit",distanceUnit) end,{width=65}) form.addSelectbox(sensorLabels,1,true,function(index) distanceSensorID=sensorIDs[index] system.pSave("distanceSensorID",distanceSensorID) distanceSensorParam=sensorParams[index] system.pSave("distanceSensorParam",distanceSensorParam) end,{width=178}) end local function init(code) startStopSwitch=system.pLoad("startStopSwitch") windowValue1=system.pLoad("windowValue1") if not windowValue1 then windowValue1=1 end windowValue2=system.pLoad("windowValue2") if not windowValue2 then windowValue2=2 end altitudeSensorID=system.pLoad("altitudeSensorID",1) altitudeSensorParam=system.pLoad("altitudeSensorParam",1) distanceSensorID=system.pLoad("distanceSensorID",1) distanceSensorParam=system.pLoad("distanceSensorParam",1) distanceUnit=system.pLoad("distanceUnit") if not distanceUnit then distanceUnit=1 end initSensors() system.registerTelemetry(1,"Glide Performance Window",2,printWindow) 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 telSensors=system.getSensors() local altitudeSensor=system.getSensorByID(altitudeSensorID,altitudeSensorParam) local distanceSensor=system.getSensorByID(distanceSensorID,distanceSensorParam) if startStopSwitchStateOld<=0 then timeStart,altitudeStart,distanceStart=timeNow,nil,nil 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.0",name="Glide Performance"}