int main(int argc, const char **argv) { int i; playerc_client_t *client; playerc_position2d_t *position2d; // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6666); if (0 != playerc_client_connect(client)) return -1; // Create and subscribe to a position2d device. position2d = playerc_position2d_create(client, 1); if (playerc_position2d_subscribe(position2d, PLAYER_OPEN_MODE)) return -1; // Make the robot spin! if (0 != playerc_position2d_set_cmd_vel(position2d, 0.25, 0, DTOR(40.0), 1)) return -1; for (i = 0; i < 200; i++) { // Wait for new data from server playerc_client_read(client); // Print current robot pose printf("position2d : %f %f %f\n", position2d->px, position2d->py, position2d->pa); } // Shutdown playerc_position2d_unsubscribe(position2d); playerc_position2d_destroy(position2d); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
int main(int argc, char **argv) { // Initialize movement parameters (default to a square) int n = 4; int l = 65; int v = 50; int a = 90; // Parse command-line arguements int option_index = 0; static struct option long_options[] = { {"line", 0, 0, 0 }, {"triangle", 0, 0, 1 }, {"square", 0, 0, 2 }, {"pentagon", 0, 0, 3 }, {"hexagon", 0, 0, 4 }, {0, 0, 0, 0 } }; int shape_set = false; int angle_set = false; int num_set = false; int reverse_direction = false; int smooth_acceleration = false; int c; while ((c = getopt_long(argc, argv, "l:v:n:a:rs", long_options, &option_index)) != -1) switch (c) { case 0: // line n = 2; a = 180; shape_set = true; break; case 1: // triangle n = 3; a = 120; shape_set = true; break; case 2: // square n = 4; a = 90; shape_set = true; break; case 3: // pentagon n = 5; a = 72; shape_set = true; break; case 4: // hexagon n = 6; a = 60; shape_set = true; break; case 'l': // 0 to 200 cm (default 65) l = atoi(optarg); break; case 'v': // 0 to 50 cm/sec (default 50) v = atoi(optarg); break; case 'n': // 0 to 10 (default 4) n = atoi(optarg); num_set = true; break; case 'a': // 0 to 180 degrees (default 90) a = atoi(optarg); angle_set = true; break; case 'r': reverse_direction = true; break; case 's': smooth_acceleration = true; break; case '?': if (optopt == 'v' || optopt == 'a' || optopt == 'l' || optopt == 'n') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: printf("Bad arguments.\n\nUsage: dead_reckoning -n <number of sides> " "-l <length of each side> -v <velocity (cm/sec)> " "-a <turning angle (degrees)>"); exit(1); } int i = 0; for (i = optind; i < argc; i++) printf ("Non-option argument %s\n", argv[i]); if(!shape_set && (!num_set || !angle_set)) { printf("\nYou need to specify a shape (by name, or with the arguments " "'n' and 'a').\n\nUsage: dead_reckoning --<shape> " "[-l <side length>] [-v <velocity>] [-r] [-s]\n\n" "Alternative usage: dead_reckoning -n <number of sides> " "-a <angle of each corner in degrees> " "[-l <length of each side>] [-v <velocity>]\n\n" "Valid shapes are: line, triangle, square, pentagon, hexagon\n" "Side length is in cm. Valid range is [10,200]. Default value is 65.\n" "Velocity is in cm/sec. Valid range is [10,50]. Default value is 50.\n" "The -r option reverses the turn direction (clockwise by default)\n" "The -s option enables smooth acceleration and deceleration\n\n" "e.g. dead_reckoning --hexagon\n" "e.g. dead_reckoning --hexagon -l 100\n" "e.g. dead_reckoning --hexagon -l 100 -v 40\n" "e.g. dead_reckoning -n 6 -a 60 -l 100 -v 40\n\n"); exit(1); } // Initialize player i = 0; playerc_client_t *client; playerc_position2d_t *position2d; // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6665); if (0 != playerc_client_connect(client)) return -1; // Create and subscribe to a position2d device. position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYER_OPEN_MODE)) return -1; // Read initial position playerc_client_read(client); double start_x = position2d->px; double start_y = position2d->py; double start_a = position2d->pa; // Convert parameters to motor control arguments double max_vel = 0.01 * v; int move_time = l * 28000; move_time *= 50.0 / (double)v; double max_turn_speed = 1.57; // 90 degrees per second int turn_time = (int)(((double)a/90.0) * 560000); int d = -1; if (reverse_direction) d = 1; // Move the robot (e.g. square) double accel_rate = 0.01; // 2 meters per sec^2 double turn_accel_rate = 0.0314; // 360 degrees per sec^2 int cycle_time = 5000; // 5 miliseconds for (i = 0; i < n; ++i) { // FORWARD int acceleration_time = 0; double vel = 0; int t = 0; if (smooth_acceleration) { // accelerate (up to max speed in 0.25 seconds) acceleration_time = (max_vel / accel_rate) * cycle_time; for (; vel < max_vel && t < move_time / 2; t += cycle_time, vel += accel_rate, usleep(cycle_time)) if (playerc_position2d_set_cmd_vel(position2d, vel, 0, 0, 1)) return -1; } //max forward speed if (playerc_position2d_set_cmd_vel(position2d, max_vel, 0, 0, 1)) return -1; usleep(move_time - acceleration_time); // not 2x acceleration time in order to make // up for ground not covered durring acceleration // (at an average of half of full speed) if (smooth_acceleration) { // decelerate for (; vel >= 0; vel -= accel_rate, usleep(cycle_time)) if (playerc_position2d_set_cmd_vel(position2d, vel, 0, 0, 1)) return -1; } // TURN double turn_speed = 0; if (smooth_acceleration) { // accelerate (up to max speed in 0.25 seconds) acceleration_time = (max_turn_speed / turn_accel_rate) * cycle_time; for (t = 0; turn_speed < max_turn_speed && t < move_time / 2; t += cycle_time, turn_speed += turn_accel_rate, usleep(cycle_time)) if (playerc_position2d_set_cmd_vel(position2d, 0, 0, d * turn_speed, 1)) return -1; } // max turn speed if (playerc_position2d_set_cmd_vel(position2d, 0, 0, d * max_turn_speed, 1)) return -1; usleep(turn_time - acceleration_time); if (smooth_acceleration) { // decelerate for (; turn_speed >= 0; turn_speed -= turn_accel_rate, usleep(cycle_time)) if (playerc_position2d_set_cmd_vel(position2d, 0, 0, d * turn_speed, 1)) return -1; } } // Stop if (playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1)) return -1; // Read final position, get error playerc_client_read(client); double error_x = position2d->px - start_x; double error_y = position2d->py - start_y; double error_a = position2d->pa - start_a; // Report odometry error printf("\n\nMovement error according to onboard odometry:\n" "\tX pos: %f cm\n\tY pos: %f cm\n\tAngle: %f degrees\n", error_x * 100, error_y * 100, error_a * -57.325); // Shutdown playerc_position2d_unsubscribe(position2d); playerc_position2d_destroy(position2d); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
int main(int argc, const char **argv) { //Variaveis int degrees,PosRelX,PosRelY; float radians,Dlaser,ODM_ang, ang; int width = 500, height = 500; //Coloque o tamanho do mapa aqui (em pixel) int centroX = (width / 2); int centroY = (height / 2); playerc_client_t *client; playerc_laser_t *laser; playerc_position2d_t *position2d; CvPoint pt,pt1,pt2; CvScalar cinzaE,preto,cinzaC; char window_name[] = "Mapa"; IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 ); cvNamedWindow(window_name, 1 ); preto = CV_RGB(0, 0, 0); //Para indicar obstaculos cinzaE = CV_RGB(92, 92, 92); //Para indicar o desconhecido cinzaC = CV_RGB(150, 150, 150); //Para indicar espacos livres printf ("debug: 11 - INICIO\n"); client = playerc_client_create(NULL, "localhost", 6665); printf ("debug: 12\n"); if (playerc_client_connect(client) != 0) return -1; printf ("debug: 13\n"); laser = playerc_laser_create(client, 0); printf ("debug: 21\n"); if (playerc_laser_subscribe(laser, PLAYERC_OPEN_MODE)) return -1; printf ("debug: 22\n"); position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYERC_OPEN_MODE) != 0) { printf ("err1\n"); fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } printf ("debug: 23\n"); if (playerc_client_datamode (client, PLAYERC_DATAMODE_PULL) != 0) { printf ("err2\n"); fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } printf ("debug: 24\n"); if (playerc_client_set_replace_rule (client, -1, -1, PLAYER_MSGTYPE_DATA, -1, 1) != 0) { printf ("err3\n"); fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } playerc_position2d_enable(position2d, 1); // Liga os motores printf ("debug: 25\n"); playerc_position2d_set_odom(position2d, 0, 0, 0); // Zera o odômetro cvSet(image, cinzaE,0); //Preencha a imagem com fundo cinza escuro pt.x = centroX; // Zera a coordenada X pt.y = centroY; // Zera a coordenada Y /* if( 0 != playerc_position2d_set_cmd_vel(position2d, 0, 0, DTOR(40.0), 1)) return -1; */ while(1) { printf ("debug: 26\n"); playerc_client_read(client); printf ("debug: 27\n"); //cvSaveImage("mapa1.jpg",image,0); printf ("debug: 28\n"); //playerc_client_read(client); printf ("debug: 29\n"); for (degrees = 2; degrees <= 360; degrees+=2) { printf ("debug: 30\n"); Dlaser = laser->scan[degrees][0]; printf ("debug: 31\n"); if (Dlaser < 8) { radians = graus2rad (degrees/2); //Converte o angulo do laser em graus para radianos printf ("debug: 32\n"); ODM_ang = position2d->pa; //Obtem o angulo relativo do robo ang = ((1.5*PI)+radians+ODM_ang); //Converte o angulo relativo em global printf ("debug: 33\n"); PosRelX = arredonda(position2d->px); //Posicao X relativa do robo PosRelY = arredonda(position2d->py); //Posicao Y relativa do robo printf ("debug: 34\n"); pt1.y = (centroY-PosRelY); //Coordenada y global do robo pt1.x = (centroX+PosRelX); //Coordenada x global do robo //converte coordenadas polares para retangulares (global) printf ("debug: 35\n"); pt.y = (int)(pt1.y-(sin(ang)*Dlaser*10)); pt.x = (int)(pt1.x+(cos(ang)*Dlaser*10)); printf ("debug: 36\n"); //Desenha a area livre cvLine(image, pt1,pt,cinzaC, 1,4,0); printf ("debug: 37\n"); //Marca o objeto no mapa cvLine(image, pt,pt,preto, 1,4,0); printf ("debug: 38\n"); //Mostra o resultado do mapeamento na tela //cvShowImage(window_name, image ); printf ("debug: 39\n"); //cvWaitKey(10); printf ("debug: 40\n"); } } } //Desconecta o player printf ("debug: 41\n"); playerc_laser_unsubscribe(laser); printf ("debug: 42\n"); playerc_laser_destroy(laser); printf ("debug: 43\n"); playerc_client_disconnect(client); printf ("debug: 44\n"); playerc_client_destroy(client); printf ("debug: 45\n"); //Destroi a janela OpenCV cvReleaseImage(&image); printf ("debug: 46\n"); cvDestroyWindow(window_name); printf ("debug: 47\n"); return 0; }
int main(int argc, const char **argv) { int i; int porta = 6665; double x, y; char livre; char end_ip[20]; // OpenCV Variables char wndname[30] = "Drawing Demo"; int line_type = CV_AA; // change it to 8 to see non-antialiased graphics CvPoint pt1, pt2; IplImage* image; int width = MAX_X, height = MAX_Y; // 200 x 100 pixels // Player-Stage Variables playerc_client_t *client; playerc_position2d_t *position2d; playerc_laser_t *laser; // Create a window image = cvCreateImage(cvSize(width, height), 8, 3); cvNamedWindow(wndname, 1); cvZero(image); pt1.x = 100; pt1.y = MAX_Y; pt2.x = 100; pt2.y = MAX_Y - 80; cvLine(image, pt1, pt2, CV_RGB(255, 255, 255), 2, line_type, 0); pt2.x = 20; pt2.y = MAX_Y; cvLine(image, pt1, pt2, CV_RGB(255, 255, 255), 2, line_type, 0); pt2.x = 180; pt2.y = MAX_Y; cvLine(image, pt1, pt2, CV_RGB(255, 255, 255), 2, line_type, 0); cvShowImage(wndname, image); cvWaitKey(1000); cvZero(image); pt1.x = 20; pt1.y = MAX_Y; pt2.x = 160; pt2.y = MAX_Y - 80; cvRectangle(image, pt1, pt2, CV_RGB(255, 255, 255), 2, line_type, 0); cvShowImage(wndname, image); cvWaitKey(1000); strcpy(end_ip, "localhost"); if (argc >= 2) /* Get Port */ porta = atoi(argv[1]); if (argc >= 3) /* Get IP Address */ strcpy(end_ip, argv[2]); printf("Porta: %d\n", porta); printf("IP: %s\n", end_ip); client = playerc_client_create(NULL, end_ip, porta); if (playerc_client_connect(client) != 0) return -1; // Connect to Position position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYERC_OPEN_MODE) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } // Enable motor control playerc_position2d_enable(position2d, 1); // Connect to Laser laser = playerc_laser_create(client, 0); if (playerc_laser_subscribe(laser, PLAYERC_OPEN_MODE)) return -1; // Read several times the robot data (delay) playerc_client_read(client); playerc_client_read(client); playerc_client_read(client); playerc_client_read(client); playerc_client_read(client); while (1) { playerc_client_read(client); // scan for free 100 cm in front of robot livre = 1; cvZero(image); for (i = 0; i < 360; i++) { if ((laser->scan[i][0]) < 0.5) livre = 0; // Debug: if (laser->scan[i][0] <= 0) printf("#"); if (laser->scan[i][0] < 7.8) { x = laser->scan[i][0] * cos(laser->scan[i][1] + 3.1415926 / 2.0); y = laser->scan[i][0] * sin(laser->scan[i][1] + 3.1415926 / 2.0); pt1.x = (int) (x * 10 + 100); pt1.y = (int) (MAX_Y - y * 10); cvCircle(image, pt1, 2, CV_RGB(255, 255, 255), 1, line_type, 0); } } cvShowImage(wndname, image); // if free moves, otherwise turns if (livre) playerc_position2d_set_cmd_vel(position2d, 0.2, 0, 0.0, 1); else playerc_position2d_set_cmd_vel(position2d, 0.0, 0, 0.4, 1); cvWaitKey(10); } playerc_laser_unsubscribe(laser); playerc_laser_destroy(laser); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
int player_update() { int i; if (NULL != playerc_client_read(g_client)) { if (NULL != g_camera) { // Decompress the image if necessary playerc_camera_decompress(g_camera); assert(allocated_size > g_camera->image_count*3); // figure out the colorspace switch (g_camera->format) { case PLAYER_CAMERA_FORMAT_MONO8: // we should try to use the alpha layer, // but for now we need to change // the image data for (i=0;i<g_camera->image_count;++i) { memcpy(g_img+i*3, g_camera->image+i, 3); } break; case PLAYER_CAMERA_FORMAT_MONO16: { int j = 0; // Transform to MONO8 for (i = 0; i < g_camera->image_count; i++, j+=2) { g_img[i*3+1] = g_img[i*3+2] = g_img[i*3+3] = ((unsigned char)(g_camera->image[j]) << 8) + (unsigned char)(g_camera->image[j+1]); } break; } case PLAYER_CAMERA_FORMAT_RGB888: // do nothing memcpy(g_img, g_camera->image, g_camera->image_count); break; default: g_print("Unknown camera format: %i\n", g_camera->format); exit(-1); } g_width = g_camera->width; g_height = g_camera->height; } if (NULL != g_blobfinder) { g_blob_count = PLAYERCAM_MAX_BLOBS < g_blobfinder->blobs_count ? PLAYERCAM_MAX_BLOBS : g_blobfinder->blobs_count; memcpy(g_blobs, g_blobfinder->blobs, g_blob_count*sizeof(player_blobfinder_blob_t)); if ((g_width != g_blobfinder->width) || (g_height != g_blobfinder->height)) { g_print("camera and blobfinder height or width do not match %d,%d != %d,%d\n",g_width,g_height,g_blobfinder->width,g_blobfinder->height); // should we die here? //exit(-1); } } } else { g_print("ERROR reading player g_client\n"); //exit(-1); } return 0; }
int player_init(int argc, char *argv[]) { int csize, usize, i; if(get_options(argc, argv) < 0) { print_usage(); exit(-1); } // Create a g_client object and connect to the server; the server must // be running on "localhost" at port 6665 g_client = playerc_client_create(NULL, g_hostname, g_port); playerc_client_set_transport(g_client, g_transport); if (0 != playerc_client_connect(g_client)) { fprintf(stderr, "error: %s\n", playerc_error_str()); exit(-1); } /* if (0 != playerc_client_datafreq(g_client, 20)) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } */ // Create a camera proxy (device id "camera:index") and susbscribe g_camera = playerc_camera_create(g_client, g_camera_index); if (0 != playerc_camera_subscribe(g_camera, PLAYER_OPEN_MODE)) { fprintf(stderr, "camera error: %s\n", playerc_error_str()); fprintf(stderr, "playercam will attempt to continue without a camera\n"); playerc_camera_destroy(g_camera); g_camera = NULL; } // Create a blobfinder proxy (device id "blobfinder:index") and susbscribe g_blobfinder = playerc_blobfinder_create(g_client, g_blobfinder_index); if (0 != playerc_blobfinder_subscribe(g_blobfinder, PLAYER_OPEN_MODE)) { fprintf(stderr, "blobfinder error: %s\n", playerc_error_str()); fprintf(stderr, "playercam will attempt to continue without a blobfinder\n"); playerc_blobfinder_destroy(g_blobfinder); g_blobfinder = NULL; } if ((NULL == g_camera) && (NULL == g_blobfinder)) { fprintf(stderr, "we need either a camera or blobfinder! aborting\n"); exit(-1); } // Get up to 10 images until we have a valid frame (g_wdith > 0) for (i=0, g_width=0; i < 10 && g_width==0 && NULL != playerc_client_read(g_client); ++i) { if (NULL != g_camera) { // Decompress the image csize = g_camera->image_count; playerc_camera_decompress(g_camera); usize = g_camera->image_count; g_print("camera: [w %d h %d d %d] [%d/%d bytes]\n", g_camera->width, g_camera->height, g_camera->bpp, csize, usize); g_width = g_camera->width; g_height = g_camera->height; if (allocated_size != usize) { g_img = realloc(g_img, usize); allocated_size = usize; } } else // try the blobfinder { g_print("blobfinder: [w %d h %d]\n", g_blobfinder->width, g_blobfinder->height); g_width = g_blobfinder->width; g_height = g_blobfinder->height; usize = g_width * g_height * 3; if (allocated_size != usize) { g_img = realloc(g_img, usize); allocated_size = usize; } // set the image data to 0 since we don't have a camera memset(g_img, 128, usize); } } g_window_width = g_width; g_window_height = g_height; assert(g_width>0); assert(g_height>0); playerc_client_datamode(g_client,PLAYER_DATAMODE_PULL); playerc_client_set_replace_rule(g_client,-1,-1,PLAYER_MSGTYPE_DATA,-1,1); return 0; }
/*double randomInt(int low, int high) { return low + (high - low) * (rand()/(RAND_MAX * 1.0 )); } // Main function for the program*/ int main(int argc, const char **argv) { playerc_client_t *client; playerc_position2d_t *position2d; int cycle, index=0; double dist,angle,fidAngle = 0,lineAngle=0, fidDist=0, prevYaw=0,posAngle=0; // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6665); if (0 != playerc_client_connect(client)) return -1; // Create and subscribe to a position2d device. position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYER_OPEN_MODE)) return -1; // Initiating random walk movement /*if (0 != playerc_position2d_set_cmd_vel(position2d, randomInt(0.1,1) ,randomInt(0.1,1),DTOR(randomInt(-20,20)) ,1)) return -1; fprintf(stdout, "robot random positions \n");*/ //looping in random positions int i; for (i = 0; i<=10; i++) { // Wait for new data from server playerc_client_read(client); fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n", position2d->px, position2d->py, position2d->pa); // Random walk is continued till finding first marker if (0 != playerc_position2d_set_cmd_vel(position2d, 3 ,0 , 0,1)) return -1; usleep(1000); } for (i = 0; i<=10; i++) { // Wait for new data from server playerc_client_read(client); fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n", position2d->px, position2d->py, position2d->pa); // Random walk is continued till finding first marker if (0 != playerc_position2d_set_cmd_vel(position2d, 0 ,0 , 1,1)) return -1; usleep(1000); } for (i = 0; i<=10; i++) { // Wait for new data from server playerc_client_read(client); fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n", position2d->px, position2d->py, position2d->pa); // Random walk is continued till finding first marker if (0 != playerc_position2d_set_cmd_vel(position2d, 3 ,0 , 0,1)) return -1; usleep(1000); } for (i = 0; i<=10; i++) { // Wait for new data from server playerc_client_read(client); fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n", position2d->px, position2d->py, position2d->pa); // Random walk is continued till finding first marker if (0 != playerc_position2d_set_cmd_vel(position2d, 0 ,0 , 1,1)) return -1; usleep(1000); } for (i = 0; i<=10; i++) { // Wait for new data from server playerc_client_read(client); fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n", position2d->px, position2d->py, position2d->pa); // Random walk is continued till finding first marker if (0 != playerc_position2d_set_cmd_vel(position2d, 3 ,0 , 0,1)) return -1; usleep(1000); } while(1) {if (0 != playerc_position2d_set_cmd_vel(position2d, 0,0 , 0,1)) return -1; } playerc_position2d_unsubscribe(position2d); playerc_position2d_destroy(position2d); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
int main(int argc, const char **argv) { int i; playerc_client_t *client; nsdnet_t *device; // Takes a parameter for the index. int index = 0; if (argc < 2) { printf("Needs one parameter.\n"); return 0; } else { index = atoi(argv[1]); printf("Using index %d\n", index); } // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6665); if (0 != playerc_client_connect(client)) { printf("Could not connect\n"); return -1; } // Load the plugin interface if (playerc_add_xdr_ftable (player_plugininterf_gettable (), 0) < 0) printf("Could not add xdr functions\n"); // Create and subscribe to a device using the interface. device = nsdnet_create(client, index); if (nsdnet_subscribe(device, PLAYER_OPEN_MODE) != 0) { printf("Could not subscribe\n"); return -1; } // Get our clientid if (nsdnet_property_get(device, "self.id")) { printf("Failed to get registered device id...\n"); return -1; } // Print out what we get... printf("Client id: %s\n", device->propval); // Get the list of clients if (nsdnet_get_listclients(device)) { printf("Failed to get list of clients...\n"); return -1; } // Print out what we get... printf("Clients: "); for (i = 0; i < device->listclients_count; i++) { printf("%s ", device->listclients[i]); } printf("\n"); // Endless loop... for (i = 0; i < 1000; i++) { if (playerc_client_peek(client, 0) == 1) { playerc_client_read(client); while (device->queue_head != device->queue_tail) { nsdmsg_t *msg; int err; if ((err = nsdnet_receive_message(device, &msg))) { if (err < 0) printf("ERROR\n"); else printf("%s: %s [%d]\n", msg->clientid, msg->msg, i); } } usleep(0); } else { printf("Sending Hello World\n"); if (nsdnet_send_message(device, NULL, 12, "Hello World")) { printf("Could not broadcast 'Hello World'\n"); return -1; } usleep(100000); } } // Shutdown nsdnet_unsubscribe(device); nsdnet_destroy(device); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
int main(int argc, const char **argv) { int i; playerc_client_t *client; player_pose2d_t position2d_vel; player_pose2d_t position2d_target; playerc_position2d_t *position2d; // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6665); if (0 != playerc_client_connect(client)) return -1; // Create and subscribe to a position2d device. position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYER_OPEN_MODE)) return -1; // Make the robot spin! if (0 != playerc_position2d_set_cmd_vel(position2d, 0.25, 0, DTOR(40.0), 1)) return -1; for (i = 0; i < 50; i++) { playerc_client_read(client); // Print current robot pose printf("position2d : %f %f %f\n", position2d->px, position2d->py, position2d->pa); } /* con el comando playerc_position2d_set_cmd_vel() simplemente se establece una * consigna de velocidad la cual es independiente de la posición en la que se * encuentra el robot */ position2d_target.px = 2; position2d_target.py = -3; position2d_target.pa = 0; // Move to pose playerc_position2d_set_cmd_pose(position2d, position2d_target.px , position2d_target.py, position2d_target.pa , 1); printf("position2d : %f %f %f\n", position2d->px, position2d->py, position2d->pa); // Stop when reach the target while (sqrt(pow(position2d->px - position2d_target.px,2.0) + pow(position2d->py - position2d_target.py,2.0)) > 0.05 ) { playerc_client_read(client); // Print current robot pose printf("position2d : %f %f %f\n", position2d->px, position2d->py, position2d->pa); } /* Con el comando position2d_set_cmd_pose() se establece una pose de destino. * Cuando se utiliza este método la información obtenida de la odometría sí que * afecta a la trayectoria seguida y al punto final */ position2d_target.px = 0; position2d_target.py = -3; position2d_target.pa = 0; position2d_vel.px = 0.6; position2d_vel.py = 0; position2d_vel.pa = 0; // Move to pose playerc_position2d_set_cmd_pose_with_vel(position2d, position2d_target, position2d_vel, 1); // Stop when reach the target while (sqrt(pow(position2d->px - position2d_target.px,2.0) + pow(position2d->py - position2d_target.py,2.0)) > 0.05 ) { playerc_client_read(client); // Print current robot pose printf("position2d : %f %f %f\n", position2d->px, position2d->py, position2d->pa); } /* Con el comando playerc_position2d_set_cmd_pose_with_vel se hace lo mismo que * con playerc_position2d_set_cmd_pose() pero además se puede indicar una * consigna de velocidad */ // Shutdown playerc_position2d_unsubscribe(position2d); playerc_position2d_destroy(position2d); playerc_client_disconnect(client); playerc_client_destroy(client); return 0; }
// Basic test for truth device. int test_truth(playerc_client_t *client, int index) { int t; double pos_i[3], rot_i[3]; double pos_f[3], rot_f[3]; void *rdevice; playerc_truth_t *device; printf("device [truth] index [%d]\n", index); device = playerc_truth_create(client, index); TEST("subscribing (read)"); if (playerc_truth_subscribe(device, PLAYER_READ_MODE) != 0) { FAIL(); return -1; } PASS(); for (t = 0; t < 3; t++) { TEST("getting pose (req/rep)"); if (playerc_truth_get_pose(device, pos_f + 0, pos_f + 1, pos_f + 2, rot_f + 1, rot_f + 1, rot_f + 2) != 0) { FAIL(); return -1; } PASS(); printf("truth: [%6.3f %6.3f %6.3f]\n", pos_f[0], pos_f[1], rot_f[2]); } TEST("setting pose"); pos_i[0] = 2; pos_i[1] = 3; pos_i[2] = 0; rot_i[0] = 0; rot_i[1] = 0; rot_i[2] = M_PI/2; if (playerc_truth_set_pose(device, pos_i[0], pos_i[1], pos_i[2], rot_i[0], rot_i[1], rot_i[2]) != 0) { FAIL(); return -1; } PASS(); TEST("getting pose (req/rep)"); if (playerc_truth_get_pose(device, pos_f + 0, pos_f + 1, pos_f + 2, rot_f + 1, rot_f + 1, rot_f + 2) != 0) { FAIL(); return -1; } PASS(); printf("truth: [%6.3f %6.3f %6.3f]\n", pos_f[0], pos_f[1], rot_f[2]); TEST("checking values for consitency"); if (fabs(pos_f[0] - pos_i[0]) > 0.001 || fabs(pos_f[1] - pos_i[1]) > 0.001 || fabs(rot_f[2] - rot_i[2]) > 0.001) FAIL(); else PASS(); for (t = 0; t < 3; t++) { TEST1("reading data (attempt %d)", t); do rdevice = playerc_client_read(client); while (rdevice == client); if (rdevice == device) { PASS(); printf("truth: [%6.3f %6.3f %6.3f]\n", device->pos[0], device->pos[1], device->rot[2]); } else FAIL(); } TEST("unsubscribing"); if (playerc_truth_unsubscribe(device) != 0) FAIL(); else PASS(); playerc_truth_destroy(device); return 0; }
int main(int argc, const char **argv) { int r, i, j; playerc_client_t *client; playerc_position2d_t *position2d; //sonar playerc_sonar_t *sonar; celda celdas[25]; int actual=0; int forward=1; int no_solucion=0; int flag_celda_final=0; // Create a client and connect it to the server. client = playerc_client_create(NULL, "localhost", 6665); if (playerc_client_connect(client) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } // Create and subscribe to a position2d device. position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYER_OPEN_MODE) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } // Fixing initial position playerc_position2d_set_odom(position2d, 0.0, 0.0, 0.0); // Create and subscribe to a sonar device sonar = playerc_sonar_create(client, 0); if (playerc_sonar_subscribe(sonar, PLAYER_OPEN_MODE) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } /* Obtener la geometría de los sensores de ultrasonidos sobre el pioneer 2 */ if (playerc_sonar_get_geom(sonar) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } // Enable motors playerc_position2d_enable(position2d, 1); playerc_client_read(client); inspeccionar_celda(sonar,&celdas[actual]); //Rutina para asegurarse de que ni nos dejamos un camino atrás, //ni estamos ya en la salida:"Ponemos el culo contra la pared" if(celda_final(&celdas[actual])) {//Si no veo ninguna pared girar_dch(client,position2d); // giro para comprobar la espalda inspeccionar_celda(sonar,&celdas[actual]); if(celdas[actual].pared[D_DCH]) { // había pared detrás girar_izq(client,position2d); //establecer orientación de referencia aquí theta = D_ARRIBA; inspeccionar_celda(sonar,&celdas[actual]); } else { printf("Ya estamos en la salida\n"); flag_celda_final=1; } } else { // me pongo de espaldas a la pared if(celdas[actual].pared[D_DCH]) { //si tengo pared a la dcha girar_izq(client,position2d); } else if(celdas[actual].pared[D_IZQ]) { //si tengo pared a la izda girar_dch(client,position2d); } else if(celdas[actual].pared[D_ARRIBA]) { //si la tengo en fente girar_180(client,position2d); } //establecer orientación de referencia aquí theta = D_ARRIBA; inspeccionar_celda(sonar,&celdas[actual]); } while(!(flag_celda_final || no_solucion)) { printf("celda: %d [%d,%d]\n",actual, x, y); r = mejor_ruta(celdas, actual, &forward); if (forward) { ir_direccion(client, position2d, r); actual++; inspeccionar_celda(sonar,&celdas[actual]); } else if (actual > 0) { ir_direccion(client, position2d, r); actual--; } else { no_solucion=1; printf("no hay solucion\n"); } if ((flag_celda_final = celda_final(&celdas[actual])) != 0) { printf("Fuera del laberinto!!\n\n"); printf("Camino:\n"); printf("-------\n"); for (i=0; i <= actual; i++) { for (j = 0; (j < 3) && (celdas[i].pared[j] != RUTA); j++) ; printf("(%d,%d) - %s\n", celdas[i].pos[0], celdas[i].pos[1], dirs[j]); } } } // Unsuscribe and Destroy // position2d playerc_position2d_unsubscribe(position2d); playerc_position2d_destroy(position2d); // sonar playerc_sonar_unsubscribe(sonar); playerc_sonar_destroy(sonar); // client playerc_client_disconnect(client); playerc_client_destroy(client); // End return 0; }
// Basic WSN test int test_wsn(playerc_client_t *client, int index) { int t;//, i, j; void *rdevice; playerc_wsn_t *device; printf("device [wsn] index [%d]\n", index); device = playerc_wsn_create(client, index); TEST("subscribing (read)"); if (playerc_wsn_subscribe(device, PLAYER_OPEN_MODE) == 0) PASS(); else FAIL(); for (t = 0; t < 10; t++) { TEST1("reading data (attempt %d)", t); do rdevice = playerc_client_read(client); while (rdevice == client); if (rdevice == device) { PASS(); printf("Node type: %d, with ID %d and parent %d holds:\n" "accel_{X,Y,Z} : [%f,%f,%f]\n" "magn_{X,Y,Z} : [%f,%f,%f]\n" "temperature : [%f]\n" "light : [%f]\n" "microphone : [%f]\n" "battery voltage: [%f]\n", device->node_type, device->node_id, device->node_parent_id, device->data_packet.accel_x, device->data_packet.accel_y, device->data_packet.accel_z, device->data_packet.magn_x, device->data_packet.magn_y, device->data_packet.magn_z, device->data_packet.temperature, device->data_packet.light, device->data_packet.mic, device->data_packet.battery); } else { FAIL(); break; } } TEST("setting the data frequency rate"); if(playerc_wsn_datafreq(device, -1, -1, 1) < 0) FAIL(); else { sleep(3); PASS(); } TEST("enabling all LEDs"); if(playerc_wsn_set_devstate(device, -1, -1, 3, 7) < 0) FAIL(); else { sleep(3); PASS(); } /* TEST("going to sleep"); if(playerc_wsn_power(device, 2, -1, 0) < 0) FAIL(); else { sleep(3); PASS(); } TEST("waking up"); if(playerc_wsn_power(device, 1, -1, 1) < 0) FAIL(); else { sleep(3); PASS(); } */ TEST("unsubscribing"); if (playerc_wsn_unsubscribe(device) == 0) PASS(); else FAIL(); playerc_wsn_destroy(device); return 0; }
//Prints position data of the robot, used for debugging and tracking void printPos(playerc_client_t * client, playerc_position2d_t * pos2D, playerc_bumper_t * bumper) { playerc_client_read(client); printf("\n\nStopped in position : %f %f %f bumpers: %d %d\n\n", pos2D->px, pos2D->py, pos2D->pa, bumper->bumpers[0], bumper->bumpers[1]); }
int main(int argc, const char **argv) { //Variables int degrees,PosRelX,PosRelY; float radians,Dlaser,ODM_ang, ang; int width = 500, height = 500; //Create the size of the map here (in pixel) int centroX = (width / 2); int centroY = (height / 2); playerc_client_t *client; playerc_laser_t *laser; playerc_position2d_t *position2d; CvPoint pt,pt1,pt2; CvScalar cinzaE,preto,cinzaC; char window_name[] = "Map"; IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 ); cvNamedWindow(window_name, 1 ); preto = CV_RGB(0, 0, 0); //for indicating obstacles cinzaE = CV_RGB(92, 92, 92); //To indicate the stranger cinzaC = CV_RGB(150, 150, 150); //To indicate free spaces client = playerc_client_create(NULL, "localhost", 6665); if (playerc_client_connect(client) != 0) return -1; laser = playerc_laser_create(client, 0); if (playerc_laser_subscribe(laser, PLAYERC_OPEN_MODE)) return -1; position2d = playerc_position2d_create(client, 0); if (playerc_position2d_subscribe(position2d, PLAYERC_OPEN_MODE) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } if (playerc_client_datamode (client, PLAYERC_DATAMODE_PULL) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } if (playerc_client_set_replace_rule (client, -1, -1, PLAYER_MSGTYPE_DATA, -1, 1) != 0) { fprintf(stderr, "error: %s\n", playerc_error_str()); return -1; } playerc_position2d_enable(position2d, 1); // initialise motors playerc_position2d_set_odom(position2d, 0, 0, 0); // Set odometer to zero cvSet(image, cinzaE,0); //set the image colour to dark pt.x = centroX; // Zero coordinate for x pt.y = centroY; // Zero coordinate for y while(1) { playerc_client_read(client); cvSaveImage("mapa.jpg",image); playerc_client_read(client); for (degrees = 2; degrees <= 360; degrees+=2) { Dlaser = laser->scan[degrees][0]; if (Dlaser < 8) { radians = graus2rad (degrees/2); //Convert the angle of the laser to radians ODM_ang = position2d->pa; //Obtain the angle relative to the robot ang = ((1.5*PI)+radians+ODM_ang); //Converte the angle relative to the world PosRelX = arredonda(position2d->px); //Position x relative to robot PosRelY = arredonda(position2d->py); //Position y relative to robot pt1.y = (centroY-PosRelY); //Co-ordinated global y of the robot pt1.x = (centroX+PosRelX); //Co-ordinated global x of the robot //t converts polar coordinates for rectangular (global) pt.y = (int)(pt1.y-(sin(ang)*Dlaser*10)); pt.x = (int)(pt1.x+(cos(ang)*Dlaser*10)); //The free area draws cvline cvLine(image, pt1,pt,cinzaC, 1,4,0); //marks the object in the map cvLine(image, pt,pt,preto, 1,4,0); //Shows the result of the map to the screen cvShowImage(window_name, image ); cvWaitKey(10); } } } //Disconnect player playerc_laser_unsubscribe(laser); playerc_laser_destroy(laser); playerc_client_disconnect(client); playerc_client_destroy(client); //Destroy the OpenCV window cvReleaseImage cvReleaseImage(&image); cvDestroyWindow(window_name); return 0; }
// Basic ptz test int test_ptz(playerc_client_t *client, int index) { int t; void *rdevice; playerc_ptz_t *device; double period; printf("device [ptz] index [%d]\n", index); device = playerc_ptz_create(client, index); TEST("subscribing (read)"); if (0 == playerc_ptz_subscribe(device, PLAYER_OPEN_MODE)) PASS(); else FAIL(); period = 10 / M_PI * 2; for (t = 0; t < 20; t++) { TEST1("reading data (attempt %d)", t); do rdevice = playerc_client_read(client); while (rdevice == client); if (rdevice == device) { PASS(); printf("ptz: [%d %d %d]\n", (int) (device->pan * 180 / M_PI), (int) (device->tilt * 180 / M_PI), (int) (device->zoom * 180 / M_PI)); } else { FAIL(); break; } TEST1("writing data (attempt %d)", t); if (playerc_ptz_set(device, sin(t / period) * M_PI / 2, sin(t / period) * M_PI / 3, (1 - t / 20.0) * M_PI) != 0) { FAIL(); break; } PASS(); } TEST1("writing data (attempt %d)", t); if (playerc_ptz_set(device, 0, 0, M_PI) != 0) FAIL(); else PASS(); TEST("unsubscribing"); if (playerc_ptz_unsubscribe(device) == 0) PASS(); else FAIL(); playerc_ptz_destroy(device); return 0; }
// Basic test for log device. int test_log(playerc_client_t *client, int index) { int t; // int isplayback=0; playerc_log_t *device; printf("device [log] index [%d]\n", index); device = playerc_log_create(client, index); TEST("subscribing (read)"); if (playerc_log_subscribe(device, PLAYER_OPEN_MODE) != 0) { FAIL(); return -1; } PASS(); TEST("getting log state"); if(playerc_log_get_state(device) != 0) { FAIL(); return -1; } PASS(); if(device->type == PLAYER_LOG_TYPE_WRITE) { TEST("starting logging"); if(playerc_log_set_write_state(device,1) != 0) { FAIL(); return -1; } PASS(); } else { TEST("rewinding logfile"); if(playerc_log_set_read_rewind(device) != 0) { FAIL(); return -1; } PASS(); TEST("starting playback"); if(playerc_log_set_read_state(device,1) != 0) { FAIL(); return -1; } PASS(); } TEST("getting log state"); if(playerc_log_get_state(device) != 0) { FAIL(); return -1; } if(device->state != 1) { FAIL(); return -1; } PASS(); // let it log/playback TEST("logging/playback proceeding"); for(t=0;t<50;t++) { if(!playerc_client_read(client)) { FAIL(); return -1; } } PASS(); if(device->type == PLAYER_LOG_TYPE_WRITE) { TEST("stopping logging"); if(playerc_log_set_write_state(device,0) != 0) { FAIL(); return -1; } PASS(); } else { TEST("stopping playback"); if(playerc_log_set_read_state(device,0) != 0) { FAIL(); return -1; } PASS(); } TEST("getting log state"); if(playerc_log_get_state(device) != 0) { FAIL(); return -1; } if(device->state != 0) { FAIL(); return -1; } PASS(); TEST("unsubscribing"); if (playerc_log_unsubscribe(device) != 0) { FAIL(); return -1; } PASS(); playerc_log_destroy(device); return 0; }
// Basic localize test int test_localize(playerc_client_t *client, int index) { int t, i; void *rdevice; playerc_localize_t *device; //double min, max; //int resolution, intensity; printf("device [localize] index [%d]\n", index); device = playerc_localize_create(client, index); TEST("subscribing (read)"); if (playerc_localize_subscribe(device, PLAYER_READ_MODE) == 0) PASS(); else { FAIL(); return(-1); } // deprecated: get map from map interface now #if 0 TEST("get map"); if (playerc_localize_get_map(device) == 0) PASS(); else FAIL(); #endif /* FIX TEST("set configuration"); min = -M_PI/2; max = +M_PI/2; resolution = 100; intensity = 1; if (playerc_localize_set_config(device, min, max, resolution, intensity) == 0) PASS(); else FAIL(); TEST("get configuration"); if (playerc_localize_get_config(device, &min, &max, &resolution, &intensity) == 0) PASS(); else FAIL(); TEST("check configuration sanity"); if (abs(min + M_PI/2) > 0.01 || abs(max - M_PI/2) > 0.01) FAIL(); else if (resolution != 100 || intensity != 1) FAIL(); else PASS(); */ for (t = 0; t < 10; t++) { TEST1("reading data (attempt %d)", t); do rdevice = playerc_client_read(client); while (rdevice == client); if (rdevice == device) { PASS(); printf("localize: [%d %14.3f] [%d] ", device->pending_count, device->pending_time, device->hypoth_count); for (i = 0; i < device->hypoth_count; i++) printf("[%6.3f, %6.3f %6.3f] ", device->hypoths[i].mean[0], device->hypoths[i].mean[1], device->hypoths[i].mean[2]); printf("\n"); } else { FAIL(); break; } } TEST("unsubscribing"); if (playerc_localize_unsubscribe(device) == 0) PASS(); else FAIL(); playerc_localize_destroy(device); return 0; }
// Basic test for position3d device. int test_position3d(playerc_client_t *client, int index) { int t; void *rdevice; playerc_position3d_t *device; printf("device [position3d] index [%d]\n", index); device = playerc_position3d_create(client, index); TEST("subscribing (read/write)"); if (playerc_position3d_subscribe(device, PLAYER_OPEN_MODE) < 0) { FAIL(); return -1; } PASS(); #if 0 // TODO TEST("getting geometry"); if (playerc_position3d_get_geom(device) == 0) PASS(); else FAIL(); printf("position3d geom: [%6.3f %6.3f %6.3f] [%6.3f %6.3f]\n", device->pose[0], device->pose[1], device->pose[2], device->size[0], device->size[1]); TEST("enabling motors"); if (playerc_position3d_enable(device, 1) == 0) PASS(); else FAIL(); #endif for (t = 0; t < 300; t++) { TEST1("reading data (attempt %d)", t); do rdevice = playerc_client_read(client); while (rdevice == client); if (rdevice == device) { PASS(); printf("position3d: [%14.3f] [%+7.3f %+7.3f %+7.3f] [%+7.3f %+7.3f %+7.3f]\n", device->info.datatime, device->pos_x, device->pos_y, device->pos_z, device->pos_roll * 180/M_PI, device->pos_pitch * 180/M_PI, device->pos_yaw * 180/M_PI); } else { FAIL(); break; } #if 0 // TODO TEST1("writing data (attempt %d)", t); if (playerc_position3d_set_velocity(device, 0.10, 0, 0, 0, 0, 0.2, 1) != 0) { FAIL(); break; } PASS(); #endif } #if 0 // TODO TEST1("writing data (attempt %d)", t); if (playerc_position3d_set_velocity(device, 0, 0, 0, 0, 0, 0, 1) != 0) FAIL(); else PASS(); TEST("disabling motors"); if (playerc_position3d_enable(device, 0) == 0) PASS(); else FAIL(); TEST("unsubscribing"); if (playerc_position3d_unsubscribe(device) != 0) FAIL(); else PASS(); #endif playerc_position3d_destroy(device); return 0; }