Fine-tuning your ActivityBot's motion can be tricky, especially when you want your robot to make shapes like triangles, circles, hexagons, etc, as it drives. Fortunately, Parallax-friend and teacher Nikos Giannakopoulos [1]has provided this series of ActivityBot-based tutorials that'll get your robot moving in circles (or squares, or maybe pentagons) with just a few basic library commands and some simple trigonometry.
Although some math is a necessary part of these tutorials, there are plenty of graphics and pre-written code examples so that anyone can work through and enjoy them, regardless of experience level.
This tutorial series will explain how we can make polygonal and circular orbits using a differential wheeled robot [5]. You'll go through some examples of regular polygons and circles, first done by using mathematics, and then by running some SimpleIDE code examples. These tutorials are meant to be done on Parallax's ActivityBot, but the concepts can be extended to various other robots (like the S2), with some modification.
First, we'll need to study and analyze the problem of how to program rotation. We should be able to program the robot to rotate to the correct direction (clockwise or counterclockwise) corresponding to the various angles 0 ° < ω < 360 ° that we want to turn.
In Go Certain Distances [6], you can see that the drive_goto(26, -25) command makes the ActivityBot execute a 90-degree right turn. Let’s see this example in closer detail and try to create a mathematical formula to calculate the appropriate number of “ticks” that correspond to some angle ω.
As the ActivityBot makes a rotation, it moves its wheels on the red circle (radius: 52.9 mm). If the robot makes a turn equal to 1 radian (rad), each wheel will move a distance equal to the radius r of the red circle. Thus 1 rad rotation of the ActivityBot is equivalent to movement of the wheels of distance:
Changing rad to degrees, we take the formula:
So if we want to program the ActivityBot to rotate ω degrees we can multiply by 0.284 in order to find the appropriate number of wheel ticks.
To verify the above formula, we can see that a rotation of 90 degrees = 90 * 0.284 ticks = 25.56 ticks = 26 ticks (rounded up).
In the image above, there is a table that has useful information for the coming activities. Below we have provided an enlarged view of it:
In the chart, the text under "ActivityBot command" reads: for counterclockwise rotation, change the signs. This means that to reverse the direction of motion, change positive values to negative, and negative values to positive. For example, drive_goto(34, -34) would become drive_goto(-34, 34), etc.
A polygon is a closed shape that has N-number of vertices and N-number of sides (N ≥ 3). Specifically, something that is called a regular polygon is one in which all sides are of equal length. We will only be dealing with regular polygons for these tutorials.
Some examples of regular polygon types are below:
Regular polygons with (N ≥ 36) are of special interest, because in terms of shape they are very close to the circle. Although they are not real circles (real circles lack any vertices), in practice these polygons have so many vertices that are close enough together that they can represent a circle with high precision.
We will try now to do orbits of regular polygons with the ActivityBot.
It is essential to know the external angle of the regular polygon in order to give the robot the appropriate command to turn properly. An external angle (or exterior angle) is the angle that is formed outside of the polygon between one side and a line extended from another side adjacent to it. It can be difficult to visualize from just reading, so take a look at the image below for visual examples.
The external angle ω , of a regular polygon (N ≥ 3) is given by the formula: ω=360/N.
So, in order to make (clockwise) an equilateral triangle with side S, using the ActivityBot we must give the following commands:
If we want the robot to do a square path of side S (clockwise), we need to give the following commands:
If we want the robot to do a regular pentagon path of side S (clockwise) we need to give the following commands:
Based on the exercises above, we can give a general equation for every regular polygon with N vertices (Ν≥3) and side S (clockwise):
As we said before, the circle is special because we can approach it as a regular polygon with 36 (or more) vertices. We will use exactly 36 vertices for this example.
So, if we want the robot to do a circle (clockwise) we need to give the following commands (note: the exterior angle values may be difficult to see in the image but each is 10 degrees):
Keep in mind that the S mustn’t be a big number because otherwise your circle will be very large! Choose small numbers for the S values. For example, in the next example S = 8 ticks.
The following SimpleIDE code programs the ActivityBot to make a circle with radius of approximately 15 cm:
The question that might arises from this example is: what length should the S be in order to make a circle with a specific radius (R)? In other words, can I calculate the S as a function of R? Math theory shows us that “Cosines Law” is the answer to this. Looking at the triangle OA1A2 (below) we can see that:
So now we are almost ready for the final algorithm that will be able to create any circle with a given radius R, or create a polygon within a circle of radius R.
The following flowchart describes the final algorithm for creating a circle with a given R:
This algorithm also works well for all other regular polygons.
Keep track of variables! The code below uses lowercase variables n and r for the same variables we have been calling N and R so far. The code comments can help you if you get confused.
/* Final Circle Polygon Algorithm.c Makes the ActivityBot do a circle or regular polygon (By Nikos Giannakopoulos) N = 3 equilateral triangle, N = 4 square, N = 5 regular pentagon, N = 6 regular hexagon....N = number of polygon's vertices and sides, N >= 36 circle R = the radius of one circle (unit in mm) or of the circle in which the polygon is registered. */ #include "simpletools.h" // Include simpletools #include "abdrive.h" int main() // Main function { float pi, ext_pol_angle,side; int n = 36; // N = number of polygon's vertices int r = 150; // R = the radius of the circle in // which the polygon is registered // (unit = mm) e.g. r = 150mm pi = 4 * atan(1); // the number ᴨ = 3.14 tan(ᴨ/4) = 1 ext_pol_angle = 2 * pi / n; // external angle of the polygon in rad int ticks_for_angle = round(ext_pol_angle * (52.9/2.35)); // number of ticks // for drive_goto to // make robot turn the // external angle side = sqrt(2) * r * sqrt(1-cos(2*pi/n)); // side length of the regular polygon int Side_ticks = round(side/3.25); // number of ticks for drive_goto to drive // the side length of the polygon for (int i = 1; i <= n; i++) // repeats forward and right turn commands // N times to complete the polygon shape { drive_goto(Side_ticks,Side_ticks); // Move forward pause(10); drive_goto(ticks_for_angle, -ticks_for_angle); // Turn right or left pause(10); } }
As you can see, the program could also make your ActivityBot form any regular polygon within a circle of radius r. Below are some polygons and circles that you can make with your AcrivityBot by changing the values of variables n and r in the above code.
Links
[1] https://learn.parallax.com/teacher/nikos-giannakopoulos
[2] http://learn.parallax.com/activitybot/software-and-programming
[3] http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
[4] https://learn.parallax.com/activitybot/navigation-basics
[5] http://en.wikipedia.org/wiki/Differential_wheeled_robot
[6] https://learn.parallax.com/activitybot/go-certain-distances
[7] https://learn.parallax.com/regular-polygons-and-circles/calculating-angles-rotation