Course 8 / Lecture 1:

Introduction to visual sensors

In this lecture you will learn about the basics of vision systems. The focus will be on the color sensor. You will learn how to use this visual sensor to make decisions and to direct a robot to act according to the received information. In this lecture you will build a color sorting robot.



Task #1:

Write code to set motors to 15%!

In the first task you only need to use the red candies, so put some in the tank of the Sorter robot. After that open up SNAP, and start with the dragging of 'When green flag clicked' entry point. Then with the 'Set motor speed' to block set the speed of the motors to 15%, with that you make sure that the robot won't shoot the candies, only just push them. Next, place a 'Forever' block, so the robot can handle multiple candies. In that block, add the 'Wait 0.5 secs' block like in Figure 2, so the it will wait half a second till the next examination.

Figure 2 - The program waits before the examination of the next candy

Task #2:

Extend code to push the red candies into the cup!

Next, you need to examine that if the candy is really a red one. With an 'if' statement you can do it easily. Just add it with an equality operator. To the left hand side of it, drag the 'Color' block, and select the Color sensor. To the right hand side just type 'red'. In that 'if', first drag 'Wait 1.2 secs' block, so it will only push the candy, after it waited that time. Lastly, to push the candy, drag the 'Rotate B by 90° at 50%' block, where you need to select the B motor, that is responsible to push the candies out of the robot. You need to rotate that motor by 90 degrees, so only one candy can go through at one examination. And for the speed, you can type 50% as it was in Figure 3.

Figure 3 - Motor B pushed the candy into the cup

Task #3:

Expand code to shake the candy if the sensor can't sense its color properly!

Because the candies have got different shapes, it can easily get stuck somewhere before the sensing point, and the color sensor can't see the color of it. To avoid that, you need to drag a second 'if' block, where the condition is if the color of the color sensor is equals to black like in Figure 4. In that case you need to move the motor B a little bit to place the candy into the right position. You can easily do that with two 'Rotate B by 20°/-20° at 80 %'. The first one rotates the B motor by 20 degrees, the second one rotates the motors by -20 degrees. Between them place a 'Wait 1 secs' block, so the candy can move into position in that time.

Figure 4 - Black color means the failure of color detection

Task #4:

Improve code to sort two types of candies into different cups!

At this point you can push candies into a cup using the robot, but the Sorter is built for sorting different types of candies. So, let's start with two types. Now, fill the robot's tank not only with red but with green candies and place two cups in front of the robot. In the SNAP code the first thing that you need to modify is to add another 'if' statement. This one has got also an '=' operator in the head of it, but this time the color of the Color sensor needs to be 'green' (Figure 5). In that 'if' you also need to place the 'Wait 1.2 secs' and 'Rotate B by 90° at 50%' blocks as it was in case of red candies.

Figure 5 - Sorter now can make difference between two colors

Task #5:

Rotate motor A to the right cup!

With these modifications your Sorter robot can make difference between red and green candies, but it still push them into the same cup. To place the candies into the right cups, you need to turn the angle of the A motor, which can rotate the tongue of the robot to the right cup. For that, just drag the 'Set motor A to' blocks into both 'if's like in Figure 6. Here you need to type the angle, that you want to rotate the motor. It is important to mention here, that 0° angle means the initial angle of the motor at the start of the program, so whatever if the Sorter is facing completely forward, or the motor is turned to the right or the left way, the initial angle of the motor will always be zero degrees. In this example the cup for the red candies is exactly in front of the robot, so the angle it set to 0 degrees, and for the cup for green candies the motor A have to turned by 275°.

Figure 6 - Motor A rotated to the right cup

Task #6:

Sort the whole bag of candies!

To sort more than two types of candies sounds much more difficult than sort only two types, but it isn't. You only have to add more 'if's with the same structure as it was in the previous ones. So, to be simple, you can just right click on one of these 'if', and select 'duplicate'. By doing this, the complete 'if' block will appear and you can place it underneath the other 'if's you created before (Figure 7). In this example it's done only one time, but if you have got more colors of candies, you can do it as many times as it can cover all colors. In the newly placed 'if' you only need to modify the color that you want to be compared, and the angle of the A motor, to turn the robot to the right cup.

Figure 7 - The whole bag can be sorted

Figure 8 - The code can detect candies with different colors and sort them into cups

More information


Copyright © 2000- |Ozeki Ltd | |
Page: 6136 | | Login
Thank you for visiting this page