Skip to content

Commit

Permalink
VoiceControlRobot
Browse files Browse the repository at this point in the history
  • Loading branch information
raghavbhatnagar96 committed Mar 15, 2015
0 parents commit 9de0bdd
Showing 1 changed file with 262 additions and 0 deletions.
262 changes: 262 additions & 0 deletions voice.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libplayerc/playerc.h>
#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: <s> DIAL ONE </s>\n", line) == 0)
{

choice=1;
break;
}

if (strcmp("sentence1: <s> DIAL TWO </s>\n", line) == 0)
{
choice=2;
break;
}
if (strcmp("sentence1: <s> DIAL THREE </s>\n", line) == 0)
{
choice=3;
break;
}
if (strcmp("sentence1: <s> DIAL FOUR </s>\n", line) == 0)
{
choice=4;
break;

}
if (strcmp("sentence1: <s> DIAL FIVE </s>\n", line) == 0)
{
choice=5;
break;
}
if (strcmp("sentence1: <s> DIAL SIX </s>\n", line) == 0)
{
choice=6;
break;
}
if (strcmp("sentence1: <s> DIAL SEVEN </s>\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;
}





0 comments on commit 9de0bdd

Please sign in to comment.