By now, you’ve gotten the hang of building models that move on wheels. Another fun, but slightly more challenging, kind of model you can build is an animal robot that uses legs to move instead of wheels. In this chapter, you’ll build and program ANTY (see Figure 13-1). ANTY is a six-legged, insect-like creature that walks around and responds to its environment by expressing different types of behavior based on what its sensors “see.”
The Infrared Sensor serves as ANTY’s eyes, allowing the robot to detect objects in its surroundings and to find food. The Color Sensor in the robot’s tail enables ANTY to detect changes in its environment. Different surroundings—that is, different colored objects—make the robot behave differently. Green objects give the robot a sense of safety, meaning it can take a nap. Red objects indicate danger, making the robot shake aggressively to scare off enemies. Blue objects scare ANTY, prompting it to run away. Finally, yellow objects make the robot hungry, causing it to start looking for food.
ANTY uses two motor assemblies to walk, each of which controls a set of three legs. Turning one motor forward makes the three legs connected to it step forward, as shown in Figure 13-2. Turning both motors forward simultaneously makes the whole robot walk forward.
But this works only if the legs on the left side are in the opposite position of the legs on the right side. For example, the left legs should be in position 2 when the right legs are in position 4 to ensure that there are always at least three legs in contact with the ground. This will happen when one motor is exactly 180 degrees ahead of the other. (The green dot in position 4 is 180 degrees ahead of the green dot in position 2.)
The robot uses the Touch Sensor to determine the absolute position of each motor so that the motors can be placed in opposite positions when the program begins. The robot knows that a set of legs is in position 1 when the Touch Sensor is pressed by a cam element, as shown in Figure 13-2.
After you’ve built the robot, you’ll make a My Block that places the legs on either side in the required position, after which the robot can start walking using Move Tank blocks. As long as both motors turn at the same speed, the motors will remain 180 degrees apart.
Figure 13-2. Turning the motor forward makes the three legs (black lines) step forward. The motor moves the middle leg by rotating the pivot (green dot) in a circle. In turn, the middle leg makes the outer legs move using a set of beams (blue lines). After one full rotation, the legs are back in position 1, and the robot has moved forward.
Now that you’ve learned a bit about the robot’s functionality, you’re ready to build it. Follow the directions on the next pages, but first select the pieces you need (see Figure 13-3).
You learned earlier that you make ANTY walk forward by having both motors rotate forward simultaneously, but only after the legs on either side are placed in opposite positions (180 degrees apart). You’ll create a My Block to place the right legs in position 1 and the left legs in position 3 (see Figure 13-2), using the fact that a motor presses the Touch Sensor with a cam element when it’s in position 1. You’ll use this block at the start of each program for ANTY.
Once the block completes running, Move Tank blocks will make the robot walk by turning both motors at the same speed to keep them in opposite positions.
Because both motors use the same Touch Sensor to detect their absolute position, the robot doesn’t know whether the sensor is being pressed by the left motor (B), the right motor (C), or both. However, you can still determine the position of each motor by moving only one motor at a time, following these steps in order:
Turn both motors simultaneously until the Touch Sensor is released. (The robot can do this by repeatedly moving both motors forward by a small amount until the sensor is released.)
Rotate the left motor forward until the Touch Sensor is bumped. The left legs are now just beyond position 1.
Rotate the left motor 180 degrees forward. The left legs are now just beyond position 3.
Rotate the right motor forward until the Touch Sensor is bumped. The right legs are now just beyond position 1, exactly opposite the left legs.
Create a new project called ANTY. Place and configure the blocks that implement these steps, as shown in Figure 13-4. Then, turn them into a My Block called Opposite and run the block to test it.
If the My Block does not place the legs in opposite positions, even if your program looks just like the one given, you may have incorrectly attached the cam elements. Review building ANTY and fix your robot if necessary. (You may not need to take the whole robot apart: It’s easier to leave the cam elements in place. Just disconnect the middle legs from the motors and connect them to match the instructions.)
Once the legs are in place, you make the robot walk forward using Move Tank blocks configured to rotate both motors forward at the same speed. The robot can turn left or right by rotating one of the motors backward. You can use these techniques to create an obstacle avoidance program.
Rather than using Move blocks in On mode, as when programming the EXPLOR3R, this program has the motors make four complete turns using On For Rotations mode. This movement is repeated until the Infrared Sensor’s proximity measurement drops below 50%. Then, the robot turns left by reversing the left motor and turning the right motor forward three rotations.
Figure 13-4. The configuration of the blocks (left) and the completed Opposite My Block (right). I used a Sequence Wire to split the program in two for better visibility, but you won’t need to do this in your program.
Create the ObstacleAvoid program using the instructions in Figure 13-5. Note that it’s necessary to rotate both motors at the same speed and for a fixed number of complete turns to ensure that the legs always end up in the original opposite position, ready to take more steps. If you program a movement that makes your motors end in a different position, you’ll need to run the Opposite My Block again to reset the legs.
Next, you’ll create the program that makes your robot express different kinds of behavior based on changes in its surroundings. We’ll start with the most complicated behavior—searching for food—so we can test this part of the program separately. When you’re ready, you’ll combine it with the robot’s other behaviors.
We’ll mimic the search for food by making the robot search for the infrared beacon. You’ll use the techniques you learned in Chapter 8. ANTY looks for the beacon, walks toward it, and stops once it’s found it. To accomplish this, the robot takes two steps to the left or to the right, depending on which side it sees the beacon, and then it takes two steps forward. It repeats this behavior until it sees the beacon up close, indicating that it has reached its destination.
Create the FindingFood program, as shown Figure 13-6. Once you’ve verified that your program works, turn the Loop block with its contents into a My Block called Find so that you can use it in your next program. (Don’t include the Opposite block in the Find My Block—you’ll place it elsewhere in the final program.)
The final program makes ANTY express different kinds of behavior based on the color it sees with the Color Sensor in its tail. For example, ANTY will take a quick nap when you hold a green object near the sensor, because the color green indicates the safety of a grass field.
Let’s begin by creating the basic structure of the ColorBehavior program. The robot should repeatedly check which color it sees and do something different in response to each color. You program the robot to do this with a Switch block in Color Sensor – Measure – Color mode that you place in a Loop block, as shown in Figure 13-7.
The switch has five cases: No Color, Green, Yellow, Blue, and Red. The default case is No Color, so the blocks on this tab will run when black, white, or brown is detected. The next step is to add blocks to each tab of the switch.
In the absence of green, yellow, blue, or red, ANTY simply sits still while making a chirping sound. Place a Sound block on the No Color tab of the switch, as shown in Figure 13-8. Note that the Insect chirp sound file plays two chirp sounds, so the sensor takes a measurement after every second chirp.
When ANTY senses the green grass of a large field, it knows it’s safe to take a quick nap. Add the blocks that make the robot do this to the Green tab, as shown in Figure 13-9.
Seeing a yellow object makes ANTY hungry, causing it to start looking for food (the beacon) using the Find block, as shown in Figure 13-10. See finding food if you haven’t made the My Block yet.
Blue objects indicate the presence of predators. ANTY’s best option is to simply run away, using Move Tank blocks, as shown in Figure 13-11. The blocks here make the robot take five steps forward and then two steps to the left.
Red causes ANTY to become aggressive and shake violently in order to scare off enemies, as shown in Figure 13-12.
Figure 13-10. ANTY locates the infrared beacon and walks toward it when its Color Sensor sees yellow.
You can use the EV3 to build not only vehicles and machines but also robotic animals and creatures. In this chapter, you built ANTY, a six-legged walker. You learned how to make it walk with two Large Motors combined with the Touch Sensor to detect the absolute position of each motor.
In addition to building a walking robot, you created a program that controls your robot’s behavior, so that ANTY would perform different actions based on sensor input to imitate the behavior of a real animal.
Now try some of the Discoveries to explore this robot design further. What cool creatures will you make?