Continuing rewriting engine
This commit is contained in:
parent
902d2d18bb
commit
1650ac9a6d
57
koptilnya/engine_remastered/clutch.txt
Normal file
57
koptilnya/engine_remastered/clutch.txt
Normal 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
|
||||||
@ -27,7 +27,8 @@ function Engine:initialize(config, clutch)
|
|||||||
self._fwInertia = self.flywheelMass * self.flywheelRadius ^ 2 / 2
|
self._fwInertia = self.flywheelMass * self.flywheelRadius ^ 2 / 2
|
||||||
self._limiterTime = 0
|
self._limiterTime = 0
|
||||||
|
|
||||||
self._clutch = clutch
|
self.clutch = clutch
|
||||||
|
clutch.engine = self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Engine:getInputs()
|
function Engine:getInputs()
|
||||||
@ -39,13 +40,13 @@ end
|
|||||||
function Engine:getOutputs()
|
function Engine:getOutputs()
|
||||||
return {
|
return {
|
||||||
RPM = 'number',
|
RPM = 'number',
|
||||||
Torque = 'number'
|
Engine_Torque = 'number'
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Engine:updateOutputs()
|
function Engine:updateOutputs()
|
||||||
wire.ports.RPM = self.rpm
|
wire.ports.RPM = self.rpm
|
||||||
wire.ports.Torque = self.torque
|
wire.ports.Engine_Torque = self.torque
|
||||||
end
|
end
|
||||||
|
|
||||||
function Engine:getThrottle()
|
function Engine:getThrottle()
|
||||||
@ -77,7 +78,7 @@ function Engine:update()
|
|||||||
|
|
||||||
local realInitialTorque = maxInitialTorque * masterThrottle
|
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
|
self.torque = realInitialTorque + self.friction
|
||||||
|
|
||||||
|
|||||||
7
koptilnya/engine_remastered/enums/gearbox.txt
Normal file
7
koptilnya/engine_remastered/enums/gearbox.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
types = {
|
||||||
|
MANUAL = 'MANUAL',
|
||||||
|
AUTO = 'AUTOMATIC/ROBOT',
|
||||||
|
CVT = 'CVT'
|
||||||
|
}
|
||||||
|
|
||||||
|
return types
|
||||||
10
koptilnya/engine_remastered/factories/gearbox.txt
Normal file
10
koptilnya/engine_remastered/factories/gearbox.txt
Normal 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
|
||||||
0
koptilnya/engine_remastered/gearboxes/cvt.txt
Normal file
0
koptilnya/engine_remastered/gearboxes/cvt.txt
Normal file
29
koptilnya/engine_remastered/gearboxes/manual.txt
Normal file
29
koptilnya/engine_remastered/gearboxes/manual.txt
Normal 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
|
||||||
@ -1,18 +1,28 @@
|
|||||||
|
-- Core components
|
||||||
|
--
|
||||||
-- @include ./engine.txt
|
-- @include ./engine.txt
|
||||||
|
-- @include ./clutch.txt
|
||||||
|
--
|
||||||
|
-- Helpers & stuff
|
||||||
|
-- @include ./factories/gearbox.txt
|
||||||
-- @include /koptilnya/libs/table.txt
|
-- @include /koptilnya/libs/table.txt
|
||||||
require('./engine.txt')
|
require('./engine.txt')
|
||||||
|
require('./clutch.txt')
|
||||||
|
require('./factories/gearbox.txt')
|
||||||
|
|
||||||
require('/koptilnya/libs/table.txt')
|
require('/koptilnya/libs/table.txt')
|
||||||
|
|
||||||
Vehicle = class('Vehicle')
|
Vehicle = class('Vehicle')
|
||||||
|
|
||||||
function Vehicle:initialize(config)
|
function Vehicle:initialize(config)
|
||||||
|
-- should probably validate config here
|
||||||
if config == nil then
|
if config == nil then
|
||||||
throw('Vehicle config not provided')
|
throw('Vehicle config not provided')
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 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 = GearboxBuilder:create(config.Gearbox, self.clutch)
|
self.gearbox = GearboxFactory.create(config.Gearbox, self.clutch)
|
||||||
-- 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)
|
||||||
@ -20,7 +30,7 @@ function Vehicle:initialize(config)
|
|||||||
-- return SystemsBuilder:create(config)
|
-- return SystemsBuilder:create(config)
|
||||||
-- end)
|
-- 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.axles)
|
||||||
-- self.components = table.add(self.components, self.systems)
|
-- self.components = table.add(self.components, self.systems)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user