Reworking vehicle controller

This commit is contained in:
Ivan Grachyov 2021-11-12 20:47:12 +05:00
parent 1650ac9a6d
commit b5fb04cce8
7 changed files with 169 additions and 30 deletions

View File

@ -1,5 +1,5 @@
config = { config = {
Name = "Jopa", Name = 'Jopa',
Engine = { Engine = {
IdleRPM = 900, IdleRPM = 900,
MaxRPM = 7000, MaxRPM = 7000,
@ -35,7 +35,7 @@ config = {
MaxTorque = 600 MaxTorque = 600
}, },
Gearbox = { Gearbox = {
Type = "AUTO", Type = 'AUTO',
ShiftDuration = 0.2, ShiftDuration = 0.2,
ShiftSmoothness = 0.3, ShiftSmoothness = 0.3,
Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83}, Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83},
@ -61,10 +61,10 @@ config = {
FinalDrive = 1 FinalDrive = 1
}}, }},
Systems = {{ Systems = {{
Type = "LAUNCH", Type = 'LAUNCH',
RPMLimit = 3500 RPMLimit = 3500
}, { }, {
Type = "TRACTION", Type = 'TRACTION',
Slip = 1.5 Slip = 1.5
}} }}
} }

View File

@ -38,11 +38,15 @@ function Clutch:getOutputs()
} }
end end
function Clutch:update() function Clutch:getPress()
if self._gearbox.type == gearboxTypes.MANUAL then if self._gearbox ~= nil and self._gearbox.type == gearboxTypes.MANUAL then
self.press = wire.ports.Clutch return wire.ports.Clutch
else
return self.press
end end
end
function Clutch:update()
local someConversionCoeff = 0.10472 local someConversionCoeff = 0.10472
local engRPM = self._engine and self._engine.rpm or 0 local engRPM = self._engine and self._engine.rpm or 0
@ -51,7 +55,7 @@ function Clutch:update()
local gboxRatioNotZero = gboxRatio ~= 0 and 1 or 0 local gboxRatioNotZero = gboxRatio ~= 0 and 1 or 0
self.slip = ((engRPM - gboxRPM) * someConversionCoeff) * gboxRatioNotZero / 2 self.slip = ((engRPM - gboxRPM) * someConversionCoeff) * gboxRatioNotZero / 2
self.targetTorque = math.clamp(self.slip * self.stiffness * (1 - self.press), -self.maxTorque, self.maxTorque) self.targetTorque = math.clamp(self.slip * self.stiffness * (1 - self:getPress()), -self.maxTorque, self.maxTorque)
self.torque = math.lerp(self.damping, self.torque, self.targetTorque) self.torque = math.lerp(self.damping, self.torque, self.targetTorque)
end end

View File

@ -36,11 +36,11 @@ Vehicle:new({
}, },
Clutch = { Clutch = {
Stiffness = 22, Stiffness = 22,
Damping = 0.8, Damping = 0.5,
MaxTorque = 600 MaxTorque = 600
}, },
Gearbox = { Gearbox = {
Type = "AUTO", Type = "MANUAL",
ShiftDuration = 0.2, ShiftDuration = 0.2,
ShiftSmoothness = 0.3, ShiftSmoothness = 0.3,
Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83}, Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83},

View File

@ -0,0 +1,33 @@
-- @include ./wire_component.txt
require('./wire_component.txt')
Differential = class('Differential', WireComponent)
function Differential:initialize(config)
self.power = config.Power or 1
self.coast = config.Coast or 1
self.preload = config.Preload or 10
self.viscousCoeff = config.ViscousCoeff or 0.9
self.usePowerBias = config.UsePowerBias or 10
self.distributionCoeff = config.DistributionCoeff or 1
self.maxCorrectingTorque = config.MaxCorrectingTorque or 200
self.avgRPM = 0
self.leftWheel = nil
self.rightWheel = nil
end
function Differential:getInputs()
local leftWheelName = 'Axle_' + self.order + '_LeftWheel'
local rightWheelName = 'Axle_' + self.order + '_RightWheel'
local inputs = {}
inputs[leftWheelName] = 'entity'
inputs[rightWheelName] = 'entity'
return inputs
end

View File

