EV3 Scratch 指南

EV3 Scratch 指南 > Drawing robot(绘图机器人)

Drawing robot 绘图机器人

In this chapter we build a drawing robot.
在本章中,我们将构建一个绘图机器人。

This robot uses three motors:
该机器人使用三个电机:

  • the small one to lift the pen
    小的举起笔
  • the large ones to move
    大的要移动

To have a higher precision, it uses the small wheels. The pen is place right in the center between the two wheels.
为了获得更高的精度,它使用了小轮子。笔位于两个轮子之间的正中央。

Drawing robot   绘图机器人

Lift the pen  抬起笔

Try to turn up the small motor lever to the verticl. If the horizontal postion was 0°, it will be -90°.
尝试将小电机杆调高到垂直。如果水平位置为 0°,则为 -90°。

The angle decreseases as we lift the pen. We can now program the up/down buttons to move the pen. As we want to make this movement as quick as possible, we set the speed to 100%.
当我们抬起笔时,角度会变小。我们现在可以对向上/向下按钮进行编程来移动笔。由于我们希望尽快完成此移动,因此我们将速度设置为 100%。

For the down movement we set a time. This is necessary, as we let the motor hit a mechanical limit. This trick is a calibration without using a sensor. We move the motor to a known position.
对于向下移动,我们设置了一个时间。这是必要的,因为我们让电机达到机械极限。这个技巧是在不使用传感器的情况下进行校准。我们将电机移动到已知位置。

Drawing robot   绘图机器人
  • up: move by 45°  向上:移动 45°
  • down: move during 0.1 seconds
    向下:在 0.1 秒内移动

Define functions  定义函数

A program becomes much readable and versatile when using function. Let’s define two functions up and down
使用 function 时,程序变得更具可读性和通用性。让我们定义两个函数 up 和 down

Drawing robot   绘图机器人

Now we can use this two functions and associate them with the buttons. The code is much more readable.
现在我们可以使用这两个函数并将它们与按钮相关联。代码的可读性要强得多。

Drawing robot   绘图机器人

But there is another advantage. In a large program, we may use the pen in many places. If we change the pen mechanics, or correct a bug with the pen up/down movement, there is one single place to make such a correction
但还有另一个好处。在一个大型程序中,我们可能会在很多地方使用 pen。如果我们更改了笔的机制,或者纠正了笔上/下移动的错误,那么只有一个地方可以进行这样的更正

Move the robot  移动机器人

We go no to moving the robot. We are going to use motors B and C for movement. In order to obtain precise drawing results, we set the speed to 20%.
我们不移动机器人。我们将使用电机 B 和 C 进行移动。为了获得精确的绘图结果,我们将速度设置为 20%。

Drawing robot   绘图机器人

So how much does the robot advance with 1 rotation ? It is difficult to measure from the robot, but it becomes easy if the robot is going to draw a line.
那么机器人旋转 1 圈会前进多少呢?从机器人进行测量很困难,但如果机器人要画一条线,就变得很容易了。

Drawing robot   绘图机器人

Now you can measure the line. It is about 94 mm long.
现在您可以测量线条。它大约有 94 毫米长。

Create a move function  创建 move 函数

Now we have all the information to create a move function with an argument. So go ahead and create a new function with one parameters and these labels.
现在我们有了创建带有参数的 move 函数的所有信息。因此,请继续创建一个具有一个参数和这些标签的新函数。

Drawing robot   绘图机器人

With the rule of three we can calculate the number of rotations for any distance. The number of rotations is distance/94.
使用 3 法则,我们可以计算任何距离的旋转次数。旋转次数为 distance/94

Drawing robot   绘图机器人

Now we can call this function with a specfic argument. For example 50 mm. Try it and measure the length of the line.
现在我们可以使用 specfic 参数调用这个函数。例如 50 毫米。试一试并测量线的长度。

Drawing robot   绘图机器人

Create a line function  创建一个 line 函数

We can go one step further, and directly create a line function.
我们可以更进一步,直接创建一个 line 函数。

Drawing robot   绘图机器人

