Introduction
In this chapter we will develop a few applications utilizing a pen at simulated robot center that
can be lowered to allow the robot to leave a trace on the floor as it moves.
With the proper program we can convert the robot into a device for drawing vector graphics
(meaning we specify a line to be drawn with a starting point, an angle, and a length).
There are many industrial applications for a robot that can draw on a surface. A robot could cut
intricate and complex designs out of metal sheets if a laser cutter is substituted for the pen.
Imagine a robot that can draw the yard lines and other messages and designs on a football field
once the desired data has been given to it.
8.1 DrawBot
The command to lower and raise the pen is:
rPen ExprN1 {,ExprN2}
If ExprN1 is 0, the pen will be raised and, if it is any number other than 0 the pen will be lowered.
You can also use the constants Up and Down . ExprN1 has to be a number. The pen is up when you
first initialize and rLocate the robot. ExprN2 is optional. If it is specified then the color of the pen
will be set to ExprN2. If it is not specified then the color of the pen will be set to the first color on
the invisible colors list.
If you have not specified an invisible colors list then the pen color will be black. ExprN2 should be
a valid color number.
If you dont place the pens color in the list of invisible colors the robot will crash if it ever
encounters its own trace. You must specify any colors you are likely to draw on the floor with the
pen as invisible colors so the robot will be able to drive over them. You can reissue the rPen
command with a different color to draw as many colors as you desire.
8.1.1 Drawing Circles
//---Draw Circles
MainProgram:
//---Change these values to change the position of the circle
R_Init_X = 100
R_Init_Y = 300
R_Init_Heading = 0
rLocate R_Init_X,R_Init_Y,R_Init_Heading
rInvisible Magenta
LineWidth 4
//---Change these values to change the size of the circle
fStep = 1
tStep = 1
gosub DrawCircle
rTurn 90 //--move out of the way
rForward 40
End
//======================================================
DrawCircle:
rPen Down //--start drawing
for i = 1 to 360/tStep
rForward fStep
rturn tStep
next
//DrawRectangles
MainProgram:
//changethesetomaketherectanglehave
//differentorientationandposition
R_Init_X=100
R_Init_Y=300
R_Init_Heading=45
rLocateR_Init_X,R_Init_Y,R_Init_Heading
rInvisibleMagenta
LineWidth4
//changethesevariablestochangesizeoftherectangle
RectWidth=100
RectHeight=50
gosubDrawRectangle
rTurn45//moveoutoftheway
rForward40
End
//======================================================
DrawRectangle:
rPenDown//startdrawing
forI=1to2
rturn90
rForwardRectWidth
rTurn90
rForwardRectHeight
next
rPenUp//stopdrawing
Return
//======================================================
FIGURE 8.2 Drawing rectangles.
02 MainProgram:
03
//----change these to make the triangle have
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
End
//======================================================
DrawTriangle:
rPen Down //--start drawing
For i = 1 to 3
rForward Sides[i-1]
rTurn 180-Angles[i-1]
next
rPen Up //--stop drawing
Return
//======================================================
The Data statements on Lines 14 and 15 create two arrays that hold the sides and angles.
01
02
03
04
05
06
07
08
09
10
14 for I = 1 to MaxDim(SomeShape,1)/2
15
J = (I-1)*2
16
if SomeShape[J] = "f" then rForward SomeShape[J+1]
17
if SomeShape[J] = "t" then rTurn SomeShape[J+1]
18
if SomeShape[J] = "p" then rPen SomeShape[J+1]
19 next
20 End
FIGURE 8.5 Drawing any shape.
t = "t"
f = "f"
p = "p"
d = 10*sqrt(2)
//--A
data SomeShape;
data SomeShape;
data SomeShape;
//--B
data SomeShape;
data SomeShape;
data SomeShape;
data SomeShape;
//--C
data SomeShape;
data SomeShape;
data SomeShape;
data SomeShape;
FIGURE 8.6 The robot can write (well, only three letters so far).
MainProgram:
goSubCreate_Font
Scale=5
rLocate100,500,30
rInvisibleDarkGray
Message="HelloWorld"
gosubPrint_Message
rForward30
End
//======================================================
Print_Message:
Message=upper(Message)
fori=1toLength(Message)
=Ascii(SubString(Message,i,1))Ascii("A")
ifL<0orL>26thenL=26
Inst_No=0
whiletrue
Inst=Letters[L,Inst_No*2+1]
ifInst=fthenrForwardScale*Letters[L,Inst_No*2+2]
ifInst=tthenrTurnLetters[L,Inst_No*2+2]
ifInst=pthenrPenLetters[L,Inst_No*2+2]
ifInst=ethenbreak
Inst_No=Inst_No+1
wend
rForwardScale
next
Return
FIGURE 8.8 Letter-writing program.
Create_Font:
f = 1
t = 2
p = 3
e = -1000
d = sqrt(2)
//------------Fonts
data letters; Char(Ascii("A")+26), f,6, e
data letters; "A", p,down, t,-90, f,5, t,45, f,d, t,45
data letters; f,4, t,45, f,d, t,45, f,2, t,90, f,6
data letters; f,-6, t,-90, f,3, t,-90, p,up, e
data letters; "B", p,down, t,-90, f,6, t,90, f,4, t,45, f,d
data letters; t,45, f,1, t,90, f,5, f,-5
data letters; t,-135, f,d, t,45, f,2, t,45, f,d, t,45
data letters; f,5, p,up, f,-6, t,180, e
data letters; "C", t,-90, f,1, p,down, f,4, t,45, f,d, t,45
data letters; f,4, t,45, f,d, t,45, p,up, f,4, t,45, p,down
data letters; f,d, t,45, f,4, t,45, f,d, p,up, f,-d, t,135
data letters; f,5, e
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
data
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
letters;
data
data
data
data
data
letters;
letters;
letters;
letters;
letters;