Control a rover [Workshop]
Control a rover
1. Overview
This codelab is part of the Viam for educators program, and provides a suggested delivery plan and resources for instructors interested in delivering a hands-on workshop. Take these resources, and make it your own!
Workshop Overview
- Goal: Introduce components and services as concepts and operate a rover base using an SDK
- Audience: Beginner developers with basic programming and hardware skills
- Duration: ~2-3 hours
What You'll Teach
- How to build a TypeScript application to control a machine in Viam.
Watch the Video
See a demonstration of controlling the rover using a TypeScript app in this video.
Drive a rover using the Viam TypeScript SDK
2. What participants will need
Review the list of required components (hardware, software), and determine which materials are provided or required for participants to bring on their own. Provide instructions to participants, giving them ample time to prepare and complete prerequisites.
Hardware and supplies requirements
- 1 - Viam rover
- 1 - Raspberry Pi 4
- 1 - USB flash drive or microSD card to use with the Pi on the rover, along with an adapter or reader
- 4 - 18650 batteries (with charger) or an RC type battery with dimensions no greater than 142mm x 47mm x 60mm (LxWxH) (with charger)
Software and other device requirements
- A Linux, macOS or Windows computer that can run SDK code
- Preferred IDE, such as VS Code
- Node.js installed
- Sign up for a free Viam account, and then sign in to the Viam app
3. Pre-workshop setup for instructors
Learning Objectives
Review the suggested learning objectives, and adjust it according to your goals and audience.
- How to use environment variables with your Viam machine
- How to implement reusable configuration fragments
- How to use the Viam TypeScript SDK to control your machine
Agenda
Review the suggested agenda, and adjust it according to your goals and audience.
- Introduction (10 mins)
- Hardware Assembly (30 mins)
- Software Setup (30 mins)
- Hands-On Experiment (40 mins)
- Q&A/Wrap-Up (10 mins)
Pre-workshop checklist ✅
Review these setup steps as you're planning the workshop to help prepare for it.
- Planning and Logistics
- Verify equipment inventory: Decide which materials are provided or required for participants to bring on their own. Order any materials and supplies that are provided to participants.
- Determine rover allocation for individuals, groups, or virtual access: Determine whether rovers are provided for each participant, group of participant, or accessed virtually through Viam's Try Viam experience or at another external location.
- Confirm workstation setup: Verify the workshop room layout supports hardware work, such as tables, and space to drive the rovers. Ensure participants have power outlets and internet access. Have spare supplies, such as Raspberry Pis, adapters/readers, and cables, in case of failures. For virtual workshops, confirm logistics for breakout sessions, providing support, and checkpoints.
- Communicate prerequisites: Share a participant guide prior to the workshop with learning objectives and instructions for installing software and preparing laptops. Provide a checklist for participants to confirm readiness.
- Establish support channels: Determine how participants can get support for real-time troubleshooting (e.g. Slack or Discord channel for virtual workshops, additional helpers in the room for on-site workshops)
- Request Viam stickers: Don't forget to request Viam stickers for the workshop.
- Content Preparation
- Prepare workshop teaching materials: Review the Delivery Plan, sample slide deck, and provided resources. Tailor these materials to suit your participants' needs. Decide whether to present them to the group or provide them for independent reference, such as through a web browser or printed handouts.
- Determine extensibility: During the Hands-on Experiment portion of the delivery plan, there are many options to guide the workshop on a different learning path. Determine if this section will be self-guided exploration, a group exercise, or self-paced learning for participants to explore on their own after the workshop.
- Prepare code and documentation: Review the provided code and add comments as necessary.
- Review discussion topics: Review the suggested group discussion topics and adjust for your participants.
- Review quiz questions: Review the suggested quiz questions and adjust for your participants. Determine if knowledge assessment will be completed throughout the workshop or afterwards.
- Dry Run and Testing
- Test the workshop project: Run through the build following the prepared documentation to ensure it works as expected. Simulate common issues and prepare troubleshooting tips for these scenarios. If physical space is limited, consider sharing a rover or setting up the rover in a remote location to be viewed through a webcam.
- Bring backups: In addition to the backup supplies mentioned earlier during planning, make sure batteries are charged.
4. Delivery Plan - overview
Feel free to make a copy of this sample slide deck, customize it, and make it your own.
During the workshop, instructors can present this customizable slide deck (see above) tailored to your specific workshop needs. Alternatively, learners can follow step-by-step instructions and checkpoints independently by referencing the pages linked below in a web browser.
- Hardware Assembly
- Set up your Raspberry Pi
- Set up your Viam rover
- Software Setup
- Configure your machine
- Configure your Viam rover with a fragment
- Test drive your rover
- Hands-On Experiment
- Control your rover with TypeScript
- Customize your TypeScript web application
5. Delivery Plan - Hardware Assembly
Set up your Raspberry Pi
The Raspberry Pi boots from a USB flash drive (or microSD card). You need to install Raspberry Pi OS on a USB flash drive that you will use with your Pi. For more details about alternative methods of setting up your Raspberry Pi, refer to the Viam docs.
Install Raspberry Pi OS
- Connect the USB flash drive (or microSD card) to your computer.
- Download the Raspberry Pi Imager and launch it.
- Click CHOOSE DEVICE. Select your model of Pi, which is Raspberry Pi 4.
- Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
- Click CHOOSE STORAGE. From the list of devices, select the USB flash drive you intend to use in your Raspberry Pi.
- Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS.
- Check Set hostname and enter the name you would like to access the Pi by in that field, for example,
rover. - Select the checkbox for Set username and password and set a username (for example, your first name) that you will use to log into the Pi. If you skip this step, the default username will be
pi(not recommended for security reasons). And specify a password. - Connect your Pi to Wi-Fi so that you can run
viam-serverwirelessly. Check Configure wireless LAN and enter your wireless network credentials. - Select the SERVICES tab, check Enable SSH, and select Use password authentication.
- Save your updates, and confirm
YESto apply OS customization settings. ConfirmYESto erase data on the USB flash drive. You may also be prompted by your operating system to enter an administrator password. After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to the USB flash drive. - Remove the USB flash drive from your computer when the installation is complete.
Connect with SSH
- Place the USB flash drive into your Raspberry Pi and boot the Pi by plugging it in to an outlet. A red LED will turn on to indicate that the Pi is connected to power.
- Once the Pi is started, connect to it with SSH. From a command line terminal window, enter the following command. The text in <> should be replaced (including the < and > symbols themselves) with the user and hostname you configured when you set up your Pi.
ssh <USERNAME>@<HOSTNAME>.local
- If you are prompted "Are you sure you want to continue connecting?", type "yes" and hit enter. Then, enter the password for your username. You should be greeted by a login message and a command prompt.
- Update your Raspberry Pi to ensure all the latest packages are installed
sudo apt update
sudo apt upgrade
Enable communication protocols
- Launch the Pi configuration tool by running the following command
sudo raspi-config
- Use your keyboard to select "Interface Options", and press return.
- Enable the relevant protocols to support our hardware. Enable the I2C protocol on your Pi to get readings from the power sensor and accelerometer when controlling your rover.
- Confirm the options to enable the serial login shell and serial interface. And reboot the Pi when you're finished.
sudo reboot
Set up your Viam rover
Follow these steps to assemble your Viam rover.
Add the power supply: Turn the rover over so that you can see the battery housing. Place four 18650 batteries (taking care to ensure correct polarity orientation) inside the battery pack to provide power to the rover, which can be turned on and off through the power switch.
To be able to attach the Raspberry Pi, unscrew the top of the rover with the biggest Allen key.
Use the smallest Allen key and the provided M2.5 screws to attach the Raspberry Pi to your rover through the standoffs on the motherboard. The Raspberry Pi 4 should be mounted such that the USB ports are to the right, as viewed from above.
Use the ribbon cable to connect the Raspberry Pi 4 to the motherboard.
Connect the webcam's USB lead to any USB port on your Pi.
Screw the top plate back on with the biggest Allen key.
Use the power switch to turn the rover on. Wait a second for the rover motherboard to power on. If the Pi has power, the lights on the Raspberry Pi will light up.
6. Delivery Plan - Software Setup
Configure your machine
- In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine.
- Click View setup instructions.
- Install
viam-serveron the Raspberry Pi device that you want to use to communicate with and control your rover. Select theLinux / Aarch64platform for the Raspberry Pi to control the rover, and leave your installation method asviam-agent. - Use the
viam-agentto download and installviam-serveron your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi. The setup page will indicate when the machine is successfully connected.
Configure your Viam rover with a fragment
- In the Viam app, find the CONFIGURE tab. It's time to configure your hardware.
- Click the + icon in the left-hand menu and select Insert Fragment to browse the available configuration fragments.
- Select
ViamRover2-2024-rpi4-a. - Click Insert fragment again to add the fragment to your machine.
- Notice adding this fragment adds several hardware components to your machine's JSON configuration. Each component is listed in the left sidebar, along with a corresponding panel on the right with more details.
- Click Save in the top right to save and apply your configuration changes.
Test drive your rover
- Under the CONFIGURE tab of the Viam app, some resources have a TEST section on the bottom half of their configuration pane which you can expand and interact with to test out controlling the component. When you configure a component on the CONFIGURE tab, it will also appear on the CONTROL tab which gives you an interface to interact with it.
- Find the CONTROL tab. This is where you can control your Viam rover. Find the
viam_basein the left sidebar, and click on it. Notice the components in the left sidebar correspond to a more detailed view on the right side of the page. - Within the
viam_basecard, toggle on the Keyboard control so that you can control the rover using keyboard keys WASD. - Continue exploring the different components of your rover.
7. Delivery Plan - Hands-on Experiment
Control your rover with TypeScript
Now that you know how to manually control the rover using the Viam app, let's control the rover programmatically with code using one of Viam's SDKs. You can run control code from anywhere; it does not necessarily have to be run on the same machine that runs viam-server.
- From the command line within your terminal window of your laptop (not SSH'd into your Pi), let's use a command line utility to quickly set up a new TypeScript project. Input the following command:
$ npm create robot-app
- At the command-line prompt, input your project name. Or hit Enter to accept the default project name,
my-viam-project. - Select the project template
controlRover, and hit Enter. - Once the project setup is completed, follow the command-line instructions. Navigate into your new project directory. In the example shown below, we are using the default project name
my-viam-project.
$ cd my-viam-project
- In the Viam app, find the CONNECT page, select the TypeScript SDK, and turn on Include API key to reveal your machine credentials within the code sample. Use these machine credentials to update the
.envfile in your new project directory, and save your changes. - Start the application server. Your web app should be running at
http://localhost:5173/
$ npm start
- Open your browser's devtools, for example, by right-clicking and selecting Inspect, to see the browser's console logs. Make sure there is adequate space surrounding your rover. And then press the Click me button in the web application to see what happens to your rover.
Customize your TypeScript web application
Now it's time to customize the TypeScript web application and make it your own. In your preferred IDE, consider the following suggestions for adding more functionality to your web application:
- Program the business logic: Edit
src/main.tsto change the logic being run.- Consider moving the rover along a different route using the Viam
baseAPI, for example,spinthe rover. - Use the Viam
SLAMservice for mapping or the Viam Navigation service for GPS - Integrate with third-party cloud services and APIs to enhance the functionality of your robot, such as voice control, data logging, or GPS tracking.
- Consider moving the rover along a different route using the Viam
- Program the frontend: Edit
index.htmlto change the layout of the web application.- Add descriptive text to display each action directly on the webpage, instead of logging it to the console.
- Display a camera feed within your web application using the Viam
cameraAPI
8. Additional resources and troubleshooting
Additional resources
- The website pinout.xyz is a helpful resource with the exact layout and role of each pin for Raspberry Pi. When working with Viam, make sure to reference the physical pin numbers, and not the GPIO numbers listed on
pinout.xyz. - The demo video provides a brief overview of the project.
- Viam APIs provide consistent methods of working with components, such as a
base,camera, and services, such ascomputer vision. Refer to the Viam documentation for API reference for all components and services. - Besides TypeScript, Viam provides SDKs in various programming languages to write control code for the backend, frontend, and mobile applications.
Common pitfalls and troubleshooting guidance
- Safety
- Lithium-ion batteries may pose a flammable hazard. This product requires four 18650 lithium-ion batteries OR an RC-type battery. DO NOT connect multiple power sources simultaneously. Refer to the battery manufacturer's operating instructions to ensure safe operation of the Viam Rover.
- Security
- Do not share your API credentials publicly. Sharing this information could compromise your system security by allowing unauthorized access to your machine, or to the computer running your machine.
- Flashing firmware
- Ensure everyone remembers the
hostnameandusernamethey set while flashing their Raspberry Pi, as they will need this when they SSH into the Pi.
- Ensure everyone remembers the
- Alternative setups
- If you're not using a Raspberry Pi 4, you can use different boards with your Viam Rover 2 with some alternative board configurations.
- Configuring machine and peripherals
- If any problems occur while setting up the machine and peripherals in the Viam app, check under the LOGS tab to see what might be going wrong.
9. Post-Workshop Resources
Group discussion topics 🗣️
Review suggested topics for discussion at key points during the workshop.
- What were the biggest challenges participants faced during the workshop (e.g., assembly, coding, debugging)?
- Share troubleshooting experiences and how participants resolved issues.
- What components are included in the configuration fragment, and how could we use them?
Quiz questions ❓
Review the suggested quiz questions below to evaluate participants' understanding of key concepts. These questions can be used to assess knowledge either during (see multiple choice answers in the delivery plan) or after the workshop.
- Hardware Assembly
- What is the purpose of setting a custom
hostnameduring the Raspberry Pi OS configuration? - Which protocol must be enabled on the Raspberry Pi to allow it to communicate with the power sensor and accelerometer?
- What is the purpose of setting a custom
- Software Setup
- Why is it important to connect your machine to the Viam app during setup?
- What is a benefit to using a Viam configuration fragment?
- Hands-on Experiment
- What is the primary role of
viam-serverin the system architecture of your rover setup? - What is the purpose of the
.envfile in your TypeScript project?
- What is the primary role of
Next-level projects
Here are some ideas for expanding and enhancing the current project to create autonomous behaviors.
- Detect color with a webcam guide: to add this capability to your rover to respond to the environment
- Navigate with a rover base tutorial: to set up user-defined waypoints for your rover to move to in the order that you specify or dynamically adapt using inputs like GPS or vision
- Train a custom ML model guide: to enable object detection or classification and program the rover to act accordingly, such as moving toward or avoiding detected objects
- Add sensors and other components to extend the functionality of your rover
- Use the Viam mobile SDK to build an iOS or Android application to control the machine's components
Post-workshop resources for participants
- Viam documentation for reference
- Viam Discord community for inspiration and support
- Viam how-to guides for task-based learning, codelabs for hands-on learning, and tutorials for project-based learning.