-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStepperControl.h
More file actions
115 lines (95 loc) · 2.71 KB
/
StepperControl.h
File metadata and controls
115 lines (95 loc) · 2.71 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
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
/**
* Library for 28byj-48 Stepper Motor
*
* @author Erhan Yilgör
*/
#ifndef StepperControl_h
#define StepperControl_h
#include "Arduino.h"
typedef void (StepperCallback)();
class StepperControl
{
public:
static const uint32_t DefaultStepCount = 4096;
enum StepDirection
{
Forward,
Backward
};
enum StepType
{
WaveDrive,
FullStep,
HalfStep
};
StepperControl(const uint32_t rotationStepCount, const uint8_t pin1, const uint8_t pin2, const uint8_t pin3, const uint8_t pin4);
~StepperControl();
void SetFullRotationStepCount(const uint32_t rotationSteps);
void SetDirection(const StepDirection direction);
void SetRotationSpeed(const double rpm);
void SetStepType(const StepType type);
void Step(const uint32_t steps);
void RemoveAllActions();
void StartAction();
inline StepType GetCurrentStepType()
{
return _stepType;
}
inline StepDirection GetInvertedDirection()
{
return _direction == Forward ? Backward : Forward;
}
inline StepDirection GetCurrentDirection()
{
return _direction;
}
inline uint32_t GetCurrentFullRotationStepCount()
{
return _rotationSteps;
}
inline double GetCurrentRotationSpeed()
{
return _rpm;
}
inline static uint32_t GetStepsFromDegreesForSettings(const double degrees, const StepType type, const uint32_t roationSteps = DefaultStepCount)
{
const double rotation = degrees / 360.0;
return rotation * (roationSteps / (type == WaveDrive ? 2L : 1L));
}
inline uint32_t GetStepsFromDegrees(const double degrees)
{
return GetStepsFromDegreesForSettings(degrees, _stepType, _rotationSteps);
}
struct StepperAction
{
public:
StepType Type;
StepDirection Direction;
uint32_t Steps;
uint32_t StartDelay;
uint32_t EndDelay;
double Rpm;
StepperCallback* DidEndCallback;
};
void AddStepperAction(const StepperAction& action);
void AddStepperActionArray(const StepperAction actions[], const uint8_t count);
static void CopyActionValuesFromTo(const StepperAction& source, StepperAction& destination);
private:
void Think();
void ThinkAction();
void ThinkWaveDrive();
void ThinkFullStep();
void ThinkHalfStep();
uint8_t _pins[4];
int _steps;
StepDirection _direction;
StepType _stepType;
uint32_t _rotationSteps;
double _rpm;
unsigned long _stepDelay;
unsigned long _lastStepTime;
StepperAction* _stepperActions;
uint8_t _currentActionIndex;
uint8_t _actionCount;
};
#endif