How to Control your Megasquirt Outputs with Lua and canbus

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
Canyonfive
Posts: 69
Joined: Mon Jan 29, 2018 11:20 pm

How to Control your Megasquirt Outputs with Lua and canbus

Post by Canyonfive »

I thought this might be helpful, you can control the Programmable On/Off outputs as well as the PWM outputs on a mega squirt using your RaceCapture though Canbus.
Here are the outputs im referencing.
Image

On the Racecapture side, the possibilities are only limited by your imagination. the high level view is you will be sending an integer (0-255) over canbus to the Megasquirt and mapping it to a generic sensor. Which you can then use to control your outputs. If you using the PWM you could send over 2 and use one for each axis. Below is an example Lua for turning on a programmable On/Off output on the megasquirt when your speed is above 1 MPH, manifold pressure is below 150kpa, and coolant is above 160 deg F. With two conditions you can just use the Megasquirt but with 3 you either have to do loops or this.

setTickRate(10)
function onTick()
output = 0
local S = getChannel("Speed")
local M = getChannel("MAP")
local C = getChannel("Coolant")
if S > 1 and M < 150 and C > 160
then output = 1 end
msg = {output} --format output as a canbus message
txCAN(0, 1000, 0, msg)
end
--as a reminder, to save space this can be shortened, always be thinking about that...

So when the conditions are right send a 1 to the megasquirt on ID (in decimal) 1000 with std 11bit headers.


On the Megasquirt side, you'll need to set the Can receiving to receive on whatever ID you use (1000 in the example) with a size of 1U. I map them to the CANadc channels
Photos on this thread of some of these screens >>https://www.roadraceautox.com/img/Canyo ... ontrol.jpg

then under generic Sensors, map a sensor using the Canadc you chose as the input.

then use that sensor you made as the input for an output. When sensor = 1, then activate (choose your port).

You can control the PWM outputs the same way, either as a table or a curve, with two sensors.

brentp
Site Admin
Posts: 6046
Joined: Wed Jan 24, 2007 6:36 am

Re: How to Control your Megasquirt Outputs with Lua and canbus

Post by brentp »

Very cool. thanks for sharing!

Post Reply