Finished stuff with manual gearbox

This commit is contained in:
Ivan Grachyov 2021-11-13 01:52:35 +05:00
parent b5fb04cce8
commit 54202989c1
9 changed files with 190 additions and 97 deletions

View File

@ -62,7 +62,7 @@ local Colors =
ReverseLights = Color(120,120,120),
RearLightsGlass = Color(150,40,40),
FrontLightsBase = Color(30,30,30),
FrontLights = Color(170,180,180),
FrontLights = Color(10,10,10),
FrontLightsGlass = Color(140,140,140),
Floor = Color(50,50,50),
Dashboard = Color(50,50,50),

View File

@ -15,31 +15,42 @@ function Clutch:initialize(config)
self.targetTorque = 0
self.torque = 0
self._engine = nil
self._gearbox = nil
self.engine = nil
self.gearbox = nil
end
function Clutch:linkEngine(eng)
self._engine = eng
self.engine = eng
end
function Clutch:linkGearbox(gbox)
self._gearbox = 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 {
ClutchTorque = 'number',
ClutchSlip = 'number'
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
if self.gearbox ~= nil and self.gearbox.type == gearboxTypes.MANUAL then
return wire.ports.Clutch
else
return self.press
@ -49,9 +60,9 @@ 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 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

View File

@ -36,19 +36,19 @@ Vehicle:new({
},
Clutch = {
Stiffness = 22,
Damping = 0.5,
Damping = 0.8,
MaxTorque = 600
},
Gearbox = {
Type = "MANUAL",
Type = 'MANUAL',
ShiftDuration = 0.2,
ShiftSmoothness = 0.3,
Ratios = {13.45, 8.12, 5.51, 4.16, 3.36, 2.83},
Reverse = 14.41
},
Axles = {{
Power = 1,
Coast = 1,
Power = 0.3,
Coast = 0.15,
Preload = 10,
UsePowerBias = 10,
ViscousCoeff = 0.96,
@ -56,8 +56,8 @@ Vehicle:new({
DistributionCoeff = 0.4,
FinalDrive = 1
}, {
Power = 1,
Coast = 1,
Power = 0.4,
Coast = 0.3,
Preload = 10,
UsePowerBias = 10,
ViscousCoeff = 0.96,
@ -66,10 +66,10 @@ Vehicle:new({
FinalDrive = 1
}},
Systems = {{
Type = "LAUNCH",
Type = 'LAUNCH',
Limit = 3500
}, {
Type = "TRACTION",
Type = 'TRACTION',
Limit = 1.5
}}
})

View File

@ -1,12 +1,17 @@
-- @server
-- @include /koptilnya/libs/constants.txt
-- @include ./wire_component.txt
require('/koptilnya/libs/constants.txt')
require('./wire_component.txt')
Differential = class('Differential', WireComponent)
function Differential:initialize(config)
function Differential:initialize(config, order)
self.power = config.Power or 1
self.coast = config.Coast or 1
self.finalDrive = config.FinalDrive or 1
self.preload = config.Preload or 10
self.order = order or 1
self.viscousCoeff = config.ViscousCoeff or 0.9
self.usePowerBias = config.UsePowerBias or 10
@ -16,18 +21,87 @@ function Differential:initialize(config)
self.avgRPM = 0
self.leftWheel = nil
self.rightWheel = nil
self.leftWheelInput = 'Axle' .. self.order .. '_LeftWheel'
self.rightWheelInput = 'Axle' .. self.order .. '_RightWheel'
self.leftWheel = wire.ports[self.leftWheelInput]
self.rightWheel = wire.ports[self.rightWheelInput]
hook.add('input', 'wire_axle_update' .. self.order, function(key, val)
if key == self.leftWheelInput then
self.leftWheel = val
end
if key == self.rightWheelInput then
self.rightWheel = val
end
end)
end
function Differential:getInputs()
local leftWheelName = 'Axle_' + self.order + '_LeftWheel'
local rightWheelName = 'Axle_' + self.order + '_RightWheel'
local inputs = {}
inputs[leftWheelName] = 'entity'
inputs[rightWheelName] = 'entity'
inputs[self.leftWheelInput] = 'entity'
inputs[self.rightWheelInput] = 'entity'
return inputs
end
function Differential:linkGearbox(gbox)
self.gearbox = gbox
end
function Differential:update()
if isValid(self.leftWheel) and isValid(self.rightWheel) then
local lwav = math.rad(self.leftWheel:getAngleVelocity()[2])
local rwav = math.rad(self.rightWheel:getAngleVelocity()[2]) * -1
self.avgRPM = (lwav + rwav) / 2 * RAD_TO_RPM * self.finalDrive
-- RAV = relative anglar velocity
local lwrav = rwav - lwav
local rwrav = lwav - rwav
local lwInertia = self.leftWheel:getInertia():getLength()
local rwInertia = self.rightWheel:getInertia():getLength()
local lwtq = lwrav * lwInertia
local rwtq = rwrav * rwInertia
-- Calculating damping torque here
local lwDampingTorque = self.viscousCoeff * lwtq
local rwDampingTorque = self.viscousCoeff * rwtq
-- Whether use diff power or diff coast
local usePower = self.gearbox and self.gearbox.torque > self.usePowerBias or false
local usedCoeff = usePower and self.power or self.coast
local lwPowerCoeff = usedCoeff
local rwPowerCoeff = usedCoeff
-- this could probably be shortened?
-- if lwtq - rwtq > self.preload then
-- lwPowerCoeff = usedCoeff
-- rwPowerCoeff = usedCoeff
-- end
-- if rwtq - lwtq > self.preload then
-- lwPowerCoeff = usedCoeff
-- rwPowerCoeff = usedCoeff
-- end
local lwCorrectingTorque = math.clamp(lwDampingTorque * lwPowerCoeff, -self.maxCorrectingTorque,
self.maxCorrectingTorque)
local rwCorrectingTorque = math.clamp(rwDampingTorque * rwPowerCoeff, -self.maxCorrectingTorque,
self.maxCorrectingTorque)
local gboxTorque = self.gearbox and self.gearbox.torque or 0
local driveTorque = gboxTorque * self.distributionCoeff * 52.49 * self.finalDrive
local lwCorrectedTorque = lwCorrectingTorque * 600 + driveTorque
local rwCorrectedTorque = rwCorrectingTorque * 600 + driveTorque
self.leftWheel:applyTorque(lwCorrectedTorque * -self.leftWheel:getRight() * TICK_INTERVAL)
self.rightWheel:applyTorque(rwCorrectedTorque * self.rightWheel:getRight() * TICK_INTERVAL)
end
end

View File

@ -28,7 +28,7 @@ function Engine:initialize(config, clutch)
self._limiterTime = 0
self.clutch = clutch
clutch.engine = self
clutch:linkEngine(self)
end
function Engine:getInputs()

View File

@ -5,6 +5,6 @@ require('../gearboxes/manual.txt')
GearboxFactory = class('GearboxFactory')
function GearboxFactory.create(config, clutch)
return ManualGearbox:new(config, clutch)
function GearboxFactory.create(...)
return ManualGearbox:new(...)
end

View File

@ -1,4 +1,7 @@
Gearbox = class('Gearbox')
-- @include ../wire_component.txt
require('../wire_component.txt')
Gearbox = class('Gearbox', WireComponent)
function Gearbox:initialize(config, clutch, axles)
self.ratios = config.Ratios
@ -11,9 +14,40 @@ function Gearbox:initialize(config, clutch, axles)
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 {
Upshift = 'number',
Downshift = 'number'
}
end
function Gearbox:getOutputs()
return {
Gearbox_RPM = 'number',
Gearbox_Torque = 'number',
Gearbox_Gear = 'number',
Gearbox_Ratio = 'number'
}
end
function Gearbox:updateOutputs()
wire.ports.Gearbox_RPM = self.rpm
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
@ -36,14 +70,13 @@ function Gearbox:update()
self.torque = self.clutch.torque * self.ratio
end
local axlesRPM = table.map(self.axles, function(diff)
return diff.avgRPM
end)
local maxAxlesRPM = 0
local maxAxlesRPM = math.max(unpack(axlesRPM))
if #self.axles > 0 then
maxAxlesRPM = math.max(unpack(table.map(self.axles, function(diff)
return diff.avgRPM
end)))
end
self.rpm = maxAxlesRPM * self.ratio
end
function Gearbox:shift()
end

View File

@ -5,30 +5,21 @@ require('./base.txt')
ManualGearbox = class('ManualGearbox', Gearbox)
function ManualGearbox:initialize(config, clutch)
Gearbox.initialize(self, config, clutch)
function ManualGearbox:initialize(...)
Gearbox.initialize(self, ...)
-- self.ratios = config.Ratios
-- self.reverse = config.Reverse
function shiftFunc()
local upshift = wire.ports.Upshift or 0
local downshift = wire.ports.Downshift or 0
-- self.rpm = 0
-- self.torque = 0
-- self.gear = 0
return upshift - downshift
end
-- self:recalcRatio()
-- self.axles = {}
-- self.clutch = nil
-- self.torque = 0
-- self.shiftWatcher = watcher(function()
-- return wire.ports.Upshift - wire.ports.Downshift
-- end, function(val)
-- if val ~= 0 then
-- self:shift(val)
-- end
-- end)
self.shiftWatcher = watcher(shiftFunc, function(val)
if val ~= 0 then
self:shift(val)
end
end)
end
function ManualGearbox:recalcRatio()
@ -41,36 +32,23 @@ function ManualGearbox:recalcRatio()
end
end
function ManualGearbox:getInputs()
return {
Upshift = 'number',
Downshift = 'number'
}
function ManualGearbox:shift(dir)
self:setGear(self.gear + dir)
end
function ManualGearbox:getOutputs()
return {
Gearbox_RPM = 'number',
Gearbox_Torque = 'number'
}
end
-- function ManualGearbox:getOutputs()
-- return {
-- Gearbox_RPM = 'number',
-- Gearbox_Torque = 'number'
-- }
-- end
function ManualGearbox:updateOutputs()
-- function ManualGearbox:updateOutputs()
end
-- end
function ManualGearbox:update()
-- if self.clutch ~= nil then
-- self.torque = self.clutch.torque * self.ratio
-- end
Gearbox.update(self)
-- local axlesRPM = table.map(self.axles, function(diff)
-- return diff.avgRPM
-- end)
-- local maxAxlesRPM = math.max(unpack(axlesRPM))
-- self.rpm = maxAxlesRPM * self.ratio
-- self.shiftWatcher()
self.shiftWatcher()
end

View File

@ -2,12 +2,14 @@
--
-- @include ./engine.txt
-- @include ./clutch.txt
-- @include ./differential.txt
--
-- Helpers & stuff
-- @include ./factories/gearbox.txt
-- @include /koptilnya/libs/table.txt
require('./engine.txt')
require('./clutch.txt')
require('./differential.txt')
require('./factories/gearbox.txt')
require('/koptilnya/libs/table.txt')
@ -23,20 +25,20 @@ function Vehicle:initialize(config)
self.clutch = Clutch:new(config.Clutch)
self.engine = Engine:new(config.Engine, self.clutch)
local axleOrder = 0
self.axles = table.map(config.Axles, function(config)
return Differential:new(config)
axleOrder = axleOrder + 1
return Differential:new(config, axleOrder)
end)
self.gearbox = GearboxFactory.create(config.Gearbox, self.clutch, self.axles)
-- self.axles = table.map(config.Axles, function(config)
-- return Differential:new(config)
-- end)
-- self.systems = table.map(config.Systems, function(config)
-- return SystemsBuilder:create(config)
-- return SystemsFactory.create(config)
-- end)
self.components = {self.clutch, self.engine, self.gearbox} -- , self.gearbox, self.clutch}
self.components = {self.clutch, self.engine, self.gearbox}
self.components = table.add(self.components, self.axles)
-- self.components = table.add(self.components, self.axles)
-- self.components = table.add(self.components, self.systems)
@ -50,11 +52,6 @@ function Vehicle:initialize(config)
wire.adjustPorts(inputs, outputs)
-- for _, ent in self.entities do
-- ent:createInputs()
-- ent:createOutputs()
-- end
hook.add('tick', 'vehicle_update', function()
local outputs = {}