LEGO Spike Prime Robotics

Introduction & Connection

Meet your robot, form a functional team, learn how to power up the hub and connect it to the Spike Prime software over Bluetooth.

What is a robot?

A robot is a machine that can be programmed to carry out tasks automatically. What separates a robot from an ordinary machine is that a robot can sense its environment, think (process that information using a program), and then act by moving or responding, all without a human steering every step.

Sense
Gathers information about its surroundings using sensors, like distance, colour, and force.
Think
Runs your program to make decisions based on what the sensors are reporting.

Over the coming lessons you will build up all three of these skills with your team's LEGO Spike Prime robot, finishing with an autonomous navigation challenge.

Priorities for today

Step 1: Form your team

Arrange yourselves into functional groups of 2 or 3. Choose carefully: the behaviour of one member impacts everyone in that group. If your team cannot work productively together, every member's progress (and assessment) suffers.

2 members ✓3 members ✓

Once your group is settled, collect your robotics gear and get familiar with the basics.

An assembled LEGO Spike Prime driving base robot
Your Spike Prime driving base

Getting connected

Bluetooth to laptop

We will be coding through the Spike Prime online platform, connecting to the robot over Bluetooth. This keeps your robot free to roam. No cables, no tethering.

  • Open the online platform in Chrome
  • Sign in and create a new project
  • Connect to your team's robot only (the robot's name matches your team number)
Open the Spike Prime platform ↗
LEGO Education Spike app icon

The hardware

The yellow hub is the brain of your robot. It holds the battery, the light matrix display, and the ports that your motors and sensors plug into. Two controls matter today:

Turning the robot on
Press and hold the large centre button on the hub until the light comes on and the hub plays its start-up sound. To power off, hold the same button until it shuts down.

Watch this walkthrough of the hub hardware before you power up:

WATCH: Spike Prime hub — power & Bluetooth▶ YouTube

The Spike Prime tutorials

The Spike Prime software has excellent inbuilt tutorials. They will teach you:

  • The basics of coding with Spike Prime blocks
  • How to connect your robot to the Spike Prime software
  • How to understand the sensor values shown on the screen

This video shows you how to connect and where to find the tutorials:

WATCH: Connecting & accessing the tutorials▶ YouTube

Your tasks

Task: Tutorials 1–5
  1. Open the Spike Prime platform in Chrome and choose "Start".
  2. Work through the inbuilt tutorials 1–5.
The Spike lobby screen with the Start button
Choose "Start" from the lobby
The Spike software homepage showing the tutorial list
Complete tutorials 1–5
Note: You may need to adjust the port letter that each sensor is connected to, so your code matches your robot's actual wiring.
Extension: Finished tutorials 1–5? Continue on to Tutorial 6.

Assessment

Across this unit you will complete the following assessments:

  1. The square (assessed in class)
  2. Collision avoidance (assessed in class)
  3. Line follower / Map navigation (film + code screenshots uploaded) — this is your main assessment!
Bonus: There are additional challenges scattered throughout this unit. If you complete them, include them in your main assessment as they may help to bump your result higher!!!
LEGO Spike Prime Robotics

Basic Movement & Sensing

This lesson is all about precision: getting your robot to move dead straight, and to make accurate, repeatable turns.

Start of lesson (5 minutes)

  1. Collect your team's robot
  2. Open the Spike Prime software in Chrome
  3. Click on the "New Project" button (directly underneath the "Start" button you selected for the tutorials), and select the "Word Blocks" option.
The New Project dialog with the Word Blocks option selected
Select "Word Blocks", then "Create"
Animation of a robot connecting
  1. Connect the software to your robot via Bluetooth
Which robot is yours? The robot's name is the same as your team number — Team #4 will be using "Robot 4".

Self-learning skill development

Skill 1

Moving straight

Find out how to code your robot to move forward and backward. Using the movement blocks, you can be very accurate with the distance that your robot travels.

Skill 2

Accurate turns

Once you can control the basic movement of a robot, we need to be able to get it to turn — accurately, and by the same amount every time.

Class brainstorm

What does the code shown below do? In your groups, look at this code and devise answers to the following:

Spike Prime code blocks with values highlighted in coloured boxes
  • Is the green rectangle necessarily accurate for your robot?
  • Why would we only have a 30% movement speed?
  • Stop options are [brake], [hold position] and [coast] — what do you think these mean?
  • The stop options aren't visible by default, but they can be added by clicking on "Show block extensions" at the bottom of the code options, and then selecting "More Movement" (+ you may need to scroll down to see them!)
The Show block extensions icon at the bottom of the code options The More Movement extension card: set the individual motor speed on a Driving Base or make it hold its position

And what do you think this code does? Look closely at the last few blocks — what has been added, and what does the "right: 100" value control?

