標籤:style blog ar io 使用 sp strong on 檔案
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 64int 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 valuesdouble ir0_value = wb_distance_sensor_get_value(ir0);double ir1_value = wb_distance_sensor_get_value(ir1);//Computer the motor speedsdouble 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;}
代碼根據注釋很容易理解,但是以後擴充需要用到更多的函數就要查看reference manual了,大家一起學習。那麼我把如何使用controller操控robot的注意事項說一下
1)world建立完成以後,我們就要建立controller,這裡我們要記得我們當初在differentialWheels節點的controller域填的名字嗎,那麼我們就要嚴格按照這個名字在Wizard->new Robot Controller填上完全一致的名字,否則在連結controller源檔案時會報錯。
2)我們也可以使用VC6.0來進行源檔案的編寫,這樣更方便快捷,這裡要注意的要在VC6.0中添加代碼根據注釋很容易理解,但是以後擴充需要用到更多的函數就要查看reference manual了,大家一起學習。那麼我把如何使用controller操控robot的注意事項說一下world建立完成以後,我們就要建立controller,這裡我們要記得我們當初在differentialWheels節點的controller域填的名字嗎,那麼我們就要嚴格按照這個名字在Wizard->new Robot Controller填上完全一致的名字,否則在連結controller源檔案時會報錯。我們也可以使用VC6.0來進行源檔案的編寫,這樣更方便快捷。
(1) 選擇Project功能表列中的Setting功能表項目,在彈出的Project Settings對話方塊中選擇C\C++屬性頁面後,然後在該屬性頁面中的Category下拉框中選擇Preprocessor在其Additional Include Directories文字框中手動輸入{$WEBOTSHOME}\include
(2) 然後再到Link屬性頁面中,在該屬性頁面中的Category下拉框中選擇General選項,將Output Files Name文字框中的Release\simulation.exe,改為simulation.exe,然後在其Object/library modules文字框中手動添加controller.lib
(3) 最後在Category下拉框中選擇Input選項,在其Additional library path文字框中輸入{$WEBOTSHOME}\lib。
3) 最後運行時要注意將WorldInfo的runRealTime 改為true 否則,機器人會跟打了雞血一樣。
這個完整的例子講的差不多了,後面就要進入深入的研究了,大家一起學習~
Webots入門(二)-build up a controller