## Swarm Robotics in the Classroom

dark mode light mode Search Menu
Search

Dave Catchpole on Flickr

Some people love to code. Others prefer to use the computer to design and draw. With Tinkercad Codeblocks, we can combine coding with computer aided design to create precise designs for 3D printing or importing into other applications.

In this example we will design a simple log cabin, similar to what we could build by hand with Lincoln Logs. To start, let’s head to the Tinkercad site: https://www.tinkercad.com/

If you do not have an account, find the Join Now button toward the top right corner of the screen in order to get started. Otherwise, sign in and head to Codeblocks!

Once you sign in, you should see the Codeblocks option in the left margin of the screen:

When you select Codeblocks, the Create New Codeblock option appears. Click it and choose New Design to get started.

Following those steps opens up a split screen as shown below:

On the left most part of the screen we see blocks of code, similar to Scratch or Blockly. The middle section is where we can drag and drop blocks to create a program, and the right hand side is where we see the result of our program.

Since we will make a log cabin in this example, start by dragging out the cylinder block.

To make this cylinder more “log-like” we will make the following changes:

1. Rotate it 90 degrees
2. Increase its length (height) to 80
3. Decrease its radius to 5
4. Set the color to brown

If we zoom in on the log (as shown below), we will notice two important properties. First, half of our log is below the plane. To fix this, we need to move it up by one radius (5 units). Second, we see three lines (red, green, and blue) that represent the three dimensional coordinate system. To move the log up, we move it along the blue z axis.

The complete code to create a log of length 80 units, rotated 90 degrees, and raised up to the level of the plane is shown below:

Now that we have the code for a log, we can make it a “log object” with a purple “Create New Object” block. By doing so we can have Tinkercad quickly create more logs and stack them on top of each other.

We decided that stacking six more logs on top of our original looks like a good height, as shown below. The entire stack of logs represents one object, which we named “wall”.

Each log needs to be placed two radii (2 * 5), one diameter, above the previous log. However, if anyone (including ourselves) looks at this code later, they might not realize where the numbers 10, 20, …60 come from. Some people refer to numbers like this found in code as magic numbers.

To avoid having magic numbers in our code, we can create a variable that we’ll name “diameter” (with a size of 10) and replace the numbers circled in orange with the variable diameter, as shown below.

If you have coded before, you may have learned that usually when we have parts of code that repeat over and over, such as the Add Copy of Object and Move blocks above, we can shorten our code with a “repeat” or “for” loop block. Tinkercad Codeblocks has this feature as well. It is not necessary to use, but it makes for much slicker, quicker, code.

The image below produces the exact same log wall as the previous bit of code, making use of a “for loop” to add a copy of a log and move it up by the correct amount in the z direction.

With one wall made, the bulk of our work is done. Now we just need to add in three more walls. The code for a new wall is shown below:

In the image below we add a copy of the second wall. Then we rotate it 90 degrees to form a right angle with the left and right side walls (wall and wall2). Finally we center it in the x direction and move it back 35 units in the y direction to create a 5 unit overlap with the walls that it intersects.

Finally we create a 4th wall by copying our 3rd wall and moving it -70 units in the y direction to bring it to the front of the cabin (again with a 5 unit overlap of the left and right side walls). The complete code and an image of all four walls that are generated is shown below:

The entire process can be soon in the following gif:

Stacking the logs to form the walls of the cabin is the repetitive part of this project. Now that it is complete, it is easiest to name our project and export it as an .stl (or .obj) file. Then we can upload our project into a standard (not Codeblocks) Tinkercad file and add features such as a roof, doorway, and windows as shown below.

#### Codeblocks designs for 3D printing and laser cutting

https://www.robmorrill.com/codeblocks