You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Webots接收器节点通信报错:无效设备标签的解决方法

修复Webots接收器机器人的无效设备标签错误

问题场景

编写了Webots接收器机器人代码,编译成功但运行时出现以下错误:

Error: wb_receiver_enable(): invalid device tag.
Error: wb_receiver_get_sampling_period(): invalid device tag.

用户代码如下:

#include <webots/robot.h>
#include <webots/receiver.h>

#include <stdio.h>
#include <math.h>

#define TIME_STEP 64

int main(int argc, char **argv) {
  /* necessary to initialize webots stuff */
  wb_robot_init();
  WbDeviceTag rx = wb_robot_get_device("receiver");
  wb_receiver_enable(rx, 64);
  printf("Receiver sampling period: %d",wb_receiver_get_sampling_period(rx));
  
  while (wb_robot_step(TIME_STEP) != -1) {
    
    if (wb_receiver_get_queue_length(rx) > 0) {
      const char *message = wb_receiver_get_data(rx);
      const double *dir = wb_receiver_get_emitter_direction(rx);
      double signal = wb_receiver_get_signal_strength(rx);
      printf("received: %s (signal=%g, dir=[%g %g %g])\n",
             message, signal, dir[0], dir[1], dir[2]);
      wb_receiver_next_packet(rx);
    }
  };

  /* Enter your cleanup code here */

  /* This is necessary to cleanup webots resources */
  wb_robot_cleanup();

  return 0;
}

修复步骤

  • 核对接收器设备名称
    打开Webots场景编辑器,选中接收器机器人节点,找到已添加的Receiver设备,查看其Name属性。确保代码中wb_robot_get_device("receiver")的字符串和设备名称完全一致(大小写敏感),比如设备名为my_receiver,则代码需改为wb_robot_get_device("my_receiver")。

  • 确认机器人已添加Receiver设备
    检查机器人节点的子节点列表,确认存在Receiver设备。若未添加,右键机器人节点 → Add → Devices → Receiver。

  • 添加设备有效性校验(可选)
    在启用接收器前增加校验,提前排查设备不存在的问题,修改代码片段如下:

    WbDeviceTag rx = wb_robot_get_device("receiver");
    if (rx == 0) {
      fprintf(stderr, "Error: Receiver device not found!\n");
      wb_robot_cleanup();
      return 1;
    }
    wb_receiver_enable(rx, TIME_STEP); // 使用定义的TIME_STEP保持统一
    
  • 发射器端配套检查
    确保发射器机器人已添加Emitter设备,且代码正确发送数据,示例如下:

    #include <webots/robot.h>
    #include <webots/emitter.h>
    #include <string.h>
    
    #define TIME_STEP 64
    
    int main(int argc, char **argv) {
      wb_robot_init();
      WbDeviceTag tx = wb_robot_get_device("emitter");
      wb_emitter_enable(tx, TIME_STEP);
      
      while (wb_robot_step(TIME_STEP) != -1) {
        const char *msg = "Hello from emitter";
        wb_emitter_send(tx, msg, strlen(msg) + 1);
      }
      
      wb_robot_cleanup();
      return 0;
    }
    

    注意发射器和接收器的通信频道需保持一致(默认频道为0,若修改需两边同步)。


内容的提问来源于stack exchange,提问作者gadjet's garage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 07:40:40