Lua Scripting
Posted: Thu Jul 08, 2021 7:40 pm
Code: Select all
function onTick()
auxV = getAuxAnalog(0)
print('Hello analog ' .. auxV)
end
Code: Select all
t = Timer.new();
function onTick()
auxV = getAuxAnalog(0)
if auxV > 2 then
t.reset(t);
end
val = t.getElapsedSeconds(t);
print('Hello analog ' .. auxV .. " " .. val)
end
2021-09-18_17_29_36_863: EngineState: LUA: Tearing down instance...
2021-09-18_17_29_36_864: EngineState: LUA loading script length: 193...
2021-09-18_17_29_36_865: EngineState: LUA ERROR loading script: not enough memory
2021-09-18_17_29_36_865: EngineState: LUA: Tearing down instance...
2021-09-18_17_29_36_867: EngineState: Writing pending configuration...
Code: Select all
t = Timer.new();
function onTick()
auxV = getAuxAnalog(0)
if auxV > 2 then
t:reset();
end
val = t:getElapsedSeconds();
print('Hello analog ' .. auxV .. " " .. val)
end
Code: Select all
startPwm(0, 800, 0.1)
-- direction
startPwm(1, 80, 1.0)
-- disable
startPwm(2, 80, 0.0)
pid = Pid.new()
pid:setP(2)
pid:setMinValue(-100)
pid:setMaxValue(100)
biasCurveIndex = findCurveIndex("bias")
function onTick()
local targetVoltage = getAuxAnalog(0)
local target = interpolate(1, 0, 3.5, 100, targetVoltage)
-- clamp 0 to 100
target = math.max(0, target)
target = math.min(100, target)
print('Decoded target: ' .. target)
local tps = getSensor("TPS1")
tps = (tps == nil and 'invalid TPS' or tps)
print('Tps ' .. tps)
pid:setTarget(target)
local output = pid:get(tps)
local bias = curve(biasCurveIndex, target)
print('bias ' .. bias)
print('pid output ' .. output)
local duty = (bias + output) / 100
isPositive = duty > 0;
pwmValue = isPositive and duty or -duty
setPwmDuty(0, pwmValue)
dirValue = isPositive and 1 or 0;
setPwmDuty(1, dirValue)
print('pwm ' .. pwmValue .. ' dir ' .. dirValue)
print('')
end
Code: Select all
txPayload = {}
function onTick()
auxV = getAuxAnalog(0)
print('Hello analog ' .. auxV )
-- first byte: integer part, would be autoboxed to int
txPayload[1] = auxV
-- second byte: fractional part, would be autoboxed to int, overflow would be ignored
txPayload[2] = auxV * 256;
auxV = getAuxAnalog(1)
print('Hello analog ' .. auxV )
txPayload[3] = auxV
txPayload[4] = auxV * 256;
auxV = getAuxAnalog(2)
print('Hello analog ' .. auxV )
txPayload[5] = auxV
txPayload[6] = auxV * 256;
txCan(1, 0x600, 1, txPayload)
Code: Select all
startPwm(0, 800, 0.1)
-- direction
startPwm(1, 80, 1.0)
-- disable
startPwm(2, 80, 0.0)
pid = Pid.new(2, 0, 0, -100, 100)
biasCurveIndex = findCurveIndex("bias")
canRxAdd(0x600)
voltageFromCan = 0
function onCanRx(bus, id, dlc, data)
print('got CAN id=' .. id .. ' dlc=' .. dlc)
voltageFromCan = data[2] / 256.0 + data[1]
end
function onTick()
local targetVoltage = getAuxAnalog(0)
-- local target = interpolate(1, 0, 3.5, 100, targetVoltage)
local target = interpolate(1, 0, 3.5, 100, voltageFromCan)
-- clamp 0 to 100
target = math.max(0, target)
target = math.min(100, target)
print('Decoded target: ' .. target)
local tps = getSensor("TPS1")
tps = (tps == nil and 'invalid TPS' or tps)
print('Tps ' .. tps)
local output = pid:get(target, tps)
local bias = curve(biasCurveIndex, target)
print('bias ' .. bias)
local duty = (bias + output) / 100
isPositive = duty > 0;
pwmValue = isPositive and duty or -duty
setPwmDuty(0, pwmValue)
dirValue = isPositive and 1 or 0;
setPwmDuty(1, dirValue)
print('pwm ' .. pwmValue .. ' dir ' .. dirValue)
print('')
end
MRE is indeed set to allow less memory usage than Proteus for the Lua VM. I've just made a PR to increase the allocation for both MRE and Proteus.mtmotorsport wrote: ↑Sun Feb 27, 2022 10:00 pmIs there a significant memory difference between MRE and Proteus? Reason for asking is my own relatively simple script and most of the example scripts cause a error loading script : not enough memory. Or is this something addressed with firmware? I tried the release firmware dated 2022.02.18 for MRE
Code: Select all
setTickRate(1)
local cm = 0
function onTick()
if cm == 0 then
txCan(1, 0x14EF1E11, 1, { 0x06, 0x6E, 0x0E, 0x6E, 0x2E, 0x6D, 0x6D, 0xFF })
txCan(1, 0x14EF1E11, 1, { 0x07, 0x6E, 0x6E, 0x6E, 0x1C, 0x2C, 0x6E, 0xFF })
txCan(1, 0x14EF1E11, 1, { 0x00, 0x01, 0xFF, 0x00, 0xFE, 0xFF, 0x00, 0xFF })
txCan(1, 0x14EF1E11, 1, { 0x00, 0x02, 0xFF, 0X00, 0xFE, 0xFF, 0x04, 0xFF })
txCan(1, 0x14EF1E11, 1, { 0x00, 0x03, 0x32, 0x01, 0xFC, 0xFF, 0x04, 0xFF })
print('hello can ')
cm = 1
else
print('hello can sent')
end
end
One day we should simply adjust MRE for F7 or H7 chips but that's not going to happen in 2022. On microRusEFI you cannot simply swap F4 for F7/H7 unfortunatellymtmotorsport wrote: ↑Thu Mar 03, 2022 7:06 pmI see the latest firmware has 25k of Lua memory available! That's great.
I can't see to find where whatever libraries are in use are listed.
This is the reference for the functions we've added: https://github.com/rusefi/rusefi/wiki/Lua-Scriptingmtmotorsport wrote: ↑Thu Mar 03, 2022 7:06 pmI can't see to find where whatever libraries are in use are listed. I'm a little new to Lua and just looking for ways to optimize my code.
Driver Throttle Intent is a synthetic sensor that's simply multiplexed to either throttle position or pedal position, based on whether you have electronic throttles or not. There were places in the firmware that we needed to determine "is the driver pushing the pedal" not just "is the throttle open", so we added that fake sensor to map to whatever sensor is connected to the driver's foot. In your case, TPS means the actual throttle plate, AcceleratorPedal means the thing inside the cabin the human touches, and DriverThrottleIntent is a copy of AcceleratorPedal.Zeiss wrote: ↑Fri Mar 04, 2022 5:10 pmIn the list of sensors there is a sensor called DriverThrottleIntent. What is the unit of this sensor, is it percent, is it angle, what is it? And it is the correct sensor, which I need or should I use AcceleratorPedal instead?
Thank you very much for this explanation! I will then use the AcceleratorPedal sensor because EGS and ASC/MSR want a signal from the accelerator pedal, not the TPS.mck1117 wrote: ↑Fri Mar 04, 2022 8:53 pmDriver Throttle Intent is a synthetic sensor that's simply multiplexed to either throttle position or pedal position, based on whether you have electronic throttles or not. There were places in the firmware that we needed to determine "is the driver pushing the pedal" not just "is the throttle open", so we added that fake sensor to map to whatever sensor is connected to the driver's foot. In your case, TPS means the actual throttle plate, AcceleratorPedal means the thing inside the cabin the human touches, and DriverThrottleIntent is a copy of AcceleratorPedal.Zeiss wrote: ↑Fri Mar 04, 2022 5:10 pmIn the list of sensors there is a sensor called DriverThrottleIntent. What is the unit of this sensor, is it percent, is it angle, what is it? And it is the correct sensor, which I need or should I use AcceleratorPedal instead?
So I have to divide this value by 100 and put them into setPwmDuty.
I'm able to add all 25 txCan config messages required, and then the txCan messages required for my PDM to turn on the fuel pumps, rad fans, ignition coils, etc. Where I seem to run out of memory is trying to read can message, compare two bytes, compile can message data packet and then tx this new can message. But I think it must be my lack of knowledge, because available memory drops by 10k trying to do this.AndreyB wrote: ↑Fri Mar 04, 2022 2:17 amOne day we should simply adjust MRE for F7 or H7 chips but that's not going to happen in 2022. On microRusEFI you cannot simply swap F4 for F7/H7 unfortunatellymtmotorsport wrote: ↑Thu Mar 03, 2022 7:06 pmI see the latest firmware has 25k of Lua memory available! That's great.
I can't see to find where whatever libraries are in use are listed.
We only have math library attached at the moment that's because Lua libraries are pretty heavy on F4 we would probably not have more of these.
https://github.com/rusefi/rusefi/issues/3970 is the best hope for Lua MRE progress but not a huge priority. Let's see if you can get your stuff to work with current setup.
Code: Select all
-- READ CAN SENSOR
if id == 351211806 then
id1 = data [1]
if id1 == 128 then
tt1 = data[6] * 256.0 + data[5]
end
end
function transTemp ()
print("Trans Sensor---" ..tt1)
temp = findCurveIndex("gmtemp")
local tt = curve(temp, tt1)
print("Trans Temp?------------" ..tt)
end