forked from TurBoss/turbo-grua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RampsStepper.cpp
87 lines (73 loc) · 2.09 KB
/
RampsStepper.cpp
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
#include "RampsStepper.h"
#include <Arduino.h>
RampsStepper::RampsStepper(int aStepPin, int aDirPin, int aEnablePin) {
setReductionRatio(1, 3200); // 16 * 200
//setReductionRatio(1, 200);
stepPin = aStepPin;
dirPin = aDirPin;
enablePin = aEnablePin;
stepperStepPosition = 0;
stepperStepTargetPosition = 0;
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
disable();
}
void RampsStepper::enable(bool value) {
digitalWrite(enablePin, !value);
}
void RampsStepper::disable() {
digitalWrite(enablePin, HIGH);
}
bool RampsStepper::isOnPosition() const {
return stepperStepPosition == stepperStepTargetPosition;
}
int RampsStepper::getPosition() const {
return stepperStepPosition;
}
void RampsStepper::setPosition(int value) {
stepperStepPosition = value;
stepperStepTargetPosition = value;
}
void RampsStepper::stepToPosition(int value) {
stepperStepTargetPosition = value;
}
void RampsStepper::stepRelative(int value) {
value += stepperStepPosition;
stepToPosition(value);
}
float RampsStepper::getPositionRad() const {
return stepperStepPosition / radToStepFactor;
}
void RampsStepper::setPositionRad(float rad) {
setPosition(rad * radToStepFactor);
}
void RampsStepper::stepToPositionRad(float rad) {
stepperStepTargetPosition = rad * radToStepFactor;
}
void RampsStepper::stepRelativeRad(float rad) {
stepRelative(rad * radToStepFactor);
}
void RampsStepper::update() {
while (stepperStepTargetPosition < stepperStepPosition) {
digitalWrite(dirPin, HIGH);
delayMicroseconds(5);
digitalWrite(stepPin, HIGH);
delayMicroseconds(20);
digitalWrite(stepPin, LOW);
delayMicroseconds(5);
stepperStepPosition--;
}
while (stepperStepTargetPosition > stepperStepPosition) {
digitalWrite(dirPin, LOW);
delayMicroseconds(5);
digitalWrite(stepPin, HIGH);
delayMicroseconds(20);
digitalWrite(stepPin, LOW);
delayMicroseconds(5);
stepperStepPosition++;
}
}
void RampsStepper::setReductionRatio(float gearRatio, int stepsPerRev) {
radToStepFactor = gearRatio * stepsPerRev / 2 / PI;
};