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
This error shows up because there is no device named
"receiver"
in yourRobot
node. What kind of robot are using? If it is aRobot
node defined in the world file (.wbt
), you should add to thechildren
list of yourRobot
node aReceiver
node named"receiver"
(which is the default name). If you are using a PROTO-based robot, likeE-Puck
orNao
, you should check whether the proto file already contains aReceiver
node and use itsname
instead of"receiver"
. If it doesn't contain anyReceiver
node, you should be able to add aReceiver
node in someextensionSlot
of the robot.The very same principle applies to the
Emitter
node as well.