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

           


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

5. Lua Script for the TI Innovator™ Hub Rover



Download supporting file 1 for this tutorial: Lua_Rover.tns

Download supporting file 2 for this tutorial: TI_Rover_BASIC_Sampler.tns

All about the TI Innovator™ Rover

Texas Instruments TI-Nspire Scripting Support Page

       
  

Scripting for the Innovator Hub: Lesson 5: Lua Script for the TI Innovator™ Rover

Top of Page

 
Multiple Pathways to Controlling a Robot

This lesson and the two that follow describe three different approaches to working with robot vehicles. This lesson introduces the TI Innovator™ Hub Rover, and the next, the Norland E3 Innovator™ Robot - both built on the amazing TI Innovator™ Hub. The third lesson describes building a robot from just a TI LaunchPad board (or a "hacked" Innovator Hub!)

The TI Rover offers the simplest possible access to robotics for students. The TI-BASIC coding language, ideal for students from middle years upwards, has been enhanced with commands specific to the Rover. Connecting the varied sensors and functionality of the vehicle to the Innovator Hub is as simple as "CONNECT RV". Have a peek at the next lesson and compare this to the Norland robot, without the custom commands, which instead uses the generalised DIGITAL.IN/OUT and ANALOG.IN/OUT commands, all of which must be connected individually to the BB ports of the Hub.

Note, however, that while this simplicity makes it quick and easy for students to work with the Rover, it also masks what is actually happening in the background. Those who are interested to go further with robotics may well derive benefit from experimenting, as I did, to find the various connections which link the BB ports to the drive motors and the HC-SR04 ultrasonic sensor. Further investigation is required to see how the gyroscope and colour input sensor (Adafruit TCS34725) are connected.

  

Below, you will find both simple TI-BASIC programs as well as the full Lua script which may be used to control the TI-Innovator Hub Rover. If the student focus is upon simple coding, then TI-BASIC offers a great solution. You will find in the downloads for this tutorial a TI Rover BASIC Sampler written using Lua. As outlined in Tutorial 40, this can offer a very convenient way for students to easily access the various TI-BASIC programs required to run the robot, but also, more importantly, the means by which they can quickly and easily edit and explore these programs, instantly trying out their changes to see the effect.

The Lua script offers the same control features as the TI-BASIC examples, plus other options for drawing on the power of the Hub.

  

 
    
  1. A range of new settings variables are added, allowing the user to control such features as robot speed, run time, timer step, etc. Take note of the use of var.monitor and on.varChange which will update the script immediately any change occurs in these monitored values.

    These may be stored for convenience in a spreadsheet on page 1.2.

  2.   
      

  3. On-screen instructions are included in on.paint, and on.resize serves as a full reset function (coupled with on.escapeKey).

    Study the graphical controller for the robot, which works neatly with the arrow keys.

  4.   
      

  5. Note the changes to mouseDown and mouseUp.

    Note also the utility function str2list which allows easy separation of Hub rxValues which may not always be numeric.

  6.   
      
  7. are defined here: individually for the four directions, and then the autoDrive function and a nice Polygon function.

  8.   
      

  9. OneShotTimer is added to the Innovator functions, which are otherwise unchanged.

  
  

At this point, it is also instructive to compare the Lua functions we are defining to their TI-BASIC counterparts.

Define fd(time)=Prgm

Send "CONNECT RV "
Disp "FORWARD "&string(time)&" s"
Send "SET RV FORWARD "&string(time)
wait time
Send "DISCONNECT RV "
EndPrgm

Define bk(time)=Prgm

Send "CONNECT RV "
Disp "BACKWARD "&string(time)&" s"
Send "SET RV BACKWARD "&string(time)
wait time
Send "DISCONNECT RV "
EndPrgm

Define lt(angle)=Prgm

Send "CONNECT RV "
Disp "LEFT "&string(angle)&" DEGREES"
Send "SET RV LEFT "&string(angle)
wait 0.2
Send "DISCONNECT RV "
EndPrgm

Define rt(angle)=Prgm

Send "CONNECT RV "
Disp "RIGHT "&string(angle)&" DEGREES"
Send "SET RV RIGHT "&string(angle)
wait 0.2
Send "DISCONNECT RV "
EndPrgm

