Attempts to fix new vehicle system

This commit is contained in:
Nikita Kruglickiy 2022-07-29 22:06:02 +03:00
parent ca83cc2515
commit ec65efcafc
4 changed files with 42 additions and 18 deletions

View File

@ -11,13 +11,19 @@ Differential = class('Differential', PowertrainComponent)
function Differential:initialize(config, order)
PowertrainComponent.initialize(self, 'Axle')
self.wireOutputs = {
Diff_Torque = 'number'
}
local prefix = 'Axle' .. (order or '') .. '_'
self.finalDrive = config.FinalDrive or 4
self.inertia = config.Inertia or 0.2
self.biasAB = config.Bias or 0.5
self.coastRamp = config.CoastRamp or 0.5
self.powerRamp = config.PowerRamp or 1
self.preload = config.Preload or 10
self.stiffness = config.Stiffness or 0.1
self.slipTorque = config.SlipTorque or 1000
local wheelConfig = {
@ -33,6 +39,8 @@ function Differential:initialize(config, order)
end
function Differential:updateWireOutputs()
wire.ports.Diff_Torque = self.torque
if self.outputA ~= nil then
self.outputA:updateWireOutputs()
end
@ -60,7 +68,7 @@ function Differential:queryAngularVelocity(angularVelocity)
local aW = self.outputA:queryAngularVelocity(angularVelocity)
local bW = self.outputB:queryAngularVelocity(angularVelocity)
return (aW - bW) * self.finalDrive * 0.5
return (aW + bW) * self.finalDrive * 0.5
end
function Differential:forwardStep(torque, inertia)
@ -69,12 +77,14 @@ function Differential:forwardStep(torque, inertia)
end
local aW = self.outputA:queryAngularVelocity(self.angularVelocity)
local bW = -self.outputB:queryAngularVelocity(self.angularVelocity)
local bW = self.outputB:queryAngularVelocity(self.angularVelocity)
local aI = self.outputA:queryInertia()
local bI = self.outputB:queryInertia()
self.torque = torque * self.finalDrive
local tqA, tqB = self:_openDiffTorqueSplit(
torque * self.finalDrive,
self.torque,
aW,
bW,
aI,
@ -87,13 +97,10 @@ function Differential:forwardStep(torque, inertia)
self.slipTorque
)
--tqA = tqA * aI * TICK_INTERVAL
--tqB = tqB * bI * TICK_INTERVAL
tqA = self.outputA:forwardStep(tqA, inertia * 0.5 + aI)
tqB = self.outputB:forwardStep(tqB, inertia * 0.5 + bI)
return tqA + tqB
return (tqA + tqB) / self.finalDrive
end
function Differential:_openDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
@ -109,7 +116,7 @@ function Differential:_lockingDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preloa
local bTqCorr = (w - bW) * bI -- / dt
bTqCorr = bTqCorr * stiffness
local biasA = clamp(0.5 + (bW - aW) * 0.1 * stiffness, 0, 1)
local biasA = math.clamp(0.5 + (bW - aW) * 0.1 * stiffness, 0, 1)
return tq * biasA + aTqCorr, tq * (1 - biasA) * bTqCorr
end

View File

@ -18,7 +18,8 @@ function Wheel:initialize(config, prefix)
[prefix .. 'Wheel'] = 'entity'
}
self.wireOutputs = {
[prefix .. 'WheelRPM'] = 'number'
[prefix .. 'WheelRPM'] = 'number',
[prefix .. 'WheelTorque'] = 'number'
}
self.entity = NULL_ENTITY
@ -32,8 +33,7 @@ function Wheel:initialize(config, prefix)
end
if config.CalculateInertia then
--self.entity:setInertia(calculateWheelInertia(val))
--print(calculateWheelInertia(val))
self.entity:setInertia(calculateWheelInertia(val))
end
self.inertia = self.entity:getInertia().y
@ -41,8 +41,17 @@ function Wheel:initialize(config, prefix)
end)
end
function Wheel:getRadius()
if not isValid(self.entity) then
return 0
end
return self.entity:getModelRadius() * UNITS_TO_METERS
end
function Wheel:updateWireOutputs()
wire.ports[self.prefix .. 'WheelRPM'] = self:getRPM()
wire.ports[self.prefix .. 'WheelTorque'] = self.torque
end
function Wheel:queryAngularVelocity()
@ -54,9 +63,14 @@ function Wheel:forwardStep(torque, inertia)
return 0
end
--self.entity:setInertia(self.entity:getInertia():setY(inertia))
self.entity:applyTorque(torque * self.direction * self.entity:getRight())
self.angularVelocity = self.entity:getAngleVelocity().y * self.direction * TICK_INTERVAL
local initialAngularVelocity = self.angularVelocity
return self.angularVelocity * self.inertia
--self.entity:setInertia(self.entity:getInertia():setY(inertia))
self.torque = math.deg(torque) * 1.33 * -self.direction
self.entity:applyTorque(self.torque * TICK_INTERVAL * self.entity:getRight())
self.angularVelocity = math.rad(self.entity:getAngleVelocity().y) * self.direction
local deltaOmegaTorque = (self.angularVelocity - initialAngularVelocity) * self.inertia / TICK_INTERVAL
return torque
end

View File

@ -6,3 +6,4 @@ TICK_INTERVAL = game.getTickInterval()
RAD_TO_RPM = 9.5493
RPM_TO_RAD = 0.10472
UNITS_PER_METER = 39.37
UNITS_TO_METERS = 0.01905

View File

@ -1,3 +1,7 @@
--@include ./constants.txt
require('./constants.txt')
function getLocalVelocity(entity)
if not entity:isValid() then
return
@ -11,7 +15,5 @@ function calculateWheelInertia(entity)
return Vector(0)
end
local originalInertia = entity:getInertia()
return Vector(originalInertia)
return entity:getInertia():setY(0.5 * entity:getMass() * math.pow(entity:getModelRadius() * UNITS_TO_METERS, 2))
end