67 lines
1.6 KiB
Plaintext
67 lines
1.6 KiB
Plaintext
--@include ../powertrain_component.txt
|
|
|
|
local PowertrainComponent = require('../powertrain_component.txt')
|
|
|
|
local Gearbox = class('Gearbox', PowertrainComponent)
|
|
|
|
function Gearbox:initialize(vehicle, name, config)
|
|
PowertrainComponent.initialize(self, vehicle, name, config)
|
|
|
|
self.wireInputs = {
|
|
Upshift = 'number',
|
|
Downshift = 'number'
|
|
}
|
|
self.wireOutputs = {
|
|
Gearbox_RPM = 'number',
|
|
Gearbox_Torque = 'number',
|
|
Gearbox_Ratio = 'number'
|
|
}
|
|
|
|
self.type = config.Type or 'MANUAL'
|
|
self.inertia = config.Inertia or 1000
|
|
|
|
self.ratio = 0
|
|
end
|
|
|
|
function Gearbox:updateWireOutputs()
|
|
wire.ports.Gearbox_RPM = self:getRPM()
|
|
wire.ports.Gearbox_Torque = self.torque
|
|
wire.ports.Gearbox_Ratio = self.ratio
|
|
end
|
|
|
|
function Gearbox:queryInertia()
|
|
if self.output == nil or self.ratio == 0 then
|
|
return self.inertia
|
|
end
|
|
|
|
return self.inertia + self.output:queryInertia() / math.pow(self.ratio, 2)
|
|
end
|
|
|
|
function Gearbox:queryAngularVelocity(angularVelocity)
|
|
self.angularVelocity = angularVelocity
|
|
|
|
if self.output == nil or self.ratio == 0 then
|
|
return angularVelocity
|
|
end
|
|
|
|
return self.output:queryAngularVelocity(angularVelocity) * self.ratio
|
|
end
|
|
|
|
function Gearbox:forwardStep(torque, inertia)
|
|
if self.output == nil then
|
|
return torque
|
|
end
|
|
|
|
if self.ratio == 0 then
|
|
self.output:forwardStep(0, self.inertia)
|
|
|
|
return torque
|
|
end
|
|
|
|
self.torque = torque * self.ratio
|
|
|
|
return self.output:forwardStep(self.torque, (inertia + self.inertia) * math.pow(self.ratio, 2)) / self.ratio
|
|
end
|
|
|
|
return Gearbox
|