Gyro Boy is a self-balancing robot.
Gyro Boy 是一个自平衡机器人。
In this section we will do a bit of reverse engineering. The code for a balancing robot is quite complex.
在本节中,我们将进行一些逆向工程。平衡机器人的代码相当复杂。
You can find the official project in the EV3 Classroom under Home > Core Set Models > Gyro Boy.
您可以在 EV3 机器人教室的 首页 > 核心套装型号 > 陀螺仪男孩 。
Or you can download the file here: Gyro Boy 1.lmsp
或者您可以在此处下载文件: 陀螺男孩 1.lmsp
The initial file has a total of 262 blocks. You can see this in the contextual menu.
初始文件总共有 262 个块。您可以在上下文菜单中看到这一点。
Let’s try to simplify this program and keep only the essential blocks.
让我们尝试简化此程序并仅保留必要的块。
Finally we manage to go down to 98 blocks.
最后,我们设法减少到 98 个区块。
You can download the file here: Gyro Boy 2.lmsp
您可以在此处下载文件: 陀螺男孩 2.lmsp
At startup we: 在启动时,我们:
This is the reset function which:
这是重置函数,它:
The first part is about timing. We need to know the time dt it takes for the feedback loop. The loop time dt is used to calculate :
第一部分是关于时间的。我们需要知道反馈循环所需的时间 dt。循环时间 dt 用于计算:
This is a classic 3-line algorithm.
这是一个经典的 3 行算法。
Then we calculte the angle from the angular velocity. Why not reading the light-blue built-in gyro angle variable ?
然后我们根据角速度计算角度。为什么不读取浅蓝色内置的陀螺仪角度变量呢?
Well, it’s less precise and it does not seem to be possible to read both, angular velocity and angle at the same time.
嗯,它不太精确,而且似乎不可能同时读取角速度和角度。
Mathematically speaking we obtain the angle by integrating the angular speed. That’s what we are doing here. The formula for discrete integration is:
从数学上讲,我们通过对角速度进行积分来获得角度。这就是我们在这里所做的。离散集成的公式为:
angle += rate * dt
For the wheel we are in the opposite position. We measure an angle, and calculate the rate (speed) eed with a derivative.
对于轮子,我们处于相反的位置。我们测量一个角度,然后用导数计算速率(速度)。
First we measure the wheel angle, which is the sum of both rotation sensors. If the robot moves in a straight line, they add up. If the robot pivots and stays in place, they cancel each other.
首先,我们测量车轮角度,这是两个旋转传感器的总和。如果机器人沿直线移动,它们就会相加。如果机器人旋转并保持在原位,它们会相互抵消。
Again, why not use the dark blue motor speed variable ?
同样,为什么不使用深蓝色电机速度变量呢?
Well, it’s less precise, because we don’t know exactly what dt is used internally.
嗯,它不太精确,因为我们不知道内部到底用了什么 dt。
The formula for discrete derivation is:
离散推导的公式为:
rate = (angle - angle0) / dt
The robot tries to stay at the target position. The target position is the path lenght when speed is integrated over time:
机器人尝试保持在目标位置。目标位置是速度随时间积分时的路径长度:
target += speed * dt
In technical terms we have a PD controller: P for proporional control and D for Derivative control.
从技术上讲,我们有一个 PD 控制器:P 用于局部控制,D 用于微分控制。
The magic of the control system happens here. The power is a sum of 5 weighted quantities which describe the state.
控制系统的魔力就在这里发生。幂是描述状态的 5 个加权量之和。
Concerning the signs we have this situation:
关于迹象,我们有这样的情况:
In all this cases the power must be positive, to counter the tendency of the robot to fall.
在所有这些情况下,功率必须是正的,以抵消机器人掉落的趋势。
The last part controls the motors.
最后一部分控制电机。
First we limit the power to the range [-100 .. 100]. We use a little trick to get the sign:
首先,我们将功率限制在 [-100 .. 100] 范围内。我们用一个小技巧来得到这个标志:
sign(power) = power/abs(power)
Each time we are in normal regime (not in saturation) we reset the fall time.
每次我们处于正常状态(不是饱和状态)时,我们都会重置坠落时间 。
We use differential steering for the two motors. If the saturation regime lasts more then 1 second, we stop running.
我们对两个电机使用差速转向。如果饱和状态持续超过 1 秒,我们将停止运行。
If the robot falls and runs in saturation for more than 1 second, we:
如果机器人跌落并饱和运行超过 1 秒,我们将:
It’s now very easy to add remote control functionalty. First we use the large button to implement an emergency stop.
现在添加远程控制功能非常容易。首先,我们使用大按钮实现紧急停止。
We use the left side buttons to control speed.
我们使用左侧按钮来控制速度。
and we use the right side buttons to steer.
我们使用右侧按钮进行转向。