From 9de0bdd6730ec4a1775678e8abd0e68678bde78c Mon Sep 17 00:00:00 2001 From: Raghav Bhatnagar Date: Mon, 16 Mar 2015 03:50:01 +0530 Subject: [PATCH] VoiceControlRobot --- voice.c | 262 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 262 insertions(+) create mode 100644 voice.c diff --git a/voice.c b/voice.c new file mode 100644 index 0000000..90cdf97 --- /dev/null +++ b/voice.c @@ -0,0 +1,262 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "playerc.h" +// Random numbers generated for random walk movement by robots +int read() +{ int choice=0; + FILE *in; + char line[511]; + in = fopen("input.dat", "r"); + while(!feof(in)) + { + fgets(line, 511 , in); + if (strcmp("sentence1: DIAL ONE \n", line) == 0) + { + + choice=1; + break; + } + + if (strcmp("sentence1: DIAL TWO \n", line) == 0) + { + choice=2; + break; + } + if (strcmp("sentence1: DIAL THREE \n", line) == 0) + { + choice=3; + break; + } + if (strcmp("sentence1: DIAL FOUR \n", line) == 0) + { + choice=4; + break; + + } + if (strcmp("sentence1: DIAL FIVE \n", line) == 0) + { + choice=5; + break; + } + if (strcmp("sentence1: DIAL SIX \n", line) == 0) + { + choice=6; + break; + } + if (strcmp("sentence1: DIAL SEVEN \n", line) == 0) + { + choice=7; + break; + } + + } + fclose(in); + return choice; +} + + +char* getFileCreationTime(char *path) +{ + struct stat attr; + stat(path, &attr); + return ctime(&attr.st_mtime); +} + +void timeDelay (int secs)//time delay by s seconds +{ + int retTime = time(0) + secs; + while (time(0) < retTime); +} + +int exists(const char *filename) +{ + struct stat buffer; + return (stat (filename, &buffer) == 0); +} + +void align(int x,playerc_position2d_t *position2d) +{ +int i; +i = 0; + +while(i<1) +{ +playerc_position2d_set_cmd_vel(position2d, 0, 0, x*(-1*DTOR(6)), 1); +sleep(1); +playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1); +sleep(1); +i+=1; +} +} + + +void move_straight(int x,playerc_client_t *client,playerc_position2d_t *position2d) +{ +static int count = 1; +int i = 0; +while(i<1) +{ + // 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 + playerc_position2d_set_cmd_vel(position2d, x , 0 , 0 , 1 ); + sleep(1); +playerc_position2d_set_cmd_vel(position2d, 0 , 0 , 0 , 0 ); +i+=1; +} +//playerc_position2d_set_cmd_vel(position2d, 0 , 0 , 0 , 1 ); +sleep(1); +if(count%3 == 0) +{ +align(x,position2d); +fprintf(stdout,"aligned"); +} +count += 1; +playerc_position2d_set_cmd_vel(position2d, 0 , 0 , 0 , 0 ); + + +} + + + +int turn_left(playerc_client_t *client,playerc_position2d_t *position2d) + +{ + playerc_client_read(client); + fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n",position2d->px, position2d->py, position2d->pa); + playerc_position2d_set_cmd_vel(position2d, 0,0 , DTOR(92),1); + sleep(1); + playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1); + sleep(1); +} + + + + +int turn_right(playerc_client_t *client,playerc_position2d_t *position2d) + +{ + playerc_client_read(client); + fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n",position2d->px, position2d->py, position2d->pa); + playerc_position2d_set_cmd_vel(position2d, 0,0 , -1*DTOR(92),1); + sleep(1); + playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1); + sleep(1); +} +int rotate120(playerc_client_t *client,playerc_position2d_t *position2d) +{ + playerc_client_read(client); + fprintf(stdout, "X: %3.2f, Y: %3.2f, Yaw: %3.2f \n",position2d->px, position2d->py, position2d->pa); + playerc_position2d_set_cmd_vel(position2d, 0,0 , -1*DTOR(122),1); + sleep(1); + playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1); + sleep(1); +} + +int square(playerc_client_t *client,playerc_position2d_t *position2d) +{ + playerc_client_read(client); + move_straight(1,client,position2d); + turn_left(client,position2d); + + move_straight(1,client,position2d); + + turn_left(client,position2d); + move_straight(1,client,position2d); + + turn_left(client,position2d); + move_straight(1,client,position2d); + + turn_left(client,position2d); + sleep(1); +} + + +int triangle(playerc_client_t *client,playerc_position2d_t *position2d) +{ + playerc_client_read(client); + move_straight(1,client,position2d); + rotate120(client,position2d); + move_straight(1,client,position2d); + rotate120(client,position2d); + move_straight(1,client,position2d); + + sleep(1); +} + +int main(int argc, const char **argv) +{ + while(1) + { + char* time = getFileCreationTime("input.dat"); + int i,count=0; + 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)) + { + fprintf(stdout,"Error!!"); + 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; + int choice,ch; + + playerc_client_read(client); + if(exists("input.dat") + { + if(strcmp(time, getFileCreationTime("input.dat")); + { + choice=read(); + switch(choice) + { + case 1 : {move_straight(1,client,position2d);break;} + case 2 : {turn_left(client,position2d);break;} + case 3 : {turn_right(client,position2d);break;} + case 4 : {move_straight(-1,client,position2d);break;} + case 5 : {square(client,position2d);break;} + case 6 : {triangle(client,position2d);break;} + case 7 : break; + default : {printf("Wrong Choice!!!!!\n"); break;} + } + playerc_client_read(client); + playerc_position2d_set_cmd_vel(position2d,0,0,0,0); + sleep(1); + + timeDelay(2); + } + else + { + timeDelay(5); + } + } + else + { + timeDelay(5) + } + } +playerc_position2d_unsubscribe(position2d); + +playerc_position2d_destroy(position2d); + +playerc_client_disconnect(client); +playerc_client_destroy(client); +return 0; +} + + + + +