FREE SHIPPING for all orders over $25 shipped within the USA
Tutorial #13: Rules + parts = behavior
Congratulations! You’re almost through the Cubelets Blockly tutorials. You’ve built robots that have taught you key programming concepts. You’ve also learned how to build robots that exhibit complex behaviors simply by modifying the rules that control a few parts.
In this tutorial we are going to expand on the concept of Block Values. Block Values are numbers Cubelets use to communicate with each other and just like numbers we can change them using math. We can make Block Values bigger or smaller just by using some simple math. By changing Block Values with math you can change the way your robots behave.
You will also use your Else/If statements. These statements are coding tools that allow you to create an action if something occurs, perform a different action if a different thing occurs and another action in all other cases. A simple example is… if I see you wave “hello” while looking in my direction then I will wave hello in response. If I see you wave your fist in anger at me I will run away. Otherwise, I will continue walking down the street.
- Welcome to the last Cubelets Blockly Tutorial!
- Begin by building the robot using the on-screen instructions.
- Power on your robot and be aware that your Drive Cubelets may still be running custom programs. Be sure that your robot won’t drive away when you power it on!
- Click the connection icon and complete the Bluetooth pairing process.
- In this tutorial you’ll be programming a robot with the ability to move around objects and avoid falling off tables! You’ll do this by creating three programs, one for each Drive Cubelet and one for the downward facing Distance Cubelet. (This is the Distance Cubelet that is attached to the Blocker Cubelets)
- Before we get started, download the three starter files you’ll need for this tutorial. cubelets_tutorial_13a.cubelet, cubelets_tutorial_13b.cubelet, and cubelets_tutorial_13c.cubelet
Cubelets Tutorial 13a.cubelet
- You’ll start with a program for the downward facing Distance Cubelet.
- Load the “cubelets_tutorial_13a.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file.
- Arrange the block to create a program that sets the block value to 10 if the sensor value is less than than 225. Else set the block value to 255. If you need help, click here for hints.
HINTS: Start by connecting the conditional block to the if else block. Complete the conditional by placing the “sensor value” block in the first puzzle opening and 225 in the second puzzle opening. Then use the dropdown to make the conditional statement say sensor value less than 225. Within the “do” cutout, arrange the “set block_value to” and 10 blocks so that the line reads set block value to 10. Within the “else” cutout, arrange the “set block_value to” and 255 blocks so that the line reads set block value to 255.
- Select the Distance Sensor that is connected Directly to your Blocker Cubelet, then click “Program Selected Cubelet.”
- When Cubelets Blockly has finished updating your Cubelet, you’re ready to move on.
Cubelets Tutorial 13b.cubelet
- In the next two programs you will use a Block ID and the weighted average to control your Drive Cubelets. Select the downward facing Distance Cubelet that is directly attached to the Blocker Cubelet in the Block Map and carefully write down the Block ID.
- Load the “cubelets_tutorial_13b.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file. This tutorial file has groups of code blocks arranged from top to bottom to make it easier to assemble each component.
- Your program will look for three different events that will determine the behavior of the Drive Cubelet.
- To accomplish this, you’ll need to define a new variable and connect it to the downward facing Distance Cubelet’s ID. This variable will tell your Drive Cubelet when a change in the downward facing Distance Cubelet occurs. Use the first group of blocks to create a variable called “trigger” and set it to Block Value for the Distance Cubelet ID.
- Next you’re going to set up three conditions that your Drive Cubelet might respond to. Assemble new group of blocks in the else if statement so that it can respond if…
- The trigger value is equal to 10.
- The trigger value is 255 and the weighted average is less than or equal to 120.
- The trigger value is 255 and the weighted average is greater than or equal to 121.
- Now you’ll need to program what action occurs after each event. Start with the first condition when the trigger value is equal to 10. If you were to look back at your Distance Cubelet program, you’d see that a trigger value of 10 means that the downward facing Distance Cubelet isn’t detecting anything in front of it.
- If the Distance Cubelet doesn’t see anything than that could mean the robot is dangerously close to falling off the table! Arrange the next group of blocks so that your Drive Cubelet will reverse direction, set the action value to 150 for a half second then return to forward motion.
- Great job! Now it is time to build the response for when the trigger is 255 and the weighted average is less than 120. If the trigger value is 255 then the downward facing Distance Cubelet is detecting something beneath it. This means it is probably safe for our robot to move forward. But we don’t want it to move too fast or else it might fall off the table!
- It’s time to create some code that will move the Drive Cubelet forward at a slower pace as long as it detects the table below it. Use the next group of blocks to create a line of code that sets the action value to the inverse of the weighted average minus 60. Click here for a hint.
HINT: Connect the “set actuator value to” and simple “subtraction” blocks together. Then connect the “inverse of” and “weighted average blocks.” Place them in the first puzzle piece opening. Place the 60 number in the second puzzle piece opening.
- The last step for this program is to define the action when the trigger still detects the table but the weighted average is greater than or equal to 121. Arrange the new blocks so that the action value is set to the weighted average.
- Select the Drive Cubelet that is directly connected to the Battery and Blocker Cubelets. Click “Program Selected Cubelet.”
- Once Cubelets Blockly has finished updating your Cubelet you’re ready to proceed to the next program.
Cubelets Tutorial 13c.cubelet
- Load the “cubelets_tutorial_13c.cubelet” file. Click the load icon in the toolbar. Then follow the system dialogue to select the proper file. This tutorial file has groups of code blocks arranged from top to bottom to make it easier to assemble each component.
- This program is very similar to the cubelets_tutorial_13b.cubelet program you just finished. It will need to respond to the same 3 events. To save time we’ve duplicated the conditional statements and variable definition from the previous program. Before you continue be sure to set the Block ID to match the downward facing Distance Cubelet.
- Once again you will need to create an action that prevents this Drive Cubelet from driving off the table if the downward facing Distance Cubelet no longer detects the table. Arrange the new blocks to create an action where you reverse the motor direction set the action value to 255 for 3/4ths of a second then return the motor to its normal forward rotation.
- Next you’ll build some code that will move the Drive Cubelet forward at a slower pace as long as it detects the table below it but not an object in front of it. Use the new blocks to create a line of code that sets the action value to the inverse of the weighted average minus 60. Click here for a hint.
HINT: Connect the “set actuator value to” and simple “addition” blocks together. Then connect the “inverse of” and “weighted average blocks.” Place them in the first puzzle piece opening. Place the 60 number in the second puzzle piece opening and use the dropdown to change the operation to subtract 60 from the weighted average.
Complete the final section of code by arranging the new code blocks to create a reverse behavior whenever the block value exceeds or is equal to 121. If you need a hint click here.
HINT: First position the “toggle motor direction” block in the last else if block cutout. Then set the action value to the weighted average. In the next line place the wait command and set the value to 500 milliseconds. Finally, toggle the motor direction to return the Drive Cubelet to its default spin direction.
- Excellent work! This program is complete. Select the remaining Drive Cubelet that is connected to your Distance and Bluetooth Cubelets then click “Program Selected Cubelet.”
- Once Cubelets Blockly has finished updating your Cubelet, take your robot to a table to test its behavior. If you programmed everything correctly you should see a robot that avoids objects and backs up whenever it senses the edge of the table!
Congratulations! You’ve just completed the programming tutorials for Cubelets robot blocks.
Programming Cubelets is different than programming other robots. When you program other robots you control all the parts and their rules directly. You determine how, what and when things occur in one centralized place. Programming this way can be an efficient method of building a robot, but it doesn’t teach you much about how the world really works.
In learning to program Cubelets, you learned to program the parts that interact to create something bigger than themselves.
You’ve seen that programming with Cubelets means thinking in a different way. It means knowing what a part is and what rules it follows. It also means looking at all the parts and rules working together to create something new and surprising. Learning to see all the parts, their rules and how they interact to form a collective behavior will help you make better Cubelets robots. In doing so you’ll be building the problem-solving patterns you need to understand the biggest and hardest problems in the world, the problems of complex systems.
Keep experimenting with the combination of rules and parts to design your own robots. You’ve learned many of the basic concepts for programming that will help you build some amazing robots!