A Simple Controller
The controller program reads the value of the sensor and then modifies the walking speed to avoid obstructions.
The following is the controller source code 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 ();//Initialize webots//get and enable distance sensor 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);d ouble ir1_value = Wb_distance_sensor_get_value (IR1);//computer the motor speedsdouble left_speed, Right_speed;if (Ir1_value > 500) {/** If two sensors detect something, it means facing a wall. We need to go backwards in this situation. */if (Ir0_value >) {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;} Set move Speed Wb_differential_wheels_set_speed (Left_speed, right_spEed);} return 0;}
The code is easy to understand according to the comments, but later the extension needs to use more functions to see reference Manual, we will learn together. So I'll take a look at how to control robot with a controller.
1) After the world is established, we will set up a controller, here we have to remember the Differentialwheels node in the Controller field to fill in the name, then we must strictly follow this name in the Wizard->new The Robot controller fills in the exact same name, otherwise it will error when linking the controller source file.
2) We can also use VC6.0 to write the source file, which is more convenient and quick, it is important to note that in the VC6.0 to add code according to the comments are easy to understand, but later expansion needs to use more functions to see Reference Manual, we learn together. So I'm going to take note of how to manipulate robot with a controller. After the world is set up, we have to build a controller, Here we have to remember the name we had in the controller field of the Differentialwheels node, then we have to strictly follow the name in the Wizard->new Robot controller to fill in the exact same name, Otherwise, the controller source file will be linked with an error. We can also use VC6.0 to write source files, which is more convenient and quick.
(1) Select the Setting menu item in the Project menu bar, select the C\c++ property page in the pop-Up Project Settings dialog box, and then select preprocessor in its additional in the Category drop-down box in the property page Include Directories text box manually enter {$WEBOTSHOME}\include
(2) then to the Link property page, select the General option in the Category drop-down box in the property page, and the output Files name text box in the release\ Simulation.exe, change to Simulation.exe, and then manually add Controller.lib in its Object/library modules text box
(3) Finally, in the Category drop-down box, select the input option and enter {$WEBOTSHOME}\lib in its additional Library path text box .
3) in the last run, be careful to change the worldinfo runrealtime to True otherwise, the robot will beat the chicken blood.
This complete example is almost, the back is going into the deep research, we learn together ~
Webots Getting Started (ii)-build up a controller