Spike Prime code that moves forward, stops, then starts moving with a steering value of right: 100
  • What happens after the robot stops the first time?
  • The steering dial is turned all the way to right: 100 — what kind of turn do you think this produces?
  • What would you change to make the robot turn the other way?

Assessment challenge: The square

Assessment

Starting with the code shown above, program your robot to move around a 40cm-sided (approximate) square. The robot should finish in the same location and orientation (facing the same direction) as it started. You will need to test, and adjust as necessary, depending upon the speed that your robot is travelling at whenever it makes a turn.

Developing Consolidating Extending

Low: Copied the code, modified to be suitable for your robot, with only 1 turn

High: Added straight-line movement before/after the turn

Low: All sides of the "square" are travelled on by the robot, but the angles are not close enough to right-angles.

Middle: All sides of the "square" are travelled on by the robot, with close proximity to right-angle turns.

High: Your code used "repeats" to avoid unnecessary duplication of the same set of commands. The robot faces the original direction at the end.

A repeat block used for the square path

Low: Additional utilisation of the ultrasonic sensor to stop any movement whilst an object is detected in the path of travel.

High: Once the object moves away, the robot resumes moving along the path of the square.

A wait-until block using the distance sensor A code block for resuming movement
LEGO Spike Prime Robotics

Environmental Detection & Navigation

Give your robot senses: use the distance, colour, and force sensors to detect the world around it and react.

The distance sensor

We will now be utilising a distance (ultrasonic) sensor to avoid a collision.

