rusefi_documentation/Lua-Scripting.md

646 lines
19 KiB
Markdown
Raw Permalink Normal View History

2022-01-25 17:01:06 -08:00
# Lua Scripting
## Introduction
2024-02-20 10:10:40 -08:00
rusEFI strives to offer users as much flexibility as possible, to provide a completely user-defined control strategy for both primary and auxiliary actuators.
2023-01-03 04:01:31 -08:00
## Basics
2021-04-29 10:57:27 -07:00
rusEFI provides a number of hooks to interface with the firmware and to manipulate its state and read/write the current configuration.
2024-01-19 14:57:34 -08:00
- Hooks for CAN bus communications; see [CAN bus](#can-bus).
- Inputs from sensors can be read directly; see [Input](#input).
- ECU general purpose outputs see [Output](#output).
- Aspects of the engine can be controlled directly; see [Engine Control](#engine-control).
- ECU Configurations can be accessed via the [`getCalibration()`](#getcalibrationname) hook, and manipulated via the [`setCalibration()`](#setcalibrationname-value-needevent) hook.
2023-01-05 13:08:11 -08:00
- Configuration names are dynamically updated to match the current firmware; see here for the current list: [https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/value_lookup_generated.md](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/value_lookup_generated.md).
2024-01-19 14:57:34 -08:00
- ECU internal state, i.e. logic outputs from the firmware can be read via the universal [`getOutput()`](#getoutputname) hook, and some can be altered via correspondingly named hooks i.e. `setOutputName()` where `OutputName` is name of the output, e.g. [`setClutchUpState()`](#setclutchupstatevalue). See also: [Output](#output).
2023-01-05 13:08:11 -08:00
- Output names are dynamically updated to match the current firmware; see here for the current list: [https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/output_lookup_generated.cpp](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/output_lookup_generated.cpp).
- Hooks to read values from SENT sensors; see [SENT protocol](#sent-protocol).
- A set of useful routines is provided; see [Utility](#utility).
2021-07-16 14:13:19 -07:00
Some example uses are provided in [Examples](#examples).
2021-07-16 14:13:19 -07:00
2021-04-29 10:57:27 -07:00
## Conventions
- The Lua interpreter will trigger an error if there is a mistake in the program, check the rusEFI console to see errors and script output.
2021-05-04 02:38:24 -07:00
- Unless otherwise mentioned, all `index` parameters start with the first element at index at 0.
2021-04-29 10:57:27 -07:00
## Writing Your Script
The entire Lua script is read at startup, then a script function called `onTick` is called periodically by rusEFI.
2021-04-29 10:57:27 -07:00
Here is a simple script you can run to illustrate this behavior:
2023-01-03 04:01:31 -08:00
```LUA
2021-04-29 10:57:27 -07:00
print('Hello Lua startup!')
function onTick()
print('Hello onTick()')
end
```
### Controlling the Tick Rate
2022-02-10 16:59:41 -08:00
The function `setTickRate(hz)` can be used to configure how often rusEFI calls the `onTick` function. If your script does a lot of work in the `onTick()` function it may run slower than the desired rate. Since the Lua virtual machine runs at low priority compared to other functions of the ECU, it is impossible to swamp the ECU with too much Lua work, so set the tick rate to whatever is necessary. `onCanRx` runs at the same rate as `onTick`
2021-04-29 10:40:07 -07:00
2023-01-03 04:01:31 -08:00
```LUA
2021-04-29 11:51:56 -07:00
n = 0
setTickRate(5) --set tick rate to 5hz
function onTick()
print('Hello Lua: ' ..n)
n = n + 1
end
```
### Editing Scripts
To ease editing a LUA script an editor that supports Language Server Protocol (LSP) is highly recommended.
For an option see [LuaLS/lua-language-server](https://github.com/LuaLS/lua-language-server#install)
## Hooks/Function Reference
2021-04-29 10:40:07 -07:00
2023-01-03 04:01:31 -08:00
### User Settings
2021-11-14 08:43:12 -08:00
2023-01-03 04:01:31 -08:00
#### `getOutput(name)`
2021-12-15 17:28:29 -08:00
For example ``getOutput("clutchUpState")`` ``getOutput("brakePedalState")``
See [https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/output_lookup_generated.cpp](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/output_lookup_generated.cpp) for output names.
2021-12-15 17:28:29 -08:00
2023-01-03 04:01:31 -08:00
#### `setClutchUpState(value)`
2021-12-15 17:31:23 -08:00
2023-01-03 04:01:31 -08:00
#### `setBrakePedalState(value)`
2021-12-15 17:31:23 -08:00
2022-07-07 21:41:13 -07:00
Use setBrakePedalState to tell rusEFI about CAN-based brake pedal.
2023-01-03 04:01:31 -08:00
#### `setAcRequestState(value)`
2022-07-07 21:41:13 -07:00
Use setAcRequestState to tell rusEFI about CAN-based A/C request.
2023-01-03 04:01:31 -08:00
#### `setEtbDisabled(value)`
2023-01-02 15:05:34 -08:00
2023-01-03 04:01:31 -08:00
#### `setIgnDisabled(value)`
2023-01-02 15:05:34 -08:00
2023-01-03 11:57:32 -08:00
'setIgnDisabled' function for all kinds of cranking safety systems
#### `setAcDisabled(value)`
2022-04-08 22:13:43 -07:00
2023-01-03 04:01:31 -08:00
Disable/suppress A/C functionality regardless of what and how enables it, an override kind of deal.
2022-07-07 19:36:24 -07:00
#### `getTimeSinceAcToggleMs()`
2022-04-08 22:13:43 -07:00
2023-01-03 04:01:31 -08:00
#### `getCalibration(name)`
2021-12-15 10:15:05 -08:00
2023-01-05 07:57:43 -08:00
Gets current calibration value for specified scalar setting ``name``. For example ``getCalibration("cranking.rpm")``
2021-12-15 10:15:05 -08:00
2023-01-05 13:08:11 -08:00
For complete list of possible calibration names (valid parameter values) and descriptions see [https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/value_lookup_generated.md](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/generated/value_lookup_generated.md).
2021-12-15 10:15:05 -08:00
2023-01-03 04:01:31 -08:00
#### `setCalibration(name, value, needEvent)`
2021-12-15 10:15:05 -08:00
Sets specified calibration setting to specified value. Fires calibration change event depending on needEvent parameter.
For example `setCalibration("cranking.rpm", 900, false)`
2021-12-15 10:15:05 -08:00
2024-01-01 10:57:09 -08:00
#### `burnconfig`
2024-01-01 10:35:58 -08:00
2024-03-26 12:35:39 -07:00
Schedule calibration write to flash once the engine is stopped.
2024-01-01 10:35:58 -08:00
2023-01-03 04:01:31 -08:00
#### `findSetting(name, defaultValue)`
2023-01-02 11:22:23 -08:00
2021-11-14 08:43:12 -08:00
Find User Setting with specified name and returns numeric value. Useful when script developer and script consumer are
different people, also useful while Lua script editing is available only in TS.
- Parameters
2022-07-27 00:19:23 -07:00
- `name`: Variable name, as in corresponding 'name' field in configuration
2023-01-03 04:01:31 -08:00
- `defaultValue`: value to use if specified setting not located by name
2023-01-03 04:01:31 -08:00
### Engine Control
2021-09-24 06:40:26 -07:00
#### `stopEngine()`
2021-11-26 06:29:55 -08:00
#### `setSparkSkipRatio(ratio)`
2021-11-29 19:33:12 -08:00
setSparkSkipRatio(0) to skip 0% of the ignition events, i.e. no skipping
2023-04-16 18:04:56 -07:00
setSparkSkipRatio(0.5) would skip half of ignition events. We never skip two consecutive ignitions. #torque
2021-11-29 19:33:12 -08:00
#### `setSparkHardSkipRatio(ratio)`
setSparkHardSkipRatio(0) to skip 0% of the ignition events, i.e. no skipping
setSparkHardSkipRatio(0.75) would skip 75% of ignition events. #torque
#### `setIdleAdd(percent)`
2022-08-20 21:35:00 -07:00
Percent to add to idle (incl. open loop).
#### `setFuelAdd(amount)`
2023-01-02 11:22:23 -08:00
2022-09-18 22:00:22 -07:00
Sorry not finished :(
2022-07-14 10:07:49 -07:00
Amount of fuel mass to add to injection, scaled by fuel multiplier ([`setFuelMult()`](#setfuelmultcoeff)); initially 0.
#### `setFuelMult(coeff)`
2023-01-02 11:22:23 -08:00
2022-09-18 22:00:22 -07:00
Sorry not finished :(
2022-07-14 10:07:49 -07:00
Amount to scale added fuel mass by; initially 1.0;
#### `setBoostTargetAdd(amount)`
2023-01-02 11:22:23 -08:00
2022-07-14 10:07:49 -07:00
Additive for closed loop target boost pressure.
#### `setBoostTargetMult(coeff)`
2023-01-02 11:22:23 -08:00
2022-07-14 10:07:49 -07:00
Multiplier for closed loop target boost pressure.
#### `setBoostDutyAdd(amount)`
Additive for open loop target boost pressure.
2023-01-03 04:01:31 -08:00
#### `setTimingAdd(angle)`
2023-01-02 11:22:23 -08:00
2024-02-17 09:56:39 -08:00
Use negative values to retard timing.
2021-09-24 06:40:26 -07:00
2023-01-03 04:01:31 -08:00
#### `setTimingMult(coeff)`
2021-09-24 06:40:26 -07:00
2023-04-16 18:04:56 -07:00
todo add details but ready to test! #torque
2021-09-24 06:40:26 -07:00
#### `setEtbAdd(percent)`
2021-12-02 06:20:53 -08:00
Amount of ETB to add, as a percent of the wide-open value: e.g. `10` for +10%. The value is a static amount to add to
2023-04-16 18:04:56 -07:00
the determined value, e.g. TPS of 5% w/ `10` results in 15% ETB. #torque
2021-12-02 06:20:53 -08:00
### Timer
``yourTimer = Timer.new();`` to have a new variable of Timer type
#### reset
``yourTimer:reset();`` to reset timer
#### getElapsedSeconds
``yourTimer:getElapsedSeconds();`` to get number of seconds since timer was reset
2023-01-03 04:01:31 -08:00
### CAN bus
2023-01-03 04:01:31 -08:00
#### `enableCanTx(isEnabled)`
enabled by default
use enableCanTx(false) to suppress CAN TX
2023-01-03 04:01:31 -08:00
#### `txCan(bus, ID, isExt, payload)`
2023-01-02 11:22:23 -08:00
2022-01-05 10:48:30 -08:00
- Parameters
2022-07-27 00:19:23 -07:00
- bus: hardware CAN bus index, only '1' on most rusEFI boards, '1' or '2' on Proteus
- isExt: 0 for 11 bit mode
2022-01-05 10:48:30 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAdd(id)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAdd(bus, id)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAdd(id, callback)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAdd(bus, id, callback)`
2022-07-27 00:18:05 -07:00
2023-01-03 04:01:31 -08:00
#### `canRxAddMask(id, mask)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAddMask(bus, id, mask)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAddMask(id, mask, callback)`
2023-01-02 11:22:23 -08:00
2023-01-03 04:01:31 -08:00
#### `canRxAddMask(bus, id, mask, callback)`
2022-07-27 00:18:05 -07:00
- Parameters
2022-07-27 00:19:23 -07:00
- id: CAN ID to listen to.
2022-08-21 20:14:46 -07:00
- mask: Apply a mask to the received ID before comparing to the `id` parameter. For example, passing an id of `3` and mask of 0xFF will match any frame whose last 8 bits match `3`. If omitted, no masking is applied before comparison, so only a single CAN ID will be received. Use the mask to subscribe to multiple messages with similar IDs with a single call to `canRxAddMask`.
2022-08-21 20:18:10 -07:00
- bus: Hardware CAN bus index, only '1' on most rusEFI boards, '1' or '2' on Proteus. If this parameter is omitted, messages will be received from any bus.
2022-07-27 00:19:23 -07:00
- callback: A the callback function to call when the specified ID is received. If this parameter is not passed, the default function `onCanRx` will be used.
2022-07-27 00:25:26 -07:00
Your CAN RX callback should look like this:
```lua
function onCanRx(bus, id, dlc, data)
-- Do things with CAN data!
end
```
### SENT protocol
#### `getSentValue(index)`
TODO: document parameters, response
#### `getSentValues(index)`
TODO: document parameters, response
2024-01-21 05:24:04 -08:00
### PID
2024-02-12 14:07:18 -08:00
deltaTime is measured automatically between current and previous "pid:get" invocation.
2024-01-21 05:30:01 -08:00
```language=lua
2024-01-21 05:24:04 -08:00
-- p, i, d, min, max
pid = Pid.new(2, 0, 0, -100, 100)
pid:setOffset(0.3)
pid:get(target, input)
pid:reset()
-- p, i, d, min, max
industrialPid = IndustrialPid.new(2, 0, 0, -100, 100)
industrialPid:setOffset(0.3)
industrialPid:setDerivativeFilterLoss(0.3)
industrialPid:setAntiwindupFreq(0.3)
industrialPid:get(target, input)
industrialPid:reset()
```
2023-01-03 04:01:31 -08:00
### Utility
2021-04-29 10:40:07 -07:00
2023-01-03 04:01:31 -08:00
#### `print(msg)`
2021-04-29 10:40:07 -07:00
Print a line of text to the ECU's log.
- Parameters
- `msg`: The message to print. Pass a string or number and it will be printed to the log.
- Returns
- none
2023-01-03 04:01:31 -08:00
#### `vin(index)`
2023-01-02 11:22:23 -08:00
Return VIN setting character at specified index
- Parameters
- index: zero-based index
2021-04-29 10:40:07 -07:00
#### Usage example
Program:
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2021-04-29 10:40:07 -07:00
n = 5.5
print('Hello Lua, number is: ' ..n)
```
2023-01-02 11:22:23 -08:00
2021-04-29 10:40:07 -07:00
Output:
`Hello Lua, number is 5.5`
2023-01-03 04:01:31 -08:00
#### `setTickRate(hz)`
2021-04-29 10:57:27 -07:00
2022-02-10 16:59:41 -08:00
Sets the rate at which rusEFI calls your `onTick` and `onCanRx` functions, in hz. On reset default is 10hz.
2021-04-29 10:57:27 -07:00
- Parameters
- `hz`: Desired tick rate, in hz. Values passed will be clamped to a minimum of 1hz, and maximum of 100hz.
- Returns
- none
2023-01-03 04:01:31 -08:00
#### `mcu_standby()`
2021-12-13 14:49:11 -08:00
Stops MCU.
2023-01-03 04:01:31 -08:00
#### `interpolate(x1, y1, x2, y2, x)`
2022-08-24 15:38:33 -07:00
Interpolates `x` placing it on the line defined by (x1, y1) and (x2, y2)
2023-01-03 04:01:31 -08:00
#### `findTableIndex(name)`
2023-01-02 11:22:23 -08:00
2022-07-14 04:31:10 -07:00
Find table index by specified human-readable name.
2023-01-03 04:01:31 -08:00
#### `table3d(tableIdx, x, y)`
2021-04-29 10:40:07 -07:00
Looks up a value from the specified Script Table.
2021-04-29 10:40:07 -07:00
- Parameters
- `tableIdx`: Index of the table to use. Currently 4 tables are supported, so indices 1, 2, 3, and 4 are valid.
2021-04-29 10:40:07 -07:00
- `x`: X-axis value to look up in the table (this is often RPM)
- `y`: Y-axis value to look up in the table (this is often load)
- Returns
- A number representing the value looked up from the table.
2023-01-03 04:01:31 -08:00
#### `findCurveIndex(name)`
2023-01-02 11:22:23 -08:00
2022-07-14 04:31:10 -07:00
Finds curve index by specific curve name
2023-01-03 04:01:31 -08:00
#### `curve(curveIdx, x)`
Looks up a value from the specified Script Curve.
2023-01-02 11:22:23 -08:00
- Parameters
- `tableIdx`: Index of the script to use, starting from 1.
- `x`: Axis value to look up in the table
2023-01-03 04:01:31 -08:00
#### `setDebug(index, value)`
2021-05-04 02:22:58 -07:00
Sets a debug channel to the specified value. Note: this only works when the ECU debug mode is set to `Lua`.
- Parameters
- `index`: the index of the debug channel to set, 1 thru 7 inclusive.
- `value`: the value to set the channel to
- Returns
- none
2023-01-03 04:01:31 -08:00
#### `mcu_standby`
2022-01-15 08:38:53 -08:00
Puts MCU into standby low current consumption mode.
2023-01-03 04:01:31 -08:00
### Input
2021-04-29 10:40:07 -07:00
2023-01-03 04:01:31 -08:00
#### `getSensor(name)`
2021-10-21 16:38:02 -07:00
2023-06-07 02:47:32 -07:00
Reads the specified sensor. For instance ``getSensor("AcceleratorPedal")``
2021-10-21 16:38:02 -07:00
- Parameters
2022-07-20 04:30:37 -07:00
- `name`: Name of the sensor to read. [A list of sensor names can be found here.](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/sensors/sensor_type.h)
2021-10-21 16:38:02 -07:00
- Returns
- A reading from the sensor, or `nil` if the sensor has a problem or isn't configured.
2023-01-03 04:01:31 -08:00
#### `getSensorByIndex(index)`
2021-04-29 10:40:07 -07:00
Reads the specified sensor.
- Parameters
- `index`: Index of the sensor to read. [A list of sensor indices can be found here.](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/sensors/sensor_type.h)
- Returns
- A reading from the sensor, or `nil` if the sensor has a problem or isn't configured.
2023-01-03 04:01:31 -08:00
#### `getSensorRaw(index)`
2021-04-29 10:40:07 -07:00
Reads the raw value from the specified sensor. For most sensors, this means the analog voltage on the relevant input pin.
- Parameters
- `index`: Index of the sensor to read. [A list of sensor indices can be found here.](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/sensors/sensor_type.h)
- Returns
- The raw value that yielded the sensor reading, or 0 if the sensor doesn't support raw readings, isn't configured, or has failed.
2023-01-03 04:01:31 -08:00
#### `getAuxAnalog(index)`
2021-09-18 14:13:52 -07:00
2023-01-02 11:22:23 -08:00
More or less like getSensorRaw but always voltage of aux analog input.
2021-09-18 14:13:52 -07:00
- Parameters
- `index`: Index of aux analog sensor to read. From 0 to 3
- Returns
- Voltage of sensor reading, or nil if sensor isn't configured.
2023-01-03 04:01:31 -08:00
#### `hasSensor(index)`
2021-04-29 10:57:27 -07:00
Checks whether a particular sensor is configured (whether it is currently valid or not).
- Parameters
- `index`: Index of the sensor to check. [A list of sensor indices can be found here.](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/sensors/sensor_type.h)
- Returns
- A boolean value, `true` if the sensor is configured, and `false` if not.
2023-01-03 04:01:31 -08:00
#### `getDigital(index)`
2021-04-29 10:40:07 -07:00
Reads a digital input from the specified channel.
- Parameters
- `index`: The index of the digital channel to read. See table below for values.
- Returns
- A boolean value representing the state of the input pin. `true` = high voltage (above ~2 volts), `false` = low voltage (below ~3 volts)
Valid `index` parameter values:
| Index | Channel Name |
| --- | ---:|
| 0 | Clutch down switch |
| 1 | Clutch up switch |
| 2 | Brake switch |
| 3 | AC switch |
#### `getAuxDigital(index)`
Reads a digital input from the configurable list.
- Parameters
- `index`: The index of the digital pin to read. Valid values are 0 through 7, one for each of the 8 user-defined digital pins. See "Lua Digital Aux Inputs" table under "Advanced" settings tab.
- Returns
- A boolean value representing the state of the input pin. `true` = high voltage (above ~2 volts), `false` = low voltage (below ~3 volts)
2023-01-03 04:01:31 -08:00
#### `readPin(pinName)`
2021-08-31 09:08:30 -07:00
Reads physical value of arbitrary MCU pin
2023-01-02 11:22:23 -08:00
2021-08-31 09:08:30 -07:00
- Parameters
2022-07-27 00:19:23 -07:00
- `pinName`: string name of MCU pin, for examples "PD15"
2021-04-29 10:40:07 -07:00
2023-01-03 04:01:31 -08:00
### Output
2021-04-29 10:40:07 -07:00
2024-01-19 14:57:34 -08:00
Not to be confused with internal logic 'Live View'/Log data points/gauges 'outputs'. Not to be confused with GPPWM feature.
2023-01-03 04:01:31 -08:00
#### `selfStimulateRPM(rpm)`
2022-10-28 16:50:57 -07:00
Positive value would start clicking injectors at specified RPM, zero value would stop self-stimulation.
2023-01-03 04:01:31 -08:00
#### `startPwm(index, frequency, duty)`
2021-05-20 11:43:48 -07:00
Initializes PWM on the specified index, starting at the specified frequency and duty cycle. The index selects which config field pin to use, see "Lua PWM Outputs" page in TunerStudio.
- Parameters
- `index`: The index of the PWM channel to start. Valid values are 0 through 7, one for each of the 8 channels.
2021-05-20 11:43:48 -07:00
- `frequency`: Initial frequency of the output, in hertz (cycles per second). Valid values are between 1 and 1000hz.
- `duty`: Initial duty cycle of the output. `0.0` = fully off, and `1.0` = fully on. `0.25` = on 25% of the time, off 75% of the time.
- Returns
- none
2023-01-03 04:01:31 -08:00
#### `setPwmDuty(index, duty)`
2021-05-20 11:43:48 -07:00
Set the duty cycle of the specified PWM channel.
- Parameters
- `index`: The index of the PWM channel to set. Valid values are 0 through 7, one for each of the 8 channels.
2021-05-20 11:43:48 -07:00
- `duty`: Desired duty cycle of the output. `0.0` = fully off, and `1.0` = fully on. `0.25` = on 25% of the time, off 75% of the time.
- Returns
- none
2023-01-03 04:01:31 -08:00
#### `setPwmFreq(index, frequency)`
2021-05-20 11:43:48 -07:00
- Parameters
- `index`: The index of the PWM channel to set.
- `frequency`: Initial frequency of the output, in hertz (cycles per second). Valid values are between 1 and 1000hz.
- Returns
2021-10-19 07:46:10 -07:00
- none
#### `setLuaGauge(index, value)`
In an ideal world one would be using Sensor.new("LuaGauge1") but looks like that's not the way at the moment :(
- Parameters
2024-02-18 17:48:22 -08:00
- `index`: The index of the Lua gauge to set. Valid indices are '1' and '2'.
- `value`: Desired gauge value.
- Returns
- none
2024-01-02 18:18:33 -08:00
#### `setDacVoltage(index, value)`
2024-01-02 18:18:33 -08:00
Not enabled on most boards since most boards were not developer with DAC in mind! See https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/examples/dac.txt for more info.
2021-10-19 07:46:10 -07:00
## Misc console commands
``luamemory``
2021-10-19 07:50:40 -07:00
``luareset``
## Examples
2023-07-09 18:04:52 -07:00
Read VSS from CANbus for gear detection see [honda-bcm.txt](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/examples/honda-bcm.txt)
2023-01-03 04:01:31 -08:00
### timer
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2021-10-19 07:50:40 -07:00
t = Timer.new();
2021-10-19 08:03:19 -07:00
timingAdd = 0;
2021-10-19 07:50:40 -07:00
function onTick()
auxV = getAuxAnalog(0)
2021-10-25 19:06:24 -07:00
tps = getSensor("TPS")
2021-10-19 08:00:56 -07:00
-- todo: check for NIL value which is a sign of analog input not assigned in TS
2021-10-19 07:50:40 -07:00
if auxV > 2 then
t:reset();
end
2021-10-19 08:03:19 -07:00
2021-10-19 07:50:40 -07:00
val = t:getElapsedSeconds();
2021-10-19 08:03:19 -07:00
if t:getElapsedSeconds() < 3 then
timingAdd = 10;
else
timingAdd = 0;
end
setTimingAdd(timingAdd)
2021-10-19 07:50:40 -07:00
print('Hello analog ' .. auxV .. " " .. val)
end
```
2021-10-21 16:38:02 -07:00
2023-01-03 04:01:31 -08:00
### PWM
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2023-01-03 04:01:31 -08:00
-- index 0, 100Hz, zero duty initially
2021-10-21 16:38:02 -07:00
startPwm(0, 100, 0)
function onTick()
enable_pump = getSensor("RPM") > 700 and getSensor("BatteryVoltage") > 13 and getSensor("VehicleSpeed") <60
2021-11-27 16:40:08 -08:00
-- lua does not have ternary ? : operator, this here means "1 if enable_pump and 0 otherwise"
setPwmDuty(0, enable_pump and 1 or 0)
2021-10-21 16:38:02 -07:00
2021-10-30 13:48:49 -07:00
end
```
2023-01-03 04:01:31 -08:00
### CAN transmit
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2021-10-30 13:48:49 -07:00
function onTick()
tps = getSensor("CLT")
print('TPS ' .. tps)
voltage0 = getSensor("aux0")
txPayload = {}
// first byte: integer part, would be converted to int
2021-10-30 13:48:49 -07:00
txPayload[1] = voltage0
// second byte: fractional part, would be converted to int, overflow would be ignored
2021-10-30 13:48:49 -07:00
txPayload[2] = voltage0 * 256;
txCan(1, 0x600, 1, txPayload)
2021-10-21 16:38:02 -07:00
end
2021-11-11 15:21:59 -08:00
```
2023-01-03 04:01:31 -08:00
### set sensor value
2023-01-02 11:22:23 -08:00
2022-10-28 15:01:11 -07:00
[A list of sensor names can be found here.](https://github.com/rusefi/rusefi/blob/master/firmware/controllers/sensors/sensor_type.h)
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2022-07-28 06:02:45 -07:00
vssSensor = Sensor.new("VehicleSpeed")
2022-11-06 15:41:29 -08:00
-- any value would be considered valid for three seconds
vssSensor:setTimeout(3000)
2022-07-22 03:30:36 -07:00
function onTick()
2023-01-02 11:22:23 -08:00
injectedVssValue = 123.4;
vssSensor : set(injectedVssValue)
-- here we would read the value we have just injected into the sensor.
valFromSensor = getSensor("VehicleSpeed")
2022-10-28 15:03:57 -07:00
-- we expect output to be "VSS 123.4"
2023-01-02 11:22:23 -08:00
print ("VSS " .. valFromSensor)
2022-07-22 03:30:36 -07:00
end
2022-07-20 04:30:37 -07:00
```
2023-01-03 04:01:31 -08:00
### CAN receive
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2021-11-26 06:18:59 -08:00
canRxAdd(0x500)
2021-11-26 06:11:41 -08:00
canRxAdd(0x570)
function onCanRx(bus, id, dlc, data)
2023-01-02 11:22:23 -08:00
print('got CAN id=' ..id ..' dlc=' ..dlc)
if id == 0x500 then
-- Check can state of BCM
canState = data[1]
end
if id == 0x570 then
mcu_standby()
end
2021-11-26 06:11:41 -08:00
end
```
2021-11-26 06:09:42 -08:00
2022-07-27 00:25:26 -07:00
```lua
2022-02-22 17:37:19 -08:00
function decimalToHex(num)
2023-01-02 11:22:23 -08:00
if num == 0 then
return '0'
end
local hexstr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F" }
local result = ""
while num > 0 do
local n = num % 16
result = hexstr[n + 1] ..result
num = math.floor(num / 16)
end
return result
2022-02-22 17:37:19 -08:00
end
2022-02-22 17:41:19 -08:00
function print_array(arr)
2023-01-02 11:22:23 -08:00
local str = ""
local index = 1
while arr[index] ~= nil do
str = str.." "..decimalToHex(arr[index])
index = index + 1
end
return str
2022-02-22 17:41:19 -08:00
end
2022-02-22 17:37:19 -08:00
```
2023-01-03 04:01:31 -08:00
### table
2023-01-02 11:22:23 -08:00
2022-07-27 00:25:26 -07:00
```lua
2021-11-26 06:09:42 -08:00
tableIndex = findTableIndex("duty")
TurbochargerSpeed = getSensor("TurbochargerSpeed")
2021-11-26 12:23:31 -08:00
tps = getSensor("Tps1")
2021-11-26 06:09:42 -08:00
dutyCycle = table3d(tableIndex, TurbochargerSpeed, tps)
2022-07-14 04:29:53 -07:00
sparkCutCurve = findCurveIndex("sparkcut")
sparkCutByTorque = curve(sparkCutCurve, torque)
2021-11-26 06:09:42 -08:00
```
2021-11-11 15:21:59 -08:00
## See also
2023-01-03 04:01:31 -08:00
### BMW iDrive
2022-09-13 13:49:32 -07:00
2023-01-02 11:22:23 -08:00
See <https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/examples/bmw-idrive.txt>
2022-09-13 13:49:32 -07:00
2023-01-02 11:22:23 -08:00
More examples at <https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/examples/>
2022-09-13 13:49:32 -07:00
2023-01-02 11:22:23 -08:00
See also a library for CAN data manipulation <https://github.com/rusefi/rusefi/blob/master/firmware/controllers/lua/lua_lib.h>
2022-09-13 13:49:32 -07:00
2023-01-02 11:22:23 -08:00
See also test driven development approach <https://github.com/rusefi/rusefi/tree/master/unit_tests/tests/lua>
2022-09-13 13:49:32 -07:00
[Lua Ternary Operator](http://lua-users.org/wiki/TernaryOperator)