DAS  3.1.6 - 18/09/2017
Dxl.c
Go to the documentation of this file.
1 #include <windows.h>
2 #include <stdio.h>
3 #include <math.h>
4 #include "mgui.h"
5 #include "DAS_Spatram.h"
6 #include "dcl.h"
7 #include "dil.h"
8 #include "bil.h"
9 #include "dscud.h"
10 #include "dxl.h"
11 #include "wsc.h"
12 
13 
19 //cpl CPL; //Control Panel
20 flag FLAG; //flags structure
21 //stepm STEPM; //Stepper Motors
23 
24 /* 1) Assign the AMSx Address */
25 void AMS_assadd(int w)
26 {
27  DXL.add = w;
28 }
29 
30 /* 2) Test positioning */
31 int AMS_TestPos(int id, int motor)
32 {
33 // char buf[80];
34  int d, er;
35  long ns;
36  //Assign Address
37  AMS_assadd(id);
38 
39 // if (steps > period)
40 // {
41 // nv = steps / period;
42 // }
43 // k = 1;
44  do
45  {
46 // if(k)
47 
48  sprintf(DXL.buftx, "8%02X", motor);
49  DXL_TxStr(DXL.buftx, 3);
50  d = STS0LEN;
51  er = DXL_RxStr(DXL.bufrx, &d);
52  ns = AMS_ReadStep(id, motor);
54  //}while(DXL.bufrx[0] == '0' && DXL.bufrx[1] == '0' && er == 0);
55  }while(DXL.bufrx[0] == '0' && DXL.bufrx[1] == '0' && er == 0 && ns != 0);
56 
57  return er;
58 }
59 /* 3) Read motor step */
60 unsigned long AMS_ReadStep(int id, int motor)
61 {
62 
63  char buf[80];
64  int d, er;
65  long ns;
66 
67  //Assign Address
68  AMS_assadd(id);
69 
70  sprintf(DXL.buftx, "1%02X", motor);
71  DXL_TxStr(DXL.buftx, 3);
72  d = 15;
73  er = DXL_RxStr(buf, &d);
74  if(er == 0)
75  {
76  sscanf(buf, "%lx", &ns);
77  return ns;
78  }
79 
80  return 0;
81 }
82 
83 unsigned char DXL_HexBin(char *str)
84 {
85  unsigned char ch;
86  if(str[0] <= '9')
87  ch = (str[0] - '0') << 4;
88  else
89  ch = (str[0] - '0' - 7) << 4;
90  if(str[1] <= '9')
91  ch += str[1] - '0';
92  else
93  ch += str[1] - '0' - 7;
94  return ch;
95 }
96 
97 /* 4) AMS_chkbitsts = CHECK BIT STATUS of the AMS Input Port (IOP(i) with i=0..7)
98  INPUT PARAMETERS:
99  id: AMS ADDRESS
100  bitn: BIT NUMBER --> 0 = LSB, 7 = MSB
101 
102  OUTPUT:
103  sts: BIT STATUS --> sts = 0 --> bitn = 0, sts = 1 --> bitn = 1
104 */
105 int AMS_chkbitsts_old(int id, int bitn)
106 {
107 
108 
109  int bn = bitn;
110  int sts = 2;
111  char str[80], buf[80], *s;
112  int d, er;
113  unsigned long stbyte = 0;
114 
115  //Assign Address
116  AMS_assadd(id);
117 
118  //Read AMS Input
119  sprintf(DXL.buftx, "/");
120  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
121  d = STS0LEN;
122  er = DXL_RxStr(buf, &d);
123 
124  DXL.bufrx[0] = '0';
125  DXL.bufrx[1] = '0';
126  er = 0;
127  sprintf(str,"0x%s",buf);
128  stbyte = strtol(str, &s, 16);
129 
130  switch (bn)
131  {
132  case 0:
133  sts = stbyte & 1;
134  sts = sts >> bn;
135  break;
136  case 1:
137  sts = stbyte & 2;
138  sts = sts >> bn;
139  break;
140  case 2:
141  sts = stbyte & 4;
142  sts = sts >> bn;
143  break;
144  case 3:
145  sts = stbyte & 8;
146  sts = sts >> bn;
147  break;
148  case 4:
149  sts = stbyte & 16;
150  sts = sts >> bn;
151  break;
152  case 5:
153  sts = stbyte & 32;
154  sts = sts >> bn;
155  break;
156  case 6:
157  sts = stbyte & 64;
158  sts = sts >> bn;
159  break;
160  case 7:
161  sts = stbyte & 128;
162  sts = sts >> bn;
163  break;
164  }
165 
166  if (sts == 2)
167  return 2;
168  else
169  return sts;
170 
171 }
172 
173 /* 5) Assign default parameters */
174 void AMS_Default(int id)
175 {
176 
177  char buf[80];
178  int d, er;
179 
180  //Assign Address
181  AMS_assadd(id);
182 
183  // Set default values
184  DXL_TxStr("A", 1);
185  d = ACKLEN;
186  er = DXL_RxStr(buf, &d);
187 
188 
189 }
190 
191 
192 int AMS_chkbitsts(int id, int bitn)
193 {
194 
195 
196  int sts = 2;
197 // char str[80], buf[80], *s;
198  int d, er;
199  unsigned long stbyte = 0;
200  unsigned char a;
201 
202 // buf[0] = 0;
203 // buf[1] = 0;
204  //Assign Address
205  AMS_assadd(id);
206 
207  //Read AMS Input
208 // delay (70);
209  sprintf(DXL.buftx, "/");
210  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
211  d = STS0LEN;
212 // delay (70);
213  er = DXL_RxStr(DXL.bufrx, &d);
214 // er = DXL_RxStr(buf, &d);
215 
216  if(er) Message("Serial Error!", er);
217  else
218  {
219  a = DXL_HexBin(DXL.bufrx);
220 
221 // sprintf(buf, "%d", a);
222 // MObjectSetText(CONFP.lblStatus, buf);
223 // if (a = 100)
224 // er = DXL_RxStr(buf, &d);
225 // a = DXL_HexBin(buf);
226 
227 
228 
229  switch (bitn)
230  {
231  case 0:
232  sts = (a >> 0) & 1;
233  break;
234  case 1:
235  sts = (a >> 1) & 1;
236  break;
237  case 2:
238  sts = (a >> 2) & 1;
239  break;
240  case 3:
241  sts = (a >> 3) & 1;
242  break;
243  case 4:
244  sts = (a >> 4) & 1;
245  break;
246  case 5:
247  sts = (a >> 5) & 1;
248  break;
249  case 6:
250  sts = (a >> 6) & 1;
251  break;
252  case 7:
253  sts = (a >> 7) & 1;
254  break;
255 
256 
257 
258 
259  }
260 
261 
262 
263 
264 // sprintf(buf, "A Proxy, Home = %d, Work = %d\nB Proxy, Home = %d, Work = %d\nAll bits = %02XH, %d%d%d%d%d%d%d%d bin",
265 // (a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1, a,
266 // (a >> 7) & 1, (a >> 6) & 1, (a >> 5) & 1, (a >> 4) & 1,(a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1, a & 0);
267 // Message(buf, er);
268 
269  }
270 
271 
272 
273 
274 // sprintf(str,"0x%s",buf);
275 // sprintf(str,"0x%s",DXL.bufrx);
276 // stbyte = strtol(str, &s, 16);
277 /*
278  switch (bn)
279  {
280  case 0:
281  sts = stbyte & 1;
282  sts = sts >> bn;
283  break;
284  case 1:
285  sts = stbyte & 2;
286  sts = sts >> bn;
287  break;
288  case 2:
289  sts = stbyte & 4;
290  sts = sts >> bn;
291  break;
292  case 3:
293  sts = stbyte & 8;
294  sts = sts >> bn;
295  break;
296  case 4:
297  sts = stbyte & 16;
298  sts = sts >> bn;
299  break;
300  case 5:
301  sts = stbyte & 32;
302  sts = sts >> bn;
303  break;
304  case 6:
305  sts = stbyte & 64;
306  sts = sts >> bn;
307  break;
308  case 7:
309  sts = stbyte & 128;
310  sts = sts >> bn;
311  break;
312  }
313 
314  if (sts == 2)
315  return 2;
316  else
317 
318 */
319  return sts;
320 
321 }
322 
323 
324 
325 /* 6) */
326 int AMS_Power(int id, int powa, int powb)
327 {
328  char buf[80];
329  int d, er;
330 
331  //Assign Address
332  AMS_assadd(id);
333 
334  sprintf(DXL.buftx, "&%02X%02X", 0, 0);
335  DXL_TxStr(DXL.buftx, 5);
336  d = ACKLEN;
337  er = DXL_RxStr(buf, &d);
338  delay(50);
339  //send power
340  sprintf(DXL.buftx, "&%02X%02X", powa, powb);
341  DXL_TxStr(DXL.buftx, 5);
342  d = ACKLEN;
343  er = DXL_RxStr(buf, &d);
344 
345  return er;
346 }
347 
348 /* 7) */
349 int AMS_Step(int id, int stepa, int stepb)
350 {
351  char buf[80];
352  int d, er;
353 
354  //Assign Address
355  AMS_assadd(id);
356 
357  // Send step
358  sprintf(DXL.buftx, "'%02X%02X", stepa, stepb);
359  DXL_TxStr(DXL.buftx, 5);
360  d = ACKLEN;
361  er = DXL_RxStr(buf, &d);
362 
363  return er;
364 
365 }
366 
367 /* 8) */
368 int AMS_Slope(int id, int slopea, int slopeb)
369 {
370  char buf[80];
371  int d, er;
372 
373  //Assign Address
374  AMS_assadd(id);
375 
376  // Send slope
377  sprintf(DXL.buftx, "(%02X%02X", slopea, slopeb);
378  DXL_TxStr(DXL.buftx, 5);
379  d = ACKLEN;
380  er = DXL_RxStr(buf, &d);
381 
382  return er;
383 }
384 
385 /* 9) */
386 int AMS_Speed(int id, int speeda, int speedb)
387 {
388  char buf[80];
389  int d, er;
390 
391  //Assign Address
392  AMS_assadd(id);
393 
394  // Send speed
395  sprintf(DXL.buftx, "4%02X%02X", speeda, speedb);
396  DXL_TxStr(DXL.buftx, 5);
397  d = ACKLEN;
398  er = DXL_RxStr(buf, &d);
399 
400  return er;
401 }
402 
403 /* 10) */
404 int AMS_TrackOn(int id, int adir, int bdir, unsigned long acount, unsigned long bcount)
405 {
406  char buf[80];
407  int d, er;
408 
409  //Assign Address
410  AMS_assadd(id);
411 
412  sprintf(DXL.buftx, "0%02X%08lX%02X%08lX", adir, acount, bdir, bcount);
413  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
414  d = ACKLEN;
415  er = DXL_RxStr(buf, &d);
416 
417  return er;
418 
419 }
420 
421 /* 11) */
422 int AMS_TrackOff(int id, int amot, int bmot)
423 {
424  char buf[80];
425  int d, er;
426 
427  //Assign Address
428  AMS_assadd(id);
429 
430  sprintf(DXL.buftx, "5%02X%02X", amot, bmot);
431  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
432  d = ACKLEN;
433  er = DXL_RxStr(buf, &d);
434 
435  return er;
436 
437 }
438 /* 12) */
439 int AMS_AzzeraCoord(int id, int mtr)
440 {
441  int er, d;
442  char buf[40];
443 
444  //Assign Address
445  AMS_assadd(id);
446 
447  sprintf(DXL.buftx, "3%02X", mtr);
448  DXL_TxStr(DXL.buftx,3);
449  d = ACKLEN;
450  er = DXL_RxStr(buf, &d);
451 
452  return er;
453 
454 }
455 
456 /* 13) */
457 int AMS_dirfin(int id, int dira, int dirb)
458 {
459  char buf[80];
460  int d, er;
461 
462  //Assign Address
463  AMS_assadd(id);
464 
465  // Set final direction
466  sprintf(DXL.buftx, "-%02X%02X", dira, dirb);
467  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
468  d = ACKLEN;
469  er = DXL_RxStr(buf, &d);
470 
471  return er;
472 }
473 
474 /* 14) */
475 int AMS_firmrev(int id)
476 {
477 // char buf[80];
478  int d, er;
479 
480  //Assign Address
481  AMS_assadd(id);
482 
483  // Firmware Revision
484  DXL_TxStr("%%", 1);
485  d = STS0LEN;
486  er = DXL_RxStr(DXL.bufrx, &d);
487 
488  return er;
489 }
490 
491 /* 15) */
492 void AMS_PWM(int id, int on, int off)
493 {
494  char buf[80];
495  int er, d;
496 
497  //Assign Address
498  AMS_assadd(id);
499 
500 // on = 450;
501 // off = 50;
502  sprintf(DXL.buftx, "6%08X%08X", (unsigned int)on, (unsigned int)off );
503  DXL.add = 1; //AMS2 address
504  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
505  delay(500);
506  d = ACKLEN;
507  er = DXL_RxStr(buf, &d);
508 
509 }
510 
511 
512 /* 16) */
513 int AMS_sendstep(int id, int dira, int dirb, unsigned long stepa, unsigned long stepb)
514 {
515  char buf[80];
516  int d, er;
517 
518  //Assign Address
519  AMS_assadd(id);
520 
521  //Send step number
522  sprintf(DXL.buftx,".%02X%08lX%02X%08lX", dira, stepa, dirb, stepb);
523  DXL_TxStr(DXL.buftx, 21);
524  d = ACKLEN;
525  er = DXL_RxStr(buf, &d);
526 
527  return er;
528 }
529 /* 17) */
530 int AMS_SetPolarity(int id, int homea, int worka, int homeb, int workb)
531 {
532  char buf[80];
533  int d, er;
534 
535  //Assign Address
536  AMS_assadd(id);
537 
538  //Send Polarity for home and work
539  sprintf(DXL.buftx,"*%02X%02X%02X%02X", homea, worka, homeb, workb);
540  DXL_TxStr(DXL.buftx, 21);
541  d = ACKLEN;
542  er = DXL_RxStr(buf, &d);
543 
544  return er;
545 }
546 
547 
548 /* 18) */
549 int AMS_Home(int id, int mota, int motb)
550 {
551  char buf[80];
552  int d, er;
553 
554  //Assign Address
555  AMS_assadd(id);
556 
557  //Send Polarity for home and work
558  sprintf(DXL.buftx,"+%02X%02X", mota, motb);
559  DXL_TxStr(DXL.buftx, 5);
560  d = ACKLEN;
561  er = DXL_RxStr(buf, &d);
562 
563  return er;
564 }
565 
566 /* 19) */
567 int AMS_HomeDefault(int id, int mota, int motb)
568 {
569  char buf[80];
570  int d, er;
571 
572  //Assign Address
573  AMS_assadd(id);
574 
575  //Send Polarity for home and work
576  sprintf(DXL.buftx,"<%02X%02X", mota, motb);
577  DXL_TxStr(DXL.buftx, 5);
578  d = ACKLEN;
579  er = DXL_RxStr(buf, &d);
580 
581  return er;
582 }
583 
584 void AMS_PWM_OFF(int id)
585 {
586  char buf[80];
587  int er, d;
588 
589  //Assign Address
590  AMS_assadd(id);
591 
592 // on = 450;
593 // off = 50;
594  sprintf(DXL.buftx, "7", 1);
595  DXL.add = 1; //AMS2 address
596  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
597  d = ACKLEN;
598  er = DXL_RxStr(buf, &d);
599 
600 }
601 
602 
603 
604 
605 
606 
607 void D_Positioning(MOBJECT p, int id, long tout)
608 {
609  unsigned long st, et, x;
610  long tl;
611  char buf[80];
612 
613  if (DAS.Flag.exemode == DBDEMO)
614  tout = 1000;
615 
616  st = GetTickCount();
617  do
618  {
619  et = GetTickCount();
620  x = labs(et - st);
621  //g = x % 10;
622  //sprintf(buf, " %u %u", x, g);
623  //Status (buf);
624  //MLoopWhileEvents(0);
625  //if((x % 10 == 0) && (x > 0))
626  if(x > 0)
627  {
628  if(DAS.DOption.amsprogbar)
629  {
630  D_VRange(NULL, (long) x * 100 / tout, 255, 0, 0 );
631  tl = (long) ((double)fabs(((double)(labs(et - st) - tout))/1000.0 ));
632  sprintf(buf, "%.1lf sec. left", (double)fabs(((double)(labs(et - st) - tout))/1000.0 ));
633  MObjectSetText(p, buf);
634  DAS.StepM.postime[AMS1][DAS.StepM.SM_B]=(unsigned long)(tl*1000);
635 
636  }
637  }
638  }while(labs(et - st) < tout);
639 
640 }
641 
642 
643 /* 20) */
644 int AMS_DoPos(int id, int dira, int dirb, unsigned long stepa, unsigned long stepb)
645 {
646  int er = 0;
647 // unsigned long x;
648  long ns, cont;
649  char buf[64];
650 
651  FlgSM_Stop = 0;
652 
653  //Assign Address
654  AMS_assadd(id);
655 
656 
657  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
658  {
659 
660  // Testa se il precedente posizionamento e' finito
661  if(stepa)
662  er = AMS_TestPos(id, 0);
663  if(er) return er;
664 
665  // Testa se il precedente posizionamento e' finito
666  if(stepb)
667  er = AMS_TestPos(id, 1);
668  if(er) return er;
669  }
670 
671 /*********************************/
672 
673  if(stepa && id == 0)
674  if(DAS.DOption.amsprogbar)
675  D_VRange("Positioning Mirror",0, 255, 0, 0);
676  if(stepb && id == 0)
677  if(DAS.DOption.amsprogbar)
678  D_VRange("Positioning Grating",0, 255, 255, 0);
679  if(stepa && id == 1)
680  if(DAS.DOption.amsprogbar)
681  D_VRange("Positioning Filter Wheel",0, 0, 0, 255);
682  if(stepb && id == 1)
683  if(DAS.DOption.amsprogbar)
684  D_VRange("Positioning ND Filter Wheel",0, 192, 0, 255);
685  if(stepa && id == 2)
686  if(DAS.DOption.amsprogbar)
687  D_VRange("Positioning Zenith",0, 0, 255, 255);
688  if(stepb && id == 2)
689  if(DAS.DOption.amsprogbar)
690  D_VRange("Positioning Azimuth",0, 0, 192, 255);
691 
692 /*********************************/
693 
694 
695  // Trasmette numero di passi
696  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
697  er = AMS_sendstep(id, dira, dirb, stepa, stepb);
698 
699 
700  if(stepa && id == 0)
701  {
702  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
703  cont = (long) (stepa / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_A]);
704  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[0], 0, cont);
705  }
706  if(stepb && id == 0)
707  {
708  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
709  DAS.StepM.postime[id][DAS.StepM.SM_B] = (long) (stepb / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_B]);
710  //cont = (long) (stepb / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_B]);
711  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[1], 1, DAS.StepM.postime[id][DAS.StepM.SM_B]);
712  //D_Positioning(DAS.Gui.ContrPanel.Albl_LS[1], 1, cont);
713  }
714  if(stepa && id == 1)
715  {
716  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
717  cont = (long) (stepa / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_A]);
718  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[2], 2, cont);
719  }
720  if(stepb && id == 1)
721  {
722  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
723  cont = (long) (stepb / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_B]);
724  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[3], 3, cont);
725  }
726  if(stepa && id == 2)
727  {
728  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
729  cont = (long) (stepa / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_A]);
730  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[4], 4, cont);
731  }
732  if(stepb && id == 2)
733  {
734  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
735  cont = (long) (stepb / (float) DAS.StepM.speed_Hz[id][DAS.StepM.SM_B]);
736  D_Positioning(DAS.Gui.ContrPanel.Albl_LS[5], 5, cont);
737  }
738 
739 
740 
741  if(DAS.DOption.amsprogbar)
742  D_VRange(NULL, 100, 0,0,0);
743 
744 /*********************************/
745  /* FILE DI LOG */
746  if(stepa && id == 0)
747  writelogmotor(0, 0, er, dira, stepa, 0);
748  if(stepb && id == 0)
749  writelogmotor(0, 1, er, dirb, stepb, 0);
750  if(stepa && id == 1)
751  writelogmotor(0, 2, er, dira, stepa, 1);
752  if(stepb && id == 1)
753  writelogmotor(0, 3, er, dirb, stepb, 1);
754  if(stepa && id == 2)
755  writelogmotor(0, 4, er, dira, stepa, 2);
756  if(stepb && id == 2)
757  writelogmotor(0, 5, er, dirb, stepb, 2);
758 /*********************************/
759 
760  if(er) return er;
761 
762  if ((DAS.Flag.exemode != DBDEMO) & (DAS.Flag.exemode != TESTSZAMODE))
763  {
764  // Testa se il precedente posizionamento e' finito
765  if(stepa)
766  er = AMS_TestPos(id, 0);
767  if(er) return er;
768 
769  // Testa se il precedente posizionamento e' finito
770  if(stepb)
771  er = AMS_TestPos(id, 1);
772  if(er) return er;
773 
774  /*******************************************************/
775  /* Conta numero di passi effettuati */
776  if(stepa)
777  {
778  ns = AMS_ReadStep(id, 0);
779  sprintf(buf, "Steps A: %ld", ns);
780  Status(buf);
781  }
782 
783  if(stepb)
784  {
785  ns = AMS_ReadStep(id, 1);
786  sprintf(buf, "Steps B: %ld", ns);
787  Status(buf);
788  }
789 
790  /******************************************************/
791  }
792  else //DEMO MODE
793  {
794  /* Conta numero di passi effettuati */
795  if(stepa)
796  {
797 
798  sprintf(buf, "Steps A: %ld", stepa);
799  Status(buf);
800  }
801 
802  if(stepb)
803  {
804 
805  sprintf(buf, "Steps B: %ld", stepb);
806  Status(buf);
807  }
808 
809  }
810 
811  FlgSM_Stop = 1;
812  return er;
813 }
814 
815 
816 int DB_StepMotor(int id, int motor, long step, int dir)
817 {
818 
819  char buf[80];
820 // char Dbuf[80];
821  int er = 0;
822  int c = motor, d = 0;
823 
824 
825 
826  switch(c)
827  {
828  //INPUT MIRROR
829  case 0: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][0], "SYS\\ledoff.bmp");
830  sprintf(buf, "%s", "Positioning");
831  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[0], buf);
832  DXL.add = 0;
833  er = AMS_DoPos(id, dir, 0, step, 0);
834  if (DAS.Flag.exemode != DBDEMO)
835  if(er) { Message("n Step not sent! 1", er); break; }
836  break;
837 
838  //GRATING
839  case 1: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][1], "SYS\\ledoff.bmp");
840  sprintf(buf, "%s", "Positioning");
841  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[1], buf);
842  DXL.add = 0;
843  er = AMS_DoPos(id, 0, dir, 0, step);
844  if (DAS.Flag.exemode != DBDEMO)
845  if(er) { Message("n Step not sent! 2", er); break; }
846  break;
847 
848  //FILTER WHEEL
849  case 2: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][0], "SYS\\ledoff.bmp");
850  sprintf(buf, "%s", "Positioning");
851  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[2], buf);
852 
853  DXL.add = 1;
854  er = AMS_DoPos(id, dir, 0, step, 0);
855  if (DAS.Flag.exemode != DBDEMO)
856  if(er) { Message("n Step not sent! 2", er); break; }
857  DXL.add = 0;
858  break;
859 
860  // ND FILTER WHEEL
861  case 3: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][1], "SYS\\ledoff.bmp");
862  sprintf(buf, "%s", "Positioning");
863  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[3], buf);
864 
865  DXL.add = 1;
866  er = AMS_DoPos(id, 0, dir, 0, step);
867  if (DAS.Flag.exemode != DBDEMO)
868  if(er) { Message("n Step not sent! 2", er); break; }
869  DXL.add = 0;
870  break;
871 
872 
873  //Zenith
874  case 4: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][0], "SYS\\ledoff.bmp");
875  sprintf(buf, "%s", "Positioning");
876  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[4], buf);
877 
878  DXL.add = 2;
879  er = AMS_DoPos(id, dir, 0, step, 0);
880  if (DAS.Flag.exemode != DBDEMO)
881  if(er)
882  {
883  Message("n Step not sent! 3", er);
884  break;
885  }
886  DXL.add = 0;
887  break;
888  //Azimuth
889  case 5: MPixmapSetImageFile(DAS.Gui.ContrPanel.LedSM[id][1], "SYS\\ledoff.bmp");
890  sprintf(buf, "%s", "Positioning");
891  MObjectSetText(DAS.Gui.ContrPanel.Albl_LS[5], buf);
892 
893  DXL.add = 2;
894  er = AMS_DoPos(id, 0, dir, 0, step);
895  if (DAS.Flag.exemode != DBDEMO)
896  if(er) { Message("n Step not sent! 4", er); break; }
897  DXL.add = 0;
898  break;
899 
900 
901  }
902 
903 
904  return 1;
905 }
906 
907 
908 
909 
910 /* 21) */
911 int AMS_wopto(int id, unsigned int w)
912 {
913  char buf[80];
914  int d, er;
915 
916  //Assign Address
917  AMS_assadd(id);
918  //Write the opto port
919  sprintf(DXL.buftx, "9%02X", w);
920  DXL_TxStr(DXL.buftx, 3);
921  d = ACKLEN;
922  er = DXL_RxStr(buf, &d);
923 
924  return er;
925 }
926 
927 //mtr1(or 2) = 0 --> motor off
928 //mtr1(or 2) = 1 --> motor on
929 /* 22 */
930 int AMS_MotorOn_Off(int id, int mtr1, int mtr2)
931 {
932  char buf[80];
933  int er = 0, d;
934 
935  //Assign Address
936  AMS_assadd(id);
937 
938  sprintf(DXL.buftx, "2%02X%02X", mtr1, mtr2);
939  DXL_TxStr(DXL.buftx,strlen(DXL.buftx));
940  d = ACKLEN;
941  er = DXL_RxStr(buf, &d);
942  return er;
943 }
944 
945 /* 23 */
946 int AMS_Divisore(int id, unsigned long par1, unsigned long par2)
947 {
948 
949  char buf[80];
950  int d, er;
951 
952  //Assign Address
953  AMS_assadd(id);
954 
955  //set divisore
956  sprintf(DXL.buftx,">%08lX%08lX", par1, par2);
957  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
958  d = ACKLEN;
959  er = DXL_RxStr(buf, &d);
960 
961  return er;
962 
963 }
964 
965 /* 23) */
966 int AMS_ESlope(int id, int eslopea, int eslopeb)
967 {
968  char buf[80];
969  int d, er;
970 
971  //Assign Address
972  AMS_assadd(id);
973 
974  // Send slope
975  sprintf(DXL.buftx, ";%02X%02X", eslopea, eslopeb);
976  DXL_TxStr(DXL.buftx, 5);
977  d = ACKLEN;
978  er = DXL_RxStr(buf, &d);
979 
980  return er;
981 }
982 
983 
984 
985 /* 24 */
986 int AMS_Proxy(int id)
987 {
988 
989  char buf[128];
990  int d, er;
991  unsigned char a;
992  //Assign Address
993  AMS_assadd(id);
994 
995  DXL_TxStr("/", 1);
996 
997  d = STS0LEN;
998  er = DXL_RxStr(buf, &d);
999  if(er) Message("Serial Error!", er);
1000  else
1001  {
1002  a = DXL_HexBin(buf);
1003  sprintf(buf, "A Proxy, Home = %d, Work = %d\nB Proxy, Home = %d, Work = %d\nAll bits = %02XH, %d%d%d%d%d%d%d%d bin",
1004  (a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1, a,
1005  (a >> 7) & 1,(a >> 6) & 1, (a >> 5) & 1, (a >> 4) & 1,(a >> 3) & 1, (a >> 2) & 1, (a >> 1) & 1, a & 1 );
1006  Message(buf, er);
1007  }
1008 
1009  return er;
1010 }
1011 
1012 /* 25 */
1013 int AMS_ReadAD(int id, int ch)
1014 {
1015 
1016  char buf[80];
1017  int d, er;
1018 
1019  //Assign Address
1020  AMS_assadd(id);
1021 
1022  // Send slope
1023  sprintf(DXL.buftx, ":%02X", ch );
1024  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
1025  d = ACKLEN;
1026  er = DXL_RxStr(buf, &d);
1027 
1028  return er;
1029 }
1030 
1031 
1032 
1033 /* 27 */
1042 int AMS_TestHome(int id, int par1)
1043 {
1044 
1045 // char buf[80];
1046  int d, er;
1047 
1048  //Assign Address
1049  AMS_assadd(id);
1050 
1051  sprintf(DXL.buftx,"G%02X", par1);
1052  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
1053  d = ACKLEN;
1054  er = DXL_RxStr(DXL.bufrx, &d);
1055  d = atoi(DXL.bufrx);
1056 
1057  return d;
1058 
1059 }
1060 
1061 
1062 /* 28 */
1063 //verify the if the work position has been reaced
1064 int AMS_TestWork(int id, int par1)
1065 {
1066 
1067  char buf[80];
1068  int d, er;
1069 
1070  //Assign Address
1071  AMS_assadd(id);
1072 
1073 
1074  sprintf(DXL.buftx,"H%02X", par1);
1075  DXL_TxStr(DXL.buftx, strlen(DXL.buftx));
1076  d = ACKLEN;
1077  er = DXL_RxStr(buf, &d);
1078 
1079  return er;
1080 
1081 }
1082 
1083 
1084 
1085 void Message(char *str, int er)
1086 {
1087  char buf[80];
1088  MOBJECT sh, lbl;
1089 
1090  sprintf(buf, "DEBUG: AMS%d, Err = %d", DXL.add+1, er);
1091 
1092  sh = MCreateShell(buf, SF_MODAL);
1094 
1095  lbl = MCreateLabel(sh, str, DEFAULT_FONT);
1098 
1099  sprintf(buf, "Txed: %s", DXL.buftx);
1100  lbl = MCreateLabel(sh, buf, DEFAULT_FONT);
1103 
1104  sprintf(buf, "Rxed: %s", DXL.bufrx);
1105  lbl = MCreateLabel(sh, buf, DEFAULT_FONT);
1108 
1109  MShellRealize(sh);
1110 }
1111 
1112 
1113 void delay(unsigned long d)
1114 {
1115  DWORD s, e;
1116  s = GetTickCount();
1117  do
1118  e = GetTickCount();
1119  while((unsigned long)labs(e - s) < d);
1120 }
1121 
1122 /*
1123 void Positioning(MOBJECT p, long tout)
1124 {
1125  unsigned long st, et;
1126  char buf[80];
1127  st = GetTickCount();
1128  do
1129  {
1130  et = GetTickCount();
1131  MLoopWhileEvents(1);
1132  sprintf(buf, "%d sec. left", ((labs(et - st) - tout) / 1000));
1133  MObjectSetText(p, buf);
1134  }while(labs(et - st) < tout + 50);
1135 
1136 }
1137 
1138 */
1139 
1140 unsigned char DXL_ByteHex(unsigned char val)
1141 {
1142  unsigned char ch = val;
1143  if(ch <= 9) ch += '0';
1144  else ch += '0' + 7;
1145  return ch;
1146 }
1147 
1148 void DXL_TxByte(unsigned char v)
1149 {
1150  SioPutc(DXL.com, v);
1151 }
1152 
1153 void DXL_TxStr(char *tx, int len)
1154 {
1155  unsigned char cksm = 0, ch;
1156  int c;
1157 
1158  DXL_TxByte('$');
1159 
1160  ch = DXL_ByteHex((unsigned char) ((DXL.add >> 4) & 0x0F));
1161  DXL_TxByte(ch);
1162  cksm += ch;
1163  ch = DXL_ByteHex((unsigned char) (DXL.add & 0x0F));
1164  DXL_TxByte(ch);
1165  cksm += ch;
1166  for(c = 0; c < len; c++)
1167  {
1168  DXL_TxByte(tx[c]);
1169  cksm += tx[c];
1170  }
1171  DXL_TxByte('#');
1172  ch = DXL_ByteHex((unsigned char) ((cksm >> 4) & 0x0F));
1173  DXL_TxByte(ch);
1174  ch = DXL_ByteHex((unsigned char) (cksm & 0x0F));
1175  DXL_TxByte(ch);
1176  DXL_TxByte(13);
1177 }
1178 
1179 /*
1180  * Parameters:
1181  * rx = received string
1182  * *len = num of chars to be received
1183  * Output:
1184  * *len = num of received chars
1185  *
1186  */
1187 int DXL_RxStr(char *rx, int *len)
1188 {
1189  unsigned char cksm = 0, rcksm, add;
1190  int c, p = 0;
1191  DWORD st, et;
1192 
1193  DXL.bufrx[0] = DXL.bufrx[1] = 0;
1194 
1195  st = GetTickCount();
1196  do
1197  {
1198  c = SioRxQue(DXL.com);
1199  et = GetTickCount();
1200  //}while(c < *len && labs(et - st) < 2000);
1201  }while(c < *len && labs(et - st) < 500);
1202 
1203  SioGets(DXL.com, DXL.bufrx, c);
1204 
1205 
1206  while(DXL.bufrx[p++] != '$' && p < 80);
1207 
1208  if(p == 80)
1209  // No start character
1210  return 1;
1211  p--;
1212  add = DXL_HexBin(&DXL.bufrx[p + 1]);
1213  if(add != DXL.add)
1214  // Incongruent address
1215  return 2;
1216  cksm += DXL.bufrx[p + 1];
1217  cksm += DXL.bufrx[p + 2];
1218  for(c = p + 3; c < 80; c++)
1219  {
1220  if(DXL.bufrx[c] != '#')
1221  {
1222  cksm += DXL.bufrx[c];
1223  rx[c - 3] = DXL.bufrx[c];
1224  }
1225  else
1226  {
1227  c++;
1228  rcksm = DXL_HexBin(&DXL.bufrx[c]);
1229  rx[c - 4] = 0;
1230  break;
1231  }
1232  }
1233  if(rcksm != cksm)
1234  // CheckSum error
1235  return 3;
1236  *len = c - 3;
1237  return 0;
1238 }
1239 
1240 
1241 
1242 // Open serial comunications
1243 int DXL_Open(int addr, int com, int baud)
1244 {
1245  int er = 0;
1246  DXL.speed = baud;
1247  DXL.com = com;
1248  DXL.add = addr; // Default device address
1249 
1250  SioReset(DXL.com, 64, 64);
1251  SioBaud(DXL.com, Baud19200);
1253 
1254 
1255  return er;
1256 }
1257 // Close serial comunications
1258 int DXL_Close(void)
1259 {
1260  int er;
1261  er = SioReset(DXL.com, 64, 64);
1262  return(SioDone(DXL.com));
1263 }
int AMS_Divisore(int id, unsigned long par1, unsigned long par2)
Definition: Dxl.c:946
stepm StepM
Stepper motors parameters structure.
void MObjectSetBackgroundRGB(MOBJECT obj, int r, int g, int b)
int AMS_TestWork(int id, int par1)
verify the if work position has been reached
Definition: Dxl.c:1064
gui Gui
Graphic User Interface Structure.
NoMangle int DLL_IMPORT_EXPORT SioGets(int, LPSTR, unsigned)
int AMS_Proxy(int id)
Definition: Dxl.c:986
MOBJECT LedSM[AMSMAX][2]
Leds for stepper motors status.
MOBJECT Albl_LS[8]
Active Control Panel Labels for LIS status.
MOBJECT lbl
Definition: DFileMan.c:42
#define Baud19200
Definition: WSC.H:120
unsigned char DXL_HexBin(char *str)
Definition: Dxl.c:83
int AMS_Slope(int id, int slopea, int slopeb)
Set slope to the Stepper motors Procedure for setting the slope fot the Stepper Motors.
Definition: Dxl.c:368
short DB_FG_Plbl[3]
Foreground PASSIVE Label Colors.
void MLoopWhileEvents(int discard)
void writelogmotor(int mod, int id_motore, int k, int d, long steps, unsigned char ams_id)
Write Motor Operations on Log File. Write on the SM.LOG file the commands send to the motors...
Definition: Spat_Device.c:48
optionini DOption
Options for DAS execution.
char buftx[1024]
Tx buffer.
Definition: DXL.H:21
void MShellRealize(MOBJECT obj)
void D_VRange(char *str, int val, int, int g, int b)
Create and display the status bar.
Definition: Utils.c:206
int AMS_TrackOn(int id, int adir, int bdir, unsigned long acount, unsigned long bcount)
Activate tracking Procedure for Stepper Motor tracking activation.
Definition: Dxl.c:404
#define ACKLEN
int SM_A
Motor A (su Y11) respectively: ID=0->InputMirror,ID=1->FilterWheel,ID=2->Zenith.
int AMS_Home(int id, int mota, int motb)
Home position for the selected motor the home position is reached with the standard parameters (1KHz...
Definition: Dxl.c:549
void AMS_PWM_OFF(int id)
Definition: Dxl.c:584
DXL structure. Structure for the serial communication with the AMS adapter.
Definition: DXL.H:16
void MPixmapSetImageFile(MOBJECT pixmap, const char *fname)
das structure (contain all the previous structures) .
int DXL_RxStr(char *rx, int *len)
receive string from a serial port
Definition: Dxl.c:1187
#define TESTSZAMODE
execution driven by SZAPRG file
MOBJECT MCreateLabel(MOBJECT parent, const char *text, MTFont font)
flag Flag
Structure for different flags.
int AMS_firmrev(int id)
Firmware Revision Number .
Definition: Dxl.c:475
#define DBDEMO
execution in demo mode
int AMS_Step(int id, int stepa, int stepb)
Set stepping for the Stepper motors Procedure for setting steps for the Stepper Motors.
Definition: Dxl.c:349
void AMS_PWM(int id, int on, int off)
set the on/off time on the PWM set the on/off time on the PWM.
Definition: Dxl.c:492
int speed
Baud rate.
Definition: DXL.H:18
void AMS_Default(int id)
Set default values to AMS parameters Procedure for setting the default values of power.speed, stepping and slope to the selected AMS adapter.
Definition: Dxl.c:174
NoMangle int DLL_IMPORT_EXPORT SioDone(int)
void MObjectSetText(MOBJECT obj, const char *text)
#define WordLength8
Definition: WSC.H:94
static double ch
Definition: SOLPOS.C:120
int FlgSM_Stop
Steppers Motor Activity: 0 = Motors moving; 1 = Motors stopped.
Definition: DAS_Spat.c:140
palette DPAL
Definition: 2DPlot.c:27
#define STS0LEN
NoMangle int DLL_IMPORT_EXPORT SioReset(int, int, int)
Control Flags.
int AMS_TestHome(int id, int par1)
verify if the home position has been reached
Definition: Dxl.c:1042
char bufrx[1024]
Rx buffer.
Definition: DXL.H:20
int add
Peripheral address.
Definition: DXL.H:19
int AMS_chkbitsts_old(int id, int bitn)
Definition: Dxl.c:105
unsigned char DXL_ByteHex(unsigned char val)
Definition: Dxl.c:1140
int AMS_HomeDefault(int id, int mota, int motb)
Definition: Dxl.c:567
long postime[AMSMAX][2]
time necessary for positioning
int AMS_MotorOn_Off(int id, int mtr1, int mtr2)
Motor ON-OFF. Switch On-Off the selected motor of the selected AMS.
Definition: Dxl.c:930
flag FLAG
Definition: Dxl.c:20
int DXL_Close(void)
Close COM.
Definition: Dxl.c:1258
#define DEFAULT_FONT
Definition: Mguidefs.h:877
#define SF_MODAL
Definition: Mguidefs.h:1019
void Status(char *tit)
Writes information&#39;s on the Status label .
Definition: Load.c:1556
DXL_par DXL
Definition: Dxl.c:18
void delay(unsigned long d)
Pauses for a specified number of milliseconds. .
Definition: Dxl.c:1113
int AMS_ReadAD(int id, int ch)
Definition: Dxl.c:1013
int AMS_Power(int id, int powa, int powb)
Set Power for AMS Procedure for setting the user choosed power to the selected AMS adapter...
Definition: Dxl.c:326
int AMS_ESlope(int id, int eslopea, int eslopeb)
Definition: Dxl.c:966
short DB_BG_Plbl[3]
Background PASSIVE Label Colors.
NoMangle int DLL_IMPORT_EXPORT SioPutc(int, char)
Function prototypes.
unsigned long AMS_ReadStep(int id, int motor)
read performed steps Procedure for the determination of the number of steps executed.
Definition: Dxl.c:60
int amsprogbar
display/hide prograssion bar, 0 = Hide, 1 = display
MOBJECT MCreateShell(const char *title, int flags)
NoMangle int DLL_IMPORT_EXPORT SioParms(int, int, int, int)
#define OneStopBit
Definition: WSC.H:86
int DXL_Open(int addr, int com, int baud)
Open COM.
Definition: Dxl.c:1243
void Message(char *str, int er)
Create a message shell for the AMS errors (Debug Use). .
Definition: Dxl.c:1085
int AMS_Speed(int id, int speeda, int speedb)
Set speed to the Stepper motors Procedure for setting speed for the Stepper Motors.
Definition: Dxl.c:386
Function prototypes.
int AMS_wopto(int id, unsigned int w)
write the value on the optoinsulated port write the value on the optoinsulated port.
Definition: Dxl.c:911
int SM_B
Motor B (su Y12) respectively: ID=0->Grating,ID=1->Not Connected,ID=2->Azimuth.
static double p
Definition: SOLPOS.C:131
void MLabelSetAlignment(MOBJECT p, int align)
controlpanel ContrPanel
Control Panel Structure.
int AMS_TrackOff(int id, int amot, int bmot)
tracking off Procedure for Stepper Motor tracking de-activation
Definition: Dxl.c:422
int AMS_AzzeraCoord(int id, int mtr)
Set to zero the number of steps on the selected motor Set to zero the number of steps on the selecte...
Definition: Dxl.c:439
#define AMS1
AMS1 address.
int AMS_chkbitsts(int id, int bitn)
check bit status Procedure for the determination of the bit number status.
Definition: Dxl.c:192
#define DWORD
Definition: DSCUD.H:75
NoMangle int DLL_IMPORT_EXPORT SioRxQue(int)
void D_Positioning(MOBJECT p, int id, long tout)
Callback for the void D_VRange and display also the status in the corresponding CP labels...
Definition: Dxl.c:607
static double st
Definition: SOLPOS.C:142
void * MOBJECT
Definition: Mguidefs.h:192
#define NoParity
Definition: WSC.H:78
int AMS_SetPolarity(int id, int homea, int worka, int homeb, int workb)
Definition: Dxl.c:530
#define LEFT_ALIGN
Definition: Mguidefs.h:915
double speed_Hz[AMSMAX][2]
Stepper Motors Speed [Hz].
das DAS
Definition: Dxl.c:22
int AMS_dirfin(int id, int dira, int dirb)
Set Final Directions .
Definition: Dxl.c:457
void AMS_assadd(int w)
Assign AMS address Procedure for assigment of the AMS address.
Definition: Dxl.c:25
void DXL_TxByte(unsigned char v)
Definition: Dxl.c:1148
NoMangle int DLL_IMPORT_EXPORT SioBaud(int, unsigned)
int com
Comunication port.
Definition: DXL.H:17
void MObjectSetForegroundRGB(MOBJECT obj, int r, int g, int b)
int AMS_sendstep(int id, int dira, int dirb, unsigned long stepa, unsigned long stepb)
Send steps Procedure for sending steps to the Stepper Motor.
Definition: Dxl.c:513
int on
PRG execution control variabile.
Definition: DAS_Spat.c:109
int AMS_DoPos(int id, int dira, int dirb, unsigned long stepa, unsigned long stepb)
Execute Positioning Execute Stepper positioning. Make all the control on the status of the selected m...
Definition: Dxl.c:644
void DXL_TxStr(char *tx, int len)
send string to a serial port
Definition: Dxl.c:1153
int AMS_TestPos(int id, int motor)
Test positioning status Procedure for testing the positioning status of the selected motor...
Definition: Dxl.c:31
int exemode
Set the Execution MODE (Execution modes)
int DB_StepMotor(int id, int motor, long step, int dir)
Send steps to the selected stepper motor. .
Definition: Dxl.c:816
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:44:08 for DAS - Rel. 3.1.6 - 18/09/2017.