@ -0,0 +1,49 @@
Gearbox = class('Gearbox')
function Gearbox:initialize(config, clutch, axles)
self.ratios = config.Ratios
self.reverse = config.Reverse
self.rpm = 0
self.gear = 0
self.torque = 0
self.axles = axles or {}
self.clutch = clutch
self:recalcRatio()
end
function Gearbox:setGear(gear)
if gear >= -1 and gear <= #self.ratios then
self.gear = gear
self:recalcRatio()
end
end
function Gearbox:recalcRatio()
if self.gear == -1 then
self.ratio = -self.reverse
elseif self.gear == 0 then
self.ratio = 0
else
self.ratio = self.ratios[self.gear]
end
end
function Gearbox:update()
if self.clutch ~= nil then
self.torque = self.clutch.torque * self.ratio
end
local axlesRPM = table.map(self.axles, function(diff)
return diff.avgRPM
end)
local maxAxlesRPM = math.max(unpack(axlesRPM))
self.rpm = maxAxlesRPM * self.ratio
end
function Gearbox:shift()
end

View File

@ -1,29 +1,76 @@
ManualGearbox = class('ManualGearbox') -- @include /koptilnya/libs/watcher.txt
-- @include ./base.txt
require('/koptilnya/libs/watcher.txt')
require('./base.txt')
function ManualGearbox:initialize(config) ManualGearbox = class('ManualGearbox', Gearbox)
self.ratios = config.Ratios
self.reverse = config.Reverse
self.rpm = 0 function ManualGearbox:initialize(config, clutch)
self.torque = 0 Gearbox.initialize(self, config, clutch)
self.gear = 0
self:recalcRatio() -- self.ratios = config.Ratios
-- self.reverse = config.Reverse
-- self.rpm = 0
-- self.torque = 0
-- self.gear = 0
-- self:recalcRatio()
-- self.axles = {}
-- self.clutch = nil
-- self.torque = 0
-- self.shiftWatcher = watcher(function()
-- return wire.ports.Upshift - wire.ports.Downshift
-- end, function(val)
-- if val ~= 0 then
-- self:shift(val)
-- end
-- end)
end
function ManualGearbox:recalcRatio()
if self.gear == -1 then
self.ratio = -self.reverse
elseif self.gear == 0 then
self.ratio = 0
else
self.ratio = self.ratios[self.gear]
end
end
function ManualGearbox:getInputs()
return {
Upshift = 'number',
Downshift = 'number'
}
end
function ManualGearbox:getOutputs()
return {
Gearbox_RPM = 'number',
Gearbox_Torque = 'number'
}
end
function ManualGearbox:updateOutputs()
self.axles = {}
self.clutch = {}
end end
function ManualGearbox:update() function ManualGearbox:update()
if self.clutch ~= nil then -- if self.clutch ~= nil then
self.torque = self.clutch.torque * self.ratio -- self.torque = self.clutch.torque * self.ratio
end -- end
local axlesRPM = table.map(self.axles, function(diff) -- local axlesRPM = table.map(self.axles, function(diff)
return diff.avgRPM -- return diff.avgRPM
end) -- end)
local maxAxlesRPM = math.max(unpack(axlesRPM)) -- local maxAxlesRPM = math.max(unpack(axlesRPM))
self.rpm = maxAxlesRPM * self.ratio -- self.rpm = maxAxlesRPM * self.ratio
-- self.shiftWatcher()
end end

View File

@ -22,7 +22,13 @@ function Vehicle:initialize(config)
self.clutch = Clutch:new(config.Clutch) self.clutch = Clutch:new(config.Clutch)
self.engine = Engine:new(config.Engine, self.clutch) self.engine = Engine:new(config.Engine, self.clutch)
self.gearbox = GearboxFactory.create(config.Gearbox, self.clutch)
self.axles = table.map(config.Axles, function(config)
return Differential:new(config)
end)
self.gearbox = GearboxFactory.create(config.Gearbox, self.clutch, self.axles)
-- self.axles = table.map(config.Axles, function(config) -- self.axles = table.map(config.Axles, function(config)
-- return Differential:new(config) -- return Differential:new(config)
-- end) -- end)
@ -30,7 +36,7 @@ function Vehicle:initialize(config)
-- return SystemsBuilder:create(config) -- return SystemsBuilder:create(config)
-- end) -- end)
self.components = {self.clutch, self.engine} -- , self.gearbox, self.clutch} self.components = {self.clutch, self.engine, self.gearbox} -- , self.gearbox, self.clutch}
-- self.components = table.add(self.components, self.axles) -- self.components = table.add(self.components, self.axles)
-- self.components = table.add(self.components, self.systems) -- self.components = table.add(self.components, self.systems)