User | Revision | Line number | New contents of line |
tbjazic |
0:12be56dc6182
|
1
|
#include "mbed.h"
|
tbjazic |
0:12be56dc6182
|
2
|
#include "StepperDriver.h"
|
tbjazic |
0:12be56dc6182
|
3
|
|
tbjazic |
0:12be56dc6182
|
4
|
StepperDriver::StepperDriver(PinName outputPin, PinName directionPin) : output(outputPin), direction(directionPin) {
|
tbjazic |
0:12be56dc6182
|
5
|
currentPosition = previousPosition = desiredPosition = 0;
|
Ibiltari |
4:f8dd2bc1ec7d
|
6
|
configure(1.8f, 0, 800);
|
tbjazic |
0:12be56dc6182
|
7
|
homePosition = (maxPosition - minPosition) / 2;
|
tbjazic |
2:70f593d34039
|
8
|
setSpeedSPS(250);
|
tbjazic |
2:70f593d34039
|
9
|
setPulseWidth(100e-6);
|
tbjazic |
0:12be56dc6182
|
10
|
}
|
tbjazic |
0:12be56dc6182
|
11
|
|
tbjazic |
1:9888802e71b9
|
12
|
void StepperDriver::configure(uint32_t resolution, float minDegrees, float maxDegrees) {
|
tbjazic |
1:9888802e71b9
|
13
|
setResolution(resolution);
|
tbjazic |
1:9888802e71b9
|
14
|
setDegreesRange(minDegrees, maxDegrees);
|
tbjazic |
1:9888802e71b9
|
15
|
}
|
tbjazic |
1:9888802e71b9
|
16
|
|
tbjazic |
1:9888802e71b9
|
17
|
void StepperDriver::configure(float resolution, float minDegrees, float maxDegrees) {
|
tbjazic |
1:9888802e71b9
|
18
|
setResolution(resolution);
|
tbjazic |
1:9888802e71b9
|
19
|
setDegreesRange(minDegrees, maxDegrees);
|
tbjazic |
1:9888802e71b9
|
20
|
}
|
tbjazic |
1:9888802e71b9
|
21
|
|
tbjazic |
1:9888802e71b9
|
22
|
void StepperDriver::setPosition(float desiredDegrees) {
|
tbjazic |
1:9888802e71b9
|
23
|
uint32_t position = desiredDegrees / degreesPerStep + 0.5f; // rounding
|
tbjazic |
0:12be56dc6182
|
24
|
if (position >= minPosition && position <= maxPosition) {
|
tbjazic |
0:12be56dc6182
|
25
|
desiredPosition = position;
|
tbjazic |
0:12be56dc6182
|
26
|
}
|
tbjazic |
0:12be56dc6182
|
27
|
if (desiredPosition != currentPosition && !isTickerAttached) {
|
tbjazic |
0:12be56dc6182
|
28
|
attachTicker();
|
tbjazic |
0:12be56dc6182
|
29
|
}
|
tbjazic |
0:12be56dc6182
|
30
|
}
|
tbjazic |
0:12be56dc6182
|
31
|
|
tbjazic |
1:9888802e71b9
|
32
|
float StepperDriver::getPosition() {
|
tbjazic |
1:9888802e71b9
|
33
|
return currentPosition * degreesPerStep;
|
tbjazic |
1:9888802e71b9
|
34
|
}
|
tbjazic |
1:9888802e71b9
|
35
|
|
tbjazic |
0:12be56dc6182
|
36
|
void StepperDriver::update() {
|
tbjazic |
0:12be56dc6182
|
37
|
if (desiredPosition > currentPosition) {
|
tbjazic |
0:12be56dc6182
|
38
|
direction = 1;
|
tbjazic |
0:12be56dc6182
|
39
|
currentPosition++;
|
tbjazic |
0:12be56dc6182
|
40
|
generateImpulse();
|
tbjazic |
0:12be56dc6182
|
41
|
} else if (desiredPosition < currentPosition) {
|
tbjazic |
0:12be56dc6182
|
42
|
direction = 0;
|
tbjazic |
0:12be56dc6182
|
43
|
currentPosition--;
|
tbjazic |
0:12be56dc6182
|
44
|
generateImpulse();
|
tbjazic |
0:12be56dc6182
|
45
|
} else {
|
tbjazic |
0:12be56dc6182
|
46
|
detachTicker();
|
tbjazic |
0:12be56dc6182
|
47
|
}
|
tbjazic |
0:12be56dc6182
|
48
|
previousPosition = currentPosition;
|
tbjazic |
0:12be56dc6182
|
49
|
}
|
tbjazic |
0:12be56dc6182
|
50
|
|
tbjazic |
0:12be56dc6182
|
51
|
void StepperDriver::attachTicker() {
|
tbjazic |
1:9888802e71b9
|
52
|
ticker.attach(this, &StepperDriver::update, 1/speed);
|
tbjazic |
0:12be56dc6182
|
53
|
isTickerAttached = true;
|
tbjazic |
0:12be56dc6182
|
54
|
}
|
tbjazic |
0:12be56dc6182
|
55
|
|
tbjazic |
0:12be56dc6182
|
56
|
void StepperDriver::detachTicker() {
|
tbjazic |
0:12be56dc6182
|
57
|
ticker.detach();
|
tbjazic |
0:12be56dc6182
|
58
|
isTickerAttached = false;
|
tbjazic |
0:12be56dc6182
|
59
|
}
|
tbjazic |
0:12be56dc6182
|
60
|
|
tbjazic |
0:12be56dc6182
|
61
|
void StepperDriver::generateImpulse() {
|
tbjazic |
0:12be56dc6182
|
62
|
output = 1;
|
tbjazic |
1:9888802e71b9
|
63
|
timeout.attach(this, &StepperDriver::turnOutputOff, pulseWidth);
|
tbjazic |
0:12be56dc6182
|
64
|
}
|
tbjazic |
0:12be56dc6182
|
65
|
|
tbjazic |
0:12be56dc6182
|
66
|
void StepperDriver::turnOutputOff() {
|
tbjazic |
0:12be56dc6182
|
67
|
output = 0;
|
tbjazic |
1:9888802e71b9
|
68
|
}
|
tbjazic |
1:9888802e71b9
|
69
|
|
tbjazic |
1:9888802e71b9
|
70
|
void StepperDriver::setResolution(uint32_t resolution) {
|
tbjazic |
1:9888802e71b9
|
71
|
degreesPerStep = 360.0f / resolution;
|
tbjazic |
1:9888802e71b9
|
72
|
}
|
tbjazic |
1:9888802e71b9
|
73
|
|
tbjazic |
1:9888802e71b9
|
74
|
void StepperDriver::setResolution(float resolution) {
|
tbjazic |
1:9888802e71b9
|
75
|
degreesPerStep = resolution;
|
tbjazic |
1:9888802e71b9
|
76
|
}
|
tbjazic |
1:9888802e71b9
|
77
|
|
tbjazic |
1:9888802e71b9
|
78
|
void StepperDriver::setDegreesRange(float minDegrees, float maxDegrees) {
|
tbjazic |
1:9888802e71b9
|
79
|
minPosition = 0;
|
tbjazic |
1:9888802e71b9
|
80
|
maxPosition = minPosition + 1/degreesPerStep * (maxDegrees - minDegrees);
|
tbjazic |
1:9888802e71b9
|
81
|
}
|
tbjazic |
1:9888802e71b9
|
82
|
|
tbjazic |
1:9888802e71b9
|
83
|
void StepperDriver::setSpeedSPS(float speedSPS) {
|
tbjazic |
1:9888802e71b9
|
84
|
speed = speedSPS;
|
tbjazic |
1:9888802e71b9
|
85
|
}
|
tbjazic |
1:9888802e71b9
|
86
|
|
tbjazic |
1:9888802e71b9
|
87
|
void StepperDriver::setSpeedRPS(float speedRPS) {
|
tbjazic |
1:9888802e71b9
|
88
|
speed = speedRPS * 360 / degreesPerStep;
|
tbjazic |
1:9888802e71b9
|
89
|
}
|
tbjazic |
1:9888802e71b9
|
90
|
|
tbjazic |
1:9888802e71b9
|
91
|
void StepperDriver::setSpeedRPM(float speedRPM) {
|
tbjazic |
1:9888802e71b9
|
92
|
speed = speedRPM / 60 * 360 / degreesPerStep;
|
tbjazic |
1:9888802e71b9
|
93
|
}
|
tbjazic |
1:9888802e71b9
|
94
|
|
tbjazic |
1:9888802e71b9
|
95
|
float StepperDriver::getSpeedSPS() {
|
tbjazic |
1:9888802e71b9
|
96
|
return speed;
|
tbjazic |
1:9888802e71b9
|
97
|
}
|
tbjazic |
1:9888802e71b9
|
98
|
|
tbjazic |
1:9888802e71b9
|
99
|
float StepperDriver::getSpeedRPS() {
|
tbjazic |
1:9888802e71b9
|
100
|
return speed * degreesPerStep / 360;
|
tbjazic |
1:9888802e71b9
|
101
|
}
|
tbjazic |
1:9888802e71b9
|
102
|
|
tbjazic |
1:9888802e71b9
|
103
|
float StepperDriver::getSpeedRPM() {
|
tbjazic |
1:9888802e71b9
|
104
|
return speed * degreesPerStep / 360 * 60;
|
tbjazic |
2:70f593d34039
|
105
|
}
|
tbjazic |
2:70f593d34039
|
106
|
|
tbjazic |
2:70f593d34039
|
107
|
void StepperDriver::setPulseWidth(float pulseWidth) {
|
tbjazic |
2:70f593d34039
|
108
|
if (pulseWidth > 0) {
|
tbjazic |
2:70f593d34039
|
109
|
this->pulseWidth = pulseWidth;
|
tbjazic |
2:70f593d34039
|
110
|
}
|
tbjazic |
0:12be56dc6182
|
111
|
} |