| Prev: Using robots | Up: Using robots | Next: The iCub robot |
|---|---|---|
Table of contents
The following function shows how you can inizialize the position and the orientation of a single wheeled robot in the environment, and how you can access the robot's variables. Here we assume that the user want to initialize the position of the robot within the 40x40cm central portion of the arena. RobotOnPlane is the base class for wheeled robots that live in the Arena. It has some useful functions that allow to use bi-dimensional coordinates and orientation in place of the tri-dimensional ones. The example assumes that MyExperiment is a subclass of EvoRobotExperiment.
:::C++
/*
* This example code illustrates how you can initialize and position and the orientation of a wheeled robot
* in the case of experiment involving only a single robot (i.e. nagents = 1)
*/
void MyExperiment::initTrial(int /*trial*/)
{
// lock the resource so to enable the function to access the arena and world component
farsa::ResourcesLocker locker(this);
// get the arena component
farsa::Arena* arena = getResource<farsa::Arena>("arena");
// get the robot component
farsa::RobotOnPlane* robot = getResource<farsa::RobotOnPlane>("agent[0]:robot");
// set the position of the robot by selecting a random location in the xy range [-20,20]cm
robot->setPosition(arena->getPlane(), farsa::globalRNG->getDouble(-0.2f, 0.2f), farsa::globalRNG->getDouble(-0.2f, 0.2f));
// set the orientation of the robot randomly within a 360 degrees interval
robot->setOrientation(arena->getPlane(), farsa::globalRNG->getDouble(-PI_GRECO, PI_GRECO));
// display the robot position, orientation, radius, and color on the logger windows
Logger::info(QString("Robot:%1, pos:%2, orien:%3, radius:%4, color:%5 ").arg(0).arg(robot->position()).arg(robot->orientation(arena->getPlane())).arg(robot->robotRadius()).arg(robot->robotColor().name()));
}
In the case of collective experiments in which the environment containt multiple robots, the experimental plugin might include a resourceChanged() function like the one shown below that creates and updates a list of robots so that they can be accessed directly (using the m_robot vector, in this case).
The initTrial() function below shows how the position and the orientation of the robots can be initialized for all defined robots. Notice that this example we do not check whether the chosen positions of the robots' overlap.
:::C++
/*
* We use this private function to build a list of the robots in the experiment.
* The list can be retrived with the m_robot pointer
*/
void MyExperiment::resourceChanged(QString resourceName, ResourceChangeType changeType)
{
// Calling parent function
farsa::EvoRobotExperiment::resourceChanged(resourceName, changeType);
// Here we are only interested in robots, so we build a regular expression to only check when
// a robot changes
QRegExp checkRobots("agent\\[(\\d)\\]:robot");
if (checkRobots.indexIn(resourceName) != -1) {
// Getting the index
int index = checkRobots.cap(1).toUInt();
if (changeType == Deleted) {
// Removing robot
m_robots[index] = NULL;
} else {
// Adding the robot to our list
farsa::RobotOnPlane* const robot = getResource<farsa::RobotOnPlane>();
if (m_robots.size() == index) {
m_robots.append(robot);
} else {
m_robots[index] = robot;
}
}
}
}
/*
* This example, instead, how you can initialize the position of all robots
* and how you can access the variables describing the characteristics of the robots
*/
void MyExperiment::initTrial(int /*trial*/)
{
//lock the resource so to enable the function to access the arena and world component
farsa::ResourcesLocker locker(this);
// get the arena component
farsa::Arena* arena = getResource<farsa::Arena>("arena");
// randomly set the position and the orientation of the robots
// and display the robot's state on the logger window
for (int i = 0; i < m_robots.size(); i++) {
m_robots[i]->setPosition(arena->getPlane(), farsa::globalRNG->getDouble(-0.2f, 0.2f), farsa::globalRNG->getDouble(-0.2f, 0.2f));
m_robots[i]->setOrientation(arena->getPlane(), farsa::globalRNG->getDouble(-PI_GRECO, PI_GRECO));
Logger::info(QString("Robot:%1, posx:%2, posy:%3, posz:%4, orien:%5, radius:%6, color:%7 ").arg(i).arg(m_robots[i]->position().x).arg(m_robots[i]->position().y).arg(m_robots[i]->position().z).arg(m_robots[i]->orientation(arena->getPlane())).arg(m_robots[i]->robotRadius()).arg(m_robots[i]->robotColor().name()));
}
}
Manual: CreatingNewExperiment
Manual: CustomizingRobots
Manual: Home
Manual: iCubRobot
Anonymous