|
3.35 RobotDerived from Solid.
Direct derived nodes: DifferentialWheels, Supervisor 3.35.1 DescriptionRobot is the root node for building a virtual robot. This node can be derived into either a Supervisor node or either a DifferentialWheels node. 3.35.2 Field Summary
3.35.3 Robot FunctionsNAMEwb_robot_step, wb_robot_init, wb_robot_cleanup - basic functions used for programming a robot controllerC SYNOPSIS#include <webots/robot.h>int wb_robot_step(int ms); void wb_robot_init(); void wb_robot_cleanup(); C++ SYNOPSIS#include <webots/Robot.hpp>int Robot::step(int ms); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;int Robot.step(int ms); PYTHON SYNOPSISfrom controller import Robotint Robot.step(int ms) DESCRIPTIONThe wb_robot_step() function requests that the simulator perform a simulation step of ms milliseconds; that is, to advance the simulation by this amount of time. In synchronous simulation mode, the request is always fulfilled and the function always returns 0. In asynchronous mode, the request may not be fulfilled. In this case, the return value (which we'll call dt), representing the delay, may not be 0. Let controller_date be the current time of the controller. The return value may be interpreted as follows:
For the C API, two supplementary functions are needed for running a controller. The wb_robot_init function must be called before any other controller API function call in order to initialize the robot controller library. While it is recommended to call the wb_robot_cleanup function after any other controller API function call. For the oriented-object API (Java, Python and C++), the void controller can be programmed by using only the wb_robot_step function.. MINIMAL C CONTROLLER EXAMPLE
MINIMAL C++ CONTROLLER EXAMPLE
MINIMAL JAVA CONTROLLER EXAMPLE
MINIMAL PYTHON CONTROLLER EXAMPLE
NAMEwb_robot_get_device - get a unique identifier to a deviceC SYNOPSIS#include <webots/robot.h>WbDeviceTag wb_robot_get_device(const char *name); DESCRIPTIONThis function returns a unique identifier to a device corresponding to a specified name. For example, if a robot contains a DistanceSensor node whose name field is "ds1", the function will return the unique identifier of that device. This WbDeviceTag identifier will be used subsequently for enabling, sending commands to, or reading data from this device. If the specified device is not found, the function returns 0. SEE ALSONAME- get the instance of a robot's deviceC++ SYNOPSIS#include <webots/Robot.hpp>Accelerometer *Robot::getAccelerometer(const std::string &name); Camera *Robot::getCamera(const std::string &name); Compass *Robot::getCompass(const std::string &name); Connector *Robot::getConnector(const std::string &name); DistanceSensor *Robot::getDistanceSensor(const std::string &name); Emitter *Robot::getEmitter(const std::string &name); GPS *Robot::getGPS(const std::string &name); Gripper *Robot::getGripper(const std::string &name); Gyro *Robot::getGyro(const std::string &name); LightSensor *Robot::getLightSensor(const std::string &name); Pen *Robot::getPen(const std::string &name); Receiver *Robot::getReceiver(const std::string &name); Servo *Robot::getServo(const std::string &name); TouchSensor *Robot::getTouchSensor(const std::string &name); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;Accelerometer Robot.getAccelerometer(String name); Camera Robot.getCamera(String name); Compass Robot.getCompass(String name); Connector Robot.getConnector(String name); DistanceSensor Robot.getDistanceSensor(String name); Emitter Robot.getEmitter(String name); GPS Robot.getGPS(String name); Gripper Robot.getGripper(String name); Gyro Robot.getGyro(String name); LightSensor Robot.getLightSensor(String name); Pen Robot.getPen(String name); Receiver Robot.getReceiver(String name); Servo Robot.getServo(String name); TouchSensor Robot.getTouchSensor(String name); PYTHON SYNOPSISfrom controller import RobotAccelerometer Robot.getAccelerometer(string name) Camera Robot.getCamera(string name) Compass Robot.getCompass(string name) Connector Robot.getConnector(string name) DistanceSensor Robot.getDistanceSensor(string name) Emitter Robot.getEmitter(string name) GPS Robot.getGPS(string name) Gripper Robot.getGripper(string name) Gyro Robot.getGyro(string name) LightSensor Robot.getLightSensor(string name) Pen Robot.getPen(string name) Receiver Robot.getReceiver(string name) Servo Robot.getServo(string name) TouchSensor Robot.getTouchSensor(string name) DESCRIPTIONThese functions return a reference to an object corresponding to a specified name. Depending on the called function, this object can be an instance of a Device subclasse. For example, if a robot contains a DistanceSensor node whose name field is "ds1", the function getDistanceSensor will return a reference to a DistanceSensor object. If the specified device is not found, the function returns NULL in C++, null in Java or the none in Python. SEE ALSONAMEwb_robot_battery_sensor_enable, wb_robot_battery_sensor_disable, wb_robot_battery_sensor_get_value - battery sensor functionC SYNOPSIS#include <webots/robot.h>void wb_robot_battery_sensor_enable(int ms); void wb_robot_battery_sensor_disable(); double wb_robot_battery_sensor_get_value(); C++ SYNOPSIS#include <webots/Robot.hpp>void Robot::batterySensorEnable(int ms); void Robot:batterySensorDisable(); double Robot::batterySensorGetValue(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;void Robot.batterySensorEnable(int ms); void Robot.batterySensorDisable(); double Robot.batterySensorGetValue(); PYTHON SYNOPSISfrom controller import Robotnone Robot.batterySensorEnable(int ms) none Robot.batterySensorDisable() float Robot.batterySensorGetValue() DESCRIPTIONThese functions allow you to measure the present energy level of the robot battery. First, it is necessary to enable battery sensor measurements by calling the wb_robot_battery_sensor_enable() function. The ms parameter is expressed in milliseconds and defines how frequently measurements are performed. After the battery sensor is enabled a value can be read from it by calling the wb_robot_battery_sensor_get_value() function. The returned value corresponds to the present energy level of the battery expressed in Joules (J). The wb_robot_battery_sensor_disable() function should be used to stop battery sensor measurements. NAMEwb_robot_get_basic_time_step - returns the value of the basicTimeStep field of the WorldInfo nodeC SYNOPSIS#include <webots/robot.h>double wb_robot_get_basic_time_step(); C++ SYNOPSIS#include <webots/Robot.hpp>double Robot::getBasicTimeStep(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;double Robot.getBasicTimeStep(); PYTHON SYNOPSISfrom controller import Robotfloat Robot.getBasicTimeStep() DESCRIPTIONThis function returns the value of the basicTimeStep field of the WorldInfo node. NAMEwb_robot_get_mode - get operating mode, simulation vs. real robotC SYNOPSIS#include <webots/robot.h>int wb_robot_get_mode(); C++ SYNOPSIS#include <webots/Robot.hpp>int Robot::getMode(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;int Robot.getMode(); PYTHON SYNOPSISfrom controller import Robotint Robot.getMode() DESCRIPTIONThis function returns an integer value indicating the current operating mode for the controller:
NAMEwb_robot_get_name - return the name defined in the robot nodeC SYNOPSIS#include <webots/robot.h>const char *wb_robot_get_name(); C++ SYNOPSIS#include <webots/Robot.hpp>const std::string Robot::getName(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;String Robot.getName(); PYTHON SYNOPSISfrom controller import Robotstring Robot.getName() DESCRIPTIONThis function returns the name as it is defined in the name field of the robot node (Robot, DifferentialWheels, Supervisor, etc.) in the current world file. The string returned should not be deallocated, as it was allocated by the libController shared library and will be deallocated when the controller terminates. This function is very useful to pass some arbitrary parameter from a world file to a controller program. For example, you can have the same controller code behave differently depending on the name of the robot. This is illustrated in the soccer.wbt sample demo, where the goal keeper robot runs the same control code as the other soccer players, but its behavior is different because its name was tested to determine its behavior (in this sample world, names are "b3" for the blue goal keeper and "y3" for the yellow goal keeper, whereas the other players are named "b1", "b2", "y1" and "y2"). This sample world is located in the projects/samples/demos/worlds directory of Webots. NAMEwb_robot_get_project_path - returns the full path of the current projectC SYNOPSIS#include <webots/robot.h>const char *wb_robot_get_project_path(); C++ SYNOPSIS#include <webots/Robot.hpp>const std::string Robot::getProjectPath(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;String Robot.getProjectPath(); PYTHON SYNOPSISfrom controller import Robotstring Robot.getProjectPath() DESCRIPTIONThis function returns full path of the current project, that is the directory which contains the worlds and controllers subdirectories (among others) of the current simulation world. It doesn't include the final directory separator char (slash or anti-slash). The returned pointer is a UTF-8 encoded char string. It should not be deallocated. NAMEwb_robot_get_synchronization - returns the value of the synchronization field of the Robot nodeC SYNOPSIS#include <webots/robot.h>bool wb_robot_get_synchronization(); C++ SYNOPSIS#include <webots/Robot.hpp>bool Robot::getSynchronization(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;boolean Robot.getSynchronization(); PYTHON SYNOPSISfrom controller import Robotbool Robot.getSynchronization() DESCRIPTIONThis function returns the boolean value corresponding to the synchronization field of the Robot node. NAMEwb_robot_get_time - return the current simulation time in secondsC SYNOPSIS#include <webots/robot.h>double wb_robot_get_time(); C++ SYNOPSIS#include <webots/Robot.hpp>double Robot::getTime(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;double Robot.getTime(); PYTHON SYNOPSISfrom controller import Robotfloat Robot.getTime() DESCRIPTIONThis function returns the current simulation time as a double precision floating point value. This time should correspond to the one displayed in the bottom right corner of the main Webots window. It is expressed in seconds. NAMEwb_robot_keyboard_enable, wb_robot_keyboard_disable, wb_robot_keyboard_get_key - keyboard reading functionC SYNOPSIS#include <webots/robot.h>void wb_robot_keyboard_enable(int ms); void wb_robot_keyboard_disable(); int wb_robot_keyboard_get_key(); C++ SYNOPSIS#include <webots/Robot.hpp>void Robot::keyboardEnable(int ms); void Robot::keyboardDisable(); int Robot::keyboardGetKey(); JAVA SYNOPSISimport com.cyberbotics.webots.controller.Robot;void Robot.keyboardEnable(int ms); void Robot.keyboardDisable(); int Robot.keyboardGetKey(); PYTHON SYNOPSISfrom controller import Robotnone Robot.keyboardEnable(int ms) none Robot.keyboardDisable() int Robot.keyboardGetKey() DESCRIPTIONThese functions allow you to read a key pressed on the computer keyboard from a controller program while the main window of Webots is selected and the simulation is running. First, it is necessary to enable keyboard input by calling the wb_robot_keyboard_enable() function. The ms parameter is expressed in milliseconds, and defines how frequently readings are updated. After the enable function is called, values can be read by calling the wb_robot_keyboard_get_key() function repeatedly until this function returns 0. The returned value, if non-null, is a key code corresponding to a key currently pressed. If no modifier (shift, control or alt) key is pressed, the key code is the ASCII code of the corresponding key or a special value (e.g. for the arrow keys). However, if a modifier key was pressed, the ASCII code (or special value) can be obtained by applying a logical AND with the WB_ROBOT_KEYBOARD_KEY mask to the key code. WB_ROBOT_KEYBOARD_SHIFT, WB_ROBOT_KEYBOARD_CONTROL and WB_ROBOT_KEYBOARD_ALT are OR-ed with the key code if the corresponding modifier key is pressed simultaneously. If no key is currently pressed, the function will return 0. Calling the wb_robot_keyboard_get_key() function a second time will return either 0 or the key code of another key which is currently simultaneously pressed. The function can be called up to 7 times to detect up to 7 simultaneous keys pressed. The wb_robot_keyboard_disable() function should be used to stop the keyboard readings. Note: In C++, the keyboard predefined values are located into a (static) enumeration of the Robot class. For example, Robot.KEYBOARD_CONTROL corespond to the Control key stroke. Note: In Java, the keyboard predefined values are located into a subclass (called KeyboardKey) of the Robot class. For example, Ctrl+B can be tested like this:
Note: In Python, the keyboard predefined values are located into a enumeration of the Robot class. For example, Ctrl+B can be tested like this:
NAMEwb_robot_task_new - start a new thread of executionC SYNOPSIS#include <webots/robot.h>void wb_robot_task_new(void (*task)(void *), void *param); DESCRIPTIONThis function creates and starts a new thread of execution for the robot controller. The task function is immediately called using the param parameter. It will end only when the task function returns. The Webots controller API is thread safe, however, some API functions use or return pointers to data structures which are not protected outside the function against asynchronous access from a different thread. Hence you should use mutexes (see below) to ensure that such data is not accessed by a different thread. SEE ALSONAMEwb_robot_mutex_new, wb_robot_mutex_delete, wb_robot_mutex_lock, wb_robot_mutex_unlock - mutex functionsC SYNOPSIS#include <webots/robot.h>WbMutexRef wb_robot_mutex_new(); void wb_robot_mutex_delete(WbMutexRef mutex); void wb_robot_mutex_lock(WbMutexRef mutex); void wb_robot_mutex_unlock(WBMutexRef mutex); DESCRIPTIONThe wb_robot_mutex_new() function creates a new mutex and returns a reference to that mutex to be used with other mutex functions. A newly created mutex is always initially unlocked. Mutexes (mutual excluders) are useful with multi-threaded controllers to protect some resources (typically variables or memory chunks) from being used simultaneously by different threads. The wb_robot_mutex_delete() function deletes the specified mutex. This function should be used when a mutex is no longer in use. The wb_robot_mutex_lock() function attempts to lock the specified mutex. If the mutex is already locked by another thread, this function waits until the other thread unlocks the mutex, and then it locks it. This function returns only after it has locked the specified mutex. The wb_robot_mutex_unlock() function unlocks the specified mutex, allowing other threads to lock it. SEE ALSOUsers unfamiliar with the mutex concept may wish to consult a reference on multi-threaded programming techniques for further information. ![]() ^ page top ^ |
| E-mail to webmaster | Last updated: | Copyright © 2008 Cyberbotics Ltd. |