Lua scripting with modules

TILT!Audio features a lua runtime 5.2 to control additional extensions like shakers, LED-lights and also servos.

To make this possible TILT!Audio firmware embeds a lua runtime (lua virtual machine) and loads a “init.lua” script from the sound directory.

The init.lua script already has access to all core lua language features and also the TILT!Audio bindings see lua reference. The binding allows access to the sound engine, communicate via i2c, logging messages and get information about the current installation.

Lua in its language ecosystem also offers modules and package managers that allows installing and managing modules that extend lua features. One of this package managers is luarocks.

Luarocks

Using luarocks as package manager to install a new module together with TILT!Audio is not so straight forward as you can read it on most websites, because the linux running on a TILT!Audio card is tailored down to a minimum to run as efficient as it should to produce sound and nothing else.

What is missing for example is a c compiler that most lua modules require as they come as a combination of lua code and c code.

Also luarocks requires a certain directory structure that is aware of the different lua versions and different places in the system where the lua runtime should look for “modules” (which is *.so and *.lua files).

The TILT!Audio start script does set the required environment variables to let lua runtime find modules, but still not in that way that a module installed by luarocks would expect.

Continue reading “Lua scripting with modules”

Shaker control with pwm

Using a Arduino mini pro as extension to a TILT!Audio board, you can easily control a shaker motor via pwm (pulse width modulation).

First we need a motor driver module that is capable of driving the high current motor power. e.g. something like this:

Dc motor driver module for up to 10A

The picture is more or less self-explaining the only thing that need to be connected to the Arduino mini pro is the GND / PWM input.

Now looking at the Arduino mini pro we use pin 5 as pwm output and to connect to the TILT!Audio board we use the i2c bus the is controlled by the SCL / SDC pins (together with GND / Vcc of course):

Be sure to choose a 5v type for Arduino mini pro as there are also available in 3.3v but the TILT!Audio connector for the i2c bus provides 5v. Also the 5v type runs on 16Mhz instead of only 8Mhz for 3.3v with is also good.

So connections are:

FromTo
TA-Board: GNDGND (right side)
TA-Board: VccVCC (right side)
TA-Board: SDASDA (green)
TA-Board: SCLSCL (green)
Arduino: GNDPower Driver: GND (right)
Arduino: Pin5 (PWM)Power Driver: PWM (right)

Now we create a script for the Arduino mini pro like this (see https://github.com/sker65/tiltaudio-extensions). We start with initial setup stuff:

First thing we do is switch shaker on, off and set speed (within a limited range to not make to pinball machine jump):

Second we wire these functions to i2c control:

And additionally we can create “sequences”. A sequence would play back some schema of on and off or speed up and speed downs. The idea is to define a sequence as 3 integers: command byte, value, interval in milliseconds. We define these commands:

  • 0x00: stop -> end of the sequence
  • 0x01: switch on, value -> speed
  • 0x02: switch off, value -> don’t matter
  • 0x03: ramp up, value -> target speed at end of interval

We delegate playing those sequences to the Arduino and later just trigger it from the TILT!Audio board by sending some command to the i2c bus:

And again create a function that triggers the sequence playback and wire it to i2c control:

Find the complete example on github.

To get an idea, how it finally will play all together I did a test installation: