Lua Scripting

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

Lua Scripting

Post by AndreyB »

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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.
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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...
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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 408 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
				
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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?
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
User avatar
AndreyB
Site Admin
Posts: 12185
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
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
mtmotorsport
Posts: 2
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: 12185
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 110 times
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
nmstec
contributor
contributor
Posts: 35
Joined: Tue Oct 05, 2021 9:02 pm

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 90 times
User avatar
AndreyB
Site Admin
Posts: 12185
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.
Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions
my skype is arro239
Post Reply