jotto

Untitled

Jan 5th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <webots/robot.h>
  2. #include <webots/distance_sensor.h>
  3. #include <webots/camera.h>
  4. #include <webots/motor.h>
  5. #include <stdio.h>
  6.  
  7. // time in [ms] of a simulation step
  8. #define TIME_STEP 64
  9.  
  10. // entree point of the controller
  11. int main(int argc, char **argv)
  12. {
  13.     int  r,g,b, countt;
  14.     const unsigned char *image;
  15.     // initialise the Webots API
  16.     wb_robot_init();
  17.     int drive_counter = 0;
  18.  
  19.     // internal variables
  20.     int i;
  21.     // initialise distance sensors
  22.     WbDeviceTag ds[8];
  23.     char ds_names[8][16] = {"ds_left_front", "ds_right_front", "ds_left_corner", "ds_right_corner", "ds_left_rear", "ds_right_rear", "ds_left_back", "ds_right_back" };
  24.     for (i=0; i<8 ; i++) {
  25.         ds[i] = wb_robot_get_device(ds_names[i]);
  26.         wb_distance_sensor_enable(ds[i], TIME_STEP);
  27.     }
  28.  
  29.     // initialise motors
  30.     WbDeviceTag wheels[4];
  31.     char wheels_names[4][8] = {
  32.         "wheel1", "wheel2", "wheel3", "wheel4"
  33.     };
  34.     for (i=0; i<4 ; i++) {
  35.         wheels[i] = wb_robot_get_device(wheels_names[i]);
  36.         wb_motor_set_position(wheels[i], INFINITY);
  37.     }
  38.  
  39.     // initialise camera
  40.     WbDeviceTag camera;
  41.     camera = wb_robot_get_device("color_camera");
  42.     wb_camera_enable(camera, TIME_STEP);
  43.     int width = wb_camera_get_width(camera);
  44.     int height = wb_camera_get_height(camera);
  45.     int center = 0;
  46.     double left_speed  = 1.0;
  47.     double right_speed = 1.0;
  48.     // feedback loop
  49.     while (wb_robot_step(TIME_STEP) != -1) {
  50.         int i,j;
  51.         image = wb_camera_get_image(camera);
  52.         r = 0;
  53.         g = 0;
  54.         b = 0;
  55.         countt = 0 ;
  56.         center = 0;
  57.        
  58.         if (drive_counter == 0){
  59.             drive_counter = 10;
  60.             for (i = 3*width/7; i < 4*width/7; i++) {
  61.                 for (j = height/2; j < 3*height/4; j++) {
  62.                     r = wb_camera_image_get_red(image, width, i, j);
  63.                     b = wb_camera_image_get_blue(image, width, i, j);
  64.                     g = wb_camera_image_get_green(image, width, i, j);
  65.                    
  66.                     if (b>225 && r >225 && g>225) {
  67.                         countt++;
  68.                         center+=i;
  69.                     }
  70.                 }
  71.             }
  72.            
  73.             double ds_values[8];
  74.            
  75.             for (i=0; i<8 ; i++)
  76.             ds_values[i] = wb_distance_sensor_get_value(ds[i]);
  77.            
  78.             bool sheepAtRight = ds_values[3] < 5000.0 || ds_values[5] < 5000.0;
  79.  
  80.            
  81.             left_speed  = 1.0;
  82.             right_speed = 1.0;
  83.  
  84.             if(countt == 0){
  85.                 left_speed  =  2.0;
  86.                 right_speed = 0.2;
  87.             }  
  88.  
  89.             else
  90.             {
  91.                 center /= countt;
  92.                 center -= width/2;
  93.                 if (center > width/24) {
  94.                     left_speed  =  2.5;
  95.                     right_speed = 0.1;
  96.                 }
  97.                 else if (center < -width/24) {
  98.                     left_speed  = 0.1;
  99.                     right_speed = 2.5;
  100.                 }
  101.             }
  102.             if(sheepAtRight)
  103.             {
  104.                 left_speed = 2;
  105.                 right_speed = 2;
  106.             }
  107.         }
  108.         else
  109.         {
  110.             drive_counter--;
  111.         }
  112.        
  113.         // write actuators inputs
  114.         wb_motor_set_velocity(wheels[0], left_speed);
  115.         wb_motor_set_velocity(wheels[1], right_speed);
  116.         wb_motor_set_velocity(wheels[2], left_speed);
  117.         wb_motor_set_velocity(wheels[3], right_speed);
  118.     }
  119.  
  120.     // cleanup the Webots API
  121.     wb_robot_cleanup();
  122.     return 0; //EXIT_SUCCESS
  123. }
Advertisement
Add Comment
Please, Sign In to add comment