Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <webots/camera.h>
- #include <webots/position_sensor.h>
- #include <webots/distance_sensor.h>
- #include <webots/motor.h>
- #include <webots/robot.h>
- #include <webots/utils/system.h>
- #ifdef _WIN32
- #include <Windows.h>
- #else
- #include <unistd.h>
- #endif
- #define TIME_STEP 32
- int width, height, tamanhodorobo = 27;
- WbDeviceTag right_camera, left_camera, left_motor, right_motor, empilhadeira, frds, flds, ltds, lbds, pos;
- typedef struct _rgb {
- int r, g, b;
- } rgb;
- rgb verde_min = {1000, 12000, 1000}, verde_max = {10000, 14000, 10000};
- rgb getrgbs(WbDeviceTag camera) {
- const unsigned char *image = wb_camera_get_image(camera);
- rgb ans = {0, 0, 0};
- for(int i = 0; i < width; i++) {
- for(int j = 0; j < height; j++) {
- ans.r += wb_camera_image_get_red(image, width, i, j);
- ans.g += wb_camera_image_get_green(image, width, i, j);
- ans.b += wb_camera_image_get_blue(image, width, i, j);
- }
- }
- return ans;
- }
- void pegar_tubo(){
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(left_motor, 2);
- wb_motor_set_velocity(right_motor, 2);
- double frdsvalue = wb_distance_sensor_get_value(frds);
- double fldsvalue = wb_distance_sensor_get_value(flds);
- if(frdsvalue < 60 && fldsvalue < 60) {
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 0);
- while(wb_position_sensor_get_value(pos) > 0) {
- wb_motor_set_velocity(empilhadeira, -2);
- }
- wb_motor_set_velocity(left_motor, 2);
- wb_motor_set_velocity(right_motor, 2);
- while(wb_position_sensor_get_value(pos) < 0.3) {
- wb_motor_set_velocity(empilhadeira, 2);
- }
- return;
- }
- }
- }
- void entregar_tubo(){
- while(wb_robot_step(TIME_STEP) != -1) {
- double frdsvalue = wb_distance_sensor_get_value(frds);
- double fldsvalue = wb_distance_sensor_get_value(flds);
- while(wb_position_sensor_get_value(pos) > 0) {
- wb_motor_set_velocity(empilhadeira, -2);
- }
- wb_motor_set_velocity(left_motor, -2);
- wb_motor_set_velocity(right_motor, -2);
- if(frdsvalue > 60 && fldsvalue > 60){
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 0);
- while(wb_position_sensor_get_value(pos) < 0.3) {
- wb_motor_set_velocity(empilhadeira, 2);
- }
- return;
- }
- }
- }
- double odometria() {
- double tube;
- clock_t start = clock();
- while(wb_robot_step(TIME_STEP) != -1) {
- double ltdsvalue = wb_distance_sensor_get_value(ltds);
- wb_motor_set_velocity(left_motor, 2);
- wb_motor_set_velocity(right_motor, 2);
- if(ltdsvalue < 100) {
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 0);
- break;
- }
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if(rc.b < 7000 && rc.r < 9000 && rc.g < 8000) {
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 0);
- clock_t end = clock();
- tube = 2.0*(end - start) + 27;
- return tube;
- }
- }
- clock_t end = clock();
- tube = 2.0*(end - start);
- return tube;
- }
- void alinhar_chao() {//verificado
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- int left_sensor = 0, right_sensor = 0;
- if(rc.b < 7000 && rc.r < 9000 && rc.g < 8000) {
- right_sensor = 1;
- }
- if(lc.b < 7000 && lc.r < 9000 && lc.g < 8000) {
- left_sensor = 1;
- }
- if(right_sensor && !left_sensor) {
- wb_motor_set_velocity(right_motor, -3);
- wb_motor_set_velocity(left_motor, 1);
- while(wb_robot_step(TIME_STEP) != -1) {
- rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if(abs(rc.b - lc.b) < 150 && abs(rc.g - lc.g) < 150 && abs(rc.r - lc.r) < 150) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- return;
- }
- }
- }
- if(!right_sensor && left_sensor) {
- wb_motor_set_velocity(right_motor, 1);
- wb_motor_set_velocity(left_motor, -3);
- while(wb_robot_step(TIME_STEP) != -1) {
- rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if(abs(rc.b - lc.b) < 150 && abs(rc.g - lc.g) < 150 && abs(rc.r - lc.r) < 150) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- return;
- }
- }
- }
- if(right_sensor && left_sensor) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- return;
- }
- }
- }
- void alinhar(rgb min, rgb max) {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if(rc.r >= min.r && rc.r <= max.r && rc.b >= min.b && rc.b <= max.b && rc.g >= min.g && rc.g <= max.g) {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(left_motor, 2);
- wb_motor_set_velocity(right_motor, 0);
- if(lc.r >= min.r && lc.r <= max.r && lc.b >= min.b && lc.b <= max.b && lc.g >= min.g && lc.g <= max.g) {
- return;
- }
- }
- }
- if(lc.r >= min.r && lc.r <= max.r && lc.b >= min.b && lc.b <= max.b && lc.g >= min.g && lc.g <= max.g) {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 2);
- if(rc.r >= min.r && rc.r <= max.r && rc.b >= min.b && rc.b <= max.b && rc.g >= min.g && rc.g <= max.g) {
- return;
- }
- }
- }
- }
- }
- void desviar() {
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if(rc.g < 5800 && rc.b < 5800 && rc.r < 5800) {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, -2);
- wb_motor_set_velocity(left_motor, 0);
- if(rc.g > 5800 && rc.b > 5800 && rc.r > 5800) {
- return;
- }
- }
- }
- if(lc.g < 5800 && lc.b < 5800 && lc.r < 5800) {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, -2);
- if(lc.g > 5800 && lc.b > 5800 && lc.r > 5800) {
- return;
- }
- }
- }
- return;
- }
- void giro_() {
- int j = 0;
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, -1);
- wb_motor_set_velocity(left_motor, 2);
- j++;
- if(j == 350) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- break;
- }
- }
- return;
- }
- void _giro() {
- int j = 0;
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, -1);
- j++;
- if(j == 350) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- break;
- }
- }
- return;
- }
- void _giro_() {
- _giro();
- _giro();
- return;
- }
- void vi0() {
- while(wb_robot_step(TIME_STEP) != -1) {
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- if((rc.r < 9000 && rc.g < 8000 && rc.b < 7000) || (lc.r < 9000 && lc.g < 8000 && lc.b < 7000)) {
- break;
- }
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- if(rc.r >= verde_min.r && rc.r <= verde_max.r && rc.g >= verde_min.g && rc.g <= verde_max.g && rc.b >= verde_min.b && rc.b <= verde_max.b) {
- _giro();
- }
- if(lc.r >= verde_min.r && lc.r <= verde_max.r && lc.g >= verde_min.g && lc.g <= verde_max.g && lc.b >= verde_min.b && lc.b <= verde_max.b) {
- _giro_();
- }
- }
- alinhar_chao();
- }
- void via1() {
- wb_motor_set_velocity(right_motor, -2);
- wb_motor_set_velocity(left_motor, -2);
- giro_();
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- double fr = wb_distance_sensor_get_value(frds), fl = wb_distance_sensor_get_value(flds);
- double avg = (fr + fl) / 2;
- if(avg < 150) {
- break;
- }
- rgb lc = getrgbs(left_camera);
- if(lc.r < 3200 && lc.g < 3200 && lc.b < 3200) {
- wb_motor_set_velocity(right_motor, 1);
- int j = 0;
- for(int i = 0; i < 20000000; i++) {
- j++;
- }
- wb_motor_set_velocity(right_motor, 2);
- }
- }
- giro_();
- }
- void via2() {
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- double dsl = wb_distance_sensor_get_value(ltds), dsr = wb_distance_sensor_get_value(frds);
- if(dsl > 100 || dsr < 100) {
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- break;
- }
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- rgb md = {(rc.r + lc.r) / 2, (rc.g + lc.g) / 2, (rc.b + lc.b) / 2};
- if(md.r < 5800 && md.g < 5800 && md.b < 5800) {
- return;
- }
- }
- double dsl = wb_distance_sensor_get_value(ltds), dsr = wb_distance_sensor_get_value(frds), dsb = wb_distance_sensor_get_value(lbds);
- if(dsl > 100) {
- if(dsb < 100 && dsr > 100) {
- return;
- } else if(dsb < 100 & dsr < 100) {
- giro_();
- via2();
- } else {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- int j = 0;
- for(int i = 0; i < 20000000; i++) {
- j++;
- }
- _giro();
- via2();
- }
- }
- if(dsr < 100) {
- giro_();
- via2();
- }
- }
- void vit1() {
- giro_();
- while(wb_robot_step(TIME_STEP) != -1) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- rgb lc = getrgbs(left_camera), rc = getrgbs(right_camera);
- rgb avg = {(lc.r + rc.r) / 2, (lc.g + rc.g) / 2, (lc.b + rc.b) / 2};
- if(avg.r < 5800 && avg.g < 5800 && avg.b < 5800) {
- break;
- } else {
- desviar();
- }
- }
- rgb mn = {0, 0, 0}, mx = {1700, 1700, 1700};
- alinhar(mn, mx);
- }
- void vit3(rgb cor_min, rgb cor_max) {
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- int j = 0;
- for(int i = 0; i < 20000000; i++) {
- j++;
- }
- wb_motor_set_velocity(right_motor, 0);
- wb_motor_set_velocity(left_motor, 0);
- while(wb_robot_step(TIME_STEP) != -1) {
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- rgb avg = {(lc.r + rc.r) / 2, (lc.g + rc.g) / 2, (lc.b + rc.b) / 2};
- if(avg.r >= cor_min.r && avg.r <= cor_max.r && avg.b >= cor_min.b && avg.b <= cor_max.b && avg.g >= cor_min.g && avg.g <= cor_max.g) {
- giro_();
- return;
- } else {
- _giro();
- int deu180 = 0;
- while(wb_robot_step(TIME_STEP) != -1) {
- rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- rgb aavg = {(lc.r + rc.r) / 2, (lc.g + rc.g) / 2, (lc.b + rc.b) / 2};
- wb_motor_set_velocity(right_motor, 2);
- wb_motor_set_velocity(left_motor, 2);
- if(aavg.r >= cor_min.r && aavg.r <= cor_max.r && aavg.b >= cor_min.b && aavg.b <= cor_max.b && aavg.g >= cor_min.g && aavg.g <= cor_max.g) {
- if(!deu180) {
- _giro_();
- }
- return;
- }
- if(aavg.r <= 5800 && aavg.b <= 5800 && aavg.g <= 5800) {
- _giro_();
- deu180 = 1;
- }
- }
- }
- }
- }
- void vit3_1(rgb cor_min, rgb cor_max){
- while(wb_robot_step(TIME_STEP) != -1) {
- double ltdsvalue = wb_distance_sensor_get_value(ltds);
- double lbdsvalue = wb_distance_sensor_get_value(lbds);
- if (ltdsvalue > 40 && lbdsvalue > 40){
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- rgb avg = {(lc.r + rc.r) / 2, (lc.g + rc.g) / 2, (lc.b + rc.b) / 2};
- if(avg.r >= cor_min.r && avg.r <= cor_max.r && avg.b >= cor_min.b && avg.b <= cor_max.b && avg.g >= cor_min.g && avg.g <= cor_max.g){
- wb_motor_set_velocity(left_motor, 2);
- wb_motor_set_velocity(right_motor, 2);
- }
- else{
- _giro_();
- }
- }
- _giro();
- double frdsvalue = wb_distance_sensor_get_value(frds);
- double fldsvalue = wb_distance_sensor_get_value(flds);
- while (frds > 100 || flds > 100){
- double frdsvalue = wb_distance_sensor_get_value(frds);
- double fldsvalue = wb_distance_sensor_get_value(flds);
- wb_motor_set_velocity(left_motor, 0.08*(flds - 100));
- wb_motor_set_velocity(right_motor, 0.08*(frds - 100));
- }
- wb_motor_set_velocity(left_motor, 0);
- wb_motor_set_velocity(right_motor, 0);
- }
- }
- int main(int argc, char **argv) {
- //creating our tags and variables
- int pause_counter = 0;
- int left_speed, right_speed;
- wb_robot_init();
- //initializing our tags
- frds = wb_robot_get_device("FrontRightDistanceSensor");
- flds = wb_robot_get_device("FrontLeftDistanceSensor");
- ltds = wb_robot_get_device("LeftTopDistanceSensor");
- lbds = wb_robot_get_device("LeftBottomDistanceSensor");
- pos = wb_robot_get_device("EMPPositionSensor");
- right_camera = wb_robot_get_device("RightCamera");
- left_camera = wb_robot_get_device("LeftCamera");
- width = wb_camera_get_width(right_camera);
- height = wb_camera_get_height(right_camera);
- left_motor = wb_robot_get_device("LeftWheel");
- right_motor = wb_robot_get_device("RightWheel");
- empilhadeira = wb_robot_get_device("EMPLinearMotor");
- wb_distance_sensor_enable(frds, TIME_STEP);
- wb_distance_sensor_enable(flds, TIME_STEP);
- wb_distance_sensor_enable(ltds, TIME_STEP);
- wb_distance_sensor_enable(lbds, TIME_STEP);
- wb_position_sensor_enable(pos, TIME_STEP);
- wb_camera_enable(right_camera, TIME_STEP);
- wb_camera_enable(left_camera, TIME_STEP);
- wb_motor_set_position(left_motor, INFINITY);
- wb_motor_set_position(right_motor, INFINITY);
- wb_motor_set_velocity(left_motor, 0.0);
- wb_motor_set_velocity(right_motor, 0.0);
- vi0();
- via1();
- via2();
- double l = odometria();
- printf("%lf\n", l);
- /*while (wb_robot_step(TIME_STEP) != -1) {
- rgb rc = getrgbs(right_camera), lc = getrgbs(left_camera);
- printf("Left values: %d %d %d\n", lc.r, lc.g, lc.b);
- printf("Right values: %d %d %d\n", rc.r, rc.g, rc.b);
- } */
- wb_robot_cleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement