Robots suiveurs 3 - Code Communication Initial (C++)
(Redirigé depuis Robots suiveurs 3 - Code Communication C++)
Sauter à la navigation
Sauter à la recherche
Code pour la communication du robot esclave
// File:receiver_controller.cpp
// Description: partie récepteur de communication
// Author:Clément Luton
// You may need to add webots include files such as
// <webots/DistanceSensor.hpp>, <webots/Motor.hpp>, etc.
// and/or to add some other includes
#include <webots/Robot.hpp>
#include <webots/Robot.hpp>
#include <webots/Emitter.hpp>
#include <webots/Receiver.hpp>
#include <stdio.h>
#include <string.h>
#define COMMUNICATION_CHANNEL 1
// All the webots classes are defined in the "webots" namespace
using namespace webots;
int main(int argc, char **argv) {
// create the Robot instance.
Robot *robot = new Robot();
Receiver *receiver = robot->getReceiver("receiver");
int message_printed = 0;
// get the time step of the current world.
int timeStep = (int)robot->getBasicTimeStep();
//if wrong channel
const int channel = receiver->getChannel();
if (channel != COMMUNICATION_CHANNEL)
{
receiver->setChannel(COMMUNICATION_CHANNEL);
}
receiver->enable(timeStep);
// You should insert a getDevice-like function in order to get the
// instance of a device of the robot. Something like:
// Motor *motor = robot->getMotor("motorname");
// DistanceSensor *ds = robot->getDistanceSensor("dsname");
// ds->enable(timeStep);
// Main loop:
// - perform simulation steps until Webots is stopping the controller
while (robot->step(timeStep) != -1)
{
if (receiver->getQueueLength() > 0)
{
/* read current packet's data */
const char *buffer = (const char*)receiver->getData();
const double *position= receiver->getEmitterDirection();
/* print null-terminated message */
printf("Communicating: received \"%s\"\n", buffer);
// test pour voir comment son organiser les données de position
printf("Position received:x=\"%lf\"y=\"%lf\"z=\"%lf\"\n", position[0],position[1],position[2]);
message_printed = 1;
/* fetch next packet */
receiver->nextPacket();
}
else
{
if (message_printed != 2)
{
printf("Communication broken!\n");
message_printed = 2;
}
}
};
// Enter here exit cleanup code.
delete robot;
return 0;
}
Code pour la communication du robot maître
// File:emitter_controller.cpp
// Description: Partie émetteur
// Author: Clément Luton
// You may need to add webots include files such as
// <webots/DistanceSensor.hpp>, <webots/Motor.hpp>, etc.
// and/or to add some other includes
#include <webots/Robot.hpp>
#include <webots/Emitter.hpp>
#include <webots/Receiver.hpp>
#include <stdio.h>
#include <string.h>
// All the webots classes are defined in the "webots" namespace
using namespace webots;
#define COMMUNICATION_CHANNEL 1
int main(int argc, char **argv) {
// create the Robot instance.
Robot *robot = new Robot();
// get the time step of the current world.
int timeStep = (int)robot->getBasicTimeStep();
Emitter *emitter = robot->getEmitter("emitter");
//if wrong channel
const int channel = emitter->getChannel();
if (channel != COMMUNICATION_CHANNEL)
{
emitter->setChannel(COMMUNICATION_CHANNEL);
}
emitter->setRange(0.4);
// Main loop:
// - perform simulation steps until Webots is stopping the controller
while (robot->step(timeStep) != -1)
{
/* send null-terminated message */
const char *message = "Hello!";
emitter->send(message, strlen(message) + 1);
};
// Enter here exit cleanup code.
delete robot;
return 0;
}