/
move3.c
211 lines (192 loc) · 5.53 KB
/
move3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Include header files
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <libplayerc/playerc.h>
#include "playerc.h"
// Random numbers generated for random walk movement by robots
enum bumper{left=0,right};
playerc_bumper_t *bumper;
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*10 , 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)
/*{
int i;
//for (i = 0; i<4; 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 , DTOR(100),0))
return -1;
usleep(1000);
//}
playerc_client_read(client);
if (0 != playerc_position2d_set_cmd_vel(position2d, 0,0 , 0.5,0))
return -1;
while(0 != playerc_position2d_set_cmd_vel(position2d, 0,0 , 0,1))
break;
}*/
{
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(47),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)
/*{
int i;
for (i = 0; i<4; 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,0))
return -1;
usleep(1000);
}
playerc_client_read(client);
if (0 != playerc_position2d_set_cmd_vel(position2d, 0,0 ,-1*0.5,0))
return -1;
while(0 != playerc_position2d_set_cmd_vel(position2d, 0,0 , 0,1))
break;
}*/
{
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(46),1);
sleep(1);
playerc_position2d_set_cmd_vel(position2d, 0, 0, 0, 1);
sleep(1);
}
int main(int argc, const char **argv)
{
int history[500];
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,bump1,bump2,bumpcount=0;
bumper = playerc_bumper_create(client, 0);
if (playerc_bumper_subscribe(bumper, PLAYER_OPEN_MODE))
return -1;
playerc_client_read(client);
playerc_bumper_get_geom(bumper);
usleep(10000);
printf("%d %d\n",(int)bumper->bumpers[0],(int)bumper->bumpers[1]);
do
{
count+=1;
printf("\n8.Straight 4.Left 6.Right 2.Reverse 5.exit\n");
scanf("%d",&choice);
history[count-1]=choice;
switch(choice)
{
case 8 : {move_straight(1,client,position2d);break;}
case 4 : {turn_left(client,position2d);break;}
case 6 : {turn_right(client,position2d);break;}
case 2 : {move_straight(-1,client,position2d);break;}
case 5 : break;
default : {printf("Wrong Choice!!!!!\n"); break;}
}
playerc_client_read(client);
playerc_bumper_get_geom(bumper);
if(bumpcount == 0)
{
bump1 = (int)bumper->bumpers[0];
bump2 = (int)bumper->bumpers[1];
bumpcount+=1;
}
if((bump1 != (int)bumper->bumpers[0]) || (bump2 != (int)bumper->bumpers[1]))
{
choice=5;
}
printf("%d %d \n",(int)bumper->bumpers[0],(int)bumper->bumpers[1]);
//printf("\nFurther <0-yes> : ");
//scanf("%d",&ch);
}while(choice != 5 );
/*do
{
move_straight(client,position2d);
printf("\nDo You Want To Go Straight <0-yes> ? :");
scanf("%d",&choice);
}while(choice == 0);*/
printf("\n\tRETRACING ITS PATH \n");
for(i=count-1;i>=0;i--)
{
switch(history[i])
{
case 2 : {move_straight(1,client,position2d);break;}
case 6 : {turn_left(client,position2d);break;}
case 4 : {turn_right(client,position2d);break;}
case 8 : {move_straight(-1,client,position2d);break;}
}
}
playerc_position2d_set_cmd_vel(position2d,0,0,0,0);
sleep(1);
//while(1)
//{
// if (0 != playerc_position2d_set_cmd_vel(position2d, 0,0 , 0,1))
// return -1;
//}
playerc_position2d_unsubscribe(position2d);
playerc_bumper_unsubscribe(bumper);
playerc_position2d_destroy(position2d);
playerc_bumper_destroy(bumper);
playerc_client_disconnect(client);
playerc_client_destroy(client);
return 0;
}