Megasquirt ECU: Loops

Ever wanted to turn on an output based on more than the two conditions allowed using the programmable on/off outputs?

The loop funtion are software programmable on/off conditions that can be linked together for more complex control of hardware outputs or other things like table switching etc.
Unfortunately is only applicable to Megasquirt 3 based controllers.

The loop outputs are located in the ‘Programmable On/Off Outputs 2’ menu. In this example I’ll go through setting them up to turn on two radiator fans based on a number of conditions.

Firstly  the conditions for each fan to turn on need to be determined.

Capture.PNG

Thermofan 1 requires engine coolant temperature to be above 97degC or PWM_DutyA to be greater than 20% while the engine is running (RPM>200) or the intake air temperature to be above 40degC while the engine is running (RPM>200) to turn the fan on.
Thermofan 2 requires the engine coolant temperature to be above 105degC or manifold pressure to be above 110kPa, or intake air temperature to be above 35degC while the engine is running (RPM>200) to turn on.

Next these conditions need to be added to different loops.

Capture

When using the loops both the threshold and hysteresis should be set to the loop number. Each loop state is stored into a single byte (8 bits) so the value to use for each loop is as follows:

Capture

So if only loop 7 is active the byte in binary is 0100 0000 which in decimal is 64. If loop 7 and loop 4 are active then it is 0100 1000 which in decimal is 72 (64+8).

Loop 1: 2^0 = 1
Loop 2: 2^1 = 2
Loop 3: 2^2 = 4
Loop 4: 2^3 = 8
Loop 5: 2^4 = 16
Loop 6: 2^5 = 32
Loop 7: 2^6 = 64
Loop 8: 2^7 = 128

So if you wanted to turn on the output when loop 6 is active, the threshold and hysteresis should be set to 32. It is also possible to turn on the output based on two or more of the loops by adding the values together. For example if you wanted to turn on an ouput when both loop2 and loop 4 are true, then the threshold and hysteresis should be set to 2+8=10. If you wanted to turn on an output when loop4 is true and loop5 is false then the threshold is set to 8+16=24 and hysteresis set to 8.

The physical output port 1 runs off Loop1 however the loop ends at Loop2 due to the AND condition so Loop4 would never run. An OR condition needs to be added to the output to run loop 4.

Final settings in Tunerstudio shown below.

1234567