Compare commits

...

8 Commits

3 changed files with 68 additions and 45 deletions

View File

@@ -307,6 +307,7 @@ Thrusters = {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "1", -- if you're running a standard x configuration, what prop is this equivalent to?
type = "rotator",
name = "gyroscopic_propeller_bearing_0",
thruster = nil,
@@ -334,6 +335,7 @@ Thrusters = {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "2", -- if you're running a standard x configuration, what prop is this equivalent to?
type = "rotator",
name = "gyroscopic_propeller_bearing_1",
thruster = nil,
@@ -361,6 +363,7 @@ Thrusters = {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "4", -- if you're running a standard x configuration, what prop is this equivalent to?
type = "rotator",
name = "gyroscopic_propeller_bearing_2",
thruster = nil,
@@ -388,6 +391,7 @@ Thrusters = {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "3", -- if you're running a standard x configuration, what prop is this equivalent to?
type = "rotator",
name = "gyroscopic_propeller_bearing_3",
thruster = nil,

105
main.lua
View File

@@ -410,11 +410,11 @@ local function populateThrusterValues()
thruster.power = thruster.thruster.getPower()
elseif thruster.type == "rotator" then
if thruster.transmission ~= nil then
local transmissionType = peripheral.getType(thruster.transmission)
local transmissionType = peripheral.getType(peripheral.wrap(thruster.transmission))
if transmissionType == "analog_transmission" then
thruster.power = (thruster.transmission.getSignal())/15
thruster.power = (peripheral.wrap(thruster.transmission).getSignal())/15
elseif transmissionType == "Create_RotationSpeedController" then
thruster.power = (thruster.transmission.getTargetSpeed())/256
thruster.power = (peripheral.wrap(thruster.transmission).getTargetSpeed())/256
end
else
print("ERROR: Thruster "..peripheral.getName(thruster.thruster).." has no transmission.")
@@ -789,7 +789,7 @@ function SetThrusterPower(thruster, power)
if power > 1.0 then power = 1.0 elseif power < 0.0 then power = 0.0 end
if Config.Debug then print("DEBUG: thruster "..thruster.name..": power "..power) end
--if Config.Debug then print("DEBUG: thruster "..thruster.name..": power "..power) end
if thruster.type == "rotator" then
thruster.power = power
@@ -862,15 +862,15 @@ function CreatePID(kp, ki, kd)
integral = 0, lastError = 0,
minOutput = 0.0,
maxOutput = 64.0,
maxOutput = 32.0,
update = function(self, setpoint, pv, dt)
if dt <= 0 then return 0.0 end
local error = setpoint - pv
local P = self.kp * error
local D = 0
if dt > 0 then D = self.kd * ((error - self.lastError) / dt) else D = 0 end
local D = self.kd * ((error - self.lastError) / dt)
local potential_i = self.integral + (self.ki * error * dt)
@@ -880,7 +880,7 @@ function CreatePID(kp, ki, kd)
local clampedOutput = 0
if self.minOutput <= potential_i and potential_i <= self.maxOutput then
if raw_output >= self.minOutput and raw_output <= self.maxOutput then
self.integral = potential_i
clampedOutput = raw_output
else
@@ -889,9 +889,7 @@ function CreatePID(kp, ki, kd)
self.lastError = error
local normalizedOutput = (clampedOutput - self.minOutput) / (self.maxOutput - self.minOutput)
return normalizedOutput
return clampedOutput
end
}
end
@@ -900,13 +898,13 @@ end
PIDs = {
-- angular
-- attitude PIDs
RollAttitudePID = CreatePID(4.5, 0.01, 0.05),
PitchAttitudePID = CreatePID(4.5, 0.01, 0.05),
RollAttitudePID = CreatePID(3.0, 0.01, 0.05),
PitchAttitudePID = CreatePID(3.0, 0.01, 0.05),
YawAttitudePID = CreatePID(0.1, 0.01, 0.05),
-- rate PIDs
RollRatePID = CreatePID(1.2, 0.01, 0.05),
PitchRatePID = CreatePID(1.2, 0.01, 0.05),
RollRatePID = CreatePID(0.8, 0.01, 0.05),
PitchRatePID = CreatePID(0.8, 0.01, 0.05),
YawRatePID = CreatePID(0.5, 0.01, 0.05),
-- lateral
@@ -916,7 +914,7 @@ PIDs = {
ForeAftPID = CreatePID(0.1, 0.01, 0.05),
-- rate PIDs
VerticalRatePID = CreatePID(1.2, 0.01, 0.05),
VerticalRatePID = CreatePID(0.8, 0.01, 0.05),
PortStarRatePID = CreatePID(0.1, 0.01, 0.05),
ForeAftRatePID = CreatePID(0.1, 0.01, 0.05)
}
@@ -961,39 +959,56 @@ function UpdateGlobalThrust()
for _, thruster in pairs(Thrusters) do
local desiredThrust = 0
-- Calculate desired thrust based on affectVectors and PID outputs
if thruster.affectVectors.angular.pitch ~= nil and thruster.primary_pitch_thruster then
if thruster.affectVectors.angular.pitch == "up" then
desiredThrust = desiredThrust + pitchRateOutput
else
desiredThrust = desiredThrust - pitchRateOutput
end
end
if thruster.affectVectors.angular.roll ~= nil and thruster.primary_roll_thruster then
if thruster.affectVectors.angular.roll == "port" then
desiredThrust = desiredThrust + rollRateOutput
else
desiredThrust = desiredThrust - rollRateOutput
if thruster.x_config_equivalent ~= nil then
-- Calculate desired thrust based on affectVectors and PID outputs
if thruster.affectVectors.angular.pitch ~= nil and thruster.primary_pitch_thruster then
if thruster.affectVectors.angular.pitch == "up" then
desiredThrust = desiredThrust + pitchRateOutput
else
desiredThrust = desiredThrust - pitchRateOutput
end
end
end
if thruster.affectVectors.angular.yaw ~= nil and thruster.primary_yaw_thruster then
if thruster.affectVectors.angular.yaw == "port" then
desiredThrust = desiredThrust + yawRateOutput
else
desiredThrust = desiredThrust - yawRateOutput
if thruster.affectVectors.angular.roll ~= nil and thruster.primary_roll_thruster then
if thruster.affectVectors.angular.roll == "port" then
desiredThrust = desiredThrust + rollRateOutput
else
desiredThrust = desiredThrust - rollRateOutput
end
end
if thruster.affectVectors.angular.yaw ~= nil and thruster.primary_yaw_thruster then
if thruster.affectVectors.angular.yaw == "port" then
desiredThrust = desiredThrust + yawRateOutput
else
desiredThrust = desiredThrust - yawRateOutput
end
end
if thruster.affectVectors.lateral.y == "up" then
desiredThrust = desiredThrust + throttleOutput
end
--if Config.Debug then print("DEBUG: "..thruster.name..": desiredThrust "..desiredThrust.." (from UpdateGlobalThrust)") end
SetThrusterPower(thruster, desiredThrust)
else
if Config.Debug then print("DEBUG: Thruster has x_config_equivalent set") end
local prop1 = throttleOutput + pitchRateOutput + rollRateOutput -- front-left
local prop2 = throttleOutput + pitchRateOutput - rollRateOutput -- front-right
local prop3 = throttleOutput - pitchRateOutput - rollRateOutput -- back-right
local prop4 = throttleOutput - pitchRateOutput + rollRateOutput -- back-left
if thruster.x_config_equivalent == "1" then SetThrusterPower(thruster, prop1)
elseif thruster.x_config_equivalent == "2" then SetThrusterPower(thruster, prop2)
elseif thruster.x_config_equivalent == "3" then SetThrusterPower(thruster, prop3)
elseif thruster.x_config_equivalent == "4" then SetThrusterPower(thruster, prop4)
else print("ERROR: Thruster "..thruster.name.." has invalid x_config_equivalent value")
end
end
if thruster.affectVectors.lateral.y == "up" then
desiredThrust = desiredThrust + throttleOutput
end
--if Config.Debug then print("DEBUG: "..thruster.name..": desiredThrust "..desiredThrust.." (from UpdateGlobalThrust)") end
SetThrusterPower(thruster, desiredThrust)
end
end

View File

@@ -6,6 +6,7 @@ return {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "1",
type = "rotator",
name = "gyroscopic_propeller_bearing_0",
thruster = nil,
@@ -31,6 +32,7 @@ return {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "2",
type = "rotator",
name = "gyroscopic_propeller_bearing_1",
thruster = nil,
@@ -56,6 +58,7 @@ return {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "4",
type = "rotator",
name = "gyroscopic_propeller_bearing_2",
thruster = nil,
@@ -81,6 +84,7 @@ return {
primary_altitude_thruster = true,
primary_fore_thruster = false,
primary_aft_thruster = false,
x_config_equivalent = "3",
type = "rotator",
name = "gyroscopic_propeller_bearing_3",
thruster = nil,