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. Build your own Innovator™ Hub Robot



Download supporting file for this tutorial

Texas Instruments TI-Nspire Scripting Support Page

       
  

Scripting for the Innovator Hub: Lesson 5: Build your own Innovator™ Hub Robot

Top of Page


WARNING: Failure to carefully follow power connection instructions below may result in damage to your Innovator Hub

Now let's try driving a home-made robot: using a Lua script, and also using TI-BASIC.

This robot vehicle is driven by two light servo motors. In order to limit the power demand, we will connect the voltage lead for both of these to the one 3.3V BB connection (I "piggy-backed" one onto the other). Using the more usual 5V BB port would require the other wheel to also use 5 volts, and together this is too much, so using 3.3 volts for each gives us a more reliable (if slightly slower) experience.

In addition to sending instructions to two attached servo motors to go forward, back, left and right, we also extend the activity to have the robot follow a bright light using the on-board brightness sensor, and auto-drive to avoid obstacles using an ultrasonic ranger (if you have one).

As with LaunchPad lesson 9 (in which we built a BLE robot using a bare MSP432 LaunchPad board and spare LEGO from my grandchildrens' toybox) we will supplement our main chassis - in this case, our TI Innovator™ Hub - with a few extra components:


  • Two servo motors - I used Feetech FS90R micro continuous rotation servo from eBay and paid around $AUD6 each (total around $USD10) - NOTE: NOT the identical-looking Feetech FS90 which will NOT work for our purposes!


  • Pololu wheel are designed specifically for the FeeTech FS90R servo motor.

  • You will also need a light-weight power source.

     

    I found a great 2500 mAh power bank on eBay for $AUD15! (around $USD10)

    The form factor is ideal for our robot - it sits perfectly on top of the Innovator and the TI-Nspire handheld can sit on top of it!

  

 
    
  1. We add new variables, including driver and driverConnected to generalise our autoDrive function.

    Our menu continues to grow with more examples.

  2.   
      

  3. Changes include adding help instructions to the open screen.
  4.   
      

  5. Note the changes to mouseDown and mouseUp.

  6.   
      
  7. are defined here: individually for the four directions, and the autoDrive 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(t)=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Disp "FORWARD"
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "SET SERVO 1 CCW 100 "&" TIME "&string(t)
Send "SET SERVO 2 CW 100 "&" TIME "&string(t)
EndPrgm

Define bk(t)=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "SET SERVO 1 CW 100 "&" TIME "&string(t)
Send "SET SERVO 2 CCW 100 "&" TIME "&string(t)
EndPrgm

Define lt(t)=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "SET SERVO 1 CW 100 "&" TIME "&string(t)
Send "SET SERVO 2 CW 100 "&" TIME "&string(t)
EndPrgm

Define rt(t)=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "SET SERVO 1 CCW 100 "&" TIME "&string(t)
Send "SET SERVO 2 CCW 100 "&" TIME "&string(t)
EndPrgm

Define lightseeker(m,x)=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Local b
b:=0
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "READ BRIGHTNESS "
Get b
Disp "Brightness "&string(b)
Send "SET SOUND eval(b*100) 0.1"
While b>m
Send "READ BRIGHTNESS "
Get b
Disp "Brightness "&string(b)
Send "SET SOUND eval(b*100) 0.5"
If b>x Then
Disp "FORWARD"
Send "SET SERVO 1 CCW 100 TIME 1 "
Send "SET SERVO 2 CW 100 TIME 1 "
Wait 1
Else
Disp "BACK"
Send "SET SERVO 1 CW 100 TIME 0.5 "
Send "SET SERVO 2 CCW 100 TIME 0.5 "
Wait 0.5
Disp "RIGHT"
Send "SET SERVO 1 CCW 100 TIME 0.5 "
Send "SET SERVO 2 CCW 100 TIME 0.5 "
Wait 0.5
EndIf
EndWhile
EndPrgm

Define auto()=Prgm

Send "BEGIN " DelVar iostr.str0:GetStr iostr.str0:Disp iostr.str0
Local d
d:=0
Send "CONNECT RANGER 1 IN1 "
Send "CONNECT SERVO 1 BB9 "
Send "CONNECT SERVO 2 BB10 "
Send "READ RANGER 1 "
Get d
Disp "Distance "&string(d*10)&" cm"
Send "SET SOUND eval(d*1000) 0.1"
While d>0.15
Send "READ RANGER 1 "
Get d
Disp "Distance "&string(d*10)&" cm"
Send "SET SOUND eval(d*1000) 0.5"
If d>0.3 Then
Disp "FORWARD"
Send "SET SERVO 1 CCW 100 TIME 1 "
Send "SET SERVO 2 CW 100 TIME 1 "
Wait 1
Else
Disp "BACK"
Send "SET SERVO 1 CW 100 TIME 0.5 "
Send "SET SERVO 2 CCW 100 TIME 0.5 "
Wait 0.5
Disp "RIGHT"
Send "SET SERVO 1 CCW 100 TIME 0.5 "
Send "SET SERVO 2 CCW 100 TIME 0.5 "
Wait 0.5
EndIf
EndWhile
EndPrgm

  

-- TI Innovator Init Values

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

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

local hubStrOn, hubStrOff, msgStr, rxValue, timeStr, bump, repeats, time_step, delay, arrow
local list1, list2, index, dataList = {}, {}, {}, {}
var.store("index", index)
var.store("dataList", dataList)
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 75
local speedStr = "Robot Speed: "..robotSpeed
local autoMin = 5
local autoMax = 20

var.monitor("speed")
var.monitor("runtime")
var.monitor("time_step")
var.monitor("automin")
var.monitor("automax")
var.monitor("hubstr")

function on.varChange(varList)

robotSpeed = var.recall("speed") or 75
speedStr = "Robot Speed: "..robotSpeed
runTime = var.recall("runtime") or 10
runtimeStr = "Run Time: "..runTime
time_step = var.recall("time_step") or 0.5
autoMin = var.recall("automin") or 5
autoMax = var.recall("automax") or 20
hubStrOn = string.upper(var.recall("hubstr")) or ''
if hubStrOn:find("ON") then
hubStrOff = hubStrOn:gsub("ON","OFF")
else
hubStrOff = ''
end
if hubStrOn:find("CONNECT") and hubStrOn:find(" IN") then
local list = hubStrOn:split(" ")
if tonumber(list[3]) then driver = list[2].." "..list[3] else driver = list[2] end
driverConnected = true
end
if hubStrOn ~= '' then
TI_Innovator.Send(hubStrOn)
addMsg(hubStrOn)
if hubStrOn:find("READ") then
local list = hubStrOn:split(" ")
if tonumber(list[3]) then driver = list[2].." "..list[3] else driver = list[2] end
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",
{" ©2017 Compass Learning Technologies", function() end},
{" Version "..date, function() end}, {" Contact: steve@compasstech.com.au ", function() 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 (OUT1)", 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 (OUT1)", 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 (OUT1)", 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 (OUT1)", 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 RANGER 1 (Grove) (IN1)", 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" driver = "RANGER 1" driverConnected = true autoMin = 0.3 autoMax = 4 var.store("hubstr", hubStrOn) var.store("autoMin", autoMin) var.store("autoMax", autoMax) end},
{"READ RANGER 1 (HC-SR04) (BB7/BB8)", function() hubStrOn = "READ RANGER 1" TI_Innovator.Send("CONNECT RANGER 1 BB7 BB8") math.eval("wait(0.5)") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" driver = "RANGER 1" driverConnected = true autoMin = 0.3 autoMax = 4 var.store("hubstr", hubStrOn) var.store("autoMin", autoMin) var.store("autoMax", autoMax) end},
{"READ DHT 1 (IN1)", function() hubStrOn = "READ DHT 1" TI_Innovator.Send("CONNECT DHT 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' driver = "DHT 1" timeStr = "Press mouse down to take fresh reading" driverConnected = true autoMin = 15 autoMax = 25 var.store("hubstr", hubStrOn) var.store("autoMin", autoMin) var.store("autoMax", autoMax) end},
{"READ BAROMETER (I2C)", function() hubStrOn = "READ BAROMETER" TI_Innovator.Send("CONNECT BAROMETER I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" driver = "BAROMETER" driverConnected = true driverConnected = true autoMin = 1010 autoMax = 1030 var.store("hubstr", hubStrOn) var.store("autoMin", autoMin) var.store("autoMax", autoMax) end},
{'_________________', function() end},
{"READ ACCEL 1 (I2C)", function() hubStrOn = "READ ACCEL 1" TI_Innovator.Send("CONNECT ACCEL 1 I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = '' timeStr = "Press mouse down to take fresh reading" driver = "ACCEL 1" driverConnected = true end},
{"READ ANALOG.IN 1 (IN1)", 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 (IN1)", 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",
{"ROBOT FD runTime", function() fd(runTime) end},
{"ROBOT BK runTime", function() bk(runTime) end},
{"ROBOT LT 1 secs", function() lt(1) end},
{"ROBOT RT 1 secs", function() rt(1) end},
{"autoDrive (a)", function() autoDrive(driver,runTime) 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

 
 

 
 

In our next lesson, we take these principles and create a more versatile document, which accepts user input into a text field, allowing you to try out variations, and to create your own Innovator hub commands.


  

Back to Top

  

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