webots 笔记
第一天 Controller Programming
开始
#include <webots/xyz.h>
头文件表示Webots的一个节点。
-
/* * 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; }
传感器读取
-
传感器值是调用
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 %gn", 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));
-
/* * 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 %fn", 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; }
原文地址:https://blog.csdn.net/Dream_choe/article/details/122829989
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_33324.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。