-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller for receiver robotic car
290 lines (233 loc) · 8.5 KB
/
Controller for receiver robotic car
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* File: purple_2_0.c
* Date: 08/07/2020
* Description: custom made for purple bot v2.
* Author: Azeem.
* Modifications:
*/
/*
* You may need to add include files like <webots/distance_sensor.h> or
* <webots/motor.h>, etc.
*/
#include <webots/robot.h>
#include <webots/distance_sensor.h>
#include <webots/motor.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <webots/receiver.h>
#include <webots/supervisor.h>
// black line = 490+
// white surface = 105-
/*
* You may want to add macros here.
*/
#define TIME_STEP 64
#define COMMUNICATION_CHANNEL 1
#define Communication_channel_b 2
/*
* This is the main program.
* The arguments of the main function can be specified by the
* "controllerArgs" field of the Robot node
*/
int main(int argc, char **argv) {
/* necessary to initialize webots stuff */
wb_robot_init();
// do this once only
WbNodeRef robot_node = wb_supervisor_node_get_from_def("Purple");
WbFieldRef trans_field = wb_supervisor_node_get_field(robot_node, "translation");
//internal Variables
int i,j=0,k=0,A=0;
int message_printed = 0,message_printed2 = 0;
// setup receiver
WbDeviceTag communication = wb_robot_get_device("receiver");
wb_receiver_enable(communication, TIME_STEP);
//setup Emitter
WbDeviceTag communicationb = wb_robot_get_device("Purple Receiver");
wb_receiver_enable(communicationb ,TIME_STEP);
// initialise distance sensors
WbDeviceTag DSR = wb_robot_get_device("ds_right");
wb_distance_sensor_enable(DSR, TIME_STEP);
WbDeviceTag DSL = wb_robot_get_device("ds_left");
wb_distance_sensor_enable(DSL, TIME_STEP);
// initialise motors
WbDeviceTag wheels[4];
char wheels_names[4][8] = {"wheel1", "wheel2", "wheel3", "wheel4"};
for (i = 0; i < 4; i++) {
wheels[i] = wb_robot_get_device(wheels_names[i]);
wb_motor_set_position(wheels[i], INFINITY);
wb_motor_set_velocity(wheels[i], 0.0);
}
// wb_motor_set_velocity(wheels[0], 0.0);
// wb_motor_set_velocity(wheels[1], 0.0);
// wb_motor_set_velocity(wheels[2], 0.0);
// wb_motor_set_velocity(wheels[3], 0.0);
// Initialize other sensors
WbDeviceTag lsensor = wb_robot_get_device("S2");
wb_distance_sensor_enable(lsensor, TIME_STEP);
WbDeviceTag Rsensor = wb_robot_get_device("S3");
wb_distance_sensor_enable(Rsensor, TIME_STEP);
WbDeviceTag lstopsensor = wb_robot_get_device("SS1");
wb_distance_sensor_enable(lstopsensor, TIME_STEP);
WbDeviceTag Rstopsensor = wb_robot_get_device("SS2");
wb_distance_sensor_enable(Rstopsensor, TIME_STEP);
// Define various functions
void Forward()
{
double lspeed = 4.0;
double rspeed = 4.0;
// write actuators inputs
wb_motor_set_velocity(wheels[0], lspeed);
wb_motor_set_velocity(wheels[1], rspeed);
wb_motor_set_velocity(wheels[2], lspeed);
wb_motor_set_velocity(wheels[3], rspeed);
}
void turnleft()
{
double lspeed = -2.0;
double rspeed = 4.0;
// write actuators inputs
wb_motor_set_velocity(wheels[0], lspeed);
wb_motor_set_velocity(wheels[1], rspeed);
wb_motor_set_velocity(wheels[2], lspeed);
wb_motor_set_velocity(wheels[3], rspeed);
}
void turnright()
{
double lspeed = 4.0;
double rspeed = -2.0;
// write actuators inputs
wb_motor_set_velocity(wheels[0], lspeed);
wb_motor_set_velocity(wheels[1], rspeed);
wb_motor_set_velocity(wheels[2], lspeed);
wb_motor_set_velocity(wheels[3], rspeed);
}
void Stop()
{
double lspeed = 0.0;
double rspeed = 0.0;
// write actuators inputs
wb_motor_set_velocity(wheels[0], lspeed);
wb_motor_set_velocity(wheels[1], rspeed);
wb_motor_set_velocity(wheels[2], lspeed);
wb_motor_set_velocity(wheels[3], rspeed);
}
/*
* You should declare here WbDeviceTag variables for storing
* robot devices like this:
* WbDeviceTag my_sensor = wb_robot_get_device("my_sensor");
* WbDeviceTag my_actuator = wb_robot_get_device("my_actuator");
*/
/* main loop
* Perform simulation steps of TIME_STEP milliseconds
* and leave the loop when the simulation is over
*/
while (wb_robot_step(TIME_STEP) != -1) {
const double *Svalues = wb_supervisor_field_get_sf_vec3f(trans_field);
// printf("My_Purple_ROBOT is at position: %g %g %g\n", Svalues[0], Svalues[1], Svalues[2]);
// fprintf(fptr,"%g, %g, %g\n", Svalues[0], Svalues[1], Svalues[2]);
// this is done repeatedly
const double lvalue = wb_distance_sensor_get_value(lsensor);
const double Rvalue = wb_distance_sensor_get_value(Rsensor);
const double lstopvalue = wb_distance_sensor_get_value(lstopsensor);
const double Rstopvalue = wb_distance_sensor_get_value(Rstopsensor);
const double DSRV = wb_distance_sensor_get_value(DSR);
const double DSLV = wb_distance_sensor_get_value(DSL);
// printf("Left Sensor value is %f\n , Right Sensor value is %f\n", lvalue , Rvalue);
// printf("DSLvalue is %f\n , DSRvalue is %f\n", DSLV , DSRV );
/// For Receiving Red bot signal
if (wb_receiver_get_queue_length(communication) > 0) {
/* read current packet's data */
const char *buffer = wb_receiver_get_data(communication);
if (message_printed != 1) {
/* print null-terminated message */
printf("Communication Inbound: \"%s\"\n", buffer);
printf("Purple Robot En-Route to Starting Point\n");
message_printed = 1;
A = 1;
j = 1;
const double *Svalues = wb_supervisor_field_get_sf_vec3f(trans_field);
// printf("My_Purple_ROBOT is at position: %g %g %g\n", Svalues[0], Svalues[1], Svalues[2]);
// fprintf(fptr,"%g, %g, %g\n", Svalues[0], Svalues[1], Svalues[2]);
}
/* fetch next packet */
wb_receiver_next_packet(communication);
} else {
if (message_printed != 2) {
printf("No Inbound Communication! \n");
message_printed = 2;
// j = 0;
}
}
/// For Receiving Blue bot signal
if (wb_receiver_get_queue_length(communicationb) > 0) {
/* read current packet's data */
const char *buffer2 = wb_receiver_get_data(communicationb);
if (message_printed2 != 1) {
/* print null-terminated message */
printf("Communication Inbound: \"%s\"\n", buffer2);
printf("Purple Robot En-Route to Starting Point\n");
message_printed2 = 1;
k = 1;
A = 1;
const double *Svalues = wb_supervisor_field_get_sf_vec3f(trans_field);
// printf("My_Purple_ROBOT is at position: %g %g %g\n", Svalues[0], Svalues[1], Svalues[2]);
// fprintf(fptr,"%g, %g, %g\n", Svalues[0], Svalues[1], Svalues[2]);
}
/* fetch next packet */
wb_receiver_next_packet(communicationb);
} else {
if (message_printed2 != 2) {
printf("No Inbound Communication! \n");
message_printed2 = 2;
j = 0;
}
}
if(DSLV <= 850 || DSRV <= 850)
{ turnright();}
else{
if(lvalue <= 105 && Rvalue <= 105 && A == 1) //Go forward
{ Forward();}
if(lvalue >= 110 && Rvalue <= 105 ) // turn left
{ turnleft();}
if(lvalue >= 110 && Rvalue >= 110 && j == 1) //Left turn cond 2
{ turnleft();}
if(lvalue >= 110 && Rvalue >= 110) //Right turn cond 2
{ turnright();}
if(lvalue <= 105 && Rvalue >= 110 ) //Right turn
{ turnright();}
if (lvalue >= 110 && Rvalue >=110 && DSLV <= 999 && DSRV <= 999) //Stop
{ Stop();
// fclose(fptr);
if (message_printed == 0) {
/* print null-terminated message */
printf("Purple bot has reached the initial position of Red Bot \n");
message_printed = 1;
}
else {
if (message_printed == 1) {
printf("Blue bot is En-Route to Common collection Point \n");
message_printed = 2;
}
}
const char *message = "Move To Point A";
wb_emitter_send(communicationb, message, strlen(message) + 1);
}
}
/*
* Read the sensors :
* Enter here functions to read sensor data, like:
* double val = wb_distance_sensor_get_value(my_sensor);
*/
/* Process sensor data here */
/*
* Enter here functions to send actuator commands, like:
* wb_motor_set_position(my_actuator, 10.0);
*/
};
/* Enter your cleanup code here */
/* This is necessary to cleanup webots resources */
wb_robot_cleanup();
return 0;
}