Guest User

Untitled

a guest
Jun 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. component servo_turret "servo driven turret comp";
  2.  
  3.  
  4. // Inputs
  5. pin in bit enable "enable/tool change request pin";
  6. pin in s32 abs-feedback "absolute encoder input";
  7. pin in float feedback "encoder feedback motor";
  8. pin in s32 tool-number "tool request number pin";
  9. pin in bit unlocked "turret unlocked switch";
  10. pin in bit clear "clear error & reset";
  11.  
  12. // Paramaters
  13. param rw u32 stations = 8 "max station numbers on turret";
  14. param rw u32 timeout = 15 "timeout time, default 15sec";
  15. param rw float lift-delay = 0.01 "delay for settle on lift&drop(sec)";
  16. param rw float command-inc "incremental distance for stations";
  17.  
  18. // Outputs
  19. pin out bit in-position "tool changed pin";
  20. pin out float command "out put position command for PID";
  21. pin out bit unclamp "out put bit to unclamp turret";
  22. pin out bit pid-enable "enable pin for PID of motor";
  23. pin out bit motor-direction "Pin for motor direction, 0=fordward, 1=reverse";
  24.  
  25. // debug
  26. pin out s32 state_ = 0 "state of comp, 0=idle, 1=unclamped, 2=position, 3=clamping, 4=error";
  27. pin out s32 error-code = 0;
  28. // "1=nolift, 2=invlaid tool call, 3=possition call error", 4=Lock Error"
  29. pin in u32 msg_level = RTAPI_MSG_ERR; // RTAPI_MSG_INFO
  30.  
  31. // State variables
  32. variable int last_enable = 0;
  33. variable int last_clear = 0;
  34.  
  35. // Delay timers
  36. variable float cycle_timeout;
  37. variable float lift_timeout;
  38.  
  39. function _ ;
  40. author "Robert Harpham";
  41. // Based on work by Michael Haberler
  42. description """
  43. Servo_turret.comp
  44. This compoent is for interfacing a turret or changer driven by an AC servo motor
  45. with incremental encoder feedback + absolute encoder feedback (BCD or other form)
  46.  
  47. Incremental distance must be supplied for PID distance calculation.
  48. default timeout = 15seconds
  49. default station numbers = 8
  50. Debug info - 0=idle, 1=unclamped, 2=position, 3=clamping, 4=error
  51.  
  52. Based on work by Michael Haberler
  53. """;
  54. license "GPLv2 or greater";
  55. ;;
  56. //debug defines
  57. #define MS_IDLE 0
  58. #define MS_UNCLAMPED 1
  59. #define MS_POSITION 2
  60. #define MS_CLAMPING 3
  61. #define MS_ERROR 4
  62.  
  63. //error defines
  64. #define ERR_NO_LIFT 1
  65. #define ERR_INVALID_TOOL_CALL 2
  66. #define ERR_POSITION_ERROR 3
  67. #define ERR_LOCK 4
  68.  
  69. #define MOTOR_FWD 0
  70. #define MOTOR_REV 1
  71.  
  72. #include <rtapi_math.h>
  73.  
  74. FUNCTION(_)
  75. {
  76. int fwd_hops, rev_hops;
  77.  
  78. // honor the clear pin even if not enabled.
  79. if ((clear ^ last_clear) && clear) {
  80. // positive edge on clear pin.
  81. // Reset unclamp state, clear error status, set state to idle
  82. // turn off motor
  83. error_code = 0;
  84. unclamp = 0;
  85. pid_enable = 0;
  86. state_ = MS_IDLE;
  87. rtapi_print_msg(msg_level, "cleared");
  88. }
  89.  
  90. if (enable) {
  91. if (enable ^ last_enable) { // positive edge on enable
  92. // things to do once on enable
  93. error_code = 0;
  94. state_ = MS_IDLE;
  95. last_clear = clear;
  96. }
  97.  
  98. switch (state_) {
  99.  
  100. case MS_IDLE:
  101.  
  102. //check tool call
  103. if ((tool_number < 1) || (tool_number > stations)) {
  104. //send error message
  105. error_code = ERR_INVALID_TOOL_CALL;
  106. } else {
  107. if (tool_number == abs_feedback) {
  108. in_position = 1;
  109. error_code = 0;
  110. } else {
  111. in_position = 0;
  112. //determin direction
  113. if (abs_feedback < tool_number) {
  114. //go forward
  115. fwd_hops = tool_number - abs_feedback;
  116. rev_hops = stations - fwd_hops;
  117. } else {
  118. //go backwards
  119. rev_hops = abs_feedback - tool_number;
  120. fwd_hops = stations - rev_hops;
  121. }
  122. rtapi_print_msg(msg_level, "fwdhops: %d revhops: %d", fwd_hops, rev_hops);
  123.  
  124. if ((fwd_hops > 0) && (rev_hops > 0)) {
  125. if (fwd_hops < rev_hops) {
  126. //set timeout for list
  127. lift_timeout = lift_delay;
  128. state_ = MS_UNCLAMPED;
  129. motor_direction = MOTOR_FWD;
  130. } else {
  131. lift_timeout = lift_delay;
  132. state_ = MS_UNCLAMPED;
  133. motor_direction = MOTOR_REV;
  134. }
  135.  
  136. }
  137. }
  138.  
  139. }
  140. break;
  141.  
  142. case MS_UNCLAMPED:
  143.  
  144. lift_timeout -= period * 0.000000001;
  145. unclamp = 1;
  146.  
  147. //if lifted and seen
  148. if (unlocked > 1) {
  149. // Set Timeout for position
  150. cycle_timeout = timeout;
  151. state_ = MS_POSITION;
  152. rtapi_print_msg(msg_level, "Unlock Seen");
  153. }
  154.  
  155. //if timed out something went wrong
  156. if (lift_timeout < 0.0) {
  157. unclamp = 0;
  158. pid_enable = 0;
  159. state_ = MS_IDLE;
  160. error_code = ERR_NO_LIFT;
  161. rtapi_print_msg(msg_level, "Unlock Timeout");
  162. }
  163. break;
  164.  
  165. case MS_POSITION:
  166.  
  167. cycle_timeout -= period * 0.000000001;
  168.  
  169. if (motor_direction == MOTOR_FWD) {
  170. command = feedback + command_inc;
  171. pid_enable = 1;
  172. rtapi_print_msg(msg_level, "Start forward search");
  173. } else {
  174. command = feedback - command_inc;
  175. pid_enable = 1;
  176. rtapi_print_msg(msg_level, "Start backwards search");
  177. }
  178.  
  179. if (tool_number == abs_feedback) {
  180. // target pot reached need to clamp
  181. //set timeout for drop
  182. lift_timeout = lift_delay;
  183. state_ = MS_CLAMPING;
  184. rtapi_print_msg(msg_level, "poss %d reached, Locking", abs_feedback);
  185. }
  186.  
  187. // check timeout
  188. if (cycle_timeout < 0.0 ) {
  189. unclamp = 0;
  190. pid_enable = 0;
  191. state_ = MS_IDLE;
  192. error_code = ERR_POSITION_ERROR;
  193. rtapi_print_msg(msg_level, "Position Timeout");
  194. }
  195.  
  196. break;
  197.  
  198. case MS_CLAMPING:
  199. lift_timeout -= period * 0.000000001;
  200. unclamp = 0;
  201.  
  202. //if locked and seen
  203. if (unlocked < 1) {
  204. pid_enable = 0;
  205. state_ = MS_IDLE;
  206. rtapi_print_msg(msg_level, "Lock Seen");
  207. }
  208.  
  209. //if timed out something went wrong
  210. if (lift_timeout < 0.0) {
  211. unclamp = 0;
  212. pid_enable = 0;
  213. state_ = MS_IDLE;
  214. error_code = ERR_LOCK;
  215. rtapi_print_msg(msg_level, "Lock Timeout");
  216. }
  217. break;
  218.  
  219. case MS_ERROR:
  220. // a positive edge on clear-index resets state to idle.
  221. if ((clear ^ last_clear) && clear) {
  222. state_ = MS_IDLE;
  223. error_code = 0;
  224. pid_enable = 0;
  225. }
  226. break;
  227. }
  228. } else {
  229. if (enable ^ last_enable) { // negative edge on enable
  230. // things to do once on disable
  231. ;
  232. }
  233. }
  234. last_enable = enable;
  235. last_clear = clear;
  236.  
  237. }
Add Comment
Please, Sign In to add comment