How to program the Doodle Bot to draw pictures.

How to program the Doodle Bot to draw pictures.
Introduction: This document is a supplement to the Doodle Bot instruction manual and provides
more detailed information for programming the robot. Doodle Bot is a simple robot designed to
help students and hobbyist to learn the basics of DC motor and servo motor control.
Doodle Bot is driven using 2 geared motors and uses a simple magnetic encoder on each wheel to
measure distance and speed. A small servo used to raise or lower the pen and is controlled by
sending a pulse on the servo control pin.
The Mini Driver control board is a simple, Arduino compatible controller with servo headers and
motor drivers built in. This makes it easier for novices who do not have a good understanding of
electronics.
Sample code commands: The sample code allows the user to easily write messages in English
however the robot can do a lot more than write messages. If you look in the tab [Alphabet] you will
see that each letter of the alphabet is drawn using 4 simple command functions:
•
•
•
•
Go(x)
Turn(angle)
Up()
Down()
Go(x): x is an integer that specifies the number of steps the robot should take. As the wheel
encoders change state 8 times (4 high and 4 low) per wheel revolution each step is equal to 1/8 of
the wheel circumference. This is equal to approximately 15.7mm.
The variable charsize is used as a multiplier and by default has a value of 1. If you change it for
example so that charsize=3 then the message / image will be 3 times bigger and each step will be
approximately 47.1mm.
Turn(angle): angle is an integer that specifies the turning angle. All positive values cause the robot
to turn clockwise. A negative value will cause the robot to turn anti-clockwise. This command is
more complicated to use because there are two different ways the robot can turn.
Turning method 1: The first method stops one motor and drives the other motor forward or
backward. This method provides the greatest number of possible angles but because the robot pivots
about 1 wheel it is more difficult to align the pen with its previous position.
Turning method 2: The second method drives one motor forward and one motor backward. This
method provides half as many angles but because the robot pivots about the centre of the wheels it
is easier to align the pen with its previous position.
The sample code uses the first method for angle values
from –31 to +31. This makes all possible angles
available. These angles are shown in blue.
Positive or negative values from 32 to 40 cause the
robot to turn using the second method as shown in red.
If you look at the sample code you will see that you can
also enter values 45, 90,135,180, 225, 270, 315 and 360.
The code automatically converts these values to their
equivalent value using the second method of turning.
The sample code automatically compensates for the pen
not being mounted directly between the wheels and
remembers if the pen was up or down at the time a turn was being made.
Up(): This command raises the pen.
Down(): This command lowers the pen.
The sample code does include a Stop() function however this command is not needed for drawing
and is only used within the Turn(angle) and Go(x) commands to ensure encoder accuracy.
There is also an End() function that puts the robot into a continuous loop where it does nothing
until the reset button is pressed. This can be used at the end of the text or picture to stop the robot
from repeating the loop() function.
If you have been studying the sample code you may have noticed that a variable called dspeed is
calculated based on the battery voltage. This variable is used to try and maintain a constant speed
for the motors as the battery voltage slowly drops. This helps improve the accuracy of the robot. If
the motors are going too fast then the robot may overshoot its position when the encoders tell it to
stop.
Creating new characters: To create new characters or symbols for Doodle Bot to draw you must
first draw them on a grid. This will give you a map to work with and help you ensure all characters
are the same size. As you can see in the picture below, all the numbers and letters included in the
sample code were first drawn in a grid.
The instructions for each character assume that the robot is starting in the bottom left corner of the
grid facing to the right. When you create a new character you must make sure you include
instructions that leave the robot in this position ready to write the next character.
If you look here at the code for the letter “A” and compare it to the character map above you will
see that after drawing the letter the robot raises the pen, goes to the bottom of the character and then
goes right 6.
It only had to go right 4 steps to finish in the lower right corner of the character but it went an
additional 2 steps to create a gap between the letter A it had just drawn and the next character to be
drawn.
You can make your grid any size you want. For example, if you want to create a font with more
detail in each letter or write a language using more complex characters such as an Asian language
then you might use a 15 x 15 grid for each character.
Drawing pictures: This can be done the same way as we draw numbers and letters. First we need
to create our map using 3 basic steps.
Step 1: Find a suitable picture. Remember the more complex the picture the bigger your grid must
be and the more difficult it will be to teach the robot to draw it.
Step 2: Using your favourite art program (or tracing paper, pencil and ruler if you printed the image)
place a grid on top of the image. Trace over the image to generate the lines you want the robot to
draw.
Step 3: Remove the original image and check how your drawing should look. At this point you may
want to make some changes before you start writing the code.
Going multi-colour: Although the robot can only draw one colour at a time it is possible to break
your drawing down into different colours and manually change the pen between each segment.
The simplest way to implement this is to break your code into different segments for each colour
and have a time delay of 30 seconds between each segment of code. This gives you plenty of time
to change pens and put the robot back in the correct position between each colour change.
In this example I would draw the black outline first as it then offers many starting points for the
other colours to begin drawing from.
Converting your map to code: Once you have your map, you must convert it to code for the
Doodle Bot to follow. If you are doing a fairly complex image such as the example picture of R2D2
then it is easy to loose track of the robots position and orientation as you write the code. It may be
easier to print the map and use a small printed arrow to keep track of the position and angle of the
robot as you write each command.
Choose a starting point: In the sample code, the bottom left corner was always assumed to be the
starting point for any character but if you are drawing a picture then you can choose any point in the
image. If the robot is working on a confined area such as a large piece of paper or a whiteboard then
it may be easier to start in the centre so that you have as much room as possible in all direction.
In my example I will start with the black outline at the top. The green line shows the path I want the
robot to follow. To get the robot to follow this path my code would be:
Down(); Go(1); Turn(45); Go(1); Turn(90); Go(1); Turn(90); Go(1); Turn(90); Go(1); Turn(45); Go(1);
At this point it should be noted that each square in the grid is one step wide and one step high. So if
Doodle Bot was to draw the 45° lines accurately it would really need to travel a distance of
approximately 1.4 steps (the hypotenuse of a triangle where each of the other sides =1). As the
robot cannot travel 1.4 steps, the diamond shaped eye at the top of the robots head will be a bit
smaller than shown on the map.
In this case the inaccurate distances of the 4 sides cancel out and Doodle Bot finishes this section of
the code in the correct place. When it comes time to draw the rest of the head, which has several
lines at various angles, then some adjustments in the final code may be needed to compensate. This
can only be done through trial and error.
Choosing the nearest angle: although Doodle Bot cannot turn to any angle or we can choose the
nearest angle. In many cases this will work well. Below is a table showing the different angles and
distances you will get on your grid and the nearest angle and distance the robot can turn at and
travel.
Ratio
Angle
Distance
N:1
6:1
5:1
4:1
3:1
2:1
3:2
1:1
2:3
1:2
1:3
1:4
1:5
1:6
1:N
~90
80.5
78.7
75.9
71.5
63.4
56.3
45.0
33.7
26.6
18.4
14.0
11.3
9.4
~0
~N
6.1
5.1
4.1
3.2
2.2
3.6
1.4
3.6
2.2
3.2
4.1
5.1
6.1
~N
Nearest
Angle
90
78.8
78.8
78.8
67.5
56.3
56.3
45.0
33.8
33.8
22.5
11.3
11.3
11.3
0
Nearest
Distance
N
6
5
4
3
2
4
1
4
2
3
4
5
6
N
Turn
Value
36
7
7
7
35
5
5
34
3
3
33
1
1
1
0
The ratio is the vertical distance divided by the horizontal distance and the angle is calculated using
the ATAN function. If you are using a scientific calculator then this is usually achieved by pressing
[inv] or [2nd] followed by the [Tan] button.
The distance is the hypotenuse of the triangle. The turn values shown use the second method where
possible for best accuracy.
This chart only covers 0° to 90° but adding a negative sign in front of the turn values will cover -0°
to -90° and can be easily extended to cover the entire ±180°.
This document only covers 1 possible method for drawing pictures using the supplied sample code.
Other methods are possible if you write your own code.
Good luck and enjoy!