Webots入门(二)-build up a controller

A simple controller

控制器程序读取传感器的值,然后改动行走速度来避开障碍物。

以下是控制器源码mybot_simple.c:

#include<webots/robot.h>
#include<webots/differential_wheels.h>
#include<webots/distance_sensor.h>
#define SPEED 60
#define TIME_STEP 64

int main()
{
	wb_robot_init();//初始化webots
	//获取并使能距离传感器
	WbDeviceTag ir0 = wb_robot_get_device("ir0");
	WbDeviceTag ir1 = wb_robot_get_device("ir1");
	wb_distance_sensor_enable(ir0,TIME_STEP);
	wb_distance_sensor_enable(ir1,TIME_STEP);
	
	while(wb_robot_step(TIME_STEP)!=-1){
		
		//GET distance sensor values
		double ir0_value = wb_distance_sensor_get_value(ir0);
		double ir1_value = wb_distance_sensor_get_value(ir1);
		
		//Computer the motor speeds
		double left_speed, right_speed;
		if (ir1_value > 500) {
		/*
		*假设两个传感器都检測到了某物,这意味着面对着一堵墙。这样的状况我们须要后退。
			*/
			if(ir0_value > 500){
				left_speed = -SPEED;
				right_speed = -SPEED/2;
			}
			else{
				left_speed = -ir1_value /10;
				right_speed = (ir0_value / 10) + 5;
			}
		}
		else if(ir0_value>500){
			left_speed = (ir1_value / 10) + 5;
			right_speed = -ir0_value /10;
		}
		else{
			left_speed = SPEED;
			right_speed = SPEED;
		}	
	
	//设置移动速度
	wb_differential_wheels_set_speed(left_speed, right_speed);
}
return 0;


}


         代码依据凝视非常easy理解,可是以后扩展须要用到很多其它的函数就要查看reference manual了,大家一起学习。那么我把怎样使用controller操控robot的注意事项说一下

1)world建立完毕以后,我们就要建立controller,这里我们要记得我们当初在differentialWheels节点的controller域填的名字吗,那么我们就要严格依照这个名字在Wizard->new Robot Controller填上全然一致的名字,否则在链接controller源文件时会报错。

2)我们也能够使用VC6.0来进行源文件的编写,这样更方便快捷,这里要注意的要在VC6.0中加入代码依据凝视非常easy理解,可是以后扩展须要用到很多其它的函数就要查看reference manual了,大家一起学习。那么我把怎样使用controller操控robot的注意事项说一下world建立完毕以后,我们就要建立controller,这里我们要记得我们当初在differentialWheels节点的controller域填的名字吗,那么我们就要严格依照这个名字在Wizard->new Robot Controller填上全然一致的名字,否则在链接controller源文件时会报错。我们也能够使用VC6.0来进行源文件的编写,这样更方便快捷。

    (1) 选择Project菜单条中的Setting菜单项,在弹出的Project Settings对话框中选择CC++属性页后,然后在该属性页中的Category下拉框中选择Preprocessor在其Additional Include Directories文本框中手动输入{$WEBOTSHOME}include

    (2) 然后再到Link属性页中,在该属性页中的Category下拉框中选择General选项,将Output Files Name文本框中的Releasesimulation.exe,改为simulation.exe,然后在其Object/library modules文本框中手动加入controller.lib

    (3) 最后在Category下拉框中选择Input选项,在其Additional library path文本框中输入{$WEBOTSHOME}lib。

3)  最后执行时要注意将WorldInfo的runRealTime 改为true 否则,机器人会跟打了鸡血一样。


这个完整的样例讲的差点儿相同了,后面就要进入深入的研究了,大家一起学习~



















原文地址:https://www.cnblogs.com/zfyouxi/p/4351385.html