Attempts to fix new vehicle system
This commit is contained in:
parent
ca83cc2515
commit
ec65efcafc
@ -11,13 +11,19 @@ Differential = class('Differential', PowertrainComponent)
|
|||||||
function Differential:initialize(config, order)
|
function Differential:initialize(config, order)
|
||||||
PowertrainComponent.initialize(self, 'Axle')
|
PowertrainComponent.initialize(self, 'Axle')
|
||||||
|
|
||||||
|
self.wireOutputs = {
|
||||||
|
Diff_Torque = 'number'
|
||||||
|
}
|
||||||
|
|
||||||
local prefix = 'Axle' .. (order or '') .. '_'
|
local prefix = 'Axle' .. (order or '') .. '_'
|
||||||
|
|
||||||
self.finalDrive = config.FinalDrive or 4
|
self.finalDrive = config.FinalDrive or 4
|
||||||
|
self.inertia = config.Inertia or 0.2
|
||||||
self.biasAB = config.Bias or 0.5
|
self.biasAB = config.Bias or 0.5
|
||||||
self.coastRamp = config.CoastRamp or 0.5
|
self.coastRamp = config.CoastRamp or 0.5
|
||||||
self.powerRamp = config.PowerRamp or 1
|
self.powerRamp = config.PowerRamp or 1
|
||||||
self.preload = config.Preload or 10
|
self.preload = config.Preload or 10
|
||||||
|
self.stiffness = config.Stiffness or 0.1
|
||||||
self.slipTorque = config.SlipTorque or 1000
|
self.slipTorque = config.SlipTorque or 1000
|
||||||
|
|
||||||
local wheelConfig = {
|
local wheelConfig = {
|
||||||
@ -33,6 +39,8 @@ function Differential:initialize(config, order)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Differential:updateWireOutputs()
|
function Differential:updateWireOutputs()
|
||||||
|
wire.ports.Diff_Torque = self.torque
|
||||||
|
|
||||||
if self.outputA ~= nil then
|
if self.outputA ~= nil then
|
||||||
self.outputA:updateWireOutputs()
|
self.outputA:updateWireOutputs()
|
||||||
end
|
end
|
||||||
@ -60,7 +68,7 @@ function Differential:queryAngularVelocity(angularVelocity)
|
|||||||
local aW = self.outputA:queryAngularVelocity(angularVelocity)
|
local aW = self.outputA:queryAngularVelocity(angularVelocity)
|
||||||
local bW = self.outputB:queryAngularVelocity(angularVelocity)
|
local bW = self.outputB:queryAngularVelocity(angularVelocity)
|
||||||
|
|
||||||
return (aW - bW) * self.finalDrive * 0.5
|
return (aW + bW) * self.finalDrive * 0.5
|
||||||
end
|
end
|
||||||
|
|
||||||
function Differential:forwardStep(torque, inertia)
|
function Differential:forwardStep(torque, inertia)
|
||||||
@ -69,12 +77,14 @@ function Differential:forwardStep(torque, inertia)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local aW = self.outputA:queryAngularVelocity(self.angularVelocity)
|
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 aI = self.outputA:queryInertia()
|
||||||
local bI = self.outputB:queryInertia()
|
local bI = self.outputB:queryInertia()
|
||||||
|
|
||||||
|
self.torque = torque * self.finalDrive
|
||||||
|
|
||||||
local tqA, tqB = self:_openDiffTorqueSplit(
|
local tqA, tqB = self:_openDiffTorqueSplit(
|
||||||
torque * self.finalDrive,
|
self.torque,
|
||||||
aW,
|
aW,
|
||||||
bW,
|
bW,
|
||||||
aI,
|
aI,
|
||||||
@ -87,13 +97,10 @@ function Differential:forwardStep(torque, inertia)
|
|||||||
self.slipTorque
|
self.slipTorque
|
||||||
)
|
)
|
||||||
|
|
||||||
--tqA = tqA * aI * TICK_INTERVAL
|
|
||||||
--tqB = tqB * bI * TICK_INTERVAL
|
|
||||||
|
|
||||||
tqA = self.outputA:forwardStep(tqA, inertia * 0.5 + aI)
|
tqA = self.outputA:forwardStep(tqA, inertia * 0.5 + aI)
|
||||||
tqB = self.outputB:forwardStep(tqB, inertia * 0.5 + bI)
|
tqB = self.outputB:forwardStep(tqB, inertia * 0.5 + bI)
|
||||||
|
|
||||||
return tqA + tqB
|
return (tqA + tqB) / self.finalDrive
|
||||||
end
|
end
|
||||||
|
|
||||||
function Differential:_openDiffTorqueSplit(tq, aW, bW, aI, bI, biasAB, preload, stiffness, powerRamp, coastRamp, slipTorque)
|
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
|
local bTqCorr = (w - bW) * bI -- / dt
|
||||||
bTqCorr = bTqCorr * stiffness
|
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
|
return tq * biasA + aTqCorr, tq * (1 - biasA) * bTqCorr
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,7 +18,8 @@ function Wheel:initialize(config, prefix)
|
|||||||
[prefix .. 'Wheel'] = 'entity'
|
[prefix .. 'Wheel'] = 'entity'
|
||||||
}
|
}
|
||||||
self.wireOutputs = {
|
self.wireOutputs = {
|
||||||
[prefix .. 'WheelRPM'] = 'number'
|
[prefix .. 'WheelRPM'] = 'number',
|
||||||
|
[prefix .. 'WheelTorque'] = 'number'
|
||||||
}
|
}
|
||||||
|
|
||||||
self.entity = NULL_ENTITY
|
self.entity = NULL_ENTITY
|
||||||
@ -32,8 +33,7 @@ function Wheel:initialize(config, prefix)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if config.CalculateInertia then
|
if config.CalculateInertia then
|
||||||
--self.entity:setInertia(calculateWheelInertia(val))
|
self.entity:setInertia(calculateWheelInertia(val))
|
||||||
--print(calculateWheelInertia(val))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.inertia = self.entity:getInertia().y
|
self.inertia = self.entity:getInertia().y
|
||||||
@ -41,8 +41,17 @@ function Wheel:initialize(config, prefix)
|
|||||||
end)
|
end)
|
||||||
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()
|
function Wheel:updateWireOutputs()
|
||||||
wire.ports[self.prefix .. 'WheelRPM'] = self:getRPM()
|
wire.ports[self.prefix .. 'WheelRPM'] = self:getRPM()
|
||||||
|
wire.ports[self.prefix .. 'WheelTorque'] = self.torque
|
||||||
end
|
end
|
||||||
|
|
||||||
function Wheel:queryAngularVelocity()
|
function Wheel:queryAngularVelocity()
|
||||||
@ -54,9 +63,14 @@ function Wheel:forwardStep(torque, inertia)
|
|||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
--self.entity:setInertia(self.entity:getInertia():setY(inertia))
|
local initialAngularVelocity = self.angularVelocity
|
||||||
self.entity:applyTorque(torque * self.direction * self.entity:getRight())
|
|
||||||
self.angularVelocity = self.entity:getAngleVelocity().y * self.direction * TICK_INTERVAL
|
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|||||||
@ -6,3 +6,4 @@ TICK_INTERVAL = game.getTickInterval()
|
|||||||
RAD_TO_RPM = 9.5493
|
RAD_TO_RPM = 9.5493
|
||||||
RPM_TO_RAD = 0.10472
|
RPM_TO_RAD = 0.10472
|
||||||
UNITS_PER_METER = 39.37
|
UNITS_PER_METER = 39.37
|
||||||
|
UNITS_TO_METERS = 0.01905
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
|
--@include ./constants.txt
|
||||||
|
|
||||||
|
require('./constants.txt')
|
||||||
|
|
||||||
function getLocalVelocity(entity)
|
function getLocalVelocity(entity)
|
||||||
if not entity:isValid() then
|
if not entity:isValid() then
|
||||||
return
|
return
|
||||||
@ -11,7 +15,5 @@ function calculateWheelInertia(entity)
|
|||||||
return Vector(0)
|
return Vector(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
local originalInertia = entity:getInertia()
|
return entity:getInertia():setY(0.5 * entity:getMass() * math.pow(entity:getModelRadius() * UNITS_TO_METERS, 2))
|
||||||
|
|
||||||
return Vector(originalInertia)
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user