Play LEGO Spike Prime – Design a Mission for Your Robot at Home – Part 1

You might wonder if we could make LEGO Spike robot accomplish a task at home, just like it does on FLL or WRO competition maps. But what if we do not have those maps? Do we have to buy such maps to train LEGO robot vehicles? The answer is no. You could create unique missions for your robot at home, just with your creative ideas

In this post, I will introduce a sample mission. As shown in the above diagram, the robot vehicle needs to start from the base, finds the curved red route and crosses two gaps. After that, it needs to turn at the intersection and reach to the other base. The vehicle needs to pick up a ball and send it to the initial base. A demo video is shown below.

In the following sections, I will introduce the controlling program. If you are interested in the code, you could download it through the link at the end of the post.

The port connection is shown as below.

The Main Segment

The main segment contains all the key steps of the mission. Please note that before each run, we need to ensure that the front attachment, clip, is lifted up and opened.

The first step in this mission is to start off from the base and find the red route. After that, the program will use “simple_follow_line” block to move forward alone the line. After crossing two gaps, the program uses “Turn and reach destination” block to reach the other base and calls “Up_down_fork” and “Move_clip” to put down front attachment and close the clip to catch the ball.

The program then calls “Comeback” block to return to the initial base. It calls “Move_clip” and “Up_down_fork” to loosen the clip and lift up the front attachment. The final step is for the vehicle to retrive 10 cm and stop. Mission Accomplished!

An illustration for each block and their controlling sequence is shown below.

“Start-off” Block

In this block, the program turns right by calling “move for 150 degrees at 50, 0 % speed”. The 150 degress are an estimated value. If your map size is different from mine, this value needs to be adjusted. After that, the vehicle moves forward straight until reaching red line.

“Simple_follow_line” Block

This block ensures the vehicle moving alone the red line. Since the color sensor of the LEGO Spike could not differentiate well the reflective light of red tape and ground, I use simple line follower (use colour property of sensor) to identify the route. Therefore, vehicle would follow the red line zig-zagly. If you are using black tape, the reflective light difference between route and ground is big enough to use proportional line follower method, the effect could be much better.

At the beginning of the block, the program sets “passed_gap” to 0 and resets timer. When the colour sensor at Port D identifies red colour, reset its timer again. When timer > 2, it indicates that the robot vehicle could not find red line within the last 2 seconds. The program judges that it encounters a gap. It calls “cross_gap” block, which I will introduce soon. After the vehicle passes two gaps, this program block finishes its task and exits.

“across_gap” Block

At the beginning of the block, the vehicle reverses driving until the Yaw angle reaches a certain value. Please note that in the previous block “Simple_follow_line”, whenever the vehicle reaches red line, Yaw angle will be set to 0. Therefore, when the vehicle reverses till Yaw angle is -12, it is restoring to a direction a bit left to that of Yaw angle 0.

So why don’t I just restore to the direction of Yaw angle 0? This is due to the limitation of the simple line follower method. Since the vehicle runs zig-zaggly alone the line, it might tilt from the route direction even when the color sensor is following the red line. Furthermore, the program is not sure whether the vehicle is tilting toward left or right of the route. In order to compensate for this error, the program makes the vehicle reverse to Yaw angle -12. After the vehicle moves forward straight to cross the gap, the program makes the vehicle turn right to find the red line again.

So what happens if I just make it restore to Yaw angle of 0? The vehicle would drive forward to cross the gap and stay either the left or right side of the red route. The program could not decide which direction to steer the vehicle to find the red line again.

“Turn and reach destination” Block

This block has two iterations. In each iteration, the vehicle follows the route until both colour sensors recognize the red line, which means that the vehicle reaches the intersection. In the first iteration, the vehicle will turn left by moving 200 degrees at 0, 50 % power. After that, it enters into the second iteration. The vehicle still follows the red line until reaching the next intersection, which is the front edge of the other base. The vehicle stops and this block finishes its task.

“Move_clip (Open?)” Block

This block is responsible for opening and closing the clip. At the beginning of the block, the program sets the relative position of motor B to 0. If “Open?” variable is T, the motor B will turn clockwise for 225 degrees to open the clip. if “Open?” variable is F, the motor B will turn anti-clockwise for 225 degrees to close the clip.

“Up_down_fork (Up?)” Block

This block is similar to the above “Move_clip” block. The difference lies in an extra conditional judgement of “C speed < 20”. This judgement is necessary since the up and down movement of Motor C is within a small range. When the motor C spins down to touch the ground, yet not reachches the relative position, the program will get stuck. If adding conditional judgement of “C speed > 20”, when the motor C touches the ground, the conditional judgement becomes True, the motor C stops and this block finishes its task.

“Comeback” Block

After catching the ball, the robot vehicle will return to the initial base. It does not choose the previous route. Instead, it turns left and moves forward for 128 cm. After that, it turns left again. It would be facing the base upfront. Please note that the distance of 128 cm is calculated based on trial and error. If your map is different from mine, you need to adjust its value to a proper one. The next step is for the vehicle to move forward. It stops when both colour sensors identify red colour, which is the front edge of the base.

After the main code segment finishes executing “Comeback” block, it will call “Move_clip” to put down the ball. After that, the program calls “Up_down_fork” block to lift up its front attachment. The last step for the vehicle is to retreat 10 centimeters to indicate the accomplishment of its mission.

That is all for the program to control the robot vehicle. Get interested in the LEGO Spike Prime? Draw your own map and program your robot vehicle now. Don’t forget to enjoy the coding and have fun!

Finally, If you are interested in the above code, you could download it here as a reference.

Note: All the analysis articles are copyright products of Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

2 thoughts on “Play LEGO Spike Prime – Design a Mission for Your Robot at Home – Part 1

  1. Pingback: Play LEGO Spike Prime at Home – Another Mission for Your Robot Vehicle – Part 1 – The Coding Fun

  2. Pingback: Play LEGO Spike Prime – Design a Mission for Your Robot at Home – Part 2 – The Coding Fun

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.