-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepper.h
134 lines (99 loc) · 3.36 KB
/
stepper.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _STEPPER_H
#define _STEPPER_H
#include "WProgram.h"
#define MAX_STEPPERS 4
enum STOP_MODES {
S_KEEP_ENABLED,
S_DISABLE,
};
enum HOME_DIRECTION {
H_FORWARD,
H_BACKWARD,
};
enum STEPPER_STATE {
S_READY,
S_MOVING,
S_FINISHED_MOVING,
S_HOMING_A,
S_HOMING_B,
S_FINISHED_HOMING,
S_USER_STOP,
S_ERROR,
};
class Stepper {
// Global stepper stuff
public:
static void doStepperInterrupts();
static uint8_t count();
// Returns true if the specified stepper index is in bounds
static boolean indexValid(const int index);
static Stepper& getStepper(const int index);
static void setup(unsigned int frequency_);
static int saveSettings(int offset);
static int restoreSettings(int offset);
private:
// static Stepper* registeredSteppers[MAX_STEPPERS];
static uint8_t stepperCount;
static unsigned int frequency;
static boolean registerStepper(Stepper* stepper_);
// Instance-specific stepper stuff
public:
// TODO: Implement this
Stepper(uint8_t enablePin_, uint8_t stepPin_, uint8_t directionPin_);
Stepper(uint8_t enablePin_, uint8_t stepPin_, uint8_t directionPin_, uint8_t limitPin_);
void doReset();
// newPosition Position to move to, in stepper counts
// ticks Time it should take to get there, in milliseconds
boolean moveAbsolute(long newPosition, long& time);
// steps Steps to take, in stepper counts
// ticks Time it should take to move, in milliseconds
boolean moveRelative(long steps, long& time);
// Find the limit switch, and set it to the 0 position
boolean home();
// Unconditionally stops the stepper
void stop();
// Get the position
long getPosition();
long getMaxVelocity();
boolean setMaxVelocity(long velocity_);
long getAcceleration();
boolean setAcceleration(long acceleration_);
STOP_MODES getStopMode();
boolean setStopMode(STOP_MODES stopMode_);
// Reset the position, only works if the stepper is not in motion
boolean setPosition(long position_);
boolean busy(); //< TRUE if stepper is moving
boolean checkFinished(); //< TRUE if the device just finished moving. Clears the
//< finished bit if set.
private:
// Parameters in this class are persistant across reset
struct stepperSettings {
long maxVelocity;
long acceleration;
STOP_MODES stopMode; // Determines if the motors should be disabled when not moving
HOME_DIRECTION homeDirection; // Determines which direction the axis should search for home in
boolean canHome; // True if the axis supports homing
stepperSettings(long maxVelocity_, long acceleration_, STOP_MODES stopMode_, HOME_DIRECTION homeDirection_, boolean canHome_) {
maxVelocity = maxVelocity_;
acceleration = acceleration_;
stopMode = stopMode_;
homeDirection = homeDirection_;
canHome = canHome_;
}
};
stepperSettings settings;
uint8_t enablePin;
uint8_t stepPin;
uint8_t directionPin;
uint8_t limitPin;
STEPPER_STATE state;
boolean forceStop; //< Flag to signal to cause the interrupt task to sop motion
long position; //< Current position
long stepsLeft; //< Number of counts until final position is reached
int8_t direction; //< Direction we are heading 1=pos, -1=neg
long deltax;
long deltay;
long error;
void doInterrupt();
};
#endif