Continuing rewriting engine

This commit is contained in:
Ivan Grachyov 2021-11-12 00:29:52 +05:00
parent 902d2d18bb
commit 1650ac9a6d
9 changed files with 121 additions and 7 deletions

View File

@ -0,0 +1,57 @@
-- @include ./wire_component.txt
-- @include ./enums/gearbox.txt
require('./wire_component.txt')
local gearboxTypes = require('./enums/gearbox.txt')
Clutch = class('Clutch', WireComponent)
function Clutch:initialize(config)
self.stiffness = config.Stiffness
self.damping = config.Damping
self.maxTorque = config.MaxTorque
self.press = 0
self.slip = 0
self.targetTorque = 0
self.torque = 0
self._engine = nil
self._gearbox = nil
end
function Clutch:linkEngine(eng)
self._engine = eng
end
function Clutch:linkGearbox(gbox)
self._gearbox = gbox
end
function Clutch:getInputs()
return {}
end
function Clutch:getOutputs()
return {
ClutchTorque = 'number',
ClutchSlip = 'number'
}
end
function Clutch:update()
if self._gearbox.type == gearboxTypes.MANUAL then
self.press = wire.ports.Clutch
end
local someConversionCoeff = 0.10472
local engRPM = self._engine and self._engine.rpm or 0
local gboxRPM = self._gearbox and self._gearbox.rpm or 0
local gboxRatio = self._gearbox and self._gearbox.ratio or 0
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.torque = math.lerp(self.damping, self.torque, self.targetTorque)
end

View File

@ -27,7 +27,8 @@ function Engine:initialize(config, clutch)
self._fwInertia = self.flywheelMass * self.flywheelRadius ^ 2 / 2
self._limiterTime = 0
self._clutch = clutch
self.clutch = clutch
clutch.engine = self
end
function Engine:getInputs()
@ -39,13 +40,13 @@ end
function Engine:getOutputs()
return {
RPM = 'number',
Torque = 'number'
Engine_Torque = 'number'
}
end
function Engine:updateOutputs()
wire.ports.RPM = self.rpm
wire.ports.Torque = self.torque
wire.ports.Engine_Torque = self.torque
end
function Engine:getThrottle()
@ -77,7 +78,7 @@ function Engine:update()
local realInitialTorque = maxInitialTorque * masterThrottle
local loadTorque = self._clutch and self._clutch.torque or 0
local loadTorque = self.clutch and self.clutch.torque or 0
self.torque = realInitialTorque + self.friction

View File

@ -0,0 +1,7 @@
types = {
MANUAL = 'MANUAL',
AUTO = 'AUTOMATIC/ROBOT',
CVT = 'CVT'
}
return types

View File

@ -0,0 +1,10 @@
-- @include ../gearboxes/manual.txt
require('../gearboxes/manual.txt')
-- require('../gearboxes/auto.txt')
-- require('../gearboxes/cvt.txt')
GearboxFactory = class('GearboxFactory')
function GearboxFactory.create(config, clutch)
return ManualGearbox:new(config, clutch)
end

View File

@ -0,0 +1,29 @@
ManualGearbox = class('ManualGearbox')
function ManualGearbox:initialize(config)
self.ratios = config.Ratios
self.reverse = config.Reverse
self.rpm = 0
self.torque = 0
self.gear = 0
self:recalcRatio()
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
end

View File

@ -1,18 +1,28 @@
-- Core components
--
-- @include ./engine.txt
-- @include ./clutch.txt
--
-- Helpers & stuff
-- @include ./factories/gearbox.txt
-- @include /koptilnya/libs/table.txt
require('./engine.txt')
require('./clutch.txt')
require('./factories/gearbox.txt')
require('/koptilnya/libs/table.txt')
Vehicle = class('Vehicle')
function Vehicle:initialize(config)
-- should probably validate config here
if config == nil then
throw('Vehicle config not provided')
end
-- self.clutch = Clutch:new(config.Clutch)
self.clutch = Clutch:new(config.Clutch)
self.engine = Engine:new(config.Engine, self.clutch)
-- self.gearbox = GearboxBuilder:create(config.Gearbox, self.clutch)
self.gearbox = GearboxFactory.create(config.Gearbox, self.clutch)
-- self.axles = table.map(config.Axles, function(config)
-- return Differential:new(config)
-- end)
@ -20,7 +30,7 @@ function Vehicle:initialize(config)
-- return SystemsBuilder:create(config)
-- end)
self.components = {self.engine} -- , self.gearbox, self.clutch}
self.components = {self.clutch, self.engine} -- , self.gearbox, self.clutch}
-- self.components = table.add(self.components, self.axles)
-- self.components = table.add(self.components, self.systems)