"High cohesion and low coupling" is an important philosophy in modern engineering. In scenarios such as embedded systems or edge systems, code with high cohesion and low coupling also helps to form a good architecture and achieve long-term maintenance. Therefore, this paper aims to propose a system architecture that is convenient for design and simulation to implement the design and simulation of the control system.
In this paper, a six-axis robotic arm will be taken as an example to design a simplified 6-degree-of-freedom, 6-dimensional control input system, and discuss the design and simulation architecture of the system. Simple pseudo-code will be attached in the text to help understand the basic architecture.
Firstly, let's consider a pure embedded system. To ensure the efficient and good transmission of system context, while taking into account compilation efficiency, the approach of using a handle to save the system context is considered. The pointer is the system handle, and it is initialized and destroyed by a dedicated function. At the same time, the original state data of sensor data is read by a dedicated function.
// system_handle.h
typedef struct {
double joints_pos[6];
double joints_vel[6];
double joint_torque[6];
} SystemContext;
typedef SystemContext *SystemHandle;
SystemHandle create_handle();
int destroy_handle(SystemHandle handle);
int get_joints_pos(SystemHandle handle, double *op);
int get_joints_vel(SystemHandle handle, double *op);
int set_torque(SystemHandle handle, double *inp);
Once the operations of these handles are defined, the hardware driver can use these functions to write the internal data of the sensor.
// driver.h
include "system_handle.h"
int get_state(SystemHandle handle);
int actuate(SystemHandle handle);
Furthermore, in the control function, the controller logic is specifically implemented, receiving the handle and writing the control quantity based on the internal state.
Thus the call becomes much simpler in the main loop.
// main.c
#include "system_handle.h"
#include "driver.h"
#include "control.h"
int main_loop(SystemHandle handle) {
get_state(handle);
control(handle);
actuate(handle);
return 0;
}
int main() {
SystemHandle handle = create_handle();
int loop_state = 0;
while (loop_state >= 0) {
loop_state = main_loop(handle);
}
destroy_handle(handle); // If the program mey exit the main loop.
return 0;
}
For scenarios involving low-frequency writing on the upper layer of Python, such as when a large model with slow inference outputs a target, the SystemContext can introduce fields like target_pos and target_vel to simultaneously implement the corresponding writing functions. The main program should have global variables to record the set targets and the corresponding setting functions. Here, atomic integer and sequence locks can be used for protection. In the implementation of main_loop, the target update should be achieved first, followed by the update of the body state, controller calculation, and distribution of actuators. Here is a simple example.
// system_control.dll (source code)
#include <stdint.h>
#include <stdatomic.h>
#include <pthread.h>
#include "system_handle.h"
#include "driver.h"
#include "control.h"
// ------------- Target Data -------------
_Atomic uint64_t SEQ = 2;
double TARGET_BUFFER[6];
// ------------- System Loop -------------
pthread_t SYSTEM_LOOP;
// Interface
int set_target(double *inp) {
uint64_t s = atomic_load_explicit(&SEQ, memory_order_acquire);
atomic_store_explicit(&SEQ, s + 1, memory_order_relaxed);
atomic_thread_fence(memory_order_acq_rel);
memcpy(TARGET_BUFFER, inp, 6 * sizeof(double));
atomic_store_explicit(&SEQ, s + 2, memory_order_release);
}
int update_target(SystemHandle handle) {
static uint64_t last_seq = 0;
static double local_buffer[6];
uint64_t s1, s2;
uint8_t flag = 0;
// update the local buffer
s1 = atomic_load_explicit(&SEQ, memory_order_acquire);
if (s1 != last_seq && !(s1 & 1)) {
memcpy(local_buffer, TARGET_BUFFER, 6 * sizeof(double));
atomic_thread_fence(memory_order_acq_rel);
s2 = atomic_load_explicit(&SEQ, memory_order_acquire);
if (s2 == s1) {
flag = 1;
last_seq = s2;
}
}
if (flag)
set_target_pos(handle, local_buffer);
return 0;
}
int main_loop(SystemHandle handle) {
update_target(handle);
get_state(handle);
control(handle);
actuate(handle);
return 0;
}
void *loop_fn(void *arg) {
SystemHandle handle = create_handle();
int loop_state = 0;
while (loop_state >= 0) {
loop_state = main_loop(handle);
}
destroy_handle(handle); // If the program mey exit the main loop.
return NULL;
}
int init() {
int ret = pthread_create(&SYSTEM_LOOP, NULL, &loop_fn, NULL);
if (ret != 0) {
perror("Failed to create thread.");
return -1;
}
return 0;
}
int close() {
pthread_join(SYSTEM_LOOP, NULL);
}
Here I've optimized the synchronous update logic (update_target). It only updates when the version number changes (to handle low-frequency writes), and a buffer is introduced to prevent dirty data from directly interfering with the system operation. Moreover, it does not spin to protect the high-frequency control logic. In case of read failure, the old target is continued to avoid uncontrollable spinning. If in the system design, one still prefers to introduce spinning, simply move the local buffer update in the synchronous update logic to do {...} while (s1 & 1 || s2 != s1) is sufficient. Here it is important to note the optimistic initialization in the loop. Without it, if there are no updates (s1 == last_seq) and s2 remains not equal to s1, the spin will never stop. Therefore, s2 should be equal to s1 at the beginning. As for whether s2 will change later, this will be re-verified after the possible data copying.
int update_target(SystemHandle handle) {
static uint64_t last_seq = 0;
static double local_buffer[6];
uint64_t s1, s2;
uint8_t flag = 0;
// update the local buffer
do {
// As s1 != s2 is part of the condition of spin,
// we'll have an optimistic initialization.
s2 = s1 = atomic_load_explicit(&SEQ, memory_order_acquire);
if (s1 != last_seq && !(s1 & 1)) {
memcpy(local_buffer, TARGET_BUFFER, 6 * sizeof(double));
atomic_thread_fence(memory_order_acq_rel);
s2 = atomic_load_explicit(&SEQ, memory_order_acquire);
if (s2 == s1) {
flag = 1;
last_seq = s2;
}
}
} while (s1 & 1 || s2 != s1);
if (flag)
set_target_pos(handle, local_buffer);
return 0;
}
For simulation, all needed is to compile the controller file and the handle definition file into a DLL (note that in the simulation, the write capability of the underlying state data should be exposed, such as set_joints_pos, etc.), and then the data flow can be conveniently called and controlled through the Python program. Just replace the actuate in the main loop with the corresponding dynamic update, and read the state of the corresponding system using get_state.
import numpy as np
import ctypes
class Simulator:
def __init__(self, sys_dll, **init_states):
self.sys_dll = sys_dll
self.sys_dll.create_handle.restype = ctypes.c_void_p
...
self.handle = self.sys_dll.create_handle()
# initialize the states
self.joints_pos = init_states["joints_pos"]
...
self.records = []
def get_state(self):
self.sys_dll.set_joints_pos(
self.handle,
self.joints_pos.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
)
...
def control(self):
self.sys_dll.control(self.handle)
def actuate(self):
"""system dynamics"""
u = self.sys_dll.get_torque(self.handle)
...
def step(self):
self.get_state()
self.control()
self.actuate()
self.records.append(...)
Top comments (0)