LEGO Mindstorms Class:  Lesson 7

Building a Motorized (claw) Attachment:

In the last challenge you created a passive (fixed in place) attachment, and used it to get your robot to pick up and retrieve several objects in its path.  You used the motion of the robot to help it pick up the objects.  In today's lesson, we are going to build a motorized (claw) attachment, and use it to accomplish a task.  We will also learn how to use LEGO Mindstorms "My Blocks" to simplify our programming.

Exercise 1:  Build a "Claw-Bot"

Because we need to be able to attach a motorized claw to our robot, we're going to build a whole new robot, with room underneath for a motorized attachment.  Follow the steps below to build your robot:

Step 1:  Add "hooks" to the brick.

1(a)    1(b)

Step 2:  Build attachments to the motors, and add the tires.

2(a)

2(b)

2(c)

2(d)

Step 3:  Build a rear crossbar to hold the motors in the right place.

3(a)    3(b)

3(c)

3(d)  This picture missing pieces from previous picture.

Step 4: Build the front crossbar.

4(a)

Step 5:  Attach the front crossbar to the motors.

5(a)

5(b)

Step 6:  Build the caster wheel for the rear crossbar.

6(a)   6(b)

Step 7:  Attach the wheel and some vertical struts to the rear crossbar.

7(a)

7(b)

7(c)

7(d)

7(e)

7(f)

Step 8:  Attach the Mindstorm Brick to the frame.

8(a)       8(b)

Step 9:  Build and attach the claw.

9(a)

9(b)     9(c) 9(d)

9(e)

9(f)

Step 10:  Attach the claw underneath the front crossbar, and attach wires to the motors.  Remember  the "B" and "C" motors drive the wheels, and the "A" motor powers the claw:

That's it!  You've created "Claw Bot"!

Exercise 2 - Moving the Claw:  Figure out how to get the claw to open and close.  Remember, to rotate a single motor, you'll want to use the "Move" block.  Can you figure out how to get the robot to drive to an object, grab it with the claw, and roll back to you?  Write a program called "grabobject.rbt" to accomplish this.  Your instructors will provide an object for the "Claw Bot" to grab.

Exercise 3 - Open the Claw all the Way, no Matter Where it Starts:  It can be difficult to open the claw just the right amount to grab something, so we're going write a program that will fully open the claw, no matter how opened or closed it is when it starts out, and then stop Motor "A" just when the claw is fully open.  Before we write our program, we're going to attach a bar to the motorized part of the claw, that will stop the claw from opening too wide.  Add the piece circled in the picture below to your claw:

Notice that this "stopper" piece will hit the robot if the claw tries to open too far.  Now we need to figure out how to get motor "A" to open the claw until the extra piece we added hits the robot, and then stop Motor "A" from running .  We'll use the rotation sensor to do this.  The trick we will use is to run motor "A", and keep checking the rotation sensor, which tells us how much motor "A" has moved.  When the value from the rotation sensor stops changing, that means that motor "A" has stopped turning, because our stopper piece has hit something.

First, create a program called "openclaw.rbt".

1. Start the "A" motor moving for "unlimited" duration.  Reduce the power to open the claw slowly.

2. Add a loop so we can repeatedly check if the claw has fully opened.  We'll stop looping if it has, so set the loop control to "Logic".

3. Inside the loop, read the Rotation Sensor (on the Sensors Menu) for Motor "A".

1. Now we'll do something new.  We're going to store the reading from the rotation sensor in a variable, called "Number 1".  To do this, add a Variable block (from the Data menu).  Make the data type of the variable a number (select the option called "Number 1"), and specify that we will be writing a value to the variable.  Then copy the reading from the rotation sensor to the variable using a data wire as seen in the picture below.

2. Now we're going to wait a short while before checking the rotation sensor again to see if motor "A" has moved at all.  Remember, if motor "A" has moved at all, then the claw is still opening.  Add a Wait block (from the Common menu), and set the wait time to 0.1 seconds.

3. Now we will check the value of the rotation sensor again, and compare it to the value we stored in our variable, "Number 1".  If the values are different, then the claw is still opening, and we'll continue the loop.  If the values are the same, then the claw has opened as much as it can, and we will stop the loop.   We'll add another rotation sensor block to check the value, then we'll use a "Compare" block (from the Data menu) to compare the old and new values of the rotation sensor.  We input the old and new values into the "Compare" block using data wires.  The output of the "Compare" block goes to the loop.  If the values are the same, the "Compare" block outputs "true", and the loop will stop (we specified that way back in Step 2).

4. That's the whole program!  Give it a try, and see if it works.

Exercise 4 - Put the Program in a "My Block":  If there is a program, or part of a program that you may want to re-use, the NXT  Environment lets you put it into a special block, called a "My Block".  Then, if you want to re-use the code, you don't have to copy all of the steps into your new program, you just have to copy the "My Block".  Let's try it.

1. Go to the page containing the "openclaw.rbt" program from exercise 3, and use the pointer tool to select all of the code by drawing a giant rectangle around it, and letting go.
2. Select "Make a New My Block" from the "Edit" menu.
3. The "My Block Builder" dialog box will appear.  Enter  "Fully Open Claw" for the block name, and write "Opens the Claw all the Way" for the block description.  Then click the "Next" button at the bottom of the dialog box.
4. You will see a page called the "Icon Builder".  This lets you create a picture for the front of your "Fully Open Claw" block, so that you can distinguish it from other blocks.  Drag, drop and resize different icons to create a unique picture for the front of your block.  Click "Finish" when done.

Figure 1 - The "Icon Builder" Dialog Box                                            Figure 2 - The "My Block Builder" Dialog Box

Now your new block will show up on the "Custom" Block menu that you can reach from the tabs at the bottom of the menu pane on the left side of the NXT Programming environment: .  Create a new program (with any name), and click the rightmost tab (the two blue lines) on the menu pane.  Click the top icon to select the "Fully Open Claw" that you created and insert it in your program.  Write a program that uses this custom "Fully Open Claw" to open the claw, drive to and pick up an item.

If you ever want to edit your custom "My Blocks" you can select the "My Block" in any program you are using, and choose "Edit Selected My Block" from the "Edit" menu.  Editing the code in this window will automatically change the "My Block" in any program that uses it.  This is a useful tool if you would like to re-use code in multiple programs without having to re-type it each time.