简体   繁体   English

什么是同时具有实时循环和非实时功能的更好方法

[英]What is a better approach to have realtime loop and non realtime function at the same time

Sorry for the bad title, I really don't know how to describe that in short... 抱歉标题不好,我真的不知道如何描述...

My scenario is that for a robotic application, we need a realtime loop to control the motor every 1ms. 我的情况是,对于机器人应用程序,我们需要一个实时循环来每1ms控制一次电机。 While at the same time we may want to do something that does not have realtime requirement such as path planning, image processing, object recognition, etc. Furthermore, some of the result from the non-realtime task would be sent to the realtime motor control loop to control the robot. 同时我们可能想要做一些没有实时要求的事情,例如路径规划,图像处理,对象识别等。此外,非实时任务的一些结果将被发送到实时电机控制循环来控制机器人。

For the realtime part, I use Ubuntu with RT-Preempt Patch. 对于实时部分,我使用Ubuntu和RT-Preempt Patch。 Therefore, I can run the realtime control loop in the while loop just like the sample code here . 因此,我可以在while循环中运行实时控制循环,就像这里的示例代码一样。

However, I have no idea for the non-realtime part. 但是,我不知道非实时部分。 In my humble opinion, I would new a thread in the same process and run the non-realtime task in that thread. 在我看来,我会在同一个进程中创建一个线程并在该线程中运行非实时任务。

Since I'm very new to realtime programming, I don't know what would be the problem for my design. 由于我对实时编程很新,我不知道我的设计会出现什么问题。 Besides, I wonder if there are any paradigm for designing such kind of program? 此外,我想知道是否有任何设计此类程序的范例?

---EDIT--- - -编辑 - -

Some more details about my application. 关于我的申请的更多细节。

The robot, to be more specific, is a robot arm. 更具体地说,机器人是机器人手臂。

For the realtime part, it calculate the forward kinematics, inverse kinematics, and jacobian. 对于实时部分,它计算正向运动学,反向运动学和雅可比。 And then calculate the proper output command using a simple PID controller. 然后使用简单的PID控制器计算正确的输出命令。 At last, send the motor command using EtherCAT to each motor. 最后,使用EtherCAT将电机命令发送到每个电机。

For the non-realtime part, for example, may take the PointCloud stream from kinect, do some preprocessing, calculate the pose of the object in the scene, determine a good grasp pose for the robot arm, and at last send the goal of each motor to the realtime part so that the robot arm will actually move to the goal and grasp the object. 例如,对于非实时部分,可以从kinect获取PointCloud流,进行一些预处理,计算场景中对象的姿势,确定机器人手臂的良好抓握姿势,最后发送每个的目标。电机到实时部分,以便机器人手臂实际移动到目标并抓住物体。 The whole process may take 10 sec or so. 整个过程可能需要10秒左右。 However at the same time the realtime loop should keep running and send the proper force command or position command to make the robot arm hold its original pose and stand still. 然而,同时实时循环应该继续运行并发送适当的力命令或位置命令,以使机器人手臂保持其原始姿势并保持静止。

As for communication between these two parts, for most of the occasion, the command is generated by a novel algorithm from the non-realtime part and send it to the realtime part to make the robot arm move. 对于这两个部分之间的通信,在大多数情况下,命令由来自非实时部分的新算法生成,并将其发送到实时部分以使机器人手臂移动。 However, sometimes the algorithm in the non-realtime part will need to know, for example, the current pose of the end effector. 然而,有时非实时部分中的算法将需要知道例如末端执行器的当前姿势。 Therefore, the none-realtime part will need to get the information from the forward kinematics, which resides in the realtime part. 因此,非实时部分将需要从正向运动学中获取信息,该正向运动学驻留在实时部分中。

What is the real-time loop doing exactly? 什么是实时循环? I am guessing (since you mentioned "every 1 millisecond do some motor control") do some very short computations, and output a few bytes to the motor device. 我猜(因为你提到“每1毫秒做一些电机控制”)做一些非常短的计算,并输出几个字节到电机设备。

What is the non-realtime part doing? 什么是非实时部分在做什么? I imagine some user-interface??? 我想象一些用户界面???

What kind of robot? 什么样的机器人?

Path planning may require strong real time, in particular if the robot is moving fast. 路径规划可能需要很强的实时性,特别是在机器人快速移动的情况下。 A cruise missile or a Google Car is not the same as a small robot for the robocup . 巡航导弹或Google Car与robocup的小型机器人不同 Losing a few milliseconds of communication may kill humans on a cruise missile or a Google Car, but is acceptable on the Robocup - you'll loose only a game and perhaps harm slightly your robot. 失去几毫秒的通信可能会在巡航导弹或谷歌车上杀死人类,但是在Robocup上是可以接受的 - 你只会松开一个游戏并且可能会轻微损害你的机器人。