We can now call the line function to draw a line of for example 120 mm.
我们现在可以调用 line 函数来绘制一条例如 120 mm 的线。

Drawing robot   绘图机器人

Turn the robot  转动机器人

Now let’s turn the robot on place. First we draw a line 100 mm. Then we pivot by 1 wheel rotation to the left. And finally we draw a second line of 100 mm.
现在让我们就地启动机器人。首先我们画一条 100 毫米的线。然后我们向左旋转 1 个轮子。最后,我们绘制第二条 100 毫米的线。

Drawing robot   绘图机器人

We find that the robot turns by 82 degrees. This allows us to create a turn function.
我们发现机器人旋转了 82 度。这允许我们创建一个 turn 函数。

Drawing robot   绘图机器人

Now we can call this function with a 90° angle.
现在我们可以用 90° 角调用这个函数。

Drawing robot   绘图机器人

Draw a polygon  绘制多边形

We have now everything needed to draw a regular polygon. We just use a loop to repeat n sides of a regular polygon. Then we turn an angle of 360/n degrees. For example we can draw a hexagon with a side length of 50 mm.
现在,我们已拥有绘制常规多边形所需的一切。我们只需使用一个循环来重复正多边形的 n 个边。然后我们转动 360/n 度的角度。例如,我们可以绘制一个边长为 50 mm 的六边形。

Drawing robot   绘图机器人

Now we can turn this into a function.
现在我们可以将其转换为函数。

Drawing robot   绘图机器人

Now we can use the polygon function to draw a pentagon with a side length of 40 mm.
现在我们可以使用 polygon 函数绘制一个边长为 40 mm 的五边形。

Drawing robot   绘图机器人

Draw a star  绘制星号

The the star polygon is drawn exactly as the polygon, but the turning angles are multiples of the normal polygon angle. For example, turning 360/5 results in a pentagon. However turning twice that angle, 2x360/5, creates a 5-pointed start.
星形多边形的绘制与多边形完全相同,但转弯角度是正常多边形角度的倍数。例如,转动 360/5 会产生一个五边形。但是,将该角度旋转两倍 (2x360/5) 将创建一个 5 分的起点。

We create a star function which allows as to draw n/m star polygons.
我们创建一个星形函数,允许绘制 n/m 个星形多边形。

Drawing robot   绘图机器人

Now lets draw such a 5-pointed star
现在让我们画这样一个 5 角星

Drawing robot   绘图机器人

You can download the programs so far: draw1.lmsp
到目前为止,您可以下载这些程序: draw1.lmsp

Draw a letter  绘制字母

We have everything in place to draw a letter. For exemple to draw the letter E inside a rectangle of 30 x 40 mm we do this:
我们已经准备好了绘制一封信的一切。例如,要在 30 x 40 毫米的矩形内绘制字母 E,我们这样做:

Drawing robot   绘图机器人

At the end we place the robot to the beginng of the next letter.
最后,我们将机器人放在下一个字母的开头。

A function with 3 arguments
一个有 3 个参数的函数

If you look at the previous program, you notice it’s pretty long. But it consists of a sequence of line, move and turn functions. We could combine these three functions in one. Let’s make this function with 3 arguments:
如果你看一下前面的程序,你会注意到它很长。但它由一系列 line、move 和 turn 函数组成。我们可以将这三个函数合二为一。让我们用 3 个参数制作这个函数:

Drawing robot   绘图机器人

Using this new function, we can reduce the number of function calls from 15 to 7. It is easier to understand, as each line corresponds now to a segment of the letter.
使用这个新函数,我们可以将函数调用次数从 15 次减少到 7 次。它更容易理解,因为现在每行都对应于字母的一个部分。

Drawing robot   绘图机器人

Define letters as functions
将字母定义为函数

The next step is to define a function for each letter. We define the letter E
下一步是为每个字母定义一个函数。我们定义字母 E

Drawing robot   绘图机器人

We define the letter L
我们定义字母 L

Drawing robot   绘图机器人

And now we can write the word ELLE
现在我们可以写 ELLE 这个词

Drawing robot   绘图机器人