webots 笔记
第一天 Controller Programming
开始
#include <webots/xyz.h>
头文件表示Webots的一个节点。
-
代码也可以包含C语言的标准库,比如
#include <stdio.h>
。 -
wb_robot_init()
是C语言API 的初始化函数。就是用C语言来编写控制器的话用这个函数来进行初始化- 该函数初始化控制器与Webots之间的通信,并且仅在C语言里存在。
-
最高层的控制代码一般写在 for 或者 while 循环里。在此循环里调用
wb_robot_step()
函数。- 该函数需要在所有控制器中出现,它让程序有规则的时隔。
- 它会运算32ms的仿真后反馈。这里的时间并不是实际的时间,它会是毫秒或者一分钟对应的CPU时间。
-
实例
/* * File: hello_world.c * Date: * Description: * Author: * Modifications: */ /* * You may need to add include files like <webots/distance_sensor.h> or * <webots/motor.h>, etc. */ #include <webots/robot.h> #include <stdio.h> /* * You may want to add macros here. */ #define TIME_STEP 64 /* * This is the main program. * The arguments of the main function can be specified by the * "controllerArgs" field of the Robot node */ int main(int argc, char **argv) { /* necessary to initialize webots stuff */ wb_robot_init(); /* * You should declare here WbDeviceTag variables for storing * robot devices like this: * WbDeviceTag my_sensor = wb_robot_get_device("my_sensor"); * WbDeviceTag my_actuator = wb_robot_get_device("my_actuator"); */ /* main loop * Perform simulation steps of TIME_STEP milliseconds * and leave the loop when the simulation is over */ while(1){ printf("Hello World!\n"); wb_robot_step(32); } //while (wb_robot_step(TIME_STEP) != -1) { /* * Read the sensors : * Enter here functions to read sensor data, like: * double val = wb_distance_sensor_get_value(my_sensor); */ /* Process sensor data here */ /* * Enter here functions to send actuator commands, like: * wb_motor_set_position(my_actuator, 10.0); */ //}; /* Enter your cleanup code here */ /* This is necessary to cleanup webots resources */ wb_robot_cleanup(); return 0; }
传感器读取
-
先使用设备(device)之前, 必须要给定相应的设备(device)标签。
Wb_Device_Tag
- 该标签调用
wb_robot_get_device(”string“)
。 - 该标签是隐含类型,它在控制代码里用于识别设备或者装置。
string
代表设备名称。- 如果该机器人没有此设备就返回
0
。
- 该标签调用
-
每个传感器必须使用之前被打开,相应的函数为
wb_*_enable()
。这里的星号表示传感器的类型。- 每个传感器的数据都以毫秒为单位更新。
- 一般情况下更新数据的延迟是跟控制步伐(TIME_STEP)相似,因为它是每个
wb_robot_step()
运行后被更新。 - 这个时隔也可以选择为两个step,越大的更新延迟可以提高仿真速度,比如相机。
- 关闭函数为
wb_*_disable()
。
-
传感器值是调用
wb_robot_step()
函数的时候更新的。比如用wb_distance_sensor_get_value()
找回最新值。 -
一些传感器的标签如下:
1 const double *wb_gps_get_values(WbDeviceTag tag);//GPS定位 2 const double *wb_accelerometer_get_values(WbDeviceTag tag);//加速度传感器 3 const double *wb_gyro_get_values(WbDeviceTag tag);//陀螺仪
-
GPS传感器的使用实例
const double *pos = wb_gps_get_values(gps); //OK, to read the values they should never be explicilty deleted by the controller code. printf("MY_ROBOT_is_at_position:%g %g %g\n", pos[0],pos[1],pos[2]); //OK, to copy the values double x,y,z; x = pos[0]; y = pos[1]; z = pos[2]; //OK, another wa to copy the values double a[3] = {pos[0],pos[1],pos[2]}; //OK, yet another wa to copy these values double b[3]; memcpy(b,pos,sizeof(b));
-
一些不对的用法
const double *pos = wb_gps_get_values(gps); pos[0] = 3.5; //ERROR: assigment of read-only location double a = pos[3]; //ERROR: index out of range delete [] pos; //ERROR: illegal free free(pos); //ERROR: illegal free
-
-
实例
/* * File: distance_sensor.c * Date: * Description: * Author: * Modifications: */ /* * You may need to add include files like <webots/distance_sensor.h> or * <webots/motor.h>, etc. */ #include <webots/robot.h> #include <webots/distance_sensor.h> #include <stdio.h> /* * You may want to add macros here. */ #define TIME_STEP 32 /* * This is the main program. * The arguments of the main function can be specified by the * "controllerArgs" field of the Robot node */ int main(int argc, char **argv) { /* necessary to initialize webots stuff */ wb_robot_init(); /* * You should declare here WbDeviceTag variables for storing * robot devices like this: * WbDeviceTag my_sensor = wb_robot_get_device("my_sensor"); * WbDeviceTag my_actuator = wb_robot_get_device("my_actuator"); */ WbDeviceTag ds = wb_robot_get_device("my_distance_sensor"); wb_distance_sensor_enable(ds, TIME_STEP); while(1){ wb_robot_step(TIME_STEP); double dist = wb_distance_sensor_get_value(ds); printf("sensor value is %f\n", dist); } /* main loop * Perform simulation steps of TIME_STEP milliseconds * and leave the loop when the simulation is over */ //while (wb_robot_step(TIME_STEP) != -1) { /* * Read the sensors : * Enter here functions to read sensor data, like: * double val = wb_distance_sensor_get_value(my_sensor); */ /* Process sensor data here */ /* * Enter here functions to send actuator commands, like: * wb_motor_set_position(my_actuator, 10.0); */ //}; /* Enter your cleanup code here */ /* This is necessary to cleanup webots resources */ wb_robot_cleanup(); return 0; }