forked from jhaupt/NearZero1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoll.ino
318 lines (286 loc) · 11.2 KB
/
Roll.ino
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
void Roll1_vel(){
//SET POWER DUTY CYCLE IF IN PWM MODE
if(pwm1active == true){
if (vel1 == 0){ //Set power duty cycle based on whether or not motor is moving.
duty1 = m1*minIset1 + b1;
}
else {
duty1 = m1*maxIset1 + b1;
}
}
//INCREMENT PHASE INDEX
if (dir1 == 0) { //If direction is NORMAL
phaseindex1 = phaseindex1 + vel1;
}
else if (dir1 == 1) { //If direction is REVERSE
phaseindex1 = phaseindex1 - vel1;
}
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile1 == 1){ //If the anti-cogging profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex1 = phaseindex1 - (tscoeff1*.00025*sin(2*phaseindex1+(.469+tsphase1)) + tscoeff1*.00005*sin(4*phaseindex1+(.491+tsphase1)));
}
//RESET PHASE INDEX AFTER 2PI
if (phaseindex1 >= 2*pi) { //Reset phaseindex_U1 once it completes 2*180o in phase.
phaseindex1 = 0;
}
else if (phaseindex1 <= 0){
phaseindex1 = 2*pi - phaseindex1;
}
//WRITE OUTPUTS
dutyU1 = (255/2)+(duty1*(sin(phaseindex1))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U1_High,dutyU1); //Write to the PWM pins.
dutyV1 = (255/2)+(duty1*(sin(phaseindex1+(2*pi/3))));
analogWrite(V1_High,dutyV1);
dutyW1 = (255/2)+(duty1*(sin(phaseindex1+(4*pi/3))));
analogWrite(W1_High,dutyW1);
}
void Roll1_pos(){
//Check for REVERSE directionality
if (dir1 == 1) { //If directionality is REVERSE
pos1 = (-1)*pos1;
}
//TRACK THE MOTOR AGAINST THE COMMANDED POSITION
slewvel1 = (accel1/100000)*(fabs(phaseindex1 - pos1)); //make this a signmoid func. instead of capping below
if (slewvel1 > maxslewvel1) { //cap the max slewrate
slewvel1 = maxslewvel1;
}
else if (slewvel1 < -maxslewvel1){
slewvel1 = -maxslewvel1;
}
if ((phaseindex1 - .2) > pos1){ //the constant defines the deadzone
phaseindex1 = phaseindex1 - slewvel1;
if(pwm1active == true){
duty1 = m1*maxIset1 + b1;
}
j = 0;
}
else if ((phaseindex1 + .2) < pos1){
phaseindex1 = phaseindex1 + slewvel1;
if(pwm1active == true){
duty1 = m1*maxIset1 + b1;
}
j = 0;
}
else {//they're equal, so don't move the phaseindex, but DO set the power to the low setting if in PWM mode
j++;
if (j >= 100){
if(pwm1active == true){
duty1 = m1*minIset1 + b1;
j = 100;
}
}
}
//UPDATE MOTOR POSITION
pos1_last = pos1;
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile1 == 1){ //If the anti-coffing profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex1 = phaseindex1 - (tscoeff1*.00025*sin(2*phaseindex1+(.469+tsphase1)) + tscoeff1*.00005*sin(4*phaseindex1+(.491+tsphase1)));
}
//WRITE OUTPUTS
dutyU1 = (255/2)+(duty1*(sin(phaseindex1))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U1_High,dutyU1); //Write to the PWM pins.
dutyV1 = (255/2)+(duty1*(sin(phaseindex1+(2*pi/3))));
analogWrite(V1_High,dutyV1);
dutyW1 = (255/2)+(duty1*(sin(phaseindex1+(4*pi/3))));
analogWrite(W1_High,dutyW1);
}
void Roll1_servo(){
//Check for REVERSE directionality
if (dir1 == 1) { //If directionality is REVERSE
pos1 = (-1)*pos1;
}
//TRACK THE MOTOR AGAINST THE COMMANDED POSITION
slewvel1 = (accel1/100000)*fabs(enc1_absticks - pos1); //make this a signmoid func. instead of capping below
if (slewvel1 > maxslewvel1) { //cap the max slewrate
slewvel1 = maxslewvel1;
}
else if (slewvel1 < -maxslewvel1){
slewvel1 = -maxslewvel1;
}
if ((enc1_absticks - .03) > pos1){ //the constant defines the deadzone
phaseindex1 = phaseindex1 - slewvel1;
if(pwm1active == true){
duty1 = m1*maxIset1 + b1;
}
j = 0;
}
else if ((enc1_absticks + .03) < pos1){
phaseindex1 = phaseindex1 + slewvel1;
if(pwm1active == true){
duty1 = m1*maxIset1 + b1;
}
j = 0;
}
else {//they're equal, so don't move the phaseindex, but DO set the power to the low setting
j++;
if (j >= 100){
if(pwm1active == true){
duty1 = m1*minIset1 + b1;
j = 100;
}
}
}
//UPDATE MOTOR POSITION
pos1_last = pos1;
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile1 == 1){ //If the anti-coffing profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex1 = phaseindex1 - (tscoeff1*.00025*sin(2*phaseindex1+(.469+tsphase1)) + tscoeff1*.00005*sin(4*phaseindex1+(.491+tsphase1)));
}
//RESET PHASE INDEX AFTER 2PI
if (phaseindex1 >= 2*pi) { //Reset phaseindex_U1 once it completes 2*180o in phase.
phaseindex1 = 0;
}
else if (phaseindex1 <= 0){
phaseindex1 = 2*pi - phaseindex1;
}
//WRITE OUTPUTS
dutyU1 = (255/2)+(duty1*(sin(phaseindex1))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U1_High,dutyU1); //Write to the PWM pins.
dutyV1 = (255/2)+(duty1*(sin(phaseindex1+(2*pi/3))));
analogWrite(V1_High,dutyV1);
dutyW1 = (255/2)+(duty1*(sin(phaseindex1+(4*pi/3))));
analogWrite(W1_High,dutyW1);
}
void Roll2_vel(){
//SET POWER DUTY CYCLE IF IN PWM MODE
if(pwm2active == true){
if (vel2 == 0){ //Set power duty cycle based on whether or not motor is moving.
duty2 = m2*minIset2 + b2;
}
else {
duty2 = m2*maxIset2 + b2;
}
}
//INCREMENT PHASE INDEX
if (dir2 == 0) { //If direction is NORMAL
phaseindex2 = phaseindex2 + vel2;
}
else if (dir2 == 1) { //If direction is REVERSE
phaseindex2 = phaseindex2 - vel2;
}
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile2 == 1){ //If the anti-cogging profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex2 = phaseindex2 - (tscoeff2*.00025*sin(2*phaseindex2+(.469+tsphase2)) + tscoeff2*.00005*sin(4*phaseindex2+(.491+tsphase2)));
}
//RESET PHASE INDEX AFTER 2PI
if (phaseindex2 >= 2*pi) { //Reset phaseindex_U1 once it completes 2*180o in phase.
phaseindex2 = 0;
}
else if (phaseindex2 <= 0){
phaseindex2 = 2*pi - phaseindex2;
}
//WRITE OUTPUTS
dutyU2 = (255/2)+(duty2*(sin(phaseindex2))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U2_High,dutyU2); //Write to the PWM pins.
dutyV2 = (255/2)+(duty2*(sin(phaseindex2+(2*pi/3))));
analogWrite(V2_High,dutyV2);
dutyW2 = (255/2)+(duty2*(sin(phaseindex2+(4*pi/3))));
analogWrite(W2_High,dutyW2);
}
void Roll2_pos(){
//Check for REVERSE directionality
if (dir2 == 1) { //If directionality is REVERSE
pos2 = (-1)*pos2;
}
//TRACK THE MOTOR AGAINST THE COMMANDED POSITION
slewvel2 = (accel2/100000)*(fabs(phaseindex2 - pos2)); ////make this a signmoid func. instead of capping below
if (slewvel2 > maxslewvel2) { //cap the max slewrate
slewvel2 = maxslewvel2;
}
else if (slewvel2 < -maxslewvel2){
slewvel2 = -maxslewvel2;
}
if ((phaseindex2 - .2) > pos2){ //the constant defines the deadzone
phaseindex2 = phaseindex2 - slewvel2;
if(pwm2active == true){
duty2 = m2*maxIset2 + b2;
}
j = 0;
}
else if ((phaseindex2 + .2) < pos2){
phaseindex2 = phaseindex2 + slewvel2;
if(pwm2active == true){
duty2 = m2*maxIset2 + b2;
}
j = 0;
}
else { //they're equal, so don't move the phaseindex, but DO set the power to the low setting
j++;
if (j >= 100){
if(pwm2active == true){
duty2 = m2*minIset2 + b2;
j = 100;
}
}
}
//UPDATE MOTOR POSITION
pos2_last = pos2;
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile2 == 1){ //If the anti-coffing profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex2 = phaseindex2 - (tscoeff2*.00025*sin(2*phaseindex2+(.469+tsphase2)) + tscoeff2*.00005*sin(4*phaseindex2+(.491+tsphase2)));
}
//WRITE OUTPUTS
dutyU2 = (255/2)+(duty2*(sin(phaseindex2))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U2_High,dutyU2); //Write to the PWM pins.
dutyV2 = (255/2)+(duty2*(sin(phaseindex2+(2*pi/3))));
analogWrite(V2_High,dutyV2);
dutyW2 = (255/2)+(duty2*(sin(phaseindex2+(4*pi/3))));
analogWrite(W2_High,dutyW2);
}
void Roll2_servo(){
//Check for REVERSE directionality
if (dir2 == 1) { //If directionality is REVERSE
pos2 = (-1)*pos2;
}
//TRACK THE MOTOR AGAINST THE COMMANDED POSITION
slewvel2 = (accel2/100000)*fabs(enc2_absticks - pos2); //make this a signmoid func. instead of capping below
if (slewvel2 > maxslewvel2) { //cap the max slewrate
slewvel2 = maxslewvel2;
}
else if (slewvel2 < -maxslewvel2){
slewvel2 = -maxslewvel2;
}
if ((enc2_absticks - .03) > pos2){ //the constant defines the deadzone
phaseindex2 = phaseindex2 - slewvel2;
if(pwm2active == true){
duty2 = m2*maxIset2 + b2;
}
j = 0;
}
else if ((enc2_absticks + .03) < pos2){
phaseindex2 = phaseindex2 + slewvel2;
if(pwm2active == true){
duty2 = m2*maxIset2 + b2;
}
j = 0;
}
else {//they're equal, so don't move the phaseindex, but DO set the power to the low setting
j++;
if (j >= 100){
if(pwm2active == true){
duty2 = m2*minIset2 + b2;
j = 100;
}
}
}
//UPDATE MOTOR POSITION
pos2_last = pos2;
//CALCULATE TORQUE SMOOTHING (IF ON)
if (torqueprofile2 == 1){ //If the anti-coffing profile is set to 1, make the phase index be the phase index plus (minus) a correction which is a function of the phase index. The fuction
phaseindex2 = phaseindex2 - (tscoeff2*.00025*sin(2*phaseindex2+(.469+tsphase2)) + tscoeff2*.00005*sin(4*phaseindex2+(.491+tsphase2)));
}
//RESET PHASE INDEX AFTER 2PI
if (phaseindex2 >= 2*pi) { //Reset phaseindex once it completes 2*180o in phase.
phaseindex2 = 0;
}
else if (phaseindex2 <= 0){
phaseindex2 = 2*pi - phaseindex2;
}
//WRITE OUTPUTS
dutyU2 = (255/2)+(duty2*(sin(phaseindex2))); //The duty cycle varies with the sine function, which has output between -1 and 1. That is scaled by an amplitude variable, which effectively sets the motor power.This is all offset by half the maximum duty cycle so that the lowest instantaeous duty cycle is always positive.
analogWrite(U2_High,dutyU2); //Write to the PWM pins.
dutyV2 = (255/2)+(duty2*(sin(phaseindex2+(2*pi/3))));
analogWrite(V2_High,dutyV2);
dutyW2 = (255/2)+(duty2*(sin(phaseindex2+(4*pi/3))));
analogWrite(W2_High,dutyW2);
}