- Code: Select all
-- Setup
------------------------------------------------------------
-- Called when the engine script is initialised
------------------------------------------------------------
function Setup ()
gLastTime = 0 -- clock time at last call
gLastSpeed = 0 -- speed mph at last call
gPrevREV = 0 -- Reverser position at last call (initially Off)
gPrevMC = 0 -- MC notch at last call (initially Deadman)
gDampedAcceleration = {}
for i=0, 24 do
gDampedAcceleration[i] = 0
end
gAccelerationindex = 0
end
-- Constants
-- MC notch values
OFFNOTCH = 0.0
FIRSTNOTCH = 0.27
SECONDNOTCH = 0.3
THIRDNOTCH = 0.35
FOURTHNOTCH = 0.4
FIFTHNOTCH = 0.45
SIXTHNOTCH = 0.5
SEVENTHNOTCH = 0.62
EIGHTHNOTCH = 0.7
NINTHNOTCH = 0.8
TENTHNOTCH = 0.9
ELEVENTHNOTCH = 1.0
-- Power values for Regulator
IDLE = 0.0
SERIESRESIST1 = 0.27
SERIESRESIST2 = 0.3
SERIESRESIST3 = 0.35
SERIESRESIST4 = 0.4
SERIES = 0.45
SERIESTAP = 0.5
PARALLELRESIST1 = 0.62
PARALLELRESIST2 = 0.7
PARALLELRESIST3 = 0.8
PARALLEL = 0.9
PARALLELTAP = 1.0
-- Reverser
FOR = 1
INTER = 0
REV = -1
------------------------------------------------------------
-- Update
------------------------------------------------------------
-- Called every frame to update the simulation
------------------------------------------------------------
-- Parameters:
-- inteval = time since last update
------------------------------------------------------------
function Update (inteval)
-- Get controller values
CurrentTime = Call( "*:GetSimulationTime", 0 )
CurrentSpeed = Call( "*:GetControlValue", "SpeedometerMPH", 0 )
MCvalue = Call( "*:GetControlValue", "Controller", 0 )
REVvalue = Call( "*:GetControlValue", "Reverser", 0 )
-- Reverser interlock
if (REVvalue ~= gPrevREV) then -- user tried to move reverser
if (MCvalue ~= OFFNOTCH) and (gPrevREV == FOR) then
REVvalue = FOR -- if MC not Off, leave reverser where it was (FOR)
end
if (MCvalue ~= OFFNOTCH) and (gPrevREV == INTER) then
REVvalue = INTER -- if MC not Off, leave reverser where it was (INTER)
end
if (MCvalue ~= OFFNOTCH) and (gPrevREV == REV) then
REVvalue = REV -- if MC not Off, leave reverser where it was (REV)
end
ReverserValue = REVvalue -- move reverser to new position, or not
gPrevREV = REVvalue -- save current/new value
end
-- Adjust Regulator value to softly limit speed.
-- PowerValue will be in the range specified in notch and gradually decreasing to 0.0 at target speed
if (MCvalue == OFFNOTCH) then
PowerValue = IDLE
end
if (MCvalue == FIRSTNOTCH) and (CurrentSpeed <= 5) then
PowerValue = SERIESRESIST1 * (1 - (( CurrentSpeed / 5) ^ 5 ))
end
if (MCvalue == FIRSTNOTCH) and (CurrentSpeed > 5) then
PowerValue = IDLE
end
if (MCvalue == SECONDNOTCH) and (CurrentSpeed <= 12) then
PowerValue = SERIESRESIST2 * (1 - (( CurrentSpeed / 12) ^ 5 ))
end
if (MCvalue == SECONDNOTCH) and (CurrentSpeed > 12) then
PowerValue = IDLE
end
if (MCvalue == THIRDNOTCH) and (CurrentSpeed <= 25) then
PowerValue = SERIESRESIST3 * (1 - (( CurrentSpeed / 25) ^ 5 ))
end
if (MCvalue == THIRDNOTCH) and (CurrentSpeed > 25) then
PowerValue = IDLE
end
if (MCvalue == FOURTHNOTCH) and (CurrentSpeed <= 33) then
PowerValue = SERIESRESIST4 * (1 - (( CurrentSpeed / 33) ^ 5 ))
end
if (MCvalue == FOURTHNOTCH) and (CurrentSpeed > 33) then
PowerValue = IDLE
end
if (MCvalue == FIFTHNOTCH) and (CurrentSpeed <= 40) then
PowerValue = SERIES * (1 - (( CurrentSpeed / 40) ^ 5 ))
end
if (MCvalue == FIFTHNOTCH) and (CurrentSpeed > 40) then
PowerValue = IDLE
end
if (MCvalue == SIXTHNOTCH) and (CurrentSpeed <= 45) then
PowerValue = SERIESTAP * (1 - (( CurrentSpeed / 45) ^ 5 ))
end
if (MCvalue == SIXTHNOTCH) and (CurrentSpeed > 45) then
PowerValue = IDLE
end
if (MCvalue == SEVENTHNOTCH) and (CurrentSpeed <= 55) then
PowerValue = PARALLELRESIST1 * (1 - (( CurrentSpeed / 55) ^ 5 ))
end
if (MCvalue == SEVENTHNOTCH) and (CurrentSpeed > 55) then
PowerValue = IDLE
end
if (MCvalue == EIGHTHNOTCH) and (CurrentSpeed <= 60) then
PowerValue = PARALLELRESIST2 * (1 - (( CurrentSpeed / 60) ^ 5 ))
end
if (MCvalue == EIGHTHNOTCH) and (CurrentSpeed > 60) then
PowerValue = IDLE
end
if (MCvalue == NINTHNOTCH) and (CurrentSpeed <=65) then
PowerValue = PARALLELRESIST3 * (1 - (( CurrentSpeed / 65) ^ 5 ))
end
if (MCvalue == TENTHNOTCH) and (CurrentSpeed > 65) then
PowerValue = IDLE
end
if (MCvalue == TENTHNOTCH) and (CurrentSpeed <= 75) then
PowerValue = PARALLEL * (1 - (( CurrentSpeed / 75) ^ 5 ))
end
if (MCvalue == TENTHNOTCH) and (CurrentSpeed > 75) then
PowerValue = IDLE
end
if (MCvalue == ELEVENTHNOTCH) and (CurrentSpeed <= 85) then
PowerValue = PARALLELTAP * (1 - (( CurrentSpeed / 85) ^ 5 ))
end
if (MCvalue == ELEVENTHNOTCH) and (CurrentSpeed > 85) then
PowerValue = IDLE
end
-- Determine damped acceleration/deceleration averaged over last 0.5 seconds. MPH/sec and Metres/sec.
-- Used for debug/tuning monitoring via the Control State Dialog display option.
gDampedAcceleration[gAccelerationindex] = (CurrentSpeed - gLastSpeed) / (CurrentTime - gLastTime)
gLastTime = CurrentTime
gLastSpeed = CurrentSpeed
gAccelerationindex = gAccelerationindex + 1
if (gAccelerationindex == 25) then
gAccelerationindex = 0
end
DampedValue = 0
for i=0, 24 do
DampedValue = DampedValue + gDampedAcceleration[i]
end
DampedValue = DampedValue / 25
if (gAccelerationindex == 1) then
Call( "*:SetControlValue", "AccelerationMPHsec", 0, DampedValue )
end
DampedValue = DampedValue * 0.447
if (gAccelerationindex == 1) then
Call( "*:SetControlValue", "AccelerationMetresSec", 0, DampedValue )
end
Call( "*:SetControlValue", "Regulator", 0, PowerValue )
Call( "*:SetControlValue", "Reverser", 0, REVvalue )
end
The main issue which I have encountered is that the code sends a value to the regulator when the dummy throttle is set at "OFFNOTCH", "SIXTHNOTCH", and "ELEVENTHNOTCH". The power value equation works properly at these 3 states, but none of the other states do anything to adjust the regulator. I'm a bit lost here on how to proceed. Any assistance and/or guidance would be appreciated.
