24 #include <webots/camera.h>
25 #include <webots/motor.h>
26 #include <webots/robot.h>
27 #include <webots/utils/system.h>
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"
42 WbDeviceTag
camera, left_motor, right_motor;
44 int pause_counter = 0;
45 int left_speed, right_speed;
48 const char *color_names[3] = {
"red",
"green",
"blue"};
50 const char *filenames[3] = {
"red_blob.png",
"green_blob.png",
"blue_blob.png"};
56 camera = wb_robot_get_device(
"camera");
58 width = wb_camera_get_width(
camera);
59 height = wb_camera_get_height(
camera);
66 const unsigned char *image = wb_camera_get_image(
camera);
69 if (pause_counter > 0)
78 if (pause_counter > 10) {
88 else if (pause_counter > 0) {
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);
123 if ((red > 3 * green) && (red > 3 * blue))
125 else if ((green > 3 * red) && (green > 3 * blue))
126 current_blob =
GREEN;
127 else if ((blue > 3 * red) && (blue > 3 * green))
137 if (current_blob ==
NONE) {
149 printf(
"Looks like I found a %s%s%s blob.\n", ansi_colors[current_blob], color_names[current_blob],
ANSI_COLOR_RESET);
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,
"\\");
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,
"/");
163 strcat(filepath, filenames[current_blob]);
165 wb_camera_save_image(
camera, filepath, 100);