11component orient "Provide a PID command input for orientation mode based on current spindle position, target angle and orient mode";
22
33pin in bit enable "enable angular output for orientation mode";
4- pin in s32 mode "0: rotate - shortest move; 1: always rotate clockwise; 2: always rotate counterclockwise ";
4+ pin in s32 mode "sets rotation direction ";
55pin in float position "spindle position input, unit 1 rev";
66pin in float angle "orient target position in degrees, 0 ≤ angle < 360";
77pin out float command "target spindle position, input to PID command";
88pin out float poserr "in degrees - aid for PID tuning";
99pin out bit is-oriented "This pin goes high when poserr < tolerance. Use to drive spindle.N.is-oriented";
1010pin in float tolerance = 0.5 "The tolerance in degrees for considering the align completed";
11+ pin io bit index-enable "Connect to the spindle encoder counter to reset to index before orienting";
12+
13+ param r unsigned state "state machine for index homing";
1114
1215variable int last_enable = 0;
1316variable int debounce = 0; // to prevent the in-position triggering with the spindle moving
17+ variable double target_angle = 0;
18+ variable double latched_position = 0;
1419
1520option fp yes;
1621option period no;
@@ -37,6 +42,9 @@ The *mode* pin is interpreted as follows:
3742which may be clockwise or counterclockwise.
3843* 1: the spindle rotates always rotates clockwise to the new angle.
3944* 2: the spindle rotates always rotates counterclockwise to the new angle.
45+ * 3: same as mode 0 but with homing to index before orienting to the new angle.
46+ * 4: same as mode 1 but with homing to index before orienting to the new angle.
47+ * 5: same as mode 2 but with homing to index before orienting to the new angle.
4048
4149=== HAL USAGE
4250
@@ -63,16 +71,35 @@ license "GPL";
6371
6472FUNCTION(_) {
6573
66- double target_angle;
67- double latched_position;
6874 is_oriented = 0; // spindle.is-oriented inhibits spindle.orient
69- if (enable) {
70- if (enable ^ last_enable) { // positive edge on enable
71- is_oriented = 0;
75+
76+ switch (state){
77+ case 0: // waiting
78+ if (enable) {
79+ index_enable = 0;
80+ if (enable ^ last_enable) { // positive edge on enable
81+ is_oriented = 0;
82+ if (mode > 2){
83+ index_enable = 1;
84+ state = 3;
85+ } else {
86+ state = 1;
87+ }
88+ }
89+ }
90+ break;
91+
92+ case 1: // normal orient - init
93+ if (! enable) state = 0;
7294 debounce = 0;
7395 latched_position = position; // sample now
7496 target_angle = angle/360.0;
75- switch (mode) {
97+ state = 2;
98+ break;
99+
100+ case 2: // orienting
101+ if (! enable) state = 0;
102+ switch (mode % 3) {
76103 case 0: // shortest move
77104 command = floor(latched_position+0.5-target_angle) + target_angle;
78105 break;
@@ -82,12 +109,45 @@ FUNCTION(_) {
82109 case 2: // always ccw
83110 command = floor(latched_position-target_angle) + target_angle ;
84111 break;
112+ default:
113+ rtapi_print_msg(RTAPI_MSG_ERR, "unhandled mode %i\n", mode % 3);
114+ }
115+ poserr = (position - command) * 360.0;
116+ debounce += (fabs(poserr) < tolerance && debounce <=100);
117+ is_oriented = (debounce > 100);
118+ if (is_oriented) state = 0;
119+ break;
120+
121+ case 3: // index search - init
122+ if (! enable) state = 0;
123+ latched_position = position; // sample now
124+ state = 4;
125+ break;
126+
127+ case 4: // waiting for spindle index
128+ if (! enable) state = 0;
129+ if (! index_enable){ // index has reset)
130+ state = 1;
131+ break;
132+ }
133+ switch (mode) {
134+ case 3: // shortest move to index
135+ if (0 > latched_position) {
136+ command = position + 0.5;
137+ } else {
138+ command = position - 0.5;
139+ }
140+ break;
141+ case 4: // always cw to index
142+ command = position + 0.5;
143+ break;
144+ case 5: // always ccw to index
145+ command = position - 0.5;
146+ break;
147+ default:
148+ rtapi_print_msg(RTAPI_MSG_ERR, "unhandled mode %i\n", mode);
85149 }
86- }
87- poserr = (position - command) * 360.0;
88- debounce += (fabs(poserr) < tolerance && debounce <=100);
89- is_oriented = (debounce > 100);
150+ break;
90151 }
91152 last_enable = enable;
92153}
93-
0 commit comments