73 lines
1.8 KiB
Plaintext
73 lines
1.8 KiB
Plaintext
-- @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()
|
|
if self.gearbox ~= nil and self.gearbox.type == gearboxTypes.MANUAL then
|
|
return {
|
|
Clutch = 'number'
|
|
}
|
|
else
|
|
return {}
|
|
end
|
|
end
|
|
|
|
function Clutch:getOutputs()
|
|
return {
|
|
Clutch_Torque = 'number',
|
|
Clutch_Slip = 'number'
|
|
}
|
|
end
|
|
|
|
function Clutch:updateOutputs()
|
|
wire.ports.Clutch_Torque = self.torque
|
|
wire.ports.Clutch_Slip = self.slip
|
|
end
|
|
|
|
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
|
|
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:getPress()), -self.maxTorque, self.maxTorque)
|
|
|
|
self.torque = math.lerp(self.damping, self.torque, self.targetTorque)
|
|
end
|