Page 1 of 2

Lua Scripting

Posted: Thu Jul 08, 2021 7:40 pm
by AndreyB

Re: Lua Scripting

Posted: Thu Jul 08, 2021 7:41 pm
by AndreyB
I assume that at some point FSIO would be completely removed? https://github.com/rusefi/rusefi/issues/2928

Re: Lua Scripting

Posted: Tue Aug 31, 2021 4:37 pm
by AndreyB
New method: https://github.com/rusefi/rusefi/wiki/Lua-Scripting#readpinpinname

Working on Lua auxiliary analog inputs at the moment.

Re: Lua Scripting

Posted: Sat Sep 18, 2021 9:14 pm
by AndreyB
the following works now

Code: Select all

function onTick()
   auxV = getAuxAnalog(0)
   print('Hello analog ' .. auxV)
end

Re: Lua Scripting

Posted: Sat Sep 18, 2021 9:29 pm
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...

Re: Lua Scripting

Posted: Sat Sep 18, 2021 9:33 pm
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

Re: Lua Scripting

Posted: Fri Nov 12, 2021 9:54 pm
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 31901 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
				

Re: Lua Scripting

Posted: Fri Nov 12, 2021 10:05 pm
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?

Re: Lua Scripting

Posted: Sun Nov 14, 2021 1:07 pm
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

Re: Lua Scripting

Posted: Mon Nov 15, 2021 7:17 pm
by mtmotorsport
Very excited about this

Re: Lua Scripting

Posted: Thu Nov 25, 2021 3:53 pm
by AndreyB
look at that new button!
image.png
image.png (3.63 KiB) Viewed 31603 times

Re: Lua Scripting

Posted: Thu Nov 25, 2021 8:42 pm
by nmstec
It needs fixing. Its putting spaces between all characters. Seen in pic: "0 .02" "every50msTimer : getElapsedSeconds()"

Re: Lua Scripting

Posted: Fri Nov 26, 2021 1:33 am
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.

Re: Lua Scripting

Posted: Sun Feb 27, 2022 10:00 pm
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

Re: Lua Scripting

Posted: Sun Feb 27, 2022 10:40 pm
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

Re: Lua Scripting

Posted: Sun Feb 27, 2022 10:40 pm
by mck1117
Also, if you're willing to share your script somewhere, I can make some suggestions about how to reduce memory usage.

Re: Lua Scripting

Posted: Mon Feb 28, 2022 12:31 am
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

Re: Lua Scripting

Posted: Mon Feb 28, 2022 5:57 am
by AndreyB

Re: Lua Scripting

Posted: Thu Mar 03, 2022 7:06 pm
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.

Re: Lua Scripting

Posted: Fri Mar 04, 2022 2:17 am
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.

Re: Lua Scripting

Posted: Fri Mar 04, 2022 2:19 am
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/

Re: Lua Scripting

Posted: Fri Mar 04, 2022 5:10 pm
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?

Re: Lua Scripting

Posted: Fri Mar 04, 2022 8:53 pm
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.

Re: Lua Scripting

Posted: Fri Mar 04, 2022 9:08 pm
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!

Re: Lua Scripting

Posted: Fri Mar 04, 2022 10:22 pm
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.

Re: Lua Scripting

Posted: Fri Mar 04, 2022 10:48 pm
by AndreyB
plz paste your complete script on your build thread

Re: Lua Scripting

Posted: Tue Mar 15, 2022 3:13 pm
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?

Re: Lua Scripting

Posted: Sat Mar 26, 2022 3:55 pm
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

Re: Lua Scripting

Posted: Tue Jun 28, 2022 3:42 am
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

Re: Lua Scripting

Posted: Tue Jun 28, 2022 3:58 am
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