forked from madhephaestus/ESP32AnalogRead
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStepperHardwareManager.cpp
More file actions
80 lines (71 loc) · 1.9 KB
/
StepperHardwareManager.cpp
File metadata and controls
80 lines (71 loc) · 1.9 KB
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
/*
* StepperHardwareManager.cpp
*
* Created on: May 23, 2021
* Author: aksha
*/
#include <StepperHardwareManager.h>
StepperHardwareManager::StepperHardwareManager(int *hardwareDirArray,
int *hardwareStepArray, int size) {
// TODO Auto-generated constructor stub
num = size;
DegreesToHardware = stepsPerRevolution / 360 * GearRatio;
for (int i = 0; i < num; i++) {
hardwareDirMap[i] = hardwareDirArray[i];
hardwareIndexMap[i] = hardwareStepArray[i];
}
Initialize();
}
StepperHardwareManager::~StepperHardwareManager() {
// TODO Auto-generated destructor stub
DegreesToHardware = stepsPerRevolution / 360 * GearRatio;
for (int i = 0; i < num; i += 2) {
hardwareDirMap[i] = i;
hardwareIndexMap[i] = i + 1;
}
}
float StepperHardwareManager::getCurrentValue(int Pin) {
return motors[Pin]->distanceToGo();
}
//Not needed yet, left in in case we want to put stepper motor calibration
bool StepperHardwareManager::isHardwareReady() {
if (!HardwareReady) {
Initialize();
HardwareReady = true;
}
return HardwareReady;
}
///blocking?
void StepperHardwareManager::SynchronizeMove(int milSec) {
for (int i = 0; i < num; i++) {
motors[i]->moveTo(int(ValMap[i] * DegreesToHardware));
motors[i]->setSpeed(int(ValMap[i] * DegreesToHardware) / milSec);
}
//open thread to make non-blocking
do {
SyncStep();
} while (!IsMoveDone());
}
void StepperHardwareManager::SyncStep() {
for (int i = 0; i < num; i++) {
motors[i]->runSpeedToPosition();
}
}
bool StepperHardwareManager::IsMoveDone() {
for (int i = 0; i < num; i++) {
if (motors[i]->distanceToGo() != 0)
return false;
}
return true;
}
void StepperHardwareManager::Initialize() {
if (IsInitialized)
return;
motors = new AccelStepper*[num];
for (int i = 0; i < num; i++) {
pinMode(hardwareIndexMap[i], OUTPUT);
pinMode(hardwareDirMap[i], OUTPUT);
AccelStepper Temp(hardwareIndexMap[i], hardwareDirMap[i]);
motors[i] = &Temp;
}
}