Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save futureshocked/b826e4ad968231328eb8c09ae608ce91 to your computer and use it in GitHub Desktop.
Save futureshocked/b826e4ad968231328eb8c09ae608ce91 to your computer and use it in GitHub Desktop.
This script shows how to control an LED with FIO4 by deploying a Lua scripts to the LabJack. This script also demonstrates communication between this Python script and the on-board Lua scrip via user RAM.
'''
LJ - 10.60 - lua_script_host_control.py
This script shows how to control an LED with FIO4 by deploying a Lua scripts
to the LabJack. This script also demonstrates communication between this Python
script and the on-board Lua scrip via user RAM.
Components
----------
- LabJack T4
- LED
- Cathode (short pin) to GND
- Anode (long pin) to FIO4
- Wires
- Breadboard
Course
------
Data acquisition and automation with LabJack
https://app.techexplorations.com/courses/labjack/
'''
from labjack import ljm
from time import sleep
def loadLuaScript(handle, luaScript):
"""Function that loads and begins running a lua script
"""
try:
scriptLen = len(luaScript)
# LUA_RUN must be written to twice to disable any running scripts.
print("Script length: %u\n" % scriptLen)
ljm.eWriteName(handle, "LUA_RUN", 0)
# Then, wait for the Lua VM to shut down. Some T7 firmware
# versions need a longer time to shut down than others.
sleep(0.6)
#ljm.eWriteName(handle, "LUA_RUN", 0)
ljm.eWriteName(handle, "LUA_SOURCE_SIZE", scriptLen)
ljm.eWriteNameByteArray(handle, "LUA_SOURCE_WRITE", scriptLen, luaScript)
ljm.eWriteName(handle, "LUA_DEBUG_ENABLE", 1)
ljm.eWriteName(handle, "LUA_DEBUG_ENABLE_DEFAULT", 1)
ljm.eWriteName(handle, "LUA_RUN", 1)
except ljm.LJMError:
print("Error while loading the lua script")
raise
def readLuaInfo(handle):
"""Function that selects the current lua execution block and prints
out associated info from lua
"""
try:
for i in range(10):
# The script sets the interval length with LJ.IntervalConfig.
# Note that LJ.IntervalConfig has some jitter and that this program's
# interval (set by sleep) will have some minor drift from
# LJ.IntervalConfig.
sleep(1)
print("LUA_RUN: %d" % ljm.eReadName(handle, "LUA_RUN"))
print("Value from USER_RAM1_F32: %d" % ljm.eReadName(handle, "USER_RAM1_F32"))
print("Interval delay from USER_RAM0_F32: %d" % ljm.eReadName(handle, "USER_RAM0_F32"))
# Add custom logic to control the Lua execution block
executionLoopNum = i % 2
# This will write a new interval
# time to RAM register USER_RAM0_F32
# which is address 46000
if executionLoopNum == 0:
ljm.eWriteName(handle, "USER_RAM0_F32", 800) # Change interval duration to 800ms
else:
ljm.eWriteName(handle, "USER_RAM0_F32", 200) # Change interval duration to 200ms
except ljm.LJMError:
print("Error while running the main loop")
raise
def main():
try:
luaScript = """print("Starting")
local count = 0
local mbWrite = MB.W
mbWrite(46000, 3, 1000) -- Set user RAM 46000 to 1000 to be used by the timer
LJ.IntervalConfig(0, 200) -- Set interval to 1000 for 1000ms
local checkInterval = LJ.CheckInterval
local outPin = 2004 -- FIO4 register address
local led_state = 1
while true do
if checkInterval(0) then -- interval completed
LJ.IntervalConfig(0,MB.R(46000,3)) -- Read the interval time from user RAM
mbWrite(outPin, 0, 1)
print("LED state: ", led_state)
if led_state == 1 then
mbWrite(46002, 3, 200) -- Set register "USER_RAM1_F32". Address 46002, type 3
mbWrite(outPin, 0, 1)
led_state = 0
else
mbWrite(46002, 3, 0) -- Set register "USER_RAM1_F32". Address 46002, type 3
mbWrite(outPin, 0, 0)
led_state = 1
end
end
end
"""
# Open handle to LabJack
handle = ljm.openS("ANY", "ANY", "Peter_T4")
info = ljm.getHandleInfo(handle)
print("Opened a LabJack with Device type: %i, Connection type: %i,\n"
"Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" %
(info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5]))
loadLuaScript(handle, luaScript)
print("LUA_RUN %d" % ljm.eReadName(handle, "LUA_RUN"))
print("LUA_DEBUG_NUM_BYTES: %d" % ljm.eReadName(handle, "LUA_DEBUG_NUM_BYTES"))
readLuaInfo(handle)
ljm.eWriteName(handle, "LUA_RUN", 0) # Stop the Lua script.
# If not called, the script will
# continue to run.
# Close handle
ljm.close(handle)
except ljm.LJMError:
ljm.eWriteName(handle, "LUA_RUN", 0)
# Close handle
ljm.close(handle)
raise
except Exception:
ljm.eWriteName(handle, "LUA_RUN", 0)
# Close handle
ljm.close(handle)
raise
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment