Home 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 (BB4, BB8, BB9, BB10), the HC-SR04 ultrasonic sensor (BB6 TRIG and BB5 ECHO) and colour input sensor (BB3 for the Colour Sensor white LED and I2C for the Adafruit TCS34725). Further investigation is required to see how the gyroscope is 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

  
  

Hacking the Rover™

Breadboard header connectors */
BB1 14
BB2 15
BB3 7 (ColorInput Sensor White LED)
BB4 18 (Left wheel forward)
BB5 13 (HC-SR04 Ultrasonic sensor ECHO)
BB6 12 (HC-SR04 Ultrasonic sensor TRIG)
BB7 33
BB8 40 (Left wheel back)
BB9 38 (Right wheel forward)
BB10 39 (Right wheel back)

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 STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 45