The EV3 has a timer which starts counting when the program starts.
EV3 有一个计时器,当程序启动时开始计数。
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.
计时器是一个由微处理器递增的变量。它以秒为单位显示自启动或上次定时器重置以来的时间。它具有毫秒级的精度。
The reset timer function sets the timer back to 0.
reset timer 函数将 timer 设置回 0。
The when timer event activates a single event when the timer crosses the given threshold.
当计时器超过给定阈值时,when timer 事件会激活单个事件。
We can record intermediate times and write them to the screen.
我们可以记录中间时间并将其写入屏幕。
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 次以获得良好的精度。让我们定义一个函数计时 ,它:
We will also use a self-defined function add.
我们还将使用自定义函数 add.
These are several loops used to make the timings.
这些是用于计时的几个 Loop。
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:
这让我们大致了解了不同的区块执行需要多长时间:
While basic operations take 50-100 us, the user-defined functions have a 20-times overhead.
虽然基本作需要 50-100 us,但用户定义的函数的开销是 20 倍。
To program this timer we will use the technique of the state machine. In our case we have 3 states:
为了对这个 timer 进行编程,我们将使用状态机的技术。在我们的例子中,我们有 3 种状态:
We define 3 variables: 我们定义了 3 个变量:
In the reset state we set the delay with the up/down buttons.
在 reset 状态下,我们使用 up/down 按钮设置延迟。
With the center button we set the timeout to timer + delay and switch to the count state.
使用中心按钮,我们将超时设置为 timer + delay 并切换到计数状态。
With the left button we stop the alarm sound and return to the reset state.
使用左侧按钮,我们停止闹钟声音并返回重置状态。
The state machine consists of a forever loop with 3 if* blocks which check for the 3 states.
状态机由一个 forever 循环组成,该循环具有 3 个 if* 块,用于检查 3 种状态。
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 行。