It works by detecting objects at various distances by sending out a high-pitched sound (our ears can't hear it), and measuring how long it takes for the sound to bounce back (i.e. echo). This measurement of time is then converted into a measurement of distance. More information is available in the video below.

The Spike Prime ultrasonic distance sensor

Nature invented this trick first. Dolphins and bats both use echolocation to sense their world:

WATCH: Echolocation — how dolphins & bats "see" with sound▶ YouTube

Try it: Collision avoidance

Copy this sample code into a new program/project in the Spike Prime software. Modify it to be suitable for the ports that your peripheral devices are connected to, and then test it out.

Sample Spike code: move forward until the distance sensor detects an object, then stop
Challenge 1: How close can you get your robot to an obstacle and stop, before hitting the obstacle? Do you need to add in an additional command to make the robot stop more abruptly?
Challenge 2: Can you program the robot to turn 180 degrees and move away from the obstruction after it initially stops?
Developing Consolidating Extending

Low: The sample code above has been copied in.

High: The necessary port modifications to suit your default hardware setup have been made (i.e. check which ports the motors and ultrasonic sensor are connected to).

Low: Your robot stops just before colliding with an object.

Middle: Your robot stops and then turns away from the object (180 degrees), and then stops.

High: Your robot continues to move around, avoiding the obstacles that it senses.

Low: Your robot pauses for 2 seconds when it encounters an object, and then resumes its forward movement when the object is removed. If the object remains after 2 seconds, the robot turns to face a different direction, and then continues to move in the new direction.

High: Your robot continues this process in a loop, but each time the angle is randomly generated. As soon as the object in front of the robot moves away (if < 2 seconds), the robot continues moving forwards before the 2 seconds have lapsed.

Tip:

A pick-random-number block

Colour and force sensors

The final 2 sensors that we'll be looking at in this lesson are the colour and force sensors.

The Spike Prime colour sensor

The colour sensor

  • Can detect 8 unique colours, as well as no colour
  • The optimal distance from the ground is approximately 16mm
  • 2 modes: Colour Mode and Reflected Light Mode
    • Colour: Senses the colour immediately in line with the sensor lens
    • Reflected: Provides a % value of light reflected from the sensor's inbuilt white LED lights
  • You can test for the values that your sensor will return by viewing your sensor list next to the "Connect" button
The toolbar showing the Connect button

In the example shown below, the sensor is connected to Port C, and is returning a White / 10 value.

The sensor list showing Port C returning White and 10

You can change the data that is returned by each sensor, by clicking on the connection button, and then clicking on the drop-down arrow beside each sensor:

The connection button The drop-down menu of sensor data options
The Spike Prime force sensor

The force sensor

  • Can be configured for 2 types of sensing:
    • Touch sensing (pressed, hard-pressed)
    • Force sensing (can be measured in % or Newtons)
  • 3 modes for detecting touch:
    • Pressed — even a gentle tap is detected
    • Hard-pressed — pressing the sensor about 60% in
    • Released — the sensor is not pressed
Force sensor pressed mode blocks Force sensor hard-pressed mode blocks Force sensor released mode blocks

Sensor challenges

Challenge 1

Program your robot to move forward until the sensor sees black (or, at least a different colour than what the carpet is).

Hint: Keep the movement speed low, so that your robot doesn't over-shoot the targeted colour. The following block of code will need to be used:

A wait-until block using the colour sensor to detect black
Tips:
  • you might need to change the sensor port
  • you might need to change the sensed colour from black to white, as the carpet will normally return a "black" value.
Challenge 2

Program your robot to move at 10% speed, moving forward until you press the sensor with your hand. Both "pressed" and "hard-pressed" modes will work for this.

The following commands can be used (you'll just need to get these into the correct order, but you may need to adjust the ports):

A set of jumbled Spike code blocks to be arranged into the correct order
Extension Challenge 3

Combine these both together, by creating a program that moves until it senses white, then stops, then resumes forward movement when the force sensor is pressed.

LEGO Spike Prime Robotics

Main Assessment — Line Follower + Map Navigation

This challenge will form a large component of your overall assessment in this course, and as such, you have extra time allocated to work on implementing your solution.

Choose your challenge

You have a choice of which challenge your team wishes to try. Both are assessed against the merged rubric at the bottom of this page.

Task A

Line Follower

Program an autonomous vehicle that follows the edge of a black line, then extend it with sensors, timing, and smart return behaviour.

Task B

Map Navigation

Navigate the full course map: the bottom pathway, the winding upper-left course, and the bridge junction with its red colour triggers.

Task A: Line Follower

To develop an autonomous vehicle, our algorithm uses the sensor input to make the necessary adjustments to the motion of the motors.

Look at the plan for getting a robot to follow a black line. Why would we not have the robot drive straight ahead when it senses black?

A flow plan for the line-following decision: see black turn one way, see white turn the other
Diagram of a robot zig-zagging along the right edge of a black line
Challenge: Write a program that follows the right edge of a line.
  • If your sensor sees black, turn right
  • If your sensor sees white, turn left
  • Use an If-Else block to make the decision
  • Repeat the line follower forever
  • Use Colour Mode or Reflected Light Mode (Note: RLM might work better)
Sample Spike code for a line follower using an if-else block inside a forever loop

Task B: Map Navigation

Your robot will navigate the course map shown below. It begins with the bottom pathway, winds through the upper-left course with its sharp bends, and finishes at the bridge junction, where red markers trigger special behaviour.

The LEGO course map, view 1
The course map
The LEGO course map, view 2
The course map, with the pathways highlighted
Remember: Your robot should be able to achieve everything at the lower levels — if it is assessed as Consolidating, it should have also achieved the Developing-Low, Developing-High, and Consolidating-Low requirements.
Whiteboard pseudocode: Create a variable. variable = 0. Forever: if not red and variable < 2, then if black, motor turns one way, else motor turns the other way (you already have this code). If red and variable = 0: turn, wait, variable = 1. If red and variable = 1: stop, variable = 2.
Pseudocode for the red marker behaviour

Assessment rubric (both tasks)

Find the row for the task your team has chosen. Each level assumes the levels before it have been achieved.

Developing Consolidating Extending
Task A
Line Follower

Low: Copy the code shown above.

High: Copy the program shown above, modifying the sensor ports to work with your robot. The robot follows the black line for a few seconds.

Low: The robot starts to move when the force sensor has been pressed, and then successfully follows the line.

Middle: When the robot detects an object within 10cms, it stops.

High: When the object is removed, the robot continues moving.

Low: If the object remains for longer than 2 seconds, the robot turns 180 degrees and follows the line back to the starting position.

High: A system is implemented that uses variables to keep track of the amount of time that the robot is traveling in one direction until it encounters an object. This same time is then used to restrict how far back the robot can travel after the 180-degree turn.

Task B
Map Navigation

Low: The robot follows the right-hand side of a segment of a line.

High: Code execution starts with the push button, and the robot traverses the entire bottom pathway.

The bottom pathway of the course map

Low: Successful navigation of the first 6 bends of the upper-left course.

Middle: Navigation of the entire path of the upper-left course.

High: Use of the ultrasonic sensor to assist with navigating the sharp bends (and the display lights up to show when this has been triggered). Objects will be placed near the corner shown with a blue square.

The upper-left course with its sharp bends

Low: Your robot can navigate to the bridge junction. When the robot senses the 1st red colour, it automatically turns anti-clockwise ~90 degrees, and then continues to navigate the black line.

High: The 2nd time that your robot senses red (at the bridge junction), it stops and then displays how long it has taken to complete the entire course on the hub display (in seconds).

The bridge junction on the course map The hub display showing the elapsed time

Submission

Submit via SIMON
  1. Record your robot's navigation on video.
  2. Take screenshots of your code.
  3. Upload both the footage and the code screenshots into SIMON.