At 40 m/s (144 km/hour, which is slighty above the 130km/h highway road speed limit in France), two milliseconds mean 8 cm, and if that 8 cm is human flesh, it may mean killing someone. 速度为40米/秒(144公里/小时,略高于法国130公里/小时的公路限速),2毫秒意味着8厘米,如果8厘米是人肉,则可能意味着杀死某人。

How the two parts are interacting? 两部分如何相互作用? Is the real-time part sending some information to the other part? 实时部分是否向其他部分发送了一些信息?

Perhaps the two parts could be some different processes (not threads) with some communication? 也许这两个部分可能是一些不同的进程 (而不是线程)与一些通信? Perhaps using shared memory with semaphores for synchronization. 也许使用带信号量的共享内存进行同步。 Then look at sem_overview(7) and shm_overview(7) . 然后查看sem_overview(7)shm_overview(7)

Notice that at the interface between the real-time and "non-real time" part the non-real time part might actually becomes "real-time" also. 请注意,在实时和“非实时”部分之间的接口处,非实时部分实际上也可能变为“实时”部分。

The main point is to define the conceptual interface between the two (real-time vs non real-time) parts, and decide if you can afford losing some data and some synchronization (communicating between the two parts) or not. 重点是定义两个(实时与非实时)部分之间的概念界面,并决定是否能够承受丢失一些数据和一些同步(两部分之间的通信)。

If the role of the non-real time part is simply to display or set the speed (ie some low and high threshold) you probably might afford sometimes losing some data or synchronization. 如果非实时部分的角色只是显示或设置速度(即一些低和高阈值),您可能有时会丢失一些数据或同步。 But the evil is in the details (if the robot is a real autonomous automobile driving on some highway à la Google Car, you probably should not afford losing some exchanges, and then both parts become real-time!). 但是细节就是邪恶(如果机器人是一辆真正的自动驾驶汽车,在Google Car的某些高速公路上行驶,你可能不应该失去一些交换,然后这两个部分都变成了实时!)。

How you divide your overall system into the real time vs regular parts is very important, as stressed by @Basile Starynkevitch. 正如@Basile Starynkevitch强调的那样,如何将整个系统划分为实时与常规部分相比非常重要。

When it comes to the implementation in RT-Linux, the parts of your system with real time requirements will be made into real time threads, which run inside of the kernel. 当谈到RT-Linux中的实现时,系统中具有实时要求的部分将被制作成在内核中运行的实时线程。 This allows them to run unpreempted, but has one giant drawback: there are no protections that one has come to expect in user-space. 这允许它们无阻碍地运行,但是有一个巨大的缺点:没有人在用户空间中期待的保护。 A real time thread crash means the system has crashed. 实时线程崩溃意味着系统崩溃了。 For this reason (and others) you should limit the real time components as much as possible. 出于这个原因(和其他人),您应该尽可能地限制实时组件。

The non-real time components of your system will run as regular Linux processes. 系统的非实时组件将作为常规Linux进程运行。 They can chug along with whatever expensive planning algorithms you want and the real time threads will preempt them and run as needed. 他们可以随意使用您想要的任何昂贵的计划算法,实时线程将抢占它们并根据需要运行。 The tricky part is communication, for which there are two mechanisms provided: FIFOs and shared memory. 棘手的部分是通信,为此提供了两种机制:FIFO和共享内存。

FIFOs are the easiest, and allow unidirectional communication (use two if you want bidirectional). FIFO是最简单的,并且允许单向通信(如果你想要双向,则使用两个)。 They are character devices and you don't have to worry about reading / writing overlaps. 它们是角色设备,您不必担心读/写重叠。 More from tldp.org . 更多来自tldp.org

For passing large amounts of data, shared memory is preferable. 为了传递大量数据,共享内存是更可取的。 With it, two processes map the same section into memory. 有了它,两个进程将同一部分映射到内存中。 But you have to coordinate between the processes / threads to ensure that one does not read in middle of another one writing and vice versa. 但是你必须在进程/线程之间进行协调,以确保不会在另一个写入的中间读取,反之亦然。 This is actually slightly easier to do when one of the parties is a real-time thread, because you know that the real-time thread will not be preempted. 当其中一方是实时线程时,这实际上稍微容易一些,因为您知道实时线程不会被抢占。 More from drdobbs.com . 更多来自drdobbs.com

You also need to be aware of priority inversion (when a high priority thread must wait for a low priority one to release a shared resource, for example). 您还需要了解优先级倒置(例如,当高优先级线程必须等待低优先级线程才能释放共享资源时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM