3

我想用这个问题来讨论通过 2 个或更多微控制器的网络进行同步的算法。

今天下午我开始考虑这个问题,我会让 2 个不同的微控制器交换数据,然后同步执行以同时开始工作并在大约同一时间完成工作。这是一种确保您正在运行的代码是正确的并且内存中可能没有错误的方法(您可以想象,在两个微控制器上出现完全相同错误的可能性非常低)。

我想只发送带有微控制器 1 时间戳的消息,然后第二个微控制器将接收它并将其与自己的时间戳进行比较,然后将其重新发送到微控制器 1。

那时,他们都将开始执行相同的代码,并在最后同步,在那里他们将交换彼此的结果,然后他们会得出结论,如果有什么问题,或者一切正常。

我认为这将是实现两个微控制器之间执行同步的“体面”方式,但我无法摆脱可能会有更好、更智能的方式来做到这一点。

这个“可能”适用于 2 个微控制器,但是当我想使用更多微控制器时呢?如果我想按照我之前解释的方式实现一个 5 核系统(例如 5 个 arm9 核)怎么办。我不认为这适用于几个微控制器。即使我想实现一个所有微控制器都可以完成不同工作的 5 核系统(我认为在所有情况下同步必须是最好的......我真的不知道我从来不需要超过一个内核,但从现在开始我需要这样做)

编辑:

我想用这个问题来理解通过网络同步的算法,或者在同一块板上,2个或更多微控制器。

今天下午我开始考虑这个问题,我会让 2 个不同的微控制器交换数据,然后同步执行以同时开始工作并在大约同一时间完成工作。这是一种确保您正在运行的代码是正确的并且内存中可能没有错误的方法(您可以想象,在两个微控制器上出现完全相同错误的可能性非常低)。

我想只发送带有微控制器 1 时间戳的消息,然后第二个微控制器将接收它并将其与自己的时间戳进行比较,然后将其重新发送到微控制器 1。

那时,他们都将开始执行相同的代码,并在最后同步,在那里他们将交换彼此的结果,然后他们会得出结论,如果有什么问题,或者一切正常。

我认为这将是实现两个微控制器之间执行同步的“体面”方式,但我无法摆脱可能会有更好、更智能的方式来做到这一点。

4

2 回答 2

2

There are certainly systems around where two or more processors are performing the same task and the results are compared. I know of autmatic train controls that have two systems that should produce the same result, and if one of the system DOESN'T produce the same result, the emergency brake is "pulled" and the train stops until the driver pushes the "I notice the computer isn't working right" button. The two systems have software written by two different sets of developers, who are not sharing any information on how they go about implementing their solution so as to avoid "common errors because we both thought THIS should be solved this way".

Airplane electronic usually uses an uneven number and "majority voting" to select between multiple answers from different systems - it's a bad idea to just "stop" in an ariplane. Again, systems are using different software and often also different suppliers of for example processors and languages - so one system is written in C or C++, another in Java and the third one in Pascal or ADA [as an example] - to reduce the chances of a processor, language or compiler bug causing EVERY thing in the system from going "wrong".

From a previous job working with x86, I have looked into this in a fair bit of detail for a customer who wanted to have a "backup processor running in lock-step", and they had to use a modified compiler that added I/O instructions at decision points in the code [branches, calls, returns, etc] and then external hardware to ensure that the processors were actually in sync]. So, modern processors, including such ARM have enough "clever stuff" that is fairly "unpredictable" inside the processor that it's almost impossible to make two processors run in exact lock-step with each other. Sure you can get something that "performs the same task in the same amount of time", as long as you don't measure time TOO precisely.

Superscalar execution units, asynchronous interrupts, asynchronous(ish) memory controllers, caches, content of caches, all conspire to make one processor run just a little faster or slower than the other.

So, there is a limit to how precisely you can make the systems "sync".

Network Time Protocol (NTP) does have some pretty clever algorithms for setting/syncing the time between multiple systems, which allows several systems to have the same idea of "time". But bear in mind that again, there is a limit to "sameness". It is likely several microseconds.

So, whether you can "start at the same time" depends on what you mean by "same time". Is microseconds or milliseconds OK? If so, almost certainly possible. If you mean clock-cycles, then probably not.

于 2013-03-14T21:32:00.327 回答
2

There are a number of ways to do this, depending on how custom you'd want to make the system, and how precise you need it to run.

You're on the right track with the timestamps being sent back and forth though. The "standard" way of doing local synchronization like this is the Precision Time Protocol (PTP / IEEE 1588), which works down to microsecond precision, depending on the implementation (opposed to the Network Time Protocol, NTP, which is better suited for long-distance synchronization, and can typically not achieve synchronization better than in the millisecond-range).

If you have a Linux system with Ethernet or similar running on your microcontrollers, you could thus have a look at PTPD. Or, if you want to implement something yourself, have a look at the PTP (and probably also the NTP) algorithm(s) and be inspired.

于 2013-03-14T22:27:52.343 回答