In this section we program the robot to create Morse code.
在本节中,我们将对机器人进行编程以创建摩尔斯电码。
Samuel Morse invented this code which uses dots and dashes to encode letters. He looked at the frequency of letters and assigned the shortes codes to the most frequent letters.
塞缪尔·莫尔斯 (Samuel Morse) 发明了这种代码,它使用点和破折号来编码字母。他查看了字母的频率,并将 shortes 代码分配给最频繁的字母。
Here is the code 这是代码
Create a dot 创建一个点
This section uses the Drawbot from the previous section. You will use these functios
本节使用上一节中的 Drawbot。您将使用这些 functios
You can download this program which contains already this functions as your starting point: draw.lmsp
您可以下载此程序,该程序已包含此功能作为您的起点: draw.lmsp
Let’s start with drawing a dot. That’s just a line of lenght 0 mm. We play a sound which is defined by the duration it takes to draw that dot.
让我们从画一个点开始。那只是一条长度为 0 mm 的线。我们播放一个声音,该声音由绘制该点所需的持续时间定义。
The dash is the same, just 3 mm long.
仪表板是一样的,只有 3 毫米长。
The basic symbols are separated by 3 mm of distance.
基本符号相隔 3 mm 的距离。
We could now make two functions dash and dot and place the 4 lines of code inside. However since they are very similar, we will make just dash and give it a parameter.
我们现在可以将两个函数 dash 和 dot 放在里面,并将 4 行代码放在里面。但是,由于它们非常相似,我们将只制作 dash 并为其指定一个参数。
Now we can call it with these two parameters
现在我们可以使用这两个参数来调用
Now we can program for example the code for the letter Q. It’s dash-dashes-dot-dash. We have to add an extra 3 mm space to separate it from the next letter.
现在我们可以对字母 Q 的代码进行编程。它是 dash-dashes-dot-dash。我们必须额外添加 3 毫米的间距以将其与下一个字母分开。
We could define a fonction for all 26 letters. But there is a better way
我们可以为所有 26 个字母定义一个函数。但有更好的方法
The modulo function or mod function returns the result of a divison. In our case we will use mod 10 which will give us the reminder of a divison by 10, or in other words it gives us the last digit of the number:
模函数或 mod 函数返回除数的结果。在我们的例子中,我们将使用 mod 10,它会提醒我们一个除数被 10 的分数,或者换句话说,它给我们数字的最后一位数字:
21011 mod 10 = 1
2101 mod 10 = 1
210 mod 10 = 0
21 mod 10 = 1
2 mod 10 = 2
The function floor returns the integer part of a number we get from the division by 10:
函数 floor 返回我们从除以 10 得到的数字的整数部分:
floor 2101.1 = 2101
floor 210.1 = 210
floor 2.1 = 2
It is still the letter Q, but the sequence is read from the back.
它仍然是字母 Q,但序列是从后面读取的。
We can now create a function which encodes a number sequence into Morse code. The number sequence has to be defined in reverse order. The code is composed of 3 digits:
现在,我们可以创建一个函数,将数字序列编码为摩尔斯电码。编号规则必须按相反的顺序定义。代码由 3 位数字组成:
The end marker is necessary because in numbers preceding zeros are ignored.
结束标记是必需的,因为在数字中,0 前面的 0 会被忽略。
The encode function is used to define a function for each letter (A, B, C, etc.)
encode 函数用于定义每个字母(A、B、C 等)的函数
We can compose words by using these functions. As en example we print the letters ABCDE.
我们可以使用这些函数来编写单词。例如,我们打印字母 ABCDE。
You can download the programs so far: morse.lmsp
到目前为止,您可以下载这些程序: 莫尔斯.lmsp