collectgarbage() local startStopSwitch local startStopSwitchState=0 local altitudeSensorIndex local distanceSensorIndex local sensorLabels local indexToSensor local duration,sinkRate,airspeed,glideRatio={},{},{},{} local timeStart,altitudeStart,distanceStart local function printCell(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 printRow(row,text,format) local x,y=-1,-1+row*18 x=printCell(x,y,80,text[1],format) x=printCell(x,y,80,text[2],format) x=printCell(x,y,80,text[3],format) x=printCell(x,y,80,text[4],format) lcd.drawLine(0,y,320,y) end local function printTable() printRow(0,{"Duration","Sink Rate","Airspeed","Glide Ratio"},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",glideRatio[i]) end printRow(i,text,FONT_NORMAL) end end local function initSensors() local telSensors=system.getSensors() sensorLabels={"..."} indexToSensor={0} for k,v in ipairs(telSensors) do if v.param>0 then table.insert(sensorLabels,v.sensorName..": "..v.label.." ["..v.unit.."]") table.insert(indexToSensor,k) 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.addLabel() form.addLabel({label="Sensors",font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Altitude",width=100}) form.addSelectbox(sensorLabels,altitudeSensorIndex,true,function(index) altitudeSensorIndex=index system.pSave("altitudeSensorIndex",altitudeSensorIndex) end,{width=212}) form.addRow(2) form.addLabel({label="Distance",width=100}) form.addSelectbox(sensorLabels,distanceSensorIndex,true,function(index) distanceSensorIndex=index system.pSave("distanceSensorIndex",distanceSensorIndex) end,{width=212}) end local function init(code) startStopSwitch=system.pLoad("startStopSwitch") altitudeSensorIndex=system.pLoad("altitudeSensorIndex",1) distanceSensorIndex=system.pLoad("distanceSensorIndex",1) initSensors() system.registerTelemetry(1,"Glider Performance",4,printTable) system.registerForm(1,MENU_APPS,"Glider 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 telSensors=system.getSensors() local timeNow=system.getTimeCounter() local altitudeSensor=telSensors[indexToSensor[altitudeSensorIndex]] local distanceSensor=telSensors[indexToSensor[distanceSensorIndex]] 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 and altitudeStart then sinkRate[1]=(altitudeStart-altitudeSensor.value)/duration[1] end if distanceSensor and distanceStart then airspeed[1]=(distanceSensor.value-distanceStart)*1000/duration[1] end if altitudeSensor and altitudeStart and distanceSensor and distanceStart then glideRatio[1]=(distanceSensor.value-distanceStart)*1000/(altitudeStart-altitudeSensor.value) end end end collectgarbage() end collectgarbage() return {init=init,loop=loop,author="LD",version="1.0",name="Glider Performance"}