first ai assist
This commit is contained in:
200
main.lua
200
main.lua
@@ -60,70 +60,14 @@ Config.Monitors.InstrumentPanelMonitor = nil
|
|||||||
Config.Monitors.AutopilotControlMonitor = nil
|
Config.Monitors.AutopilotControlMonitor = nil
|
||||||
|
|
||||||
-- Autopilot
|
-- Autopilot
|
||||||
Config.Autopilot.AutopilotEngaged = false -- Is auto-yaw enabled?
|
Config.Autopilot = {
|
||||||
Config.Autopilot.AutoForeAft = false -- Is auto-thrust enabled?
|
AutopilotEngaged = false,
|
||||||
Config.Autopilot.AutopilotDesiredSpeed = 0 -- Signed thrust value to compare against VelocityVectors["x"]
|
AutoForeAft = false,
|
||||||
Config.Autopilot.AutopilotDesiredHeading = 0 -- If NavTableHasTarget == true this gets ignored in favor of TargetRelativeAngle
|
AutopilotDesiredSpeed = nil,
|
||||||
|
AutopilotDesiredHeading = nil
|
||||||
-- 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Nav Table Values
|
function ThrottleInit()
|
||||||
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 = {}
|
|
||||||
local sides = {
|
local sides = {
|
||||||
"top",
|
"top",
|
||||||
"bottom",
|
"bottom",
|
||||||
@@ -143,42 +87,55 @@ local function getRelayStates()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return states
|
local function getRelayStates()
|
||||||
end
|
local states = {}
|
||||||
|
local sides = {
|
||||||
|
"top",
|
||||||
|
"bottom",
|
||||||
|
"left",
|
||||||
|
"right",
|
||||||
|
"front",
|
||||||
|
"back"
|
||||||
|
}
|
||||||
|
|
||||||
local function identifyRelay(label)
|
local names = peripheral.getNames()
|
||||||
print("\nIdentifying "..label.." throttle")
|
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")
|
print("\nPlease change the input signal for the throttle")
|
||||||
|
|
||||||
local initialStates = getRelayStates
|
local initialStates = getRelayStates()
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
os.pullEvent("redstone_relay")
|
os.pullEvent("redstone_relay")
|
||||||
|
|
||||||
local currentStates = getRelayStates
|
local currentStates = getRelayStates()
|
||||||
|
|
||||||
for pname, currentState in pairs(currentStates) do
|
for pname, currentState in pairs(currentStates) do
|
||||||
local initialState = initialStates[pname]
|
local initialState = initialStates[pname]
|
||||||
for k, v in pairs(currentState) do
|
for k, v in pairs(currentState) do
|
||||||
if initialState[k] ~= v then
|
if initialState[k] ~= v then
|
||||||
return {
|
Throttles.ForeThrottle.name = pname
|
||||||
name = pname,
|
Throttles.ForeThrottle.side = k
|
||||||
side = k
|
return
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function ThrottleInit()
|
|
||||||
Throttles.ForeThrottle = identifyRelay("fore")
|
|
||||||
Throttles.DownThrottle = identifyRelay("down")
|
|
||||||
Throttles.AftThrottle = identifyRelay("aft")
|
|
||||||
end
|
|
||||||
|
|
||||||
function PropellerInit()
|
function PropellerInit()
|
||||||
|
|
||||||
local transmissions = {}
|
local transmissions = {}
|
||||||
local propellers = {}
|
local propellers = {}
|
||||||
|
|
||||||
@@ -215,11 +172,15 @@ function PropellerInit()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
if Thrusters[peripheral.getName(pv)] ~= nil then
|
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)].thruster = pv
|
||||||
Thrusters[peripheral.getName(pv)].transmission = tv
|
Thrusters[peripheral.getName(pv)].transmission = tv
|
||||||
if Thrusters[peripheral.getName(pv)].affectVectors == nil then Thrusters[peripheral.getName(pv)].affectVectors = nil end
|
if Thrusters[peripheral.getName(pv)].affectVectors == nil then
|
||||||
Thrusters[peripheral.getName(pv)].power = (tv.getSingnal())/15
|
Thrusters[peripheral.getName(pv)].affectVectors = {}
|
||||||
|
end
|
||||||
|
Thrusters[peripheral.getName(pv)].power = (tv.getSignal())/15
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -227,7 +188,6 @@ function PropellerInit()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function ThrusterInit()
|
function ThrusterInit()
|
||||||
|
|
||||||
local thrusters = {}
|
local thrusters = {}
|
||||||
|
|
||||||
for _, v in ipairs(ThrusterTypes) do
|
for _, v in ipairs(ThrusterTypes) do
|
||||||
@@ -257,10 +217,14 @@ function ThrusterInit()
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
if Thrusters[peripheral.getName(tv)] ~= nil then
|
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)].thruster = tv
|
||||||
Thrusters[peripheral.getName(tv)].transmission = nil
|
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()
|
Thrusters[peripheral.getName(tv)].power = tv.getPower()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -272,7 +236,6 @@ function Update()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function checkIfThrusterIsIndexed()
|
local function checkIfThrusterIsIndexed()
|
||||||
|
|
||||||
local thrusters = {}
|
local thrusters = {}
|
||||||
local unindexedThrusters = {}
|
local unindexedThrusters = {}
|
||||||
|
|
||||||
@@ -335,11 +298,11 @@ local function partiallyUpdateThrusters(thrusterList)
|
|||||||
local thisThruster = {
|
local thisThruster = {
|
||||||
thruster = tv,
|
thruster = tv,
|
||||||
transmission = nil,
|
transmission = nil,
|
||||||
type = "rotator"
|
type = "rotator",
|
||||||
affectVectors = {
|
affectVectors = {
|
||||||
yaw = nil,
|
yaw = nil,
|
||||||
roll = nil,
|
roll = nil,
|
||||||
pitch = nil
|
pitch = nil,
|
||||||
lateral = nil
|
lateral = nil
|
||||||
},
|
},
|
||||||
power = tv.getPower()
|
power = tv.getPower()
|
||||||
@@ -361,13 +324,19 @@ local function partiallyUpdateThrusters(thrusterList)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function UpdateStabilization()
|
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
|
end
|
||||||
|
|
||||||
function PollThrottle()
|
function PollThrottle()
|
||||||
for _, v in ipairs(Throttles) do
|
for _, v in pairs(Throttles) do
|
||||||
|
if v.name and v.side then
|
||||||
v.input = peripheral.call(v.name, "getAnalogInput("..v.side..")")
|
v.input = peripheral.call(v.name, "getAnalogInput("..v.side..")")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function PollSensors()
|
function PollSensors()
|
||||||
@@ -393,15 +362,18 @@ function PollAltitude()
|
|||||||
local altSensor = peripheral.find("altitude_sensor") -- enforce one alt sensor
|
local altSensor = peripheral.find("altitude_sensor") -- enforce one alt sensor
|
||||||
|
|
||||||
-- Altitude Sensor
|
-- Altitude Sensor
|
||||||
|
if altSensor then
|
||||||
Altitude = altSensor.getHeight()
|
Altitude = altSensor.getHeight()
|
||||||
AirPressure = altSensor.getAirPressure()
|
AirPressure = altSensor.getAirPressure()
|
||||||
VerticalSpeed = altSensor.getVerticalSpeed()
|
VerticalSpeed = altSensor.getVerticalSpeed()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function PollNavTable()
|
function PollNavTable()
|
||||||
local navTable = peripheral.find("navigation_table") -- enforce one nav table
|
local navTable = peripheral.find("navigation_table") -- enforce one nav table
|
||||||
|
|
||||||
-- Navigation Table
|
-- Navigation Table
|
||||||
|
if navTable then
|
||||||
Heading = navTable.getHeading()
|
Heading = navTable.getHeading()
|
||||||
NavTableHasTarget = navTable.hasTarget()
|
NavTableHasTarget = navTable.hasTarget()
|
||||||
if NavTableHasTarget then
|
if NavTableHasTarget then
|
||||||
@@ -416,15 +388,29 @@ function PollNavTable()
|
|||||||
TargetVerticalOffset = 0
|
TargetVerticalOffset = 0
|
||||||
TargetRelativeAngle = 0
|
TargetRelativeAngle = 0
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
Heading = 0
|
||||||
|
NavTableHasTarget = false
|
||||||
|
BearingToTarget = 0
|
||||||
|
TargetClosureRate = 0
|
||||||
|
TargetVerticalOffset = 0
|
||||||
|
TargetRelativeAngle = 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function PollGimbal()
|
function PollGimbal()
|
||||||
local gimbalSensor = peripheral.find("gimbal_sensor") -- enforce one gimbal sensor
|
local gimbalSensor = peripheral.find("gimbal_sensor") -- enforce one gimbal sensor
|
||||||
|
|
||||||
-- Gimbal Sensor
|
-- Gimbal Sensor
|
||||||
|
if gimbalSensor then
|
||||||
Angles = gimbalSensor.getAngles()
|
Angles = gimbalSensor.getAngles()
|
||||||
AngularRates = gimbalSensor.getAngularRates()
|
AngularRates = gimbalSensor.getAngularRates()
|
||||||
LinearAcceleration = gimbalSensor.getLinearAcceleration()
|
LinearAcceleration = gimbalSensor.getLinearAcceleration()
|
||||||
|
else
|
||||||
|
Angles = {0, 0}
|
||||||
|
AngularRates = {0, 0, 0}
|
||||||
|
LinearAcceleration = {0, 0, 0}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Sigmoid(x)
|
function Sigmoid(x)
|
||||||
@@ -480,12 +466,12 @@ function UpdateGlobalThrust()
|
|||||||
-- lateral thrust
|
-- lateral thrust
|
||||||
local desiredLateralThrustVectors = {}
|
local desiredLateralThrustVectors = {}
|
||||||
|
|
||||||
for f, v in ipairs(VelocityVectors) do
|
for f, v in pairs(VelocityVectors) do
|
||||||
desiredLateralThrustVectors[f] = CustomSigmoid(v)
|
desiredLateralThrustVectors[f] = CustomSigmoid(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
local thrustDirections = {}
|
local thrustDirections = {}
|
||||||
for f, v in desiredLateralThrustVectors do
|
for f, v in pairs(desiredLateralThrustVectors) do
|
||||||
if f == "x" then
|
if f == "x" then
|
||||||
if v > 0 then table.insert(thrustDirections, { port = v }) else table.insert(thrustDirections, { star = math.abs(v) }) end
|
if v > 0 then table.insert(thrustDirections, { port = v }) else table.insert(thrustDirections, { star = math.abs(v) }) end
|
||||||
end
|
end
|
||||||
@@ -508,16 +494,18 @@ function UpdateGlobalThrust()
|
|||||||
-- angular thrust
|
-- angular thrust
|
||||||
local desiredAngluarThrustVectors = {}
|
local desiredAngluarThrustVectors = {}
|
||||||
|
|
||||||
for f, v in ipairs(AngularRates) do
|
for f, v in pairs(AngularRates) do
|
||||||
desiredAngluarThrustVectors[f] = CustomSigmoid(v)
|
desiredAngluarThrustVectors[f] = CustomSigmoid(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Need to complete this function implementation based on docs.md requirements
|
||||||
end
|
end
|
||||||
|
|
||||||
function Init()
|
function Init()
|
||||||
ThrottleInit()
|
ThrottleInit()
|
||||||
Thrusters = {}
|
Thrusters = {}
|
||||||
|
|
||||||
local configFile = io.open(ConfigPath, "r")
|
local configFile = io.open(Config.ConfigPath, "r")
|
||||||
if configFile then
|
if configFile then
|
||||||
local configContent = configFile:read("*all")
|
local configContent = configFile:read("*all")
|
||||||
local configChunk, err = load(configContent)
|
local configChunk, err = load(configContent)
|
||||||
@@ -526,10 +514,10 @@ function Init()
|
|||||||
else
|
else
|
||||||
print("Error loading config string: "..err)
|
print("Error loading config string: "..err)
|
||||||
end
|
end
|
||||||
|
configFile:close()
|
||||||
else
|
else
|
||||||
print("Could not open the config file")
|
print("Could not open the config file")
|
||||||
end
|
end
|
||||||
configFile.close()
|
|
||||||
|
|
||||||
if Config.thrusterConfigPath ~= nil then
|
if Config.thrusterConfigPath ~= nil then
|
||||||
local thrusterConfigFile = io.open(Config.thrusterConfigPath, "r")
|
local thrusterConfigFile = io.open(Config.thrusterConfigPath, "r")
|
||||||
@@ -541,20 +529,21 @@ function Init()
|
|||||||
else
|
else
|
||||||
print("Error loading thruster config string: "..err)
|
print("Error loading thruster config string: "..err)
|
||||||
end
|
end
|
||||||
|
thrusterConfigFile:close()
|
||||||
else
|
else
|
||||||
print("Could not open the thruster config file")
|
print("Could not open the thruster config file")
|
||||||
end
|
end
|
||||||
thrusterConfigFile.close()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if Config.ThrusterConfigPath ~= nil then
|
-- Initialize monitors or return early if they're not configured
|
||||||
local unindexedThrusters = checkIfThrusterIsIndexed()
|
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 unindexedThrusters ~= nil then
|
-- If we're here, the config loaded successfully and the initialization is done.
|
||||||
partiallyUpdateThrusters(unindexedThrusters)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Main()
|
function Main()
|
||||||
@@ -568,5 +557,12 @@ function Main()
|
|||||||
|
|
||||||
PollSensors()
|
PollSensors()
|
||||||
UpdateGlobalThrust()
|
UpdateGlobalThrust()
|
||||||
|
UpdateStabilization()
|
||||||
|
|
||||||
|
-- Add a small delay to avoid overloading the system
|
||||||
|
os.sleep(0.1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Run the main function when the script starts
|
||||||
|
Main()
|
||||||
Reference in New Issue
Block a user