-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.c
142 lines (111 loc) · 3.34 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
#include <bcm2835.h>
#include <stdio.h>
#define MOTOR_CS RPI_V2_GPIO_P1_22
#define MOTOR_DIR RPI_V2_GPIO_P1_24
#define MOTOR_STEP RPI_V2_GPIO_P1_26
int help(char *command)
{
printf("Steper motor driver based on Raspberry Pi + PIC (Version 1)\n");
printf("Developed by Jesus Vasquez.\n");
printf("Usage: %s <SPEED> <ACCE> <ACCE_F>\n Where:\n <SPEED> 0 = 250 rpm; 1 = 125 rpm; ... ; 7 = 1,95 rpm.\n <ACCE> 0 = without acceleration curve; 1 = with acceleration curve. and\n <ACCE_F> = acceleration factor (1..5)\n", command);
return 1;
}
int main(int argc, char **argv)
{
uint8_t microstep_index, step_index;
uint8_t direction = 0;
uint8_t speed;
uint16_t microstep_time;
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;
char *speed_str[] = {"250 rpm", "125 rpm", "62,5 rpm", "31,25 rpm", "15,64 rpm", "7,81 rpm", "3,91 rpm", "1,95 rpm"};
if (argc != 4)
return help(argv[0]);
speed = atoi(argv[1]);
if (speed > 7)
return help(argv[0]);
use_acceleration = atoi(argv[2]);
if (use_acceleration > 1)
return help(argv[0]);
acceleration_factor = atoi(argv[3]);
if ((acceleration_factor < 1) | (acceleration_factor > 5))
return help(argv[0]);
if (!bcm2835_init())
{
printf("Error during BCM2835_INIT() function\n");
return 1;
}
if (use_acceleration)
{
delay_var = 128;
delay_var_final = (1 << speed);
}
else
delay_var = (1 << speed);
microstep_time = 150 * delay_var;
printf("Steper motor driver based on Raspberry Pi + PIC (Version 1)\n");
printf("Developed by Jesus Vasquez.\n");
printf("Speed selected = %s\n", speed_str[speed]);
printf("Use acceleration curve = %d\n", use_acceleration);
printf("Acceleration factor = %d\n", acceleration_factor);
// Set the pin to be an output
bcm2835_gpio_fsel(MOTOR_CS, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(MOTOR_DIR, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(MOTOR_STEP, BCM2835_GPIO_FSEL_OUTP);
microstep_count = 0;
microstep_togo = 0;
bcm2835_gpio_write(MOTOR_CS, HIGH);
while (1)
{
if (microstep_count != microstep_togo)
{
bcm2835_gpio_write(MOTOR_CS, LOW);
direction = (microstep_count < microstep_togo);
bcm2835_gpio_write(MOTOR_DIR, direction);
bcm2835_gpio_write(MOTOR_STEP, HIGH);
bcm2835_delayMicroseconds(microstep_time);
bcm2835_gpio_write(MOTOR_STEP, LOW);
bcm2835_delayMicroseconds(microstep_time);
if(direction)
microstep_count++;
else
microstep_count--;
if (use_acceleration)
{
if (delay_var > delay_var_final)
{
speedchange_count += 10;
if (speedchange_count >= acceleration_factor)
{
speedchange_count = 0;
delay_var--;
microstep_time = 150 * delay_var;
}
}
}
}
else
{
bcm2835_gpio_write(MOTOR_CS, HIGH);
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 = 150 * delay_var;
}
}
}
bcm2835_close();
return 0;
}