Refactored vehicle components

This commit is contained in:
Nikita Kruglickiy
2022-07-28 15:44:31 +03:00
parent 7cdb222580
commit b4dccd73a6
11 changed files with 485 additions and 523 deletions

View File

@@ -1,82 +1,64 @@
-- @include ../wire_component.txt
require('../wire_component.txt')
--@include ../powertrain_component.txt
Gearbox = class('Gearbox', WireComponent)
require('../powertrain_component.txt')
function Gearbox:initialize(config, clutch, axles)
self.ratios = config.Ratios
self.reverse = config.Reverse
Gearbox = class('Gearbox', PowertrainComponent)
self.rpm = 0
self.gear = 0
self.torque = 0
function Gearbox:initialize(config)
PowertrainComponent.initialize(self, 'Gearbox')
self.axles = axles or {}
self.clutch = clutch
self.type = config.Type
clutch:linkGearbox(self)
for _, axle in pairs(axles) do
axle:linkGearbox(self)
end
self:recalcRatio()
end
function Gearbox:getInputs()
return {
self.wireInputs = {
Upshift = 'number',
Downshift = 'number'
}
end
function Gearbox:getOutputs()
return {
self.wireOutputs = {
Gearbox_RPM = 'number',
Gearbox_Torque = 'number',
Gearbox_Gear = 'number',
Gearbox_Ratio = 'number'
}
self.type = config.Type or 'MANUAL'
self.inertia = config.Inertia or 1000
self.ratio = 0
end
function Gearbox:updateOutputs()
wire.ports.Gearbox_RPM = self.rpm
function Gearbox:updateWireOutputs()
wire.ports.Gearbox_RPM = self:getRPM()
wire.ports.Gearbox_Torque = self.torque
wire.ports.Gearbox_Gear = self.gear
wire.ports.Gearbox_Ratio = self.ratio
end
function Gearbox:setGear(gear)
if gear >= -1 and gear <= #self.ratios then
self.gear = gear
self:recalcRatio()
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:recalcRatio()
if self.gear == -1 then
self.ratio = -self.reverse
elseif self.gear == 0 then
self.ratio = 0
else
self.ratio = self.ratios[self.gear]
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:update()
if self.clutch ~= nil then
self.torque = self.clutch.torque * self.ratio
function Gearbox:forwardStep(torque, inertia)
if self.output == nil then
return torque
end
local maxAxlesRPM = 0
if self.ratio == 0 then
self.output:forwardStep(0, inertia)
if #self.axles > 0 then
maxAxlesRPM = math.max(unpack(table.map(self.axles, function(diff)
return diff.avgRPM
end)))
return torque
end
self.rpm = maxAxlesRPM * self.ratio
end
self.torque = torque * self.ratio
return self.output:forwardStep(self.torque, (inertia + self.inertia) * math.pow(self.ratio, 2)) / self.ratio
end

View File

@@ -1,12 +1,22 @@
-- @include /koptilnya/libs/watcher.txt
-- @include ./base.txt
--@include /koptilnya/libs/watcher.txt
--@include ./base.txt
require('/koptilnya/libs/watcher.txt')
require('./base.txt')
ManualGearbox = class('ManualGearbox', Gearbox)
function ManualGearbox:initialize(...)
Gearbox.initialize(self, ...)
function ManualGearbox:initialize(config)
Gearbox.initialize(self, config)
table.merge(self.wireOutputs, {
Gearbox_Gear = 'number'
})
self.ratios = config.Ratios or { 3.6, 2.2, 1.5, 1.2, 1.0, 0.8}
self.reverse = config.Reverse or 3.4
self.gear = 0
function shiftFunc()
local upshift = wire.ports.Upshift or 0
@@ -20,6 +30,21 @@ function ManualGearbox:initialize(...)
self:shift(val)
end
end)
self:recalcRatio()
end
function ManualGearbox:updateWireOutputs()
Gearbox.updateWireOutputs(self)
wire.ports.Gearbox_Gear = self.gear
end
function ManualGearbox:setGear(gear)
if gear >= -1 and gear <= #self.ratios then
self.gear = gear
self:recalcRatio()
end
end
function ManualGearbox:recalcRatio()
@@ -36,19 +61,10 @@ function ManualGearbox:shift(dir)
self:setGear(self.gear + dir)
end
-- function ManualGearbox:getOutputs()
-- return {
-- Gearbox_RPM = 'number',
-- Gearbox_Torque = 'number'
-- }
-- end
-- function ManualGearbox:updateOutputs()
-- end
function ManualGearbox:update()
Gearbox.update(self)
function ManualGearbox:forwardStep(torque, inertia)
local result = Gearbox.forwardStep(self, torque, inertia)
self.shiftWatcher()
return result
end