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