遇到了ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888后没有输出的问题。
遇到了单片机串口监听会有如下类似输出:
Guru Meditation Error: Core 1 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d2cb0: 6cab6520 04adf01d e5201110
Core 1 register dump:
PC : 0x400d2cb4 PS : 0x00060730 A0 : 0x00000000 A1 : 0x3ffb4a70
A2 : 0x3ffc3d40 A3 : 0x3ffc3cb8 A4 : 0x3ffc3d74 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x800d2cb4 A9 : 0x3ffb4a50
A10 : 0x00000001 A11 : 0xfffffffd A12 : 0x00000000 A13 : 0x00000000
A14 : 0x00000001 A15 : 0x3ffc3cb8 SAR : 0x00000020 EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x40084681 LEND : 0x40084689 LCOUNT : 0x00000027
Backtrace: 0x400d2cb1:0x3ffb4a70
ELF file SHA256: 49c2f071dd978ec8
Rebooting...
ets Jul 29 2019 12:21:46
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
解决办法检查ip是否正确(wsl2可以开镜像模式,不会的问问ai)。检查WiFi是否连上,wsl用户确保防火墙给过(我就是wifi连上但是防火墙问题,也可以问问ai),wifi质量。
调试的时候在代码里多加一点打印函数以确保知道哪一步出问题了。
void microros_task(void *args)
{
// 1.设置传输协议并延迟一段时间等待设置的完成。
IPAddress agent_ip; // 用来存储 IP 地址。
agent_ip.fromString("xxx"); // 电脑/手机当前的IP地址。
set_microros_wifi_transports("xxx", "xxx", agent_ip, 8888); // 设置传输协议。告诉机器人连名称为“xxx”的WiFi,密码为“xxx”。之后去往代理服务器的IP地址为xxx,端口号为8888的地方发消息。
Serial.print("WiFi status: ");
Serial.println(WiFi.status() == WL_CONNECTED ? "Connected" : "Failed"); // 判断wifi是否连接上,ture则"Connected",false则"Failed"。
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
delay(2000); // 等待2秒,等待wifi连接。
// 2.初始化内存分配器。
allocator = rcl_get_default_allocator(); // 获取默认的内存分配器。
// 3.初始化支持,并检查错误。
if (rclc_support_init(&support, 0, NULL, &allocator) != RCL_RET_OK) { Serial.println("support init failed"); }
// 初始化支持。使用这个默认的内存分配器 allocator(它使用标准的 malloc 和 free),在没有任何命令行参数的情况下,帮我初始化好 ROS 2 客户端库的底层环境,并把所有初始化好的状态信息都保存在 support 这个结构体里,供我后续创建节点使用。
// 4.初始化节点,并检查错误。
if (rclc_node_init_default(&node, "fishbot_motion_control", "", &support) != RCL_RET_OK) { Serial.println("node init failed"); }
// 初始化节点。在由 support 提供的运行环境中,创建一个名为 fishbot_motion_control 的节点,它位于全局命名空间(""),并把这个节点的所有状态信息保存在 node 这个结构体里。
// 5.初始化执行器,并检查错误。
unsigned int num_handles = 1; // 订阅和计时器的数量,注意这是一个要改的参数(句柄的数量)。大概,比如我设置5,它就有了5个槽位,编号0-4。现在比如0-3我填满了(有东西要执行),他就会去依次询问0-3有没有执行(有没有消息),没执行要去执行他们对应的功能(有消息回调)。句柄 = 唯一识别号。
if (rclc_executor_init(&executor, &support.context, 1, &allocator) != RCL_RET_OK) { Serial.println("executor init failed"); }
// 初始化执行器。使用 allocator 这个内存分配器,在 support.context 提供的 ROS 通信上下文里,初始化一个执行器 executor,让它能够管理 num_handles 个句柄(比如x个订阅者或定时器)。
// 6.初始化订阅者,并将其添加到执行器中。
rclc_subscription_init_best_effort(&sub_cmd_vel, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(geometry_msgs, msg, Twist), "/cmd_vel"); // 订阅者句柄地址,节点句柄地址,消息类型支持(什么类型的消息)(地址),话题名称(订阅哪个话题)。ROSIDL_GET_MSG_TYPE_SUPPORT 是 ROS 2 C语言客户端库(rclc)中一个非常重要的预处理宏,它就像是订阅者与特定消息类型之间的"桥梁"或"身份证"。
rclc_executor_add_subscription(&executor, &sub_cmd_vel, &msg_cmd_vel, twist_callback, ON_NEW_DATA); // 执行器句柄地址,订阅者句柄地址,存放接收到的消息的内存空间的地址(消息缓冲区),回调函数,触发条件:有新数据时触发,一收到消息就执行回调。
// 7.循环执行器。
rclc_executor_spin(&executor); // 循环执行器。它会持续地、反复地检查所有已添加到执行器的句柄(如订阅者、定时器),并处理它们对应的回调函数。只要 ROS 2 的上下文(context)保持有效,这个循环就不会停止.
// // spin 返回说明上下文已失效;不要跑完任务,直接挂起:
// while (1) { vTaskDelay(pdMS_TO_TICKS(1000)); } // 或 vTaskDelete(NULL);
}
此外,调试的时候在setup里的打印串口不一定来得及显示,故setup()中别忘了延迟。
void setup()
{
// 初始化串口。
Serial.begin(115200); // 初始化串口通信,波特率为115200
// delay(5000); // 测试使用,确保串口能监视。
测试时,ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888一直开着,小车复位就行。
我之前串口显示:
WiFi status: Connected
IP: xxx
support init failed
node init failed
windows下打开powershell(管理员)
New-NetFirewallRule -DisplayName "Micro-ROS Agent" -Direction Inbound -Action Allow -Protocol UDP -LocalPort 8888
后来就没有问题了。
~/chapt9/fishbot_ws$ ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888
[1788770845.577995] info | UDPv4AgentLinux.cpp | init | running... | port: 8888
[1788770845.578219] info | Root.cpp | set_verbose_level | logger setup | verbose_level: 4
[1788770856.901357] info | Root.cpp | create_client | create | client_key: 0x2DA90876, session_id: 0x81
[1788770856.901571] info | SessionManager.hpp | establish_session | session established | client_key: 0x2DA90876, address: xxx
[1788770857.263512] info | ProxyClient.cpp | create_participant | participant created | client_key: 0x2DA90876, participant_id: 0x000(1)
[1788770857.460412] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x2DA90876, topic_id: 0x000(2), participant_id: 0x000(1)
[1788770857.714279] info | ProxyClient.cpp | create_subscriber | subscriber created | client_key: 0x2DA90876, subscriber_id: 0x000(4), participant_id: 0x000(1)
[1788770857.743909] info | ProxyClient.cpp | create_datareader | datareader created | client_key: 0x2DA90876, datareader_id: 0x000(6), subscriber_id: 0x000(4)
[1788771241.953583] info | Root.cpp | delete_client | delete | client_key: 0x2DA90876
[1788771241.953708] info | SessionManager.hpp | destroy_session | session closed | client_key: 0x2DA90876, address: xxx
[1788771241.953756] info | Root.cpp | create_client | create | client_key: 0x332EE297, session_id: 0x81
[1788771241.953773] info | SessionManager.hpp | establish_session | session established | client_key: 0x332EE297, address: xxx
[1788771242.306195] info | ProxyClient.cpp | create_participant | participant created | client_key: 0x332EE297, participant_id: 0x000(1)
[1788771242.369915] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x332EE297, topic_id: 0x000(2), participant_id: 0x000(1)
[1788771242.511903] info | ProxyClient.cpp | create_subscriber | subscriber created | client_key: 0x332EE297, subscriber_id: 0x000(4), participant_id: 0x000(1)
[1788771242.601624] info | ProxyClient.cpp | create_datareader | datareader created | client_key: 0x332EE297, datareader_id: 0x000(6), subscriber_id: 0x000(4)
以及
~/chapt9/fishbot_ws$ ros2 node list
/fishbot_motion_control