πŸš—Man-In-The-Loop Obstacle Avoidance Tutorial-JavaScript

Rover PWM Motor Drive Tutorial

Learn how to command the motors using the Motor Hat provided in your Rover kit.

This guide requires no prior programming experience and users should focus more on the concepts being introduced versus getting lost in the minutia of the scripting language.

Overview

In this guide you will learn how to manipulate and control the Rover based on the following basic concepts:

  1. How to generate PWM commands for the ROVER.

  2. Understand how the different PWM settings affect motor drive and direction.

  3. Visualize PWM concepts with an appreciation for generated outputs.

Mission

πŸ€– Lab: Robotics Speed & Control (rover.js)

In this lab, you will modify the JavaScript source code of your robot to see how variable speed affects handling, inertia, and "braking" distance.

πŸ› οΈ Step 1: Setting Up the Environment

  1. Open Chrome DevTools: Press F12 or Ctrl+Shift+I in your browser. This will allow you to monitor the Console for any code errors.

  2. Open the Source: Open rover.js in Visual Studio Code (or your preferred text editor).

  3. Identify the Logic: Locate the function MoveMotors(MTR1_PWM, MTR2_PWM, MTR3_PWM, MTR4_PWM) in the script.

    This function controls all four motors by assigning a PWM value to each motor:

    • MTR1_PWM – Motor 1

    • MTR2_PWM – Motor 2

    • MTR3_PWM – Motor 3

    • MTR4_PWM – Motor 4

    PWM values control both speed and direction and must be within the range -100 to 100:

    • 100 β†’ Full speed forward

    • 0 β†’ Stop

    • -100 β†’ Full speed backward

    • Values closer to 0 move slower; values closer to Β±100 move faster

    Example Motor Commands (Range: -100 to 100)

    Movement
    MTR1_PWM
    MTR2_PWM
    MTR3_PWM
    MTR4_PWM
    Description

    Forward

    100

    100

    100

    100

    All motors move forward at full speed

    Forward (slow)

    50

    50

    50

    50

    All motors move forward at half speed

    Backward

    -100

    -100

    -100

    -100

    All motors move backward at full speed

    Turn Right

    100

    -100

    100

    -100

    Left motors forward, right motors backward

    Turn Left

    -100

    100

    -100

    100

    Left motors backward, right motors forward

    Stop

    0

    0

    0

    0

    All motors stopped

Step 2: Modifying the Code

Default Speed Inputs: The motor speed inputs passed to the MoveMotors(MTR1_PWM, MTR2_PWM, MTR3_PWM, MTR4_PWM) function are typically set to 100 and -100, which represent full speed forward and full speed backward. These values are set in Rover.html.

Task: Modify the PWM input values passed into the MoveMotors() function in increments of 25, staying within the valid range of -100 to 100. For example, test values such as:

  • 25 and -25

  • 50 and -50

  • 75 and -75

  • 100 and -100

Example:

Update: After each change:

  1. Save the rover.js file

  2. Reload the rover.html page in your browser

  3. Reconnect to your ROV control board

Test: Attempt the obstacle course using each PWM setting. Observe how different input values affect the rover’s speed and control. Lower values allow more precise movement, while higher values increase speed.


🧐 Critical Thinking & Observations

πŸ›‘ 1. Where are the brakes?

If you look closely at the stop() function, you'll see it simply sets the speed to 0.

The Reality: There are no active mechanical brakes. The robot stops because of friction and inertia. At high speeds, the robot will "drift" or slide forward even after the power is cut.

πŸ›£οΈ 2. Speed vs. Handling (The "Highway" Effect)

Why do highway ramps have maximum speed signs?

  • Centrifugal Force: At high speeds, the robot (or a car) wants to keep moving in a straight line.

  • Control: Just like your robot, a car requires more distance to turn or stop as speed increases. High-speed settings make the robot "twitchy" and harder to navigate through tight obstacles.

In a car, your foot hits the brake and the response is nearly instant. In this lab, your command must travel through:

  1. JavaScript Execution β†’β†’ 2. Browser Processing β†’β†’ 3. Wi-Fi/Bluetooth Signal β†’β†’ 4. SCT Board.

  • The Lag: If there is even a half-second delay (latency), the robot may hit a wall before the "Stop" command ever reaches the wheels.


🏁 Time Trials: Leaderboard

Record your fastest clean run (no collisions!) for each speed setting.

Team Name

Speed Setting

Completion Time

Collisions

Export to Sheets


circle-info

This Rover GUI code demo contains two files:

rover.html

o This must be in the same folder as the JavaScript file

o Contains metadata about the extension and how to configure all the Gui items in the browser.

rover.js

o This module contains the underlying BLE communications.

o We will be iteratively modifying the functions specific to the Joystick buttons above.

Connect ROV Board

Click on the Bluetooth icon and make sure your device is broadcasting. Once broadcasting, click on the pair button to connect to the board.

Once you have a successful connection, the interface will report that a connection was successful.

After connecting, use the arrow keys to move the motors. Notice how the motors move as your move the commands. Press the stop button to cease all motion.

Rover.html Breakdown

Let's break down the contents of mthjoystick.html file. This code tells your browser how and where to place everything that you see on the screen demonstrated above. You can copy this code directly into a text editor and make sure to name it with the same convention above.

For starters you can see where the buttons are called out here starting on line 1, four buttons in total and one for each direction we desire to move the Rover. There are a number of styles and types that can be used to configure these buttons for a different shape and feel in the future.

rover.js Breakdown

The man-in-the-loop experiment calls for manipulating the drive strength of these buttons and observing the results as you maneuver around the obstacle course that you made. It is important when you learn to code that you appreciate certain aspects of the code while focusing your efforts on the specific regions of concern. All other sections in the mthjoystick.js javascript file maintain very low level details of the BLE layer that is beyond the scope of this exercise. Before proceeding further go ahead and copy this code into a text editor in its entirety and save it with the exact naming convention above.

man-in-the-loop exercise

To make this experiment more consistent, we will set the same PWM strength for all motors using the MoveMotors(MTR1_PWM, MTR2_PWM, MTR3_PWM, MTR4_PWM) function directly in the HTML buttons.

For example:

This command sets all four motors to a PWM value of 30, causing the rover to move forward at the same speed.

Experiment Procedure

Step 1: Save the Files Save the HTML file (rover.html) and JavaScript file (rover.js) to your local machine.

Step 2: Modify the PWM Values in the HTML Locate each button that calls the MoveMotors() function and update the PWM values so that all motors use the same strength. For example:

Ensure the values remain within the valid range of -100 to 100.

Step 3: Save and Reload

  • Save the updated rover.html file

  • Open or refresh the file in Google Chrome

  • Connect to your Rover control board

Step 4: Run the Obstacle Course Use the GUI buttons to maneuver the rover through the obstacle course.

Step 5: Record Your Observations For each PWM setting, record:

  • Number of obstacle collisions

  • Ease of controlling the rover

  • Ability to stay on course

  • Overall responsiveness

Repeat the Experiment

Repeat Steps 2–5 using PWM strengths in increments of 25:

  • 25 (slow speed)

  • 50 (medium speed)

  • 75 (fast speed)

  • 100 (maximum speed)

This will help you observe how PWM strength affects rover speed, control, and precision.

Last updated