EV3 Scratch 指南

EV3 Scratch 指南 > Timer(定时器)

Timer 定时器

The EV3 has a timer which starts counting when the program starts.
EV3 有一个计时器,当程序启动时开始计数。

Display the timer  显示计时器

The timer is a varible which is incremented by the micro-processor. It tells the time in seconds since start-up or the last timer reset. It has milli-second precision.
计时器是一个由微处理器递增的变量。它以秒为单位显示自启动或上次定时器重置以来的时间。它具有毫秒级的精度。

Timer  定时器

The reset timer function sets the timer back to 0.
reset timer 函数将 timer 设置回 0。

Timer  定时器

The when timer event activates a single event when the timer crosses the given threshold.
当计时器超过给定阈值时,when timer 事件会激活单个事件。

Timer  定时器

Record intermediate times
记录中间时间

We can record intermediate times and write them to the screen.
我们可以记录中间时间并将其写入屏幕。

Timer  定时器

Measure EV3 speed  测量 EV3 速度

Now we can measure how much it takes for the EV3 to execute its operations. The idea is to repeat a function 1000 times in a loop to have a good precision. Let’s define a function timing which:
现在我们可以测量 EV3 执行其作所需的时间。这个想法是在循环中重复一个函数 1000 次以获得良好的精度。让我们定义一个函数计时 ,它:

  • increments the line number
    递增行号
  • writes a text  编写文本
  • multiplies the timer with 1000 (to obtain microsecons)
    计时器乘以 1000(以获得 MicroSecon)
  • add us  加入我们
  • reset the timer for the next mesasurement
    重置计时器以进行下一次测量
Timer  定时器

We will also use a self-defined function add.
我们还将使用自定义函数 add.

Timer  定时器

These are several loops used to make the timings.
这些是用于计时的几个 Loop。

Timer  定时器

This is the result:  结果如下:

loop: 43 us 
set: 62 us 
mul: 92 us 
sin: 101 us 
fun: 927 us 

This gives us a rough idea how long different blocks take to execute:
这让我们大致了解了不同的区块执行需要多长时间:

  • 43 us for a loop
    43 微秒 (循环)
  • 20 us for a set (variable assignment)
    一组 20 us(变量赋值)
  • 30-40 us for a math operation (add, mul, sin, etc.)
    数学运算 30-40 us(add、mul、sin 等)
  • 900 us for a user-defined function (My block)
    用户定义的函数 (My 块) 为 900 us

While basic operations take 50-100 us, the user-defined functions have a 20-times overhead.
虽然基本作需要 50-100 us,但用户定义的函数的开销是 20 倍。

Kitchen timer  厨房计时器

To program this timer we will use the technique of the state machine. In our case we have 3 states:
为了对这个 timer 进行编程,我们将使用状态机的技术。在我们的例子中,我们有 3 种状态:

  • reset  重置
  • count  计数
  • alarm  报警

We define 3 variables:  我们定义了 3 个变量:

  • delay is the duration of the count-down in seconds
    delay 是倒计时的持续时间(以秒为单位)
  • state is one of the strings reset, count, alarm
    state 是字符串 reset、count、alarm 之一
  • timeout is the point in time of the alarm
    timeout 是告警的时间点

In the reset state we set the delay with the up/down buttons.
在 reset 状态下,我们使用 up/down 按钮设置延迟。

Timer  定时器

With the center button we set the timeout to timer + delay and switch to the count state.
使用中心按钮,我们将超时设置为 timer + delay 并切换到计数状态。

Timer  定时器

With the left button we stop the alarm sound and return to the reset state.
使用左侧按钮,我们停止闹钟声音并返回重置状态。

Timer  定时器

The state machine consists of a forever loop with 3 if* blocks which check for the 3 states.
状态机由一个 forever 循环组成,该循环具有 3 个 if* 块,用于检查 3 种状态。

  • in reset state, we just show the delay
    在 reset 状态下,我们只显示延迟
  • in count state, we show the count-down
    在 count 状态下,我们显示 count-down
  • in alarm state, we repeat the alarm sound
    闹钟状态下,我们重复闹钟声音
Timer  定时器

Notice here, that we never need to reset the timer. This can be important in timing applications for not losing precision.
请注意,我们永远不需要重置计时器。这在 timing 应用程序中对于不损失精度可能很重要。

The timer is displayed in line 1 and the state in line 2.
计时器显示在第 1 行,状态显示在第 2 行。