diff --git a/main.lua b/main.lua index 4a000fb..134ccbc 100644 --- a/main.lua +++ b/main.lua @@ -33,6 +33,8 @@ Thrusters = { } } -- Table of thruster tables +SensorData = {} + ThrusterTypes = { "thruster", "solid_fuel_thruster", @@ -455,11 +457,21 @@ function PollVelocity() -- Velocity Sensors VelocityVectors = {} - for vsindex, velsensor in pairs(velSensors) do - local vsAxis = velsensor.getAxis() - local vsVelocity = velsensor.getVelocity() - VelocityVectors[vsAxis] = vsVelocity + local next = next + if next(velSensors) == nil then + + SensorData.Velocity = {} + + for vsindex, velsensor in pairs(velSensors) do + + local vsAxis = velsensor.getAxis() + local vsVelocity = velsensor.getVelocity() + VelocityVectors[vsAxis] = vsVelocity + + SensorData.Velocity.Raw = VelocityVectors + end end + end function PollAltitude() @@ -467,19 +479,32 @@ function PollAltitude() -- Altitude Sensor if altSensor then + + SensorData.Altitude = {} + Altitude = altSensor.getHeight() AirPressure = altSensor.getAirPressure() VerticalSpeed = altSensor.getVerticalSpeed() + + SensorData.Altitude.Altitude = Altitude + SensorData.Altitude.AirPressure = AirPressure + SensorData.Altitude.VerticalSpeed = VerticalSpeed end end function PollNavTable() local navTable = peripheral.find("navigation_table") -- enforce one nav table - + -- Navigation Table if navTable then + + SensorData.NavTable = {} + Heading = navTable.getHeading() NavTableHasTarget = navTable.hasTarget() + + SensorData.NavTable.Heading = Heading + SensorData.NavTable.HasTarget = NavTableHasTarget if NavTableHasTarget then BearingToTarget = navTable.getBearing() TargetClosureRate = navTable.getClosureRate() @@ -492,13 +517,11 @@ function PollNavTable() TargetVerticalOffset = 0 TargetRelativeAngle = 0 end - else - Heading = 0 - NavTableHasTarget = false - BearingToTarget = 0 - TargetClosureRate = 0 - TargetVerticalOffset = 0 - TargetRelativeAngle = 0 + + SensorData.NavTable.TargetBearing = BearingToTarget + SensorData.NavTable.TargetClosureRate = TargetClosureRate + SensorData.NavTable.TargetVerticalOffset = TargetVerticalOffset + SensorData.NavTable.TargetRelativeAngle = TargetRelativeAngle end end @@ -507,13 +530,16 @@ function PollGimbal() -- Gimbal Sensor if gimbalSensor then + + SensorData.Gimbal = {} + Angles = gimbalSensor.getAngles() AngularRates = gimbalSensor.getAngularRates() LinearAcceleration = gimbalSensor.getLinearAcceleration() - else - Angles = {0, 0} - AngularRates = {0, 0, 0} - LinearAcceleration = {0, 0, 0} + + SensorData.Gimbal.Angles = Angles + SensorData.Gimbal.AngularRates = AngularRates + SensorData.Gimbal.LinearAcceleration = LinearAcceleration end end @@ -667,35 +693,40 @@ function displayInstrumentPanel(monitor, sensorData) m.write("Caero Attitude Control") m.setCursorPos(1, 2) m.write("----------------------") - - if sensorData then - m.setCursorPos(1, 3) - m.write("Pitch: " .. string.format("%.2f", sensorData.gimbal.pitch or 0)) - m.setCursorPos(1, 4) - m.write("Roll: " .. string.format("%.2f", sensorData.gimbal.roll or 0)) - m.setCursorPos(1, 5) - m.write("Yaw: " .. string.format("%.2f", sensorData.navTable.heading or 0)) - m.setCursorPos(1, 6) - m.write("Velocity: " .. string.format("%.2f", sensorData.velocity or 0)) - m.setCursorPos(1, 7) - m.write("Altitude: " .. string.format("%.2f", sensorData.altitude or 0)) - else - m.setCursorPos(1, 3) - m.write("Pitch: 0.00") - m.setCursorPos(1, 4) - m.write("Roll: 0.00") - m.setCursorPos(1, 5) - m.write("Yaw: 0.00") - m.setCursorPos(1, 6) - m.write("Velocity: 0.00") - m.setCursorPos(1, 7) - m.write("Altitude: 0.00") + + local next = next + if next(sensorData) ~= nil then + if sensorData then + m.setCursorPos(1, 3) + m.write("Pitch: " .. string.format("%.2f", SensorData.Gimbal.Angles.xAngle or 0)) + m.setCursorPos(1, 4) + m.write("Roll: " .. string.format("%.2f", SensorData.Gimbal.Angles.zAngle or 0)) + m.setCursorPos(1, 5) + m.write("Yaw: " .. string.format("%.2f", SensorData.NavTable.Heading or 0)) + m.setCursorPos(1, 6) + m.write("Velocity: " .. string.format("%.2f", sensorData.velocity or 0)) + m.setCursorPos(1, 7) + m.write("Altitude: " .. string.format("%.2f", SensorData.Altitude.Altitude or 0)) + else + m.setCursorPos(1, 3) + m.write("Pitch: 0.00") + m.setCursorPos(1, 4) + m.write("Roll: 0.00") + m.setCursorPos(1, 5) + m.write("Yaw: 0.00") + m.setCursorPos(1, 6) + m.write("Velocity: 0.00") + m.setCursorPos(1, 7) + m.write("Altitude: 0.00") + end + + -- Display autopilot status + local autopilotStatus = Config.Autopilot.AutopilotEngaged and "ON" or "OFF" + m.setCursorPos(1, 9) + m.write("Autopilot: " .. autopilotStatus) end - -- Display autopilot status - local autopilotStatus = Config.Autopilot.AutopilotEngaged and "ON" or "OFF" - m.setCursorPos(1, 9) - m.write("Autopilot: " .. autopilotStatus) + end function displayAutopilotControls(monitor, sensorData) @@ -763,15 +794,16 @@ function Main() Init() local sentinel = true + print("Mainloop starting. Press 'q' to stop the loop and save configuration changes.") while sentinel do -- Update monitor displays if Config.Monitors.InstrumentPanelMonitor then - displayInstrumentPanel(Config.Monitors.InstrumentPanelMonitor) + displayInstrumentPanel(Config.Monitors.InstrumentPanelMonitor, SensorData) end if Config.Monitors.AutopilotControlMonitor then - displayAutopilotControls(Config.Monitors.AutopilotControlMonitor) + displayAutopilotControls(Config.Monitors.AutopilotControlMonitor, SensorData) end local unindexedThrusters = checkIfThrusterIsIndexed()