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

           


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

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

Top of Page

As in the previous tutorial, we see two different ways to use the Innovator Hub for a "practical" purpose - this time, 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 BB9 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.

The best option (if not the simplest) is to run the servos from an external battery. Use either a 9 volt "transistor" (D-type) battery or a 4-pack battery case. Either way, you will have a positive (red) and a negative (ground, black) lead. Using a breadboard (or even part of a breadboard), connect the red (positive) lead on the same line as the red (voltage) leads from your servos. The black ground lead needs to be connected to a ground pin on your board, along with the black/ground leads from the servos, connected to other ground pins (I just used three ground BB pins). Connect the other (signal) leads from the servos to the appropriate pins on your board, and you now have a circuit which runs through your board but with no risk of frying it!

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.

  

 
    
  1. We add one new variable: drive.

    Our menu continues to grow with more examples.

  2.   
      

  3. No changes, other than adding values to the new variables.
  4.   
      

  5. Note the changes to mouseDown and mouseUp.

  6.   
      
  7. are defined here: individually for the four directions, and then the lightSeeker and autoDrive functions.

  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 = "021517"

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

local isASIavailable = false
local hubStrOn, hubStrOff, msgStr, rxValue, timeStr, delay, repeats, drive

local tempo, key, I
local list1, list2, index, dataList = {}, {}, {}, {}
var.store("index", index)
var.store("dataList", dataList)

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) 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 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET LED 1 0FF" end},
{"SET SERVO 1 CW 50 5", function() TI_Innovator.Send("CONNECT SERVO 1 BB9") 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 OUT1") 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 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET DIGITAL.OUT 1 0FF" end},
}, {"READ Samples",
{"READ BRIGHTNESS (default)", function() hubStrOn = "READ BRIGHTNESS" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ RANGER 1", function() hubStrOn = "READ RANGER 1" TI_Innovator.Send("CONNECT RANGER 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ DHT 1", function() hubStrOn = "READ DHT 1" TI_Innovator.Send("CONNECT DHT 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ ACCEL 1", function() hubStrOn = "READ ACCEL 1" TI_Innovator.Send("CONNECT ACCEL 1 I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ BAROMETER", function() hubStrOn = "READ BAROMETER" TI_Innovator.Send("CONNECT BAROMETER I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn 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 = hubStrOn 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 = hubStrOn end},
},
{"Select Sound",
{"C4", function() hubStrOn = "SET SOUND 262 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"A4", function() hubStrOn = "SET SOUND 440 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"C5", function() hubStrOn = "SET SOUND 523 TIME 1" TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff 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) hubStrOff = "Aliens" msgStr = hubStrOff end},
{"BallGame (sample)", function() tempo = 1000 I=1 list1 = notes_bgame list2 = times_bgame playTone(list1,list2) hubStrOff = "Take me out to the Ball Game" msgStr = hubStrOff end},
{"Happy Birthday (sample)", function() tempo = 1000 I=1 list1 = notes_bday list2 = times_bday playTone(list1,list2) hubStrOff = "Happy Birthday" msgStr = hubStrOff end},
{"If I Only Had a Brain (sample)", function() tempo = 4000 I=1 list1 = notes_brain list2 = times_brain playTone(list1,list2) hubStrOff = "If I only had a brain" msgStr = hubStrOff end},
{"Fur Elise (sample)", function() tempo = 1000 I=1 list1 = notes_elise list2 = times_elise playTone(list1,list2) hubStrOff = "Fur Elise" msgStr = hubStrOff end},
{"Home on the Range (sample)", function() tempo = 1000 I=1 list1 = notes_home list2 = times_home playTone(list1,list2) hubStrOff = "Home on the Range" msgStr = hubStrOff end},
{"Well-tempered Scale", function() tempo = 1000 I=1 list1 = notes_welltemp list2 = times_scale playTone(list1,list2) hubStrOff = "Well tempered scale" msgStr = hubStrOff end},
{"Harmonic Scale", function() tempo = 1000 I=1 list1 = notes_harmonic list2 = times_scale playTone(list1,list2) hubStrOff = "Harmonic scale" msgStr = hubStrOff end},
},
{"TIMER Samples",
{"wait(5)", function() wait(5) end},
{"blink(5,1)", function() blink(5,1) end},
{"alarm(5,0.5)", function() alarm(5,0.5) end},
},
{"Robot Samples",
{"CONNECT ROBOT SERVOS (BB9/BB10)", function() showKeyBoard = false
TI_Innovator.Send("CONNECT SERVO 1 BB9")
TI_Innovator.Send("CONNECT SERVO 2 BB10") end},
{"ROBOT FD 1 secs", function() fd(1) end},
{"ROBOT BK 1 secs", function() bk(1) end},
{"ROBOT LT 1 secs", function() lt(1) end},
{"ROBOT RT 1 secs", function() rt(1) end},
{"AUTO drive (DISTANCE)", function() TI_Innovator.Send("CONNECT RANGER 1 IN1") drive = "RANGER" auto() end},
{"AUTO drive (BRIGHTNESS)", function() drive = "BRIGHTNESS" lightSeeker() end},
},
}
toolpalette.register(Menu)
screen:invalidate()
end

function on.arrowKey(key)

if key == "down" then
bk(1)
elseif key == "up" then
fd(1)
elseif key == "left" then
lt(1)
else
rt(1)
end
screen:invalidate()
end

function on.charIn(ch)

if ch == "l" then drive = "BRIGHTNESS" lightSeeker() elseif ch == "a" then drive = "RANGER" auto() end
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 STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 47