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

View File

@ -38,11 +38,15 @@ function Clutch:getOutputs()
}
end
function Clutch:update()
if self._gearbox.type == gearboxTypes.MANUAL then
self.press = wire.ports.Clutch
function Clutch:getPress()
if self._gearbox ~= nil and self._gearbox.type == gearboxTypes.MANUAL then
return wire.ports.Clutch
else
return self.press
end
end
function Clutch:update()
local someConversionCoeff = 0.10472
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
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)
end

View File

@ -36,11 +36,11 @@ Vehicle:new({
},
Clutch = {
Stiffness = 22,
Damping = 0.8,
Damping = 0.5,
MaxTorque = 600
},
Gearbox = {
Type = "AUTO",
Type = "MANUAL",
ShiftDuration = 0.2,
ShiftSmoothness = 0.3,
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)
self.ratios = config.Ratios
self.reverse = config.Reverse
ManualGearbox = class('ManualGearbox', Gearbox)
self.rpm = 0
self.torque = 0
self.gear = 0
function ManualGearbox:initialize(config, clutch)
Gearbox.initialize(self, config, clutch)
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
function ManualGearbox: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
-- 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
-- self.shiftWatcher()
end

View File

@ -22,7 +22,13 @@ function Vehicle:initialize(config)
self.clutch = Clutch:new(config.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)
-- return Differential:new(config)
-- end)
@ -30,7 +36,7 @@ function Vehicle:initialize(config)
-- return SystemsBuilder:create(config)
-- 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.systems)