Lua Scripting

It's all about the code!
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Lua Scripting

Post by AndreyB »

Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

I assume that at some point FSIO would be completely removed? https://github.com/rusefi/rusefi/issues/2928
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

New method: https://github.com/rusefi/rusefi/wiki/Lua-Scripting#readpinpinname

Working on Lua auxiliary analog inputs at the moment.
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

the following works now

Code: Select all

function onTick()
   auxV = getAuxAnalog(0)
   print('Hello analog ' .. auxV)
end
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

here's the most advanced rusEFI Lua script I am currently capable of:

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
there are two problems
1) "t.reset(t);" mentioning "t" twice is a bit weird

and more importantly
2) if I attempt to change "2" into "2.5" we get
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...
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

Apparently @mck1117 knows Lua syntax, no more mentioning same field twice.

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
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

Some Lua progress:
We now have "Pid" class (todo make syntax less clunky

We now have six named curves - the idea is that one does not have to use a piece of paper to track magic curve indices
Similar names for tables are coming. I am still deciding how to approach input and output names best - I am not a huge fan of magic indices anywhere.
image.png
image.png (33.63 KiB) Viewed 30505 times
here's the my most advanced script to date:

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
				
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

One open question is indexing convention.

In TS we clearly go curves 1 to 6 and tables 1 to 4, not 0 to 5 and 0 to 3.
In addition Lua array indexing is from 1

With that it's obvious to me that findCurveIndex has to return 1-base index. As a consequence, curve function would have to accept same 1-base index?
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

A demo of where rusEFI Lua is today (hint: it's pretty cool nowadays)



Small microRusEFI runs

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)
larger Proteus runs

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
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

Very excited about this
Matt
MT Motorsport / EcotecMiata
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

look at that new button!
image.png
image.png (3.63 KiB) Viewed 30207 times
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
nmstec
contributor
contributor
Posts: 124
Joined: Tue Oct 05, 2021 9:02 pm
Location: Vancouver

Re: Lua Scripting

Post by nmstec »

It needs fixing. Its putting spaces between all characters. Seen in pic: "0 .02" "every50msTimer : getElapsedSeconds()"
Attachments
IMG_20211125_115507.jpg
IMG_20211125_115507.jpg (6.16 MiB) Viewed 30187 times
"Dave B. 5:03 PM
Mark is an ass but by far the most potent combination of knowledgeable ass, smart ass, get it done ass and determined ass. and his ass consistently puts in time."

-Dave B, Hero, Tuner, and probably has a car.
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

nmstec wrote:
Thu Nov 25, 2021 8:42 pm
It needs fixing. Its putting spaces between all characters. Seen in pic: "0 .02" "every50msTimer : getElapsedSeconds()"
Thank you for the bug report! This should be fixed now.
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

Is 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
Matt
MT Motorsport / EcotecMiata
mck1117
running engine in first post
running engine in first post
Posts: 1493
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: Lua Scripting

Post by mck1117 »

mtmotorsport wrote:
Sun Feb 27, 2022 10:00 pm
Is 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
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.

https://github.com/rusefi/rusefi/pull/3969
mck1117
running engine in first post
running engine in first post
Posts: 1493
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: Lua Scripting

Post by mck1117 »

Also, if you're willing to share your script somewhere, I can make some suggestions about how to reduce memory usage.
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

This is what is currently loaded and it consumes 73% (10974/15000 bytes). Its a bit of a mess as I've been chopping and modifying it all day trying to get it to run. The print portion is purely to see that it runs.

Ideally I need it to send about 25 can messages once on boot, and ~6 at 1hz thereafter.

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
Matt
MT Motorsport / EcotecMiata
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

I 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. I'm a little new to Lua and just looking for ways to optimize my code.
Matt
MT Motorsport / EcotecMiata
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

mtmotorsport wrote:
Thu Mar 03, 2022 7:06 pm
I 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.
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 unfortunatelly

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.
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
mck1117
running engine in first post
running engine in first post
Posts: 1493
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: Lua Scripting

Post by mck1117 »

mtmotorsport wrote:
Thu Mar 03, 2022 7:06 pm
I 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.
This is the reference for the functions we've added: https://github.com/rusefi/rusefi/wiki/Lua-Scripting

This is the language reference for the "base language": https://www.lua.org/manual/5.4/
User avatar
Zeiss
Posts: 110
Joined: Tue Oct 05, 2021 6:32 pm
Location: Germany
Github Username: HeinrichG-V12

Re: Lua Scripting

Post by Zeiss »

Hello Matt,

I will use a very small Lua script on my M70 to provide "driver requirement" for ASC/MSR and EGS as PWM signal. See this (below at Signal = DKV) and this (pseudo code! not quite finished yet).

In 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?
Regards,
Heinrich

My GitHub
mck1117
running engine in first post
running engine in first post
Posts: 1493
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: Lua Scripting

Post by mck1117 »

Zeiss wrote:
Fri Mar 04, 2022 5:10 pm
In 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?
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.

All of the TPS-related channels output 0-100, 0 indicating closed throttle, and 100 indicating fully open.
User avatar
Zeiss
Posts: 110
Joined: Tue Oct 05, 2021 6:32 pm
Location: Germany
Github Username: HeinrichG-V12

Re: Lua Scripting

Post by Zeiss »

mck1117 wrote:
Fri Mar 04, 2022 8:53 pm
Zeiss wrote:
Fri Mar 04, 2022 5:10 pm
In 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?
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.
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 pm
All of the TPS-related channels output 0-100, 0 indicating closed throttle, and 100 indicating fully open.
So I have to divide this value by 100 and put them into setPwmDuty.

Thank you!
Regards,
Heinrich

My GitHub
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

AndreyB wrote:
Fri Mar 04, 2022 2:17 am
mtmotorsport wrote:
Thu Mar 03, 2022 7:06 pm
I 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.
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 unfortunatelly

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.
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.
Matt
MT Motorsport / EcotecMiata
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

plz paste your complete script on your build thread
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

Someone has asked privately if PWM input is supported by Lua. Nope, not at the moment - it's probably possible just not implemented. RC filter and analog input could be a work around for now?
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

AndreyB wrote:
Fri Mar 04, 2022 10:48 pm
plz paste your complete script on your build thread
Done, and made a thread for another MRE Success

https://rusefi.com/forum/viewtopic.php?f=2&t=2294
Matt
MT Motorsport / EcotecMiata
mtmotorsport
Posts: 89
Joined: Wed Mar 31, 2021 10:00 pm

Re: Lua Scripting

Post by mtmotorsport »

Another Lua question. This one is not specific to my car so perhaps this can help others out as well.

rxCan data received and printing as expected in console. What we would like to do is use this value from CAN message to look up another value in a Script Curve then txCan that looked up value.

I've attached screenshots, and the code. Any pointers?

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
Attachments
TS name curve.png
TS name curve.png (221.75 KiB) Viewed 23754 times
RusEFI runing.png
RusEFI runing.png (178.17 KiB) Viewed 23754 times
Matt
MT Motorsport / EcotecMiata
User avatar
AndreyB
Site Admin
Posts: 14292
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: Lua Scripting

Post by AndreyB »

thank you for the report. latest firmware now asserts that X bins are in ascending order on start up

https://github.com/rusefi/rusefi/commit/95c3b0bca299fd9a5d888bf587be40e3a951db4a
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
Post Reply