Home TI-Nspire STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 41

           


Scripting Tutorial - Lesson 41: Lua Scripting and the TI Innovator™ Hub:

1. SENDING to the Hub



Download supporting file for this tutorial

Texas Instruments TI-Nspire Scripting Support Page

       
  

Welcome to the TI Innovator™ Hub

Top of Page

The TI Innovator™ Hub is the centerpiece of the TI-Innovator™ System, a project kit that extends the functionality of Texas Instruments (TI) graphing calculators to make coding and engineering design accessible to students in the classroom. Plug-and-play and ready-to-use with TI graphing calculators, the TI-Innovator™ Hub enables students to learn basic coding and design, use those skills to program and build working solutions, and connect science, technology, engineering and math (STEM) concepts.

The Hub itself is based around a TI MSP432 LaunchPad board, which carries on it a complex series of scripts (called 'sketches') which translate commands from the user into Hub actions and responses. To learn more about programming such boards, feel free to explore the LaunchPad tutorials on this site.

More importantly, the Innovator Hub can be programmed using TI-BASIC, the simple and readily accessible programming language available on TI handhelds (both TI-Nspire and TI-84 series) and software. Students use simple commands and programs to engage with and control the Hub, bringing together all the key elements of STEM and more.

Which raises the question: why use Lua with the Innovator Hub?

The main advantage of Lua for use with the Hub will obviously lie in the creation of interactive documents for teaching and learning. Lua and the TI-Nspire are now well-recognised as a powerful partnership for authoring, and the Innovator Hub adds significantly to the opportunities for active and appropriate student engagement across a wide range of curriculum topics, particularly in relation to STEM. For example, consider the activity shown in the animated GIF opposite, in which the Innovator brightness sensor is used to determine the growth rate of plants under different light conditions. Or have students learn the real principles of acceleration controlling a Lunar Lander!

While TI-BASIC offers a great platform for simple interactions and programs, a limitation exists which becomes obvious when seeking to collect data samples: while such samples can be readily collected, you must wait until the entire program has run before you will see the results on your handheld or computer. So a graph of light intensity, for example, will not update until all samples have been collected. This is not a limitation when using Lua on desktop or handheld.

Interacting with the Innovator Hub, using either TI-BASIC of Lua, is mostly centred around two key activities: SETTing and READing.

SENDing lies at the heart of all interactions with the Hub: whether from a TI-BASIC program or from a Lua script, commands are sent to the Hub via USB connection (also called Asynchronous Interface or ASI). Such commands must be in the right format and syntax, and a well-authored Lua script can take care of much of the uncertainty that students may face in this regard.

SET commands will involve on-board functionality, such as the in-built LEDs and speaker, or attached devices such as lights, relays, servos, and more. These will tend to be either digital (ON/OFF) or analog, and so commands will often be of the form "SET ... ON" or "SET ... OFF".

READing occurs when the Hub sends a response back to the sent request. this may be as simple as letting the device know that the hub is ready, but will commonly involve the transmission of data from Hub or attached peripheral (sensors, servo motors, LEDs, relays, and more). Such data may be in the form of a single numerical value (for example, brightness or distance), or could be a list of values - for example, a 3-axis accelerometer will send a list of three values for x, y and z axes, while a barometer will commonly send air pressure, temperature, altitude and more.

The focus of this first lesson is on the TI Innovator SEND command, and you will learn how to send commands to the Hub, and to make certain simple things happen on board.


  
  

Scripting for the Innovator Hub: Lesson 1: SENDING to the Hub

Top of Page

  
  

The heavy lifting in the scripts in this section was all achieved by Fred Fotsch - thank you Fred!

Every effort has been made to make this script as simple as possible, so there is very little in the way of layout and design - no buttons, sliders, or anything fancy. However, you will see that the Innovator functions are, in fact, far from simple, but this should not deter you from trying some of your own scripting ideas.

If you have previously worked through some or all of the Lua BLE lessons, you will understand that much of the hard work has been done, and may be readily accomplished using copy and paste. Focus on the general principles and those sections specified for the user to interact with the script.

