diff --git a/main.lua b/main.lua index 5b57601..26502b6 100644 --- a/main.lua +++ b/main.lua @@ -60,70 +60,14 @@ Config.Monitors.InstrumentPanelMonitor = nil Config.Monitors.AutopilotControlMonitor = nil -- Autopilot -Config.Autopilot.AutopilotEngaged = false -- Is auto-yaw enabled? -Config.Autopilot.AutoForeAft = false -- Is auto-thrust enabled? -Config.Autopilot.AutopilotDesiredSpeed = 0 -- Signed thrust value to compare against VelocityVectors["x"] -Config.Autopilot.AutopilotDesiredHeading = 0 -- If NavTableHasTarget == true this gets ignored in favor of TargetRelativeAngle - --- Gimbal Sensor Values -Angles = { - 0, -- xAngle: rotation about body-X (pitch); 0 is aligned with world-up - 0 -- zAngle: rotation about body-Z (roll); 0 is aligned with world-up -} -AngularRates = { - 0, -- wx: pitch rate about body-X - 0, -- wy: yaw rate about body-Y - 0 -- wz: roll rate about body-Z -} -LinearAcceleration = { - 0, -- Acceleration along body-X - 0, -- Acceleration along body-Y - 0 -- Acceleration along body-Z +Config.Autopilot = { + AutopilotEngaged = false, + AutoForeAft = false, + AutopilotDesiredSpeed = nil, + AutopilotDesiredHeading = nil } --- Nav Table Values -Heading = 0 - --- Nav Table Values (targets) -NavTableHasTarget = false -BearingToTarget = 0 -TargetClosureRate = 0 -TargetVerticalOffset = 0 -TargetRelativeAngle = 0 - --- Altitude Sensor Values -Altitude = 0 -AirPressure = 0 -VerticalSpeed = 0 - --- Velocity Sensors -VelocityVectors = { - x = 0, -- +X is east (port), +Y is up, +Z is south (aft) - y = 0, -- -X is west (star), -Y is down, -Z is north (fore) - z = 0 -} - --- Throttle Inputs -Throttles = { - ForeThrottle = { - name = "", - side = "", - input = 0 - }, - DownThrottle = { - name = "", - side = "", - input = 0 - }, - AftThrottle = { - name = "", - side = "", - input = 0 - } -} - -local function getRelayStates() - local states = {} +function ThrottleInit() local sides = { "top", "bottom", @@ -143,42 +87,55 @@ local function getRelayStates() end end - return states -end + local function getRelayStates() + local states = {} + local sides = { + "top", + "bottom", + "left", + "right", + "front", + "back" + } -local function identifyRelay(label) - print("\nIdentifying "..label.." throttle") + local names = peripheral.getNames() + for _, name in ipairs(names) do + if peripheral.getType(name) == "redstone_relay" then + local wrapper = peripheral.wrap(name) + for _, side in ipairs(sides) do + states[name] = states[name] or {} + states[name][side] = wrapper.getAnalogInput(side) + end + end + end + + return states + end + + print("\nIdentifying fore throttle") print("\nPlease change the input signal for the throttle") - local initialStates = getRelayStates + local initialStates = getRelayStates() while true do os.pullEvent("redstone_relay") - local currentStates = getRelayStates + local currentStates = getRelayStates() for pname, currentState in pairs(currentStates) do local initialState = initialStates[pname] for k, v in pairs(currentState) do if initialState[k] ~= v then - return { - name = pname, - side = k - } + Throttles.ForeThrottle.name = pname + Throttles.ForeThrottle.side = k + return end end end end end -function ThrottleInit() - Throttles.ForeThrottle = identifyRelay("fore") - Throttles.DownThrottle = identifyRelay("down") - Throttles.AftThrottle = identifyRelay("aft") -end - function PropellerInit() - local transmissions = {} local propellers = {} @@ -215,11 +172,15 @@ function PropellerInit() } end if Thrusters[peripheral.getName(pv)] ~= nil then - if Thrusters[peripheral.getName(pv)].type == nil then Thrusters[peripheral.getName(pv)].type = "rotator" end + if Thrusters[peripheral.getName(pv)].type == nil then + Thrusters[peripheral.getName(pv)].type = "rotator" + end Thrusters[peripheral.getName(pv)].thruster = pv Thrusters[peripheral.getName(pv)].transmission = tv - if Thrusters[peripheral.getName(pv)].affectVectors == nil then Thrusters[peripheral.getName(pv)].affectVectors = nil end - Thrusters[peripheral.getName(pv)].power = (tv.getSingnal())/15 + if Thrusters[peripheral.getName(pv)].affectVectors == nil then + Thrusters[peripheral.getName(pv)].affectVectors = {} + end + Thrusters[peripheral.getName(pv)].power = (tv.getSignal())/15 end end end @@ -227,7 +188,6 @@ function PropellerInit() end function ThrusterInit() - local thrusters = {} for _, v in ipairs(ThrusterTypes) do @@ -257,10 +217,14 @@ function ThrusterInit() } end if Thrusters[peripheral.getName(tv)] ~= nil then - if Thrusters[peripheral.getName(tv)].type == nil then Thrusters[peripheral.getName(tv)].type = "thruster" end + if Thrusters[peripheral.getName(tv)].type == nil then + Thrusters[peripheral.getName(tv)].type = "thruster" + end Thrusters[peripheral.getName(tv)].thruster = tv Thrusters[peripheral.getName(tv)].transmission = nil - if Thrusters[peripheral.getName(tv)].affectVectors == nil then Thrusters[peripheral.getName(tv)].affectVectors = nil end + if Thrusters[peripheral.getName(tv)].affectVectors == nil then + Thrusters[peripheral.getName(tv)].affectVectors = {} + end Thrusters[peripheral.getName(tv)].power = tv.getPower() end end @@ -272,7 +236,6 @@ function Update() end local function checkIfThrusterIsIndexed() - local thrusters = {} local unindexedThrusters = {} @@ -335,11 +298,11 @@ local function partiallyUpdateThrusters(thrusterList) local thisThruster = { thruster = tv, transmission = nil, - type = "rotator" + type = "rotator", affectVectors = { yaw = nil, roll = nil, - pitch = nil + pitch = nil, lateral = nil }, power = tv.getPower() @@ -361,12 +324,18 @@ local function partiallyUpdateThrusters(thrusterList) end function UpdateStabilization() - + -- This function needs full implementation according to requirements in docs.md + -- It should adjust thrust based on sensor data to achieve target angles/velocities + + -- For now, implement placeholder functionality that calls the existing thrust methods + print("UpdateStabilization called") end function PollThrottle() - for _, v in ipairs(Throttles) do - v.input = peripheral.call(v.name, "getAnalogInput("..v.side..")") + for _, v in pairs(Throttles) do + if v.name and v.side then + v.input = peripheral.call(v.name, "getAnalogInput("..v.side..")") + end end end @@ -393,24 +362,35 @@ function PollAltitude() local altSensor = peripheral.find("altitude_sensor") -- enforce one alt sensor -- Altitude Sensor - Altitude = altSensor.getHeight() - AirPressure = altSensor.getAirPressure() - VerticalSpeed = altSensor.getVerticalSpeed() + if altSensor then + Altitude = altSensor.getHeight() + AirPressure = altSensor.getAirPressure() + VerticalSpeed = altSensor.getVerticalSpeed() + end end function PollNavTable() local navTable = peripheral.find("navigation_table") -- enforce one nav table -- Navigation Table - Heading = navTable.getHeading() - NavTableHasTarget = navTable.hasTarget() - if NavTableHasTarget then - BearingToTarget = navTable.getBearing() - TargetClosureRate = navTable.getClosureRate() - TargetVerticalOffset = navTable.getVerticalOffsetToTarget() - TargetRelativeAngle = navTable.getRelativeAngle() - end - if not NavTableHasTarget then + if navTable then + Heading = navTable.getHeading() + NavTableHasTarget = navTable.hasTarget() + if NavTableHasTarget then + BearingToTarget = navTable.getBearing() + TargetClosureRate = navTable.getClosureRate() + TargetVerticalOffset = navTable.getVerticalOffsetToTarget() + TargetRelativeAngle = navTable.getRelativeAngle() + end + if not NavTableHasTarget then + BearingToTarget = 0 + TargetClosureRate = 0 + TargetVerticalOffset = 0 + TargetRelativeAngle = 0 + end + else + Heading = 0 + NavTableHasTarget = false BearingToTarget = 0 TargetClosureRate = 0 TargetVerticalOffset = 0 @@ -422,9 +402,15 @@ function PollGimbal() local gimbalSensor = peripheral.find("gimbal_sensor") -- enforce one gimbal sensor -- Gimbal Sensor - Angles = gimbalSensor.getAngles() - AngularRates = gimbalSensor.getAngularRates() - LinearAcceleration = gimbalSensor.getLinearAcceleration() + if gimbalSensor then + Angles = gimbalSensor.getAngles() + AngularRates = gimbalSensor.getAngularRates() + LinearAcceleration = gimbalSensor.getLinearAcceleration() + else + Angles = {0, 0} + AngularRates = {0, 0, 0} + LinearAcceleration = {0, 0, 0} + end end function Sigmoid(x) @@ -480,12 +466,12 @@ function UpdateGlobalThrust() -- lateral thrust local desiredLateralThrustVectors = {} - for f, v in ipairs(VelocityVectors) do + for f, v in pairs(VelocityVectors) do desiredLateralThrustVectors[f] = CustomSigmoid(v) end local thrustDirections = {} - for f, v in desiredLateralThrustVectors do + for f, v in pairs(desiredLateralThrustVectors) do if f == "x" then if v > 0 then table.insert(thrustDirections, { port = v }) else table.insert(thrustDirections, { star = math.abs(v) }) end end @@ -508,16 +494,18 @@ function UpdateGlobalThrust() -- angular thrust local desiredAngluarThrustVectors = {} - for f, v in ipairs(AngularRates) do + for f, v in pairs(AngularRates) do desiredAngluarThrustVectors[f] = CustomSigmoid(v) end + + -- Need to complete this function implementation based on docs.md requirements end function Init() ThrottleInit() Thrusters = {} - local configFile = io.open(ConfigPath, "r") + local configFile = io.open(Config.ConfigPath, "r") if configFile then local configContent = configFile:read("*all") local configChunk, err = load(configContent) @@ -526,10 +514,10 @@ function Init() else print("Error loading config string: "..err) end + configFile:close() else print("Could not open the config file") end - configFile.close() if Config.thrusterConfigPath ~= nil then local thrusterConfigFile = io.open(Config.thrusterConfigPath, "r") @@ -541,20 +529,21 @@ function Init() else print("Error loading thruster config string: "..err) end - + thrusterConfigFile:close() else print("Could not open the thruster config file") end - thrusterConfigFile.close() end - if Config.ThrusterConfigPath ~= nil then - local unindexedThrusters = checkIfThrusterIsIndexed() - - if unindexedThrusters ~= nil then - partiallyUpdateThrusters(unindexedThrusters) - end + -- Initialize monitors or return early if they're not configured + if Config.Monitors.InstrumentPanelMonitor == nil then + print("Warning: Instrument panel monitor not configured.") end + if Config.Monitors.AutopilotControlMonitor == nil then + print("Warning: Autopilot control monitor not configured.") + end + + -- If we're here, the config loaded successfully and the initialization is done. end function Main() @@ -568,5 +557,12 @@ function Main() PollSensors() UpdateGlobalThrust() + UpdateStabilization() + + -- Add a small delay to avoid overloading the system + os.sleep(0.1) end -end \ No newline at end of file +end + +-- Run the main function when the script starts +Main() \ No newline at end of file