从ZCM通讯简介中我们了解到ZCM还适用于多线程应用,能够提供稳定的内部通讯机制。基于此功能实现了一个内部通讯的demo工程,实现原理:发布订阅使用通一个队列,发布时往队列中写入数据,zcm获取从同一个队列中获取数据,然后按照协议解析,就能拿到一个线程中发布的数据,并根据订阅的主题回调对应的函数执行相应的功能,此demo使用轮询的方式来获取队列中的数据,会性能不足,只是提供一种思路;需要线程中的发布订阅模型还请参考uorb来实现。
-
实现ZCM的发送接收功能函数
size_t serial_get(uint8_t* data, size_t nData, void* usr)
{
if(recv_send_buf != NULL)
{
return rt_ringbuffer_get(recv_send_buf, data, nData);
}
return 0;
}size_t serial_put(const uint8_t* data, size_t nData, void* usr)
{
if(recv_send_buf != NULL)
{
return rt_ringbuffer_put(recv_send_buf, data, nData);
}
return 0;
}
2.创建ZCM对象,订阅主题
struct rt_ringbuffer* recv_send_buf = NULL;
zcm_trans_t * serial_trans = NULL;
zcm_t * serial_zcm = NULL;
int main(void)
{
rt_thread_t tid;
int count = 1;
/* set LED0 pin mode to output */
recv_send_buf = rt_ringbuffer_create(4096);
if(recv_send_buf == NULL)
{
rt_kprintf("recv send buf malloc failed\r\n");
}
serial_trans = zcm_trans_generic_serial_create(serial_get, serial_put, serial_trans, tick_now, NULL, 1514, 2048);
serial_zcm = zcm_create_from_trans(serial_trans);
zcm_subscribe(serial_zcm, "example", example_1_handle, NULL);
zcm_subscribe(serial_zcm, "example", example_2_handle, NULL);
zcm_subscribe(serial_zcm, "example", example_3_handle, NULL);
return RT_EOK;
}
3。创建2个线程,一个线程用数据数据刷新获取订阅的主题执行回调功能,一个用发布主题主题消息到总线中。
int64_t tick_now(void* usr)
{
return rt_tick_get();
}
void example_1_handle(const zcm_recv_buf_t* rbuf, const char* channel, void* usr)
{
rt_kprintf("%s, %d, %d\r\n", channel, rbuf->data_size,*((uint32_t *)rbuf->data));
}
void example_2_handle(const zcm_recv_buf_t* rbuf, const char* channel, void* usr)
{
rt_kprintf("%s, %d, %d\r\n", channel, rbuf->data_size,*((uint32_t *)rbuf->data));
}
void example_3_handle(const zcm_recv_buf_t* rbuf, const char* channel, void* usr)
{
rt_kprintf("%s, %d, %d\r\n", channel, rbuf->data_size,*((uint32_t *)rbuf->data));
}
void task_entry_1(void* param)
{
while(1)
{
zcm_flush(serial_zcm);
rt_thread_delay(1);
}
}
void task_entry_2(void* param)
{
uint32_t dt = 0;
while(1)
{
dt++;
zcm_publish(serial_zcm, "example", (uint8_t *)&dt, 1);
rt_thread_delay(10);
}
}
int main(void)
{
rt_thread_t tid;
int count = 1;
/* set LED0 pin mode to output */
recv_send_buf = rt_ringbuffer_create(4096);
if(recv_send_buf == NULL)
{
rt_kprintf("recv send buf malloc failed\r\n");
}
serial_trans = zcm_trans_generic_serial_create(serial_get, serial_put, serial_trans, tick_now, NULL, 1514, 2048);
serial_zcm = zcm_create_from_trans(serial_trans);
zcm_subscribe(serial_zcm, "example", example_1_handle, NULL);
zcm_subscribe(serial_zcm, "example", example_2_handle, NULL);
zcm_subscribe(serial_zcm, "example", example_3_handle, NULL);
tid = rt_thread_create("task_1", task_entry_1, RT_NULL, 1024, 9, 10);
rt_thread_startup(tid);
//tid = rt_thread_create("task_2", task_entry_2, RT_NULL, 1024, 10, 10);
//rt_thread_startup(tid);
uint32_t dt = 0;
while(1)
{
dt++;
zcm_publish(serial_zcm, "example", (uint8_t *)&dt, 1);
rt_kprintf("send :%d\r\n", dt);
rt_thread_delay(10);
}
return RT_EOK;
}
- 从嵌入式MCU中运行后,我们会发现三个订阅都会被回调(example_1_handle,example_2_handle,example_3_handle),并将接收的打印出来。