Programming with Toit

Your prototype should now be ready to program.

What you will learn

  • Programming with Toit
  • Controlling a differential drive robot

Terminology

over-the-air (OTA) updates
a wireless method for updating software on a device
differential drive
a drive system in which a robot separately controls the left and right motor speeds

Lecture

Before you watch the videos below, you should skim the Toit Programming Language description.

Troubleshooting

You may need to try the following:

  • Make certain that your antenna is connected securely to the board
  • Try the PomonaIoT network
  • Make sure that your computer is on the same network as the microcontroller
  • Try the local network (no access to Internet)
  • Try the jag run ... command several times

Programming with Toit

Key steps from the video:

  1. Install Toit and Jaguar
  2. Initialize Jaguar: jag setup
  3. Plug your XIAO ESP32-S3 board into your computer
  4. Flash the board: jag flash --chip esp32s3 (SSID: Claremont-ETC; password on Slack)
  5. Clone the course code: git clone https://github.com/anthonyjclark/MobileRoboticsCode
  6. In two separate terminals:
    • Monitor the serial output with: jag monitor
    • Run a program with: jag run FILE --device IP
Save the Wi-Fi credentials

Use the following command to save the Wi-Fi credentials so that you only need to type them once:

jag config wifi set --wifi-ssid SSID --wifi-password PASSWORD

Making your wheels move

Key steps from the video:

  1. Disconnect the USB cable
  2. Insert the battery
  3. Place the robot on β€œblocks”
  4. Keep an eye on the voltage tester
  5. Turn on the switch found on the expansion board
  6. Run the blink demo
  7. Turn on the switch to the motor power
  8. Run the motor demo
Viewing the serial output

Try running the motor script once while connected and once while disconnected from USB. When disconnected you will not be able to view the output of the program.

Toit Library Code

The code found in the MobileRoboticsCode repository is setup so that you can use it as a set of Toit packages.

Here is a listing of the MobileRoboticsCode directory (as of this chapter and with some files removed for clarity):

πŸ“‚ ./
β”œβ”€β”€ πŸ“‚ communication/
β”‚  β”œβ”€β”€ communication.toit
β”‚  └── demo.toit
β”œβ”€β”€ πŸ“‚ display/
β”‚  β”œβ”€β”€ demo.toit
β”‚  └── display.toit
β”œβ”€β”€ πŸ“‚ hello/
β”‚  └── hello.toit
β”œβ”€β”€ πŸ“‚ led/
β”‚  β”œβ”€β”€ demo.toit
β”‚  └── led.toit
β”œβ”€β”€ πŸ“‚ motors/
β”‚  β”œβ”€β”€ demo.toit
β”‚  └── motors.toit
└── πŸ“‚ pinout/
   └── pinout.toit

The Toit convention is to name your package file the same as its parent directory (eg, the motors directory contains a motors.toit file). You can then import the package using a relative path. For example, I import the pinout package in motors.toit with import ..pinout.

Here is an example of creating a new program using the provided packages:

cd MobileRoboticsCode
mkdir rotate
cd rotate
touch demo.toit

And here are the contents of demo.toit:

import ..motors

main:
  motors := Motors

  while true:
    motors.left-motor.set-speed 0.5
    sleep --ms=5_000

Which can be run with:

jag run demo.toit --device IP

See Toit package creation tutorial for more information.

Exercise

This exercise is a continuation from our previous chapter.

You will submit your responses on gradescope. Only one partner should submit. The submitter will add the other partner through the gradescope interface.

Additional details for using gradescope can be found here:

You should open the gradescope assignment now so that you know what to work complete.

Grading

I will grade all exercises using a scale of β€œNailed It” / β€œNot Yet”. See the course grading policy for more information, and check gradescope for deadlines.

Overview

Today’s portion of the exercise will focus on programming your microcontroller. Please first review the safety tips from the previous chapter.

After you review the safety tips, your tasks are to:

  1. Setup your programming environment
  2. Run the blink demo
  3. Run the motor demo
  4. Create a new program to make your robot turn in-place

Setup your programming environment

We are using the Toit programming language. Please read through their instructions for getting started and setting up Visual Studio Code.

You have two questions to answer on gradescope after flashing Toit on your device.

Running the Demos

Once your development environment is ready, you will run the hello and led demos.

I recommend first creating an appropriate folder on your computer. For example:

mkdir -p ~/Classes/MobileRobotics/
cd ~/Classes/MobileRobotics/

And then cloning the course code:

git clone https://github.com/anthonyjclark/MobileRoboticsCode

Now you can monitor your device in one terminal and run the led demo in another:

# In terminal 1
jag monitor

# In terminal 2
cd ~/Classes/MobileRobotics/MobileRoboticsCode/led
jag run demo.toit --device IP

Once you are comfortable with the led demo, you can move on to the motors demo. But first make sure that your wheels are not touching the ground (or table).

# In terminal 2
cd ~/Classes/MobileRobotics/MobileRoboticsCode/motors
jag run demo.toit --device IP

Before moving on to the next step, make sure you answer the questions on gradescope.

Creating a new program

The final part of this exercise is to create a new program that makes your robot turn β€œin-place” (a zero-degree turning radius) using the motors package.

You should:

  1. Write your code.
  2. Have your code reviewed by another group.
  3. Show your code to the instructor.
  4. Get a battery from the instructor.
  5. Run your code on your robot.

Don’t forget to finish up your answers on gradescope.

Wrap-Up

You should now have a mobile robot capable of moving around the room!

Resources