Define polygons()=Prgm

Local m,n,i
Send "CONNECT RV"
Request "side length (cm)",n
Request "no. sides",m
n:=((n)/(20))
For i,1,m
Send "RV FORWARD SPEED .2 M/S TIME eval(n)"
Send "RV LEFT eval(360/m) DEGREES"
EndFor
EndPrgm

Define auto()=Prgm

Local d
d:=0
Send "CONNECT RV "
Wait 0.2
Send "READ RV.RANGER "
Get d
Disp "Distance: "&string(d)&" m"
Send "SET SOUND eval(d*1000) 0.5"
Wait 0.2
While d>0.1
Send "READ RV.RANGER "
Get d
Disp "Distance: "&string(d)&" m"
Send "SET SOUND eval(d*1000) 0.5"
Wait 0.2
If d>0.3 Then
Send "SET RV FORWARD 1"
Else
Send "SET RV BACKWARD 0.5"
Send "SET RV RIGHT 15"
EndIf
EndWhile
Send "DISCONNECT RV "
EndPrgm

  

-- TI Innovator Init Values

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

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

local hubStrOn, hubStrOff, msgStr, rxValue, timeStr, repeats, time_step, delay
local list1, list2, index, dataList = {}, {}, {}, {}
var.store("index", index)
var.store("dataList", dataList)
local oldChar = ''
local help = 1
local fontSize = 20

local robotConnected = false
local time_step = var.recall("time_step") or 0.5
local runTime = var.recall("runtime") or 10
local runtimeStr = "Run Time: "..runTime
local robotSpeed = var.recall("speed") or 10
local speedStr = "Robot Speed: "..robotSpeed.." cm/s"
local colornames = {"red","green","blue","cyan","magenta","yellow","black","white","gray"}

var.monitor("speed")
var.monitor("runtime")
var.monitor("time_step")
var.monitor("hubstr")
var.monitor("polysides")
var.monitor("polylength")

function on.varChange(varList)

