-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.c
339 lines (294 loc) · 11.1 KB
/
motor.c
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <bcm2835.h>
#include <stdio.h>
#include <string.h>
#define CONTROL_VER_1
#define WINDA_M1 RPI_V2_GPIO_P1_03
#define WINDA_M2 RPI_V2_GPIO_P1_05
#define WINDA_M3 RPI_V2_GPIO_P1_07
#define WINDA_M4 RPI_V2_GPIO_P1_11
#define WINDA_DIR RPI_V2_GPIO_P1_12
#define WINDB_M1 RPI_V2_GPIO_P1_13
#define WINDB_M2 RPI_V2_GPIO_P1_15
#define WINDB_M3 RPI_V2_GPIO_P1_16
#define WINDB_M4 RPI_V2_GPIO_P1_18
#define WINDB_DIR RPI_V2_GPIO_P1_22
#define A 0
#define B 1
#ifdef CONTROL_VER_1
#define STEP_MAX 4
#else
#define STEP_MAX 8
#endif
void MotorWind_SetCurrent(uint8_t wind_index, uint8_t val)
{
uint32_t port_mask_set, port_mask_clear;
uint8_t m4, m3, m2, m1;
// Read the selected wind (0 = A, 1 = B)
if (wind_index == 0)
{
m4 = WINDA_M4;
m3 = WINDA_M3;
m2 = WINDA_M2;
m1 = WINDA_M1;
}
else
{
m4 = WINDB_M4;
m3 = WINDB_M3;
m2 = WINDB_M2;
m1 = WINDB_M1;
}
// Set the clear mask (all 4 bits)
port_mask_clear = (1 << m4) | (1 << m3) | (1 << m2) | (1 << m1);
// Set the set mask (from the val variable)
port_mask_set = 0;
if (val & 0x08)
port_mask_set |= (1 << m4);
if (val & 0x04)
port_mask_set |= (1 << m3);
if (val & 0x02)
port_mask_set |= (1 << m2);
if (val & 0x01)
port_mask_set |= (1 << m1);
bcm2835_gpio_write_multi(port_mask_clear, LOW); // put all pins to zero
bcm2835_gpio_write_multi(port_mask_set, HIGH); // put the selected bits to one
}
int help(char *command)
{
printf("Steper motor driver based on Raspberry Pi (Version 1)\n");
#ifdef CONTROL_VER_1
printf("Step control version 1.\n\n");
#else
printf("Step control version 2.\n\n");
#endif
printf("Developed by Jesus Vasquez.\n");
#ifdef CONTROL_VER_1
printf("Usage: %s <MODE> <SPEED> <ACCE> <ACCE_F> <CURRENT_MAX>\n Where:\n <MODE> 0 = 16 microsteps; 1 = 8 microsteps; 2 = 4 microsteps; 3 = 2 microsteps;\n <SPEED> 0 = 1875 rpm; 1 = 937,5 rpm; ... ; 7 = 14,6 rpm; ... ; 15 = 0,06 rpm.\n <ACCE> 0 = without acceleration curve; 1 = with acceleration curve.\n <ACCE_F> = acceleration factor (1..5). and\n <CURRENT_MAX> 0 = 3,0 A ; 1 = 2,5 A ; 2 = 2,0 A ; 3 = 1,5 A ; 4 = 1,0 A ; 5 = 0,5 A\n", command);
#else
printf("Usage: %s <MODE> <SPEED> <ACCE> <ACCE_F> <CURRENT_MAX>\n Where:\n <MODE> 0 = 32 microsteps; 1 = 16 microsteps; 2 = 8 microsteps; 3 = 4 microsteps;\n <SPEED> 0 = 937,5 rpm; 1 = 468,8 rpm; ... ; 7 = 7,32 rpm; ... ; 15 = 0,03 rpm.\n <ACCE> 0 = without acceleration curve; 1 = with acceleration curve.\n <ACCE_F> = acceleration factor (1..5).and\n <CURRENT_MAX> 0 = 3,0 A ; 1 = 2,5 A ; 2 = 2,0 A ; 3 = 1,5 A ; 4 = 1,0 A ; 5 = 0,5 A\n", command);
#endif
return 1;
}
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
//bcm2835_set_debug(1);
uint8_t microstep_index, step_index;
uint8_t direction = 0;
uint8_t mode, speed;
uint16_t microstep_time;
uint8_t lookuptable_delta;
uint8_t microstep_max;
int microstep_count, microstep_togo, microstep_input;
uint8_t speed_var = 8;
uint16_t delay_var, delay_var_final;
uint16_t speedchange_count = 0;
uint8_t use_acceleration, acceleration_factor, current_index;
uint8_t dac_vals[17];
uint8_t dac_vals_all[] = { 0, 1, 3, 4, 6, 7, 8, 10, 11, 12, 12, 13, 14, 14, 15, 15, 15, \
0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 12, 13, 13, 13, \
0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10, 10, 10, \
0, 1, 1, 2, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, \
0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, \
0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3 };
#ifdef CONTROL_VER_1
char *mode_str[] = {"Hexal step (3200 microstep/rev)", "Octal Step (1600 microstep/rev)", "Quarter Step (800 microstep/rev)", "Double Step (400 microstep/rev)"};
char *speed_str[] = {"1875 rpm", "937,5 rpm", "468,75 rpm", "234,36 rpm", "117,19 rpm", "58,59 rpm", "29,30 rpm", "14,65 rpm", "7,32 rpm", "3,66 rpm", "1,83 rpm", "0,92 rpm", "0,46 rpm", "0,23 rpm", "0,11 rpm", "0,06 rpm"};
#else
char *mode_str[] = {"Hexal step (6400 microstep/rev)", "Octal Step (3200 microstep/rev)", "Quarter Step (1600 microstep/rev)", "Double Step (800 microstep/rev)"};
char *speed_str[] = {"937,5 rpm", "468,75 rpm", "234,38 rpm", "117,19 rpm", "58,59 rpm", "29,30 rpm", "14,65 rpm", "7,32 rpm", "3,66 rpm", "1,83 rpm", "0,92 rpm", "0,46 rpm", "0,23 rpm", "0,11 rpm", "0,06 rpm", "0,03 rpm"};
#endif
char *current_str[] = {"3,0 A", "2,5 A", "2,0 A", "1,5 A", "1,0 A", "0,5 A" };
if (argc != 6)
return help(argv[0]);
mode = atoi(argv[1]);
if (mode > 3)
return help(argv[0]);
speed = atoi(argv[2]);
if (speed > 15)
return help(argv[0]);
use_acceleration = atoi(argv[3]);
if (use_acceleration > 1)
return help(argv[0]);
acceleration_factor = atoi(argv[4]);
if ((acceleration_factor < 1) | (acceleration_factor > 5))
return help(argv[0]);
current_index = atoi(argv[5]);
if (current_index > 5)
return help(argv[0]);
if (!bcm2835_init())
{
printf("Error during BCM2835_INIT() function\n");
return 1;
}
if (use_acceleration)
{
if (speed < 8)
delay_var = 256;
else
delay_var = (1 << speed);
delay_var_final = (1 << speed);
}
else
delay_var = (1 << speed);
memcpy(dac_vals, dac_vals_all + 17*current_index, 17);
microstep_time = 10 * delay_var * (uint16_t)(1 << mode);
lookuptable_delta = (uint8_t)(1 << mode);
microstep_max = (uint8_t)(1 << (4 - mode));
printf("Steper motor driver based on Raspberry Pi (Version 1)\n");
#ifdef CONTROL_VER_1
printf("Step control version 1.\n");
#else
printf("Step control version 2.\n");
#endif
printf("Developed by Jesus Vasquez.\n");
printf("Mode selected = %s\n", mode_str[mode]);
printf("Speed selected = %s\n", speed_str[speed]);
printf("Use acceleration curve = %d\n", use_acceleration);
printf("Acceleration factor = %d\n", acceleration_factor);
printf("Maximux current set = %s\n\n", current_str[current_index]);
// Set the pin to be an output
bcm2835_gpio_fsel(WINDA_M1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDA_M2, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDA_M3, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDA_M4, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDA_DIR, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDB_M1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDB_M2, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDB_M3, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDB_M4, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WINDB_DIR, BCM2835_GPIO_FSEL_OUTP);
microstep_count = 0;
microstep_togo = 0;
while (1)
{
if (microstep_count != microstep_togo)
{
direction = (microstep_count < microstep_togo);
#ifdef CONTROL_VER_1
switch(step_index)
{
case 0:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_index * lookuptable_delta]);
break;
case 1:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[microstep_index * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
break;
case 2:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_index * lookuptable_delta]);
break;
case 3:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[microstep_index * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
break;
}
#else
switch(step_index)
{
case 0:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[microstep_max * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_index * lookuptable_delta]);
break;
case 1:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_max * lookuptable_delta]);
break;
case 2:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[microstep_index * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_max * lookuptable_delta]);
break;
case 3:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, direction);
MotorWind_SetCurrent(A, dac_vals[microstep_max * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
break;
case 4:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[microstep_max * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_index * lookuptable_delta]);
break;
case 5:
bcm2835_gpio_write(WINDA_DIR, LOW);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_max * lookuptable_delta]);
break;
case 6:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[microstep_index * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[microstep_max * lookuptable_delta]);
break;
case 7:
bcm2835_gpio_write(WINDA_DIR, HIGH);
bcm2835_gpio_write(WINDB_DIR, !direction);
MotorWind_SetCurrent(A, dac_vals[microstep_max * lookuptable_delta]);
MotorWind_SetCurrent(B, dac_vals[(microstep_max - microstep_index) * lookuptable_delta]);
break;
}
#endif
if (++microstep_index >= microstep_max)
{
microstep_index = 0;
if (++step_index >= STEP_MAX)
step_index = 0;
}
bcm2835_delayMicroseconds(microstep_time);
if(direction)
microstep_count++;
else
microstep_count--;
if (use_acceleration)
{
if (delay_var > delay_var_final)
{
if (++speedchange_count >= acceleration_factor)
{
speedchange_count = 0;
delay_var--;
microstep_time = 10 * delay_var * (uint16_t)(1 << mode);
}
}
}
}
else
{
printf("Current position: %d\n", microstep_count);
printf("Number of microstep to go?: ");
scanf("%d",µstep_input);
microstep_togo += microstep_input;
printf("Going to position %d ...\n\n", microstep_togo);
if (use_acceleration)
{
if (speed < 8)
delay_var = 256;
else
delay_var = (1 << speed);
delay_var_final = (1 << speed);
microstep_time = 10 * delay_var * (uint16_t)(1 << mode);
}
}
}
bcm2835_close();
return 0;
}