camera_1.c
Go to the documentation of this file.
1 /*
2  * Copyright 1996-2019 Cyberbotics Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Description: An example of use of a camera device.
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <webots/camera.h>
25 #include <webots/motor.h>
26 #include <webots/robot.h>
27 #include <webots/utils/system.h>
28 
29 #define ANSI_COLOR_RED "\x1b[31m"
30 #define ANSI_COLOR_GREEN "\x1b[32m"
31 #define ANSI_COLOR_YELLOW "\x1b[33m"
32 #define ANSI_COLOR_BLUE "\x1b[34m"
33 #define ANSI_COLOR_MAGENTA "\x1b[35m"
34 #define ANSI_COLOR_CYAN "\x1b[36m"
35 #define ANSI_COLOR_RESET "\x1b[0m"
36 
37 #define SPEED 4
38 #define TIME_STEP 64
39 enum BLOB_TYPE { RED, GREEN, BLUE, NONE };
40 
41 int main() {
42  WbDeviceTag camera, left_motor, right_motor;
43  int width, height;
44  int pause_counter = 0;
45  int left_speed, right_speed;
46  int i, j;
47  int red, blue, green;
48  const char *color_names[3] = {"red", "green", "blue"};
49  const char *ansi_colors[3] = {ANSI_COLOR_RED, ANSI_COLOR_GREEN, ANSI_COLOR_BLUE};
50  const char *filenames[3] = {"red_blob.png", "green_blob.png", "blue_blob.png"};
51  enum BLOB_TYPE current_blob;
52 
53  wb_robot_init();
54 
55  /* Get the camera device, enable it, and store its width and height */
56  camera = wb_robot_get_device("camera");
57  wb_camera_enable(camera, TIME_STEP);
58  width = wb_camera_get_width(camera);
59  height = wb_camera_get_height(camera);
60 
61 
62 
63  /* Main loop */
64  while (wb_robot_step(TIME_STEP) != -1) {
65  /* Get the new camera values */
66  const unsigned char *image = wb_camera_get_image(camera);
67 
68  /* Decrement the pause_counter*/
69  if (pause_counter > 0)
70  pause_counter--;
71 
72  /*
73  * Case 1
74  * A blob was found recently
75  * The robot waits in front of it until pause_counter
76  * is decremented enough
77  */
78  if (pause_counter > 10) {
79  left_speed = 0;
80  right_speed = 0;
81  }
82  /*
83  * Case 2
84  * A blob was found quite recently
85  * The robot begins to turn but don't analyse the image for a while,
86  * otherwise the same blob would be found again
87  */
88  else if (pause_counter > 0) {
89  left_speed = -SPEED;
90  right_speed = SPEED;
91  }
92  /*
93  * Case 3
94  * The robot turns and analyse the camera image in order
95  * to find a new blob
96  */
97  else { // pause_counter == 0
98 
99  /* Reset the sums */
100  red = 0;
101  green = 0;
102  blue = 0;
103 
104  /*
105  * Here we analyse the image from the camera. The goal is to detect a
106  * blob (a spot of color) of a defined color in the middle of our
107  * screen.
108  * In order to achieve that we simply parse the image pixels of the
109  * center of the image, and sum the color components individually
110  */
111  for (i = width / 3; i < 2 * width / 3; i++) {
112  for (j = height / 2; j < 3 * height / 4; j++) {
113  red += wb_camera_image_get_red(image, width, i, j);
114  blue += wb_camera_image_get_blue(image, width, i, j);
115  green += wb_camera_image_get_green(image, width, i, j);
116  }
117  }
118 
119  /*
120  * If a component is much more represented than the other ones,
121  * a blob is detected
122  */
123  if ((red > 3 * green) && (red > 3 * blue))
124  current_blob = RED;
125  else if ((green > 3 * red) && (green > 3 * blue))
126  current_blob = GREEN;
127  else if ((blue > 3 * red) && (blue > 3 * green))
128  current_blob = BLUE;
129  else
130  current_blob = NONE;
131 
132  /*
133  * Case 3a
134  * No blob is detected
135  * the robot continues to turn
136  */
137  if (current_blob == NONE) {
138  left_speed = -SPEED;
139  right_speed = SPEED;
140  }
141  /*
142  * Case 3b
143  * A blob is detected
144  * the robot stops, stores the image, and changes its state
145  */
146  else {
147  left_speed = 0;
148  right_speed = 0;
149  printf("Looks like I found a %s%s%s blob.\n", ansi_colors[current_blob], color_names[current_blob], ANSI_COLOR_RESET);
150  // compute the file path in the user directory
151  char *filepath;
152 #ifdef _WIN32
153  const char *user_directory = wbu_system_short_path(wbu_system_getenv("USERPROFILE"));
154  filepath = (char *)malloc(strlen(user_directory) + 16);
155  strcpy(filepath, user_directory);
156  strcat(filepath, "\\");
157 #else
158  const char *user_directory = wbu_system_getenv("HOME");
159  filepath = (char *)malloc(strlen(user_directory) + 16);
160  strcpy(filepath, user_directory);
161  strcat(filepath, "/");
162 #endif
163  strcat(filepath, filenames[current_blob]);
164  printf(filepath);
165  wb_camera_save_image(camera, filepath, 100);
166  free(filepath);
167  pause_counter = 20;
168  }
169  }
170 
171 
172  }
173 
174  wb_robot_cleanup();
175 
176  return 0;
177 }
178 
BLOB_TYPE
BLOB_TYPE
Definition: camera_1.c:39
ANSI_COLOR_RED
#define ANSI_COLOR_RED
Definition: camera_1.c:29
camera
Definition: camera.py:1
ANSI_COLOR_BLUE
#define ANSI_COLOR_BLUE
Definition: camera_1.c:32
TIME_STEP
#define TIME_STEP
Definition: camera_1.c:38
GREEN
Definition: camera_1.c:39
main
int main()
Definition: camera_1.c:41
ANSI_COLOR_GREEN
#define ANSI_COLOR_GREEN
Definition: camera_1.c:30
NONE
Definition: camera_1.c:39
RED
Definition: camera_1.c:39
BLUE
Definition: camera_1.c:39
ANSI_COLOR_RESET
#define ANSI_COLOR_RESET
Definition: camera_1.c:35
SPEED
#define SPEED
Definition: camera_1.c:37


rosinljubljana
Author(s): Igor Banfi
autogenerated on Mon Feb 24 2020 12:32:36