Dukales

MOVIDRIVE MDX61B

Apr 15th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <constb.h>
  2. #include <iob.h>
  3.  
  4. #pragma var 380 399
  5. #pragma globals 400 581
  6.  
  7. long counter;
  8.  
  9. #define SPEED_MAX 15000
  10.  
  11. long speed;
  12.  
  13. // PROFIBUS DP
  14. GSPODATA10 podata;
  15. // PO1 - Counter
  16. // PO2 - Control word // 0 - enable/stop, 1 - perform positioning
  17. // PO3 - Actual distance
  18. // PO4 - Distance setting
  19. // PO5 - Distance near
  20. // PO6 - Distance far
  21. // PO7 - Distance window
  22. SSPIDATA10 pidata;
  23. // PI1 - Counter
  24. // PI2 - Status word
  25. // PI3 - Actual speed
  26.  
  27. long host_counter;
  28. long cw;
  29. long distance;
  30. long distance_setting;
  31. long distance_window;
  32.  
  33. Task2_IO()
  34. {
  35.   _GetSys(podata, GS_PODATA);
  36.   host_counter = (podata.PO1 & 0xFFFF);
  37.   cw = (podata.PO2 & 0xFFFF);
  38.   distance = (podata.PO3 & 0xFFFF);
  39.   distance_setting = (podata.PO4 & 0xFFFF);
  40.   distance_window = (podata.PO7 & 0xFFFF);
  41.   //_SetSys(SS_ACTPOS, podata.PO?);
  42.   pidata.PI1 = counter;
  43.   pidata.PI2 = StatusWord;
  44.   pidata.PI3 = ControlWord;
  45.   _GetSys(pidata.PI3, GS_ACTSPEED);
  46.   _SetSys(SS_PIDATA, pidata);
  47. }
  48.  
  49. direction_CW()
  50. {
  51.   speed = SPEED_MAX;
  52.   _SetSys(SS_N11, speed);
  53.   speed /= 3;
  54.   _SetSys(SS_N12, speed);
  55.   speed /= 5;
  56.   _SetSys(SS_N13, speed);
  57. }
  58.  
  59. direction_CCW()
  60. {
  61.   speed = -SPEED_MAX;
  62.   _SetSys(SS_N11, speed);
  63.   speed /= 3;
  64.   _SetSys(SS_N12, speed);
  65.   speed /= 5;
  66.   _SetSys(SS_N13, speed);
  67. }
  68.  
  69. stop()
  70. {
  71.   _BitSet(ControlWord, 1);
  72.   _BitClear(ControlWord, 2);
  73.  
  74.   _BitClear(ControlWord, 4);
  75.   _BitClear(ControlWord, 5);
  76. }
  77.  
  78. run1()
  79. {
  80.   _BitClear(ControlWord, 1);
  81.   _BitSet(ControlWord, 2);
  82.  
  83.   _BitSet(ControlWord, 4);
  84.   _BitClear(ControlWord, 5);
  85. }
  86.  
  87. run2()
  88. {
  89.   _BitClear(ControlWord, 1);
  90.   _BitSet(ControlWord, 2);
  91.  
  92.   _BitClear(ControlWord, 4);
  93.   _BitSet(ControlWord, 5);
  94. }
  95.  
  96. run3()
  97. {
  98.   _BitClear(ControlWord, 1);
  99.   _BitSet(ControlWord, 2);
  100.  
  101.   _BitSet(ControlWord, 4);
  102.   _BitSet(ControlWord, 5);
  103. }
  104.  
  105. #define MOVE_FORWARD 1
  106. #define MOVE_BACKWARD 2
  107. #define GO_POSITION_MODE 3
  108.  
  109. long delta;
  110. long direction;
  111. long target_position;
  112. long mode;
  113.  
  114. go_position()
  115. {
  116.   if (distance < target_position) {
  117.     direction_CW();
  118.     direction = 0;
  119.   } else {
  120.     direction_CCW();
  121.     direction = 1;
  122.   }
  123.   while (cw == mode) {
  124.     if (direction == 0) {
  125.       delta = target_position - distance;
  126.     } else {
  127.       delta = distance - target_position;
  128.     }
  129.     if (delta < 1) {
  130.       stop();
  131.       break;
  132.     } else {
  133.       if (delta < distance_window / 3) {
  134.         run3();
  135.       } else if (delta < distance_window) {
  136.         run2();
  137.       } else {
  138.         run1();
  139.       }
  140.     }
  141.   }
  142.   stop();
  143. }
  144.  
  145. long d0, dd;
  146. long guard_counter;
  147.  
  148. Task3_Guard()
  149. {
  150.   guard_counter = 0;
  151.   while (1) {
  152.     d0 = distance;
  153.     _Wait(1000);
  154.     if (d0 < distance) {
  155.       dd = distance - d0;
  156.     } else {
  157.       dd = d0 - distance;
  158.     }
  159.     if ((pidata.PI3 != 0) && (dd < 20)) {
  160.       ++guard_counter;
  161.       if (3 < guard_counter) {
  162.         mode = 0;
  163.         _Wait(3000);
  164.         guard_counter = 0;
  165.       }
  166.     } else {
  167.       guard_counter = 0;
  168.     }
  169.   }
  170. }
  171.  
  172. main()
  173. {
  174.   _WdOn(5000);
  175.   while ((StatusWord & 0b100) == 0)
  176.     continue;
  177.   _WdOff();
  178.  
  179.   target_position = distance;
  180.  
  181.   podata.BusType = GS_BT_FBUS;
  182.   podata.Len = pidata.Len = 10;
  183.   counter = cw =
  184.   podata.PO2 = podata.PO1 = 0;
  185.   _SetTask(ST2_START, Task2_IO);
  186.   _SetTask(ST3_START, Task3_Guard);
  187.  
  188.   _Wait(3000);
  189.   counter = host_counter;
  190.   while (1) {
  191.     if (cw == 0) {
  192.       _Wait(100);
  193.       _BitSet(ControlWord, 1);
  194.     }
  195.     if (counter != host_counter) {
  196.       mode = cw;
  197.       switch (cw) {
  198.       case MOVE_FORWARD : {
  199.         target_position = (podata.PO5 & 0xFFFF);
  200.         go_position();
  201.         break;
  202.       }
  203.       case MOVE_BACKWARD : {
  204.         target_position = (podata.PO6 & 0xFFFF);
  205.         go_position();
  206.         break;
  207.       }
  208.       case GO_POSITION_MODE : {
  209.         target_position = distance_setting;
  210.         go_position();
  211.         break;
  212.       }
  213.       default : {
  214.         target_position = distance;
  215.         _BitSet(ControlWord, 1);
  216.       }
  217.       }
  218.       counter = host_counter;
  219.     }
  220.     //_SystemCall(SC_N0, SpeedZero); // Call if speed is zero
  221.     //_WaitSystem( SC_N0 ); // Wait until speed is zero
  222.   }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment