Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Design Innovative Robots with LEGO SPIKE Prime

You're reading from   Design Innovative Robots with LEGO SPIKE Prime Seven creative STEM robotic designs to challenge your mind

Arrow left icon
Product type Paperback
Published in Jan 2022
Publisher Packt
ISBN-13 9781801811576
Length 264 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Aaron Maurer Aaron Maurer
Author Profile Icon Aaron Maurer
Aaron Maurer
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Chapter 1: Getting Started with SPIKE Prime 2. Chapter 2: Building an Industrial Robot Claw FREE CHAPTER 3. Chapter 3: Building a LEGO Guitar 4. Chapter 4: Building a Mechanical Bird 5. Chapter 5: Building a Sumobot 6. Chapter 6: Building a Dragster 7. Chapter 7: Building a Simon Says Game 8. Other Books You May Enjoy

Writing the code

For this build, we are going to focus on writing code that allows us to use the Intelligent Hub as a remote control for the arm. This will be a program that allows you to control the robot using the gyro sensor feature of the Intelligent Hub to move the claw left, right, up, and down, and to open and close the claw.

Overall, this program is simple to develop and is created to be easily adaptable for you to remix to meet your needs. Use this sample code to make sure everything works, and then begin to tweak the code to make it work to your desired needs. Make sure you have the proper ports plugged in, and then move on to the code.

Identifying the ports

If you have not plugged in your motors yet, then let's get them plugged into the proper ports. You will plug the large motor that moves the claw to the right and left to motor port E. The medium motor that controls the opening and closing of the claw will plug into port A. The medium motor that controls the arm of the claw to go up and down will use port C. Once you have the motors plugged in, then double-check that the motors are plugged in correctly.

You can check that your motors are properly plugged in by checking the Hub Connection in the software, as seen in the following screenshot:

Figure 2.54 – Hub Connection view

Figure 2.54 – Hub Connection view

It is time to write the program so that you can control the claw.

The Intelligent Hub remote-control robot program

To get started with this program, let's go ahead with the following steps to make the robot come to life:

  1. Open up the SPIKE Prime software.

    Click on New Project at the center of the main screen:

    Figure 2.55 – Choosing a new project

    Figure 2.55 – Choosing a new project

  2. On the New Project window, select the Word Blocks option, give your program name, and click on Create.
  3. When the coding canvas opens, there is already a default when program starts block provided, as shown in Figure 2.56. Let's begin with this code stack. We will add some code to align all our motors to a common starting point at the beginning of the code. Add a cyan sensor block called set yaw angle to under the when program starts block:
    Figure 2.56 – Creating the initial code stack

    Figure 2.56 – Creating the initial code stack

  4. Yaw is the orientation of Intelligent Hub turning right or left, as shown in Figure 2.57. In other words, it is the rotation around the z axis:
    Figure 2.57 – Yaw is turning Intelligent Hub left and/or right on the z axis

    Figure 2.57 – Yaw is turning Intelligent Hub left and/or right on the z axis

  5. The last two blocks we will add to this stack are blue motor blocks. Add two of the motor go shortest path to position blocks, as shown in Figure 2.58. Set one block for motor E and the other for motor C:
    Figure 2.58 – Set motors E and C to the start position

    Figure 2.58 – Set motors E and C to the start position

    We are now going to create a new code stack for the claw to close. Go to the Events code block section and select the block when left Button pressed. Change this block to right Button pressed. Add a blue motor block named run for 1 clockwise rotations. Change the settings of the block to motor A and change the settings to 7 degrees:

    Figure 2.59 – Set the claw to close

    Figure 2.59 – Set the claw to close

    You will now copy this stack and do the opposite to open. Right-click the Event block and you will see an option to Duplicate. Change this stack to left Button on the orange event block. For the blue motor block, change the direction of the turn to counterclockwise:

    Figure 2.60 – Set the claw to open

    Figure 2.60 – Set the claw to open

  6. The final code stack will be created to allow the arm to move in the direction of Intelligent Hub using angle and yaw. Begin by adding an Event block named when program starts.
  7. Add a blue motor block to adjust the motor C speed to 5%. You can adjust the speed to your liking once you get the arm moving.
  8. The next block we will add is a Control block named forever. In this block, add another blue motor block, moving motor E to the shortest path to position the block. Where the number 0 sits, you will add a cyan sensor block named Pitch Angle. Add that to 0 and then change the pitch to Yaw. Figure 2.61 will showcase what you should have at this point:
    Figure 2.61 – Controlling arm code

    Figure 2.61 – Controlling arm code

  9. Next, we will create an If Then statement and basically duplicate it two more times to complete the code. This will program the arm to follow the motion of Intelligent Hub. Go to the Control blocks and add an If Then block into the Forever block of our code. Add a green Operator block of less than. On the left side, add a cyan Sensor block of Pitch Angle, and to the right of the equation, add -3. Under that block, add a blue motor block, Start Motor, and change to motor C and in a counterclockwise direction:
    Figure 2.62 – Arm move to the left

    Figure 2.62 – Arm move to the left

  10. We will now copy this format, but use different Operator blocks for greater than and another Operator block when the pitch is between -3 and 3. See Figure 2.63 for the rest of the code to complete the sequence:
Figure 2.63 – Complete code for arm control

Figure 2.63 – Complete code for arm control

When it is all complete, your code should look like this:

Figure 2.64 – Complete code for this project

Figure 2.64 – Complete code for this project

And that is it! You did it. You built a nice-looking and working robot claw. Now comes the fun part, which is to remix the claw to make it your own. Let's look at some ideas.

You have been reading a chapter from
Design Innovative Robots with LEGO SPIKE Prime
Published in: Jan 2022
Publisher: Packt
ISBN-13: 9781801811576
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image