-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotor.ino
executable file
·116 lines (107 loc) · 2.6 KB
/
motor.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
void motor()
{
// Rewind module
if (g.rewind != 0)
{
// As long as a rewind key is being pressed, we continue to accelerate (up to the speed limit)
if (g.init == 1 && g.run == 1)
{
g.init = 0;
// Enabling the driver:
g.en = 0;
digitalWrite(ENABLE_PIN, g.en);
if (g.rewind == 1)
digitalWrite(DIR_PIN, HIGH);
else
digitalWrite(DIR_PIN, LOW);
// First time step - corresponds to the initial rewind speed:
g.dt = DT_INIT;
g.t_step = g.t + (unsigned long int)g.dt;
lcd.setCursor(0, 5 * ROW);
if (g.rewind == -1)
sprintf(g.buffer, " <--");
else
sprintf(g.buffer, " --> ");
lcd.print(g.buffer);
lcd.display();
}
else
{
if (g.t >= g.t_step)
{
// Making a microstep:
delayMicroseconds(2);
digitalWrite(STEP_PIN, 1);
delayMicroseconds(2);
digitalWrite(STEP_PIN, 0);
if (g.run == 1)
// Constant acceleration; next time step in us:
{
g.dt = g.dt / (1.0 + ACCEL * g.dt * g.dt);
if (g.dt < DT_MIN)
// Maximum speed limit:
g.dt = DT_MIN;
}
else
// Constant decelaration:
{
g.dt = g.dt / (1.0 - ACCEL * g.dt * g.dt);
if (g.dt > DT_INIT || g.dt < 1e-20)
// We slowed down enough, and will stop now:
{
g.rewind = 0;
if (g.en == 0)
// Disabling the motor:
{
g.en = 1;
digitalWrite(ENABLE_PIN, g.en);
}
Display(-1);
return;
}
}
g.t_step = g.t + g.dt;
}
}
return;
}
if (g.run == 0)
return;
if (g.init == 1)
{
g.init = 0;
// Enabling the driver:
g.en = 0;
digitalWrite(ENABLE_PIN, g.en);
digitalWrite(DIR_PIN, HIGH);
g.t0 = g.t;
g.i_step = 0;
g.t_step = g.t0 + (unsigned long int)g.dt_microstep;
lcd.setCursor(0, 5 * ROW);
sprintf(g.buffer, " 0");
lcd.print(g.buffer);
lcd.display();
}
// Making a microstep every DT_MICROSTEP
if (g.t >= g.t_step)
{
// End of shooting; disabling the motor
if (g.i_step > g.last_step)
{
g.run = 0;
if (g.en == 0)
{
g.en = 1;
digitalWrite(ENABLE_PIN, g.en);
}
return;
}
// Making a microstep:
delayMicroseconds(2);
digitalWrite(STEP_PIN, 1);
delayMicroseconds(2);
digitalWrite(STEP_PIN, 0);
g.i_step++;
g.t_step = g.t0 + (unsigned long int)(g.i_step * g.dt_microstep);
}
}