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:
- Install Toit and Jaguar
- Initialize Jaguar:
jag setup
- Plug your XIAO ESP32-S3 board into your computer
- Flash the board:
jag flash --chip esp32s3
(SSID:Claremont-ETC
; password on Slack) - Clone the course code:
git clone https://github.com/anthonyjclark/MobileRoboticsCode
- In two separate terminals:
- Monitor the serial output with:
jag monitor
- Run a program with:
jag run FILE --device IP
- Monitor the serial output with:
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:
- Disconnect the USB cable
- Insert the battery
- Place the robot on βblocksβ
- Keep an eye on the voltage tester
- Turn on the switch found on the expansion board
- Run the blink demo
- Turn on the switch to the motor power
- Run the motor demo
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:
- Setup your programming environment
- Run the blink demo
- Run the motor demo
- 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:
- Write your code.
- Have your code reviewed by another group.
- Show your code to the instructor.
- Get a battery from the instructor.
- 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!