Download this file

401 lines (325 with data), 9.7 kB

  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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*******************************************************************************
UDP program, that receives packets from any host on UDP port 8888,then
echoes the received packet back to the sender.
The use of a UDP data handler is the fastest
"official" way to implement UDP-based request/response servers.
*******************************************************************************/
#class auto
#define TCPCONFIG 1
#define MAX_UDP_SOCKET_BUFFERS 1
#define DISABLE_TCP // Not using TCP
#define LOCAL_PORT 8888
#memmap xmem
#use "dcrtcp.lib"
#use "BLxS2xx.lib"
// PWM inversion
#define PWM_INVERT 0
// base PWM frequency
#define PWM_FREQ 50000
//Defining motors pwm ports
#define motorX 0
#define motorY 1
#define motorZ 2
udp_Socket sock;
char pktbuf[1024]; // Temp root buffer for packet reassembly
int i, j, k, length;
char prov_command[6];
int int_commands[6];
int command_received;
float distance;
word counter;
int emergencyBreak;
longword remoteIP;
word remotePort;
int datagramLength;
cofunc void pulse(int motor, int mmFactor){
pulseEnable(motor);
waitfor(DelayMs(mmFactor));
pulseDisable(motor,0);
}
cofunc int readZDistance(){
//Ultrasonic Routine to get distance measure
//Sending Pulse to Ultrasonic Sensor
digOut(6,1);
waitfor(DelayMs(1));
digOut(6,0);
while(digIn(7)<1){
}
pulseEnable(8);
while(digIn(7)){
}
pulseDisable(8,0);
getCounter(9, &counter);
resetCounter(9);
distance = counter*2/5.83;
//Cleaning Buffer
memset(&pktbuf,0, sizeof(pktbuf));
//Converting float (distance) to string and saving it on pktbuf variable
sprintf(pktbuf, "%.2f", distance);
}
cofunc void adjustZHeight(int desiredZHeight){
float dif;
float actualZHeight;
int mmFactorZ;
wfd readZDistance();
actualZHeight= distance;
dif = actualZHeight - desiredZHeight;
if(dif >= 0){
digOut(5,0); //Setting motor Z direction to go down
}else{
digOut(5,1); //Setting motor Z direction to go up
dif = dif*(-1);
}
mmFactorZ = dif*1200;
wfd pulse(2,mmFactorZ);
}
void stopMotors(){
emergencyBreak = 1;
pulseDisable(0,0);
pulseDisable(1,0);
pulseDisable(2,0);
}
cofunc void moveToInitialPosition(){
int flagMotorX;
int flagMotorY;
flagMotorX=0;
flagMotorY=0;
//Setting motors direction
digOut(3,1);
digOut(4,1);
if (digIn(10)<1){
pulseEnable(motorX);
flagMotorX=1;
}
if (digIn(11)<1){
pulseEnable(motorY);
flagMotorY=1;
}
while( flagMotorX||flagMotorY ){
if (digIn(10)==1){
pulseDisable(motorX,0);
flagMotorX=0;
}
if (digIn(11)==1){
pulseDisable(motorY,0);
flagMotorY=0;
}
}
}
cofunc void manualMovement(int motor, int direction, int displacement){
int mmFactor;
if (motor == 0){
setFreq(0,100);
//Setting motor X direction
digOut(3, direction);
//Setting mmFactor for motor X
mmFactor = displacement*800;
}else if (motor == 1){
setFreq(1,70);
digOut(4,direction);
mmFactor = displacement*78;
}else if (motor == 2){
setFreq(2,50);
digOut(5,direction);
mmFactor = displacement*1200;
}
wfd pulse(motor,mmFactor);
}
cofunc void automaticMovement(int displacementX, int displacementY, int m, int n){
int r;
int c;
int directionX;
int directionY;
int mmFactorX;
int mmFactorY;
emergencyBreak = 0;
directionX = 0; //moving to the right
directionY = 0; //moving up
mmFactorX = displacementX*800;
mmFactorY = displacementY*78;
//setting motor X direction to moving left
digOut(3,directionX);
for(r=0; r<=n; r++){
wfd readZDistance();
pktbuf[7]=',';
udp_sendto(&sock, pktbuf, datagramLength, remoteIP, 8889);
//Setting motor Y direction
digOut(4,directionY);
setFreq(motorY,70);
for (c=1; c<=m; c++){
wfd pulse(motorY,mmFactorY);
//taking sample
wfd readZDistance();
if (c!=m) pktbuf[7]=',';
else{
pktbuf[7]='\n';
}
waitfor(DelayMs(1000));
udp_sendto(&sock, pktbuf, datagramLength, remoteIP, 8889);
if (emergencyBreak == 1) break;
}
if (emergencyBreak==1) break;
if(directionY == 0) directionY = 1;
else directionY = 0;
if (r!=n){
setFreq(motorX,100);
wfd pulse(motorX,mmFactorX);
}
}
memset(&pktbuf,0, sizeof(pktbuf));
pktbuf[0]='0';
udp_sendto(&sock, pktbuf, datagramLength, remoteIP, 8889);
}
cofunc offsetMovement(int displacementX, int displacementY){
emergencyBreak = 0;
if (digIn(10)==1){
wfd manualMovement(motorX,0,displacementX);
}
if ( (emergencyBreak != 1) && (digIn(11)==1) ){
wfd manualMovement(motorY,0,displacementY);
}
}
void getCommands(char command[]){
i=0;
k=0;
j=0;
memset(&int_commands[0], 0, sizeof(int_commands));
length = strlen(command);
for (i=0; i<length; i++){
if( (command[i] != 'x') ){
prov_command[j] = command[i];
j++;
}else{
int_commands[k] = atoi(prov_command);
memset(&prov_command[0], 0, sizeof(prov_command));
j=0;
k++;
}
}
switch (int_commands[0]){
case 0: printf("Movimiento Manual...\n"); break;
case 1: printf("Escanear Area...\n"); break;
case 2: printf("Movimiento Offset...\n"); break;
case 3: printf("Ajustar Altura Eje Z...\n"); break;
case 4: printf("Leer Distancia...\n"); break;
case 5: printf("Detener Motores...\n"); break;
case 6: printf("Mover a Posición Inicial...\n"); break;
default :
}
printf("Parámetros: %d\n", k);
printf("p[0]: %d\n", int_commands[0]);
printf("p[1]: %d\n", int_commands[1]);
printf("p[2]: %d\n", int_commands[2]);
printf("p[3]: %d\n", int_commands[3]);
printf("p[4]: %d\n\n", int_commands[4]);
command_received=1;
}
int echo_handler(int event, udp_Socket * s, ll_Gather * g,
_udp_datagram_info * udi)
{
remoteIP = udi->remip;
datagramLength = g->len2+g->len3;
memset(&pktbuf,0, sizeof(pktbuf));
if (event == UDP_DH_ICMPMSG) {
return 1; // Just ignore incoming ICMP errors.
}
xmem2root(pktbuf, (long)g->data2, g->len2);
xmem2root(pktbuf + g->len2, (long)g->data3, g->len3);
printf("Datos recibidos: %s\n", pktbuf);
getCommands(pktbuf);
if(strcmp(pktbuf,"4x ")!=0){
//Sending data to the sender of the packet
udp_sendto(s, pktbuf,g->len2+g->len3, udi->remip, udi->remport);
}
//Return 1 to indicate that all processing has been done.
return 1;
}
void main()
{
int channel;
command_received=0;
distance=0;
// Initialize the controller
brdInit();
//Setting PWM's
// Start PWM for motor X with 100 Hz frecuency and duty cycle of 50%
setPWM(0, 100,50,PWM_INVERT,0);
pulseDisable(0,0);
// Start PWM for motor Y with 70 Hz frecuency and duty cycle of 50%
setPWM(1,70,50,PWM_INVERT,0);
pulseDisable(1,0);
// Start PWM for motor Z with 50 Hz frecuency and duty cycle of 50%
setPWM(2,50,50,PWM_INVERT,0);
pulseDisable(2,0);
//Setting DIO 3-6 as Outputs
/*
DIO3 -> direction motor X
DIO4 -> direction motor Y
DIO5 -> direction motor Z
DIO6 -> ultrasonic sensor output
*/
for(channel = 3; channel < 7; ++channel)
{
// Set I/O to be general digital output
setDigOut(channel, 0);
}
//Setting DIO7 as digital input
//DIO7 -> ultrasonic sensor input
setDigIn(7);
//Setting DIO8 as PWM
setPWM(8,PWM_FREQ,50, PWM_INVERT,0);
pulseDisable(8,0);
//Setting DIO9 as counter
setCounter(9, BL_UP_COUNT, BL_EDGE_RISE, BL_EDGE_RISE);
//Setting DIO 10-12 as Inputs
/*
DIO10 -> switch motor X
DIO11 -> switch motor Y
DIO12 -> switch motor Z
*/
for(channel = 10; channel < 13; ++channel)
{
// Set I/O to be general digital output
setDigIn(channel);
}
// Start network and wait for interface to come up (or error exit).
sock_init_or_exit(1);
if(!udp_extopen(&sock, IF_ANY, LOCAL_PORT, -1L, 0, echo_handler, 0, 0)) {
printf("!Error al abrir puerto UDP!\n");
exit(0);
}
/* Let the stack do everything... */
for(;;){
costate{
tcp_tick(NULL);
}
costate{
if (command_received==1){
command_received=0;
if(int_commands[0]==0){
wfd manualMovement(int_commands[1], int_commands[2], int_commands[3]);
}else if(int_commands[0]==1){
wfd automaticMovement(int_commands[1], int_commands[2], int_commands[3],int_commands[4]);
}else if(int_commands[0]==2){
wfd offsetMovement(int_commands[1], int_commands[2]);
}else if(int_commands[0]==3){
wfd adjustZHeight(int_commands[1]);
}else if(int_commands[0]==4){
//Sending Distance to GUI:
wfd readZDistance();
udp_sendto(&sock, pktbuf, datagramLength, remoteIP, 8888);
}else if (int_commands[0] == 5){
stopMotors();
}else if(int_commands[0]==6){
wfd moveToInitialPosition();
}
}
}
costate{
if(command_received==1){
command_received = 0;
if (int_commands[0] == 5) stopMotors();
}
}
}
}