robotSpeed = var.recall("speed") or 10
speedStr = "Robot Speed: "..robotSpeed.." cm/s"
runTime = var.recall("runtime") or 10
runtimeStr = "Run Time: "..runTime
time_step = var.recall("time_step") or 0.5
local colornames = {"red","green","blue","cyan","magenta","yellow","black","white","gray"}
polysides = var.recall("polysides") or 5
polylength = var.recall("polylength") or 20
hubStrOn = string.upper(var.recall("hubstr")) or ''
if hubStrOn:find("ON") then
hubStrOff = hubStrOn:gsub("ON","OFF")
TI_Innovator.Send(hubStrOn)
addMsg(hubStrOn)
else
hubStrOff = ''
TI_Innovator.Send(hubStrOn)
addMsg(hubStrOn)
if hubStrOn:find("READ") then TI_Innovator.Read() end
end
screen:invalidate()
end

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",
{" ©2018 Compass Learning Technologies", function() end},
{" Version "..date, function() end},
{" Contact: steve@compasstech.com.au ", function() end},
{" Help?", function() on.help() end},
},
{"Controls",
{"Scan and Connect", function() TI_Innovator.connect() end},
{"Disconnect", function() TI_Innovator.disconnect() on.resize() 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) addMsg(hubStrOn) rxValue = "Press mouse down to toggle light" end},
{"SET COLOR.RED ON/OFF", function() hubStrOn = "SET COLOR.RED ON" hubStrOff = "SET COLOR.RED OFF" TI_Innovator.Send(hubStrOn) addMsg(hubStrOn) rxValue = "Press mouse down to toggle red LED" end},
{"SET COLOR.GREEN ON/OFF", function() hubStrOn = "SET COLOR.GREEN ON" hubStrOff = "SET COLOR.GREEN OFF" TI_Innovator.Send(hubStrOn) addMsg(hubStrOn) rxValue = "Press mouse down to toggle green LED" end},
{"SET COLOR.BLUE ON/OFF", function() hubStrOn = "SET COLOR.BLUE ON" hubStrOff = "SET COLOR.BLUE OFF" TI_Innovator.Send(hubStrOn) addMsg(hubStrOn) rxValue = "Press mouse down to toggle blue LED" end},
{"SET SOUND 220 1", function() hubStrOn = "SET SOUND 220 1" hubStrOff = "SET SOUND 0 1" TI_Innovator.Send(hubStrOn) addMsg(hubStrOn) rxValue = "Press mouse down to repeat sound" end},
{'_________________', function() 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" rxValue = "Press mouse down to toggle speaker" end},
{"SET LED 1 ON", function() hubStrOn = "SET LED 1 ON" TI_Innovator.Send("CONNECT LED 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET LED 1 0FF" rxValue = "Press mouse down to toggle LED" end},
{"SET SERVO 1 CW 50 5 (OUT3)", function() TI_Innovator.Send("CONNECT SERVO 1 OUT3") hubStrOn = "SET SERVO 1 CW 50 5" hubStrOff = "SET SERVO 1 CW 50 0" rxValue = "Press mouse down to toggle servo" end},
{"SET ANALOG.OUT 1 100", function() hubStrOn = "SET ANALOG.OUT 1 100" TI_Innovator.Send("CONNECT ANALOG.OUT 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET ANALOG.OUT 1 0" rxValue = "Press mouse down to toggle analog read" end},
{"SET DIGITAL.OUT 1 ON", function() hubStrOn = "SET DIGITAL.OUT 1 ON" TI_Innovator.Send("CONNECT DIGITAL.OUT 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET DIGITAL.OUT 1 0FF" rxValue = "Press mouse down to toggle digital read" end},
},
{"READ Samples",
{"READ BRIGHTNESS", function() hubStrOn = "READ BRIGHTNESS" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() timeStr = "Press mouse down to take fresh reading" end},
{'_________________', function() end},
{"READ RV SWITCH", function() hubStrOn = "READ RV SWITCH" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" end},
{"READ RV.RANGER (r)", function() robotConnect() hubStrOn = "READ RV.RANGER" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" end},
{"READ RV.COLORINPUT (c)", function() robotConnect() hubStrOn = "READ RV.COLORINPUT" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" end},
{'_________________', function() end},
{"READ RANGER 1", function() hubStrOn = "READ RANGER 1" TI_Innovator.Send("CONNECT RANGER 1 IN1") math.eval("wait(0.5)") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" end},
{"READ ANALOG.IN 1", function() hubStrOn = "READ ANALOG.IN 1" TI_Innovator.Send("CONNECT ANALOG.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" driver = "ANALOG.IN 1" driverConnected = true end},
{"READ DIGITAL.IN 1", function() hubStrOn = "READ DIGITAL.IN 1" TI_Innovator.Send("CONNECT DIGITAL.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" driver = "DIGITAL.IN 1" driverConnected = true end},
},
{"Sound Samples",
{"C4", function() hubStrOn = "SET SOUND 262 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff rxValue = "Press mouse down to play sound" end},
{"A4", function() hubStrOn = "SET SOUND 440 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff rxValue = "Press mouse down to play sound" end},
{"C5", function() hubStrOn = "SET SOUND 523 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff rxValue = "Press mouse down to play sound" end},
{"No tone", function() hubStrOn = "SET SOUND 0 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"Aliens (sample)", function() tempo = 1000 I=1 list1 = notes_aliens list2 = times_aliens playTone(list1,list2) rxValue = "Aliens" end},
{"BallGame (sample)", function() tempo = 1000 I=1 list1 = notes_bgame list2 = times_bgame playTone(list1,list2) rxValue = "Take me out to the Ball Game" end},
{"Happy Birthday (sample)", function() tempo = 1000 I=1 list1 = notes_bday list2 = times_bday playTone(list1,list2) rxValue = "Happy Birthday" end}, {"If I Only Had a Brain (sample)", function() tempo = 4000 I=1 list1 = notes_brain list2 = times_brain playTone(list1,list2) rxValue = "If I only had a brain" end},
{"Fur Elise (sample)", function() tempo = 1000 I=1 list1 = notes_elise list2 = times_elise playTone(list1,list2) rxValue = "Fur Elise" end},
{"Home on the Range (sample)", function() tempo = 1000 I=1 list1 = notes_home list2 = times_home playTone(list1,list2) rxValue = "Home on the Range" end},
{"Well-tempered Scale", function() tempo = 1000 I=1 list1 = notes_welltemp list2 = times_scale playTone(list1,list2) rxValue = "Well tempered scale" end},
{"Harmonic Scale", function() tempo = 1000 I=1 list1 = notes_harmonic list2 = times_scale playTone(list1,list2) rxValue = "Harmonic scale" end},
},
{"Robot Samples",
{"CONNECT RV (enter)", function() robotConnect() end},
{"DISCONNECT RV (d)", function() robotDisconnect() end},
{"READ RV SWITCH", function() hubStrOn = "READ RV SWITCH" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" end},
{"ROBOT FD runTime", function() fd(runTime) end},
{"ROBOT BK runTime", function() bk(runTime) end},
{"ROBOT LT 0.5 secs", function() lt(45) end},
{"ROBOT RT 0.5 secs", function() rt(45) end},
{"autoDrive - (a)", function() autoDrive() end},
{"poly(polysides,polylength) - (p)", function() polysides = var.recall("polysides") or 5 polylength = var.recall("polylength") or 20 poly(polysides, polylength) end},

},
{"TIMER Samples",
{"wait(runTime) (w)", function() wait(runTime) end},
{"blink(runTime,1)", function() blink(runTime,1) end},
{"alarm(runTime,0.5)", function() alarm(runTime,0.5) end},
},
}
toolpalette.register(Menu)
screen:invalidate()
end

  

Hacking the Rover™

Breadboard header connectors */
BB1 14
BB2 15
BB3 7
BB4 18
BB5 13
BB6 12
BB7 33
BB8 40
BB9 38
BB10 39

RANGER.RV
-- CONNECT RANGER 1 BB6 BB5
-- READ RANGER 1

ROVER MOVEMENT:
-- CONNECT DIGITAL.OUT 4 BB4
-- CONNECT DIGITAL.OUT 8 BB8
-- CONNECT DIGITAL.OUT 9 BB9
-- CONNECT DIGITAL.OUT 10 BB10

-- RV FORWARD: SET DIGITAL.OUT 4 ON SET DIGITAL.OUT 9 ON
-- RV BACKWARD: SET DIGITAL.OUT 8 ON SET DIGITAL.OUT 10 ON

Note that I have chosen for simplicity to use DIGITAL.OUT to control the robot movement. As with the Norland robot, ANALOG.OUT could also have been used - and is, in fact, most likely the way that the Rover is controlled, since this command requires further input, in the form of a value for the Rover speed.

The ANALOG version of these commands might take the following form (using a value like 50 as the robot speed):

-- CONNECT ANALOG.OUT 4 BB4
-- CONNECT ANALOG.OUT 8 BB8
-- CONNECT ANALOG.OUT 9 BB9
-- CONNECT ANALOG.OUT 10 BB10

-- RV FORWARD: SET ANALOG.OUT 4 50 SET ANALOG.OUT 9 50
-- RV BACKWARD: SET ANALOG.OUT 8 50 SET ANALOG.OUT 10 50

Feel free to experiment yourself with such values. Try using simple TI-BASIC programs with the standard TI Rover Hub, use the Lua script given above, or the general Lua_HubBLE.tns document.

To input custom hub commands in either Lua document is easy. For the Lua script above, just store the new hub commands as a string in variable "hubStr" using Calculator or spreadsheet.

In the Lua_HubBLE.tns document, just use the text box provided to enter one or more hub commands, then press the button that appears below the text box to activate these.

With this information, it is even possible to reflash the Hub sketch with a BLE-enabled version using Energia and control your Rover remotely using the Lua_HubBLE.tns control document. As shown in the animated GIF image, a BLE-enabled Rover could even be controlled by a TI SensorTag (both Light sensor or accelerometer will work in this document), or a Vernier Go Wireless Link with accelerometer!

Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date").
So it is possible to explore coding the Hub in new ways!

If this is something that you might like to try, or you just have further questions, please feel free to contact me!

  

 
 

 
 

In our next lesson, we take these ideas in a different direction to drive the Norland E3 Innovator Robot.


  

Back to Top

  

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