Communicating using receiver node in webots

172 Views Asked by At

I want to implement an emitter robot and a receiver robot in webots. I have written following code.

#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;
}

It compiles successfully. But when I execute it, it generate following result,

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

How can I fix this error?

I want to receive the message emitted by emitter

1

There are 1 best solutions below

0
On BEST ANSWER

This error shows up because there is no device named "receiver" in your Robot node. What kind of robot are using? If it is a Robot node defined in the world file (.wbt), you should add to the children list of your Robot node a Receiver node named "receiver" (which is the default name). If you are using a PROTO-based robot, like E-Puck or Nao, you should check whether the proto file already contains a Receiver node and use its name instead of "receiver". If it doesn't contain any Receiver node, you should be able to add a Receiver node in some extensionSlot of the robot.

The very same principle applies to the Emitter node as well.