πAI Vibe Coding Exercise
AI + RoverSE Robot (JavaScript)

Description
This flexible activity menu is designed to teach instructors how to harness large language models (LLMs) like ChatGPT to generate and refine JavaScript code that drives a four-motor robot. Each self-contained activity focuses on a core AI or robotics concept, pairing a clear learning objective with hands-on prompt exercises and real-world testing. Instructors can pick and choose which activities to run and in what order to fit their schedule and learnersβ needs.
Overview
This guide covers:
Generating PWM commands for the ROV.
Understanding how PWM settings affect motor drive and direction.
Running user-generated JavaScript using LLM output based on guided input.
Debugging Javascript
Connect RoverSE Board
Before starting the activities, save RoverLLM.html and RoverLLM.js to your local machine in the same folder by copying and saving the files below. After launching, the following user interface will be displayed.
Note: This application uses the Web Bluetooth API, which is supported only in Chromium-based browsers on desktop platforms. For optimal compatibility, use:
Google Chrome version 89 or later
Microsoft Edge version 89 or later

Click the Bluetooth icon, then press the button on the ROV Board to start broadcasting. Select your device and click pair.

If this is your first time connecting onto this device on windows, you will be presented dialog windows to grant pairing permission. Tap the popup initiate the process.

Then, click the allow button.

Then, clock close.

You should not be presented with these options in the future for this board.
If you do not complete this step, the robot will not stay connected.
Once connected, the log will indicate "connected" and all buttons will be enabled. You can use the arrow keys to move the robot forward, backward, left, and right. Use the stop button to halt all motion.
As the robot moves, the log outputs values like βM1: 0 | M2: 0 | M3: 0 | M4: 0β:
M1 = Motor1 PWM value
M2 = Motor2 PWM value
M3 = Motor3 PWM value
M4 = Motor4 PWM value

To report distance values using the MODI sensor, ensure that the MODI is attached to your ROV board. If the sensor is enabled, a distance value will be reported in millimeters. If the value remains at 0, it may be necessary to enable the sensor. Enable the sensor by clicking the "Modi on" button. Pressing this button will produce the following output:

Press the Bluetooth button to reconnect the device. Once connected, press the enable Telemetry button. If the Modi is properly connected, distance values will appear as objects are placed in front of the sensor.

To execute a predefined set of commands found in the RoverLLM.js file, click the Run My Program button. This functionality will be elaborated on later. Currently, the function moves the robot forward for three seconds at 30% speed before coming to a stop.
JavaScript and Debuggers
In this course, you will be programming your Rover using Javascript, one of the most widely used programming languages in the world. JavaScript is used to power websites, control devices, and build real-time interactive systems β including robots like your Rover.
When you write code, the computer follows your instructions exactly. But sometimes, your program may not behave the way you expect. This is completely normal β even professional programmers encounter this every day. Finding and fixing these issues is called debugging.
A debugger is a tool that helps you see what your program is doing step-by-step. It allows you to:
Pause your code while it is running
Inspect values stored in variables
Follow the exact sequence of instructions
Identify where problems occur
Fix issues more quickly and accurately
Using a debugger helps you understand not just what your code is doing, but why it is doing it.
In the next video, you will see how a debugger works and how it can help you troubleshoot and improve your Rover programs. Learning how to use a debugger is one of the most important skills you can develop as a programmer.
Joystick Code
Use the copy feature to save copy the contents of this file and save on your device as RoverLLM.html/RoverLLM.js. from both of the code blocks below.
Once you have copied RoverLLM.html/RoverLLM.js to your machine, connect your Rover and use the Javascript Debugger and look for the outputs in the console as your use the program.
On your machine, you should have two files save in the same directory as illustrated below.

If you have reached this section, you have successfully connected to your robot and issued basic commands. Now, we will use AI to develop some interesting use cases with our JavaScript Module. For each activity listed below, you will replace the StartProgram() function with a new custom version based on your AI prompts. To accomplish this:
Create a new version of your RoverLLM.html/RoverLLM.js files specific to your activity.
Replace the StartProgram() function in the template JavaScript file with the code generated from your AI prompts.
Activity 1 β LLM Setup for Coding
Objective: Configure the LLM to output code using your ROV-specific API.
Prerequisites:
Copy RoverLLM.html to activity1.html.
Copy RoverLLM.js to activity1.js.
Open activity1.html, replace `src="RoverLLM.js"` with `src="activity1.js"` towards the bottom of the file, and save.

Prompt to ChatGPT:
This prompt should return back some sample code you can try.
Next Steps
Open activity1.js in a text editor, replace the StartProgram() function with the provided code, and save the file.
Navigate to the file system, select activity1.html, and open it in a web browser.
Pair with the device.
Click the "Run My Program" button to execute the code.
Activity 2 β Sensing & Reactive Behavior
Objective: User Distance to control movement
Prerequisites:
Copy RoverLLM.html to activity2.html.
Copy RoverLLM.js to activity2.js.
In activity2.html, replace `src="RoverLLM.js"` with `src="activity2.js"` at the bottom of the file and save.

Prompt:
Next Steps:
Open activity2.js in a text editor, replace StartProgram() with ChatGPT's code, and save it.
Open activity2.html in your web browser.
Pair with your device.
Click "Run My Program" to execute the code.
Activity 3 β Random Movements
Objective: generate an algorithm with ordered steps based on user input.
Prerequisites:
Copy `RoverLLM.html` to `activity3.html`.
Copy `RoverLLM.js` to `activity3.js`.
In `activity3.html`, replace `src="RoverLLM.js"` with `src="activity3.js"` at the bottom of the file, then save.

Prompt:
Next Steps
Open activity3.js in a text editor, replace the StartProgram() function with the provided code, and save the file.
Open activity3.html in your web browser.
Pair with the device.
Click "Run My Program" to execute the code.
Last updated