To help with this, once again, I have broken the script down into main sections, which may be viewed individually (or viewed as a whole, and copied and pasted to make a full working document).

  
  
  1. First notice that we are now using APIlevel 2.7!! Specifically designed around the Innovator Hub.

    You might also see a simple function called 'addMsg', which is used to print information to the screen as well as to the script editor console. In your future scripts you probably won't need your users to see the details of connection and activity, but for our purposes here it is quite instructive to follow the process as it occurs "under the hood".

    Look closely at the three highlighted lines at the start of on.construction: these make the magic happen!

    You will see that the menu is used to save a lot of layout and screen display, and makes it easy to include a range of examples for you to explore. The default action for this document is to turn the red LED ("LIGHT") on and off by simply pressing down and up on the screen.

    Most of the commands are of the same form: "SET ... ON" or "SET ... OFF". So "SET LIGHT ON" is the default and is easily changed to, for example, "SET COLOR.BLUE ON".

    The in-built speaker is introduced here: "SET SOUND 220 5" plays an A tone for 5 seconds.

    The remaining menu items introduce external peripherals: LED, speaker, servo motor and two generic ones (very useful indeed!): ANALOG.OUT and DIGITAL.OUT. These last two may be used for any appropriate peripheral, and when used with the breadboard connectors (BB1 to BB10) give the Innovator Hub enormous flexibility.

    Note the servo: The in-built functions are simple, but when using external connections, then these must first be CONNECTed to the appropriate port: for a servo, we generally use OUT3, which supplies 5V (OUT1 and OUT2 are 3.3V ports). So choosing this menu option first sends a command to the Hub to connect a servo to OUT3. Subsequent commands will be of the form "SET SERVO 1 CW 50 5" which turns the servo clockwise for 5 seconds (or until it receives a zero time command).

  2.   
      

  3. Very simple - note the neat color change when the hub is connected?
  4.   
      

  5. Again, the user interaction here is so simple: press the mouse down and one command is sent to the Hub, release and another is sent. NOTE that the three TI_Innovator functions here are NOT required for this document - they only come into play when feedback from the Hub is read. This will be explored in the next lesson.
  6.   
      
  7. You may be well familiar with the Lua on.timer command (as was I before I started working with the Innovator) but this is another approach altogether. Think of it like an advanced "wait" command. This interesting function takes a time to wait (in milliseconds, so use 1000 to wait for 1 second), and then calls a function or list of functions and parameters. The OneShotTimer will be covered in more detail later: in this document, it is used only when the Hub is being scanned and connected - mostly because the device (developed around the handheld and its limitations) needs time to process each step of the process.

      
      

  8. If you have worked with the BLE functions for Lua, then the basic principles of this will be familiar. Feel free to browse through the functions and follow the process by which the Innovator is scanned, found, connected, etc - but expect to just copy and paste this code into your own scripts to make them "Hub-enabled"!

  
  

Once again, if you have a Hub, you can test this script yourself by copying and pasting from this page. Of course, prepared versions are also available from the download link for this page.

 
 

-- TI Innovator Init Values

platform.apilevel = '2.7'
local screen = platform.window
local date = "021517"

require 'color'
pcall(function() require 'asi' end)

local isASIavailable = false
local hubStrOn, hubStrOff, msgStr

function addMsg(input)

msgStr = input
print(msgStr)
screen:invalidate()

end

function on.construction()

--the next three lines are required for Hub connection
TI_Innovator.init(TI_InnovatorStateCallback)
TI_Innovator.setReadListener(TI_InnovatorReadCallback)
TI_Innovator.connect()

Menu={

{"About",
{" ©2017 Texas Instruments", function() end},
{" Version "..date, function() end},
{" Contact: steve.arnold@ti.com ", function() end},
},
{"Controls",
{"Scan and Connect", function() TI_Innovator.connect() end},
{"Disconnect", function() TI_Innovator.disconnect() end},
{"RESET", function() on.resize() end},
},
{"SET Samples",
{"SET LIGHT ON/OFF (default)", function() hubStrOn = "SET LIGHT ON" hubStrOff = "SET LIGHT OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.RED ON/OFF", function() hubStrOn = "SET COLOR.RED ON" hubStrOff = "SET COLOR.RED OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.GREEN ON/OFF", function() hubStrOn = "SET COLOR.GREEN ON" hubStrOff = "SET COLOR.GREEN OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.BLUE ON/OFF", function() hubStrOn = "SET COLOR.BLUE ON" hubStrOff = "SET COLOR.BLUE OFF"TI_Innovator.Send(hubStrOn) end},
{"SET SOUND 220 5", function() hubStrOn = "SET SOUND 220 5" hubStrOff = "SET SOUND 0 1" TI_Innovator.Send(hubStrOn) end},
{"SET SPEAKER 1 220 5", function() hubStrOn = "SET SPEAKER 1 220 5" TI_Innovator.Send("CONNECT SPEAKER 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET SPEAKER 1 0 1" end},
{"SET LED 1 ON", function() hubStrOn = "SET LED 1 ON" TI_Innovator.Send("CONNECT LED 1 OUT 1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET LED 1 0FF" end},
{"SET SPEAKER 1 ON", function() hubStrOn = "SET SPEAKER 1 ON" TI_Innovator.Send("CONNECT SPEAKER 1 OUT 1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET SPEAKER 1 0FF" end},
{"SET SERVO 1 CW 50 5", function() TI_Innovator.Send("CONNECT SERVO 1 OUT3") hubStrOn = "SET SERVO 1 CW 50 5" hubStrOff = "SET SERVO 1 CW 50 0" TI_Innovator.Send(hubStrOn) end},
{"SET ANALOG.OUT 1 100", function() hubStrOn = "SET ANALOG.OUT 1 100" TI_Innovator.Send("CONNECT ANALOG.OUT 1 OUT 1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET ANALOG.OUT 1 0" end},
{"SET DIGITAL.OUT 1 ON", function() hubStrOn = "SET DIGITAL.OUT 1 ON" TI_Innovator.Send("CONNECT DIGITAL.OUT 1 OUT 1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET DIGITAL.OUT 1 0FF" end},
},
}
toolpalette.register(Menu)
screen:invalidate()
end

 
 

 
 


  

Back to Top

  

Home TI-Nspire STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 41