-
Notifications
You must be signed in to change notification settings - Fork 17
/
FirmataStepper.h
146 lines (115 loc) · 3.83 KB
/
FirmataStepper.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
135
136
137
138
139
140
141
142
143
144
145
/*
FirmataStepper is a simple non-blocking stepper motor library
for 2 and 4 wire bipolar and unipolar stepper motor drive circuits
as well as EasyDriver (http://schmalzhaus.com/EasyDriver/) and
other step + direction drive circuits.
FirmataStepper (0.1) by Jeff Hoefs
EasyDriver support based on modifications by Chris Coleman
Acceleration / Deceleration algorithms and code based on:
app note: http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf
source code: http://www.atmel.com/dyn/resources/prod_documents/AVR446.zip
stepMotor function based on Stepper.cpp Stepper library for
Wiring/Arduino created by Tom Igoe, Sebastian Gassner
David Mellis and Noah Shibley.
Relevant notes from Stepper.cpp:
When wiring multiple stepper motors to a microcontroller,
you quickly run out of output pins, with each motor requiring 4 connections.
By making use of the fact that at any time two of the four motor
coils are the inverse of the other two, the number of
control connections can be reduced from 4 to 2.
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
connects to only 2 microcontroler pins, inverts the signals received,
and delivers the 4 (2 plus 2 inverted ones) output signals required
for driving a stepper motor.
The sequence of control signals for 4 control wires is as follows:
Step C0 C1 C2 C3
1 1 0 1 0
2 0 1 1 0
3 0 1 0 1
4 1 0 0 1
The sequence of controls signals for 2 control wires is as follows
(columns C1 and C2 from above):
Step C0 C1
1 0 1
2 1 1
3 1 0
4 0 0
The circuits can be found at
http://www.arduino.cc/en/Tutorial/Stepper
*/
// ensure this library description is only included once
#ifndef FirmataStepper_h
#define FirmataStepper_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define PI_2 2*3.14159
#define T1_FREQ 1000000L // provides the most accurate step delay values
#define T1_FREQ_148 ((long)((T1_FREQ*0.676)/100)) // divided by 100 and scaled by 0.676
// library interface description
class FirmataStepper
{
public:
FirmataStepper(byte interface = FirmataStepper::DRIVER,
int steps_per_rev = 200,
byte pin1 = 2,
byte pin2 = 3,
byte pin3 = 4,
byte pin4 = 5);
enum Interface
{
DRIVER = 1,
TWO_WIRE = 2,
FOUR_WIRE = 4
};
enum RunState
{
STOP = 0,
ACCEL = 1,
DECEL = 2,
RUN = 3
};
enum Direction
{
CCW = 0,
CW = 1
};
void setStepsToMove(long steps_to_move, int speed, int accel = 0, int decel = 0);
// update the stepper position
bool update();
byte version(void);
private:
void stepMotor(byte step_num, byte direction);
void updateStepPosition();
bool running;
byte interface; // Type of interface: DRIVER, TWO_WIRE or FOUR_WIRE
byte direction; // Direction of rotation
unsigned long step_delay; // delay between steps, in microseconds
int steps_per_rev; // number of steps to make one revolution
long step_number; // which step the motor is on
long steps_to_move; // total number of teps to move
byte stepDelay; // delay between steps (default = 1, increase for high current drivers)
byte run_state;
int accel_count;
long min_delay;
long decel_start;
int decel_val;
long lastAccelDelay;
unsigned long stepCount;
unsigned int rest;
float alpha; // PI * 2 / steps_per_rev
long at_x100; // alpha * T1_FREQ * 100
long ax20000; // alph a* 20000
float alpha_x2; // alpha * 2
// motor pin numbers:
byte dir_pin;
byte step_pin;
byte motor_pin_1;
byte motor_pin_2;
byte motor_pin_3;
byte motor_pin_4;
unsigned long last_step_time; // time stamp in microseconds of when the last step was taken
};
#endif