-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepio.cpp
More file actions
277 lines (227 loc) · 8.63 KB
/
Stepio.cpp
File metadata and controls
277 lines (227 loc) · 8.63 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "Stepio.h"
#include "Arduino.h"
/*
* this function turn the stepper motor S step if S>0
* the motor trun in clockwise else the motor turn in
* counter-clockwise
*/
void Stepper::move(int S) {
digitalWrite(this->diraction_pin, S < 0 ? HIGH : LOW); // if S<0 give diraction_pin 5V else 0V
this->dir = S > 0 ? 1 : -1; // if S<0 dir=-1 else dir=1
this->totalSteps = abs(S); // set totalSteps to absulte value of S
this->d = this->c0; // set delay variable to first delay value
OCR1A = this->d; // set timer1 Compare A to d value
this->stepCount = 0; // set step counter to 0
this->n = 0; // set n value to 0
this->rampUpStepCount = 0; // set ramp up step counter to 0
this->movementDone = false; // set movementDone flag to false
TIMSK1 |= (1 << OCIE1A); // enbale Compare A interupt
while (! this->movementDone ); // wait until the movement deserved is done
}
/*
* this function define drive parametre number of step in rotation
* mm in one rotation
*/
void Stepper::SetHardWare(int rotation, int mm_rev) {
this->rotation = rotation; // config number step by rotation
this->mm_rev=mm_rev; // config distance spend in mm in one motor evolution
}
/*
* the constructor method of the object that define the pins of
* the stepper
*/
Stepper::Stepper(int enable_pin, int step_pin, int diraction_pin) {
this->enable_pin = enable_pin; // config the enable pin
this->step_pin = step_pin; // config step pin
this->diraction_pin = diraction_pin; // config diraction pin
this->c0 = 1600; // set value of to 1600
}
/*
* this fuction is called to setup motor in void setup
*/
void Stepper::setup_step(){
pinMode(this->step_pin, OUTPUT); // set step pin to OUTPUT mode
pinMode(this->diraction_pin, OUTPUT); // set diraction pin to OUTPUT mode
pinMode(this->enable_pin, OUTPUT); // set enable pin to OUTPUT mode
digitalWrite(this->enable_pin, 0); // give enable 0V
cli(); // disable interuption
TCCR1A=0; // initial TCCR1A register to 0x00
TCCR1B=0; // initial TCCR1B register to 0x00
TCNT1=0; // set TIMER1 counter to 0
OCR1A = 1000; // set the comparator to 1000
TCCR1B |= (1 << WGM12); // set the clock to comparative mode
TCCR1B |= ((1 << CS11) | (1 << CS10)); // set prescaler clk/64
sei(); // enable interuption
}
/*
* this method is used to move stepper with muliple mode
*
* 0: step
* 1: °
* 2: rotation
* 3: mm
*/
void Stepper::write(char mode, int parametre) {
switch (mode) {
case 0:
this->move(parametre); // call move method
this->step += parametre; // increase number of steps
break;
case 1:
this->move(this->rotation / (360 / parametre)); // call move method
this->step += this->rotation / (360 / (parametre)); // increase number of steps
break;
case 2:
this->move(parametre*this->rotation); // call move method
this->step += (parametre) * this->rotation; // increase number of steps
break;
case 3:
this->move((this->mm_rev*this->rotation)/parametre); // call move method
this->step += ((this->mm_rev*this->rotation)/(parametre)); // increase number of steps
break;
}
this->stepPosition = this->step % this->rotation ; // update the position of motor in step
this->n_rotation = this->step / this->rotation; // update the number of rotation done by the motor
this->angle = 360/((double)this->rotation/this->stepPosition); // update the position of motor in °
this->mm = ((double)this->step/this->rotation)*this->mm_rev; // update the distance that motor parcoured
}
/*
* like write() but on one rotation
*/
void Stepper::writeAbs(char mode, int parametre) {
if (this->step / this->rotation < 1) { // check if one evolution is done
this->write(mode, parametre); // write the diserved move
}
}
/*
* this method return the initial position of the stepper
*/
int Stepper::getposition(char mode) {
switch (mode) {
case 0:
return this->stepPosition; // return the current position of motor in step
break;
case 1:
return this->angle; // return the current position of motor in °
break;
case 2:
return this->n_rotation; // return the number of evolution done by the motor
break;
case 3:
return this->mm; // return the distance spend by the motor in mm
break;
}
}
/*
* the method set the default position of the
* stepper
*
* char mode : type of speed
* 0: step/s
* 1: °/s
* 2: rotation/s
* 3: mm/s
* int parametre : new position of motor
*/
void Stepper::setposition(char mode, int parametre) {
switch (mode) {
case 0:
this->pos_init = parametre; // set position in step
break;
case 1:
this->angle_init = parametre; // set position in °
break;
case 3:
this->mm_init = parametre; // set position in mm
break;
}
}
/*
* this function is used to set the speed
* of the stepper motor
*
* int parametre : new speed of motor
* char mode : type of speed
* 0: step/s
* 1: °/s
* 2: rotation/s
* 3: mm/s
*/
void Stepper::setSpeed(char mode, int parametre) {
switch (mode) {
case 0:
this->maxSpeed = parametre; // set speed in step/s
break;
case 1:
this->maxSpeed = parametre * (this->rotation / 360); // set speed in °/s
break;
case 2:
this->maxSpeed = parametre * this->rotation; // set speed in rotation/s
break;
case 3:
this->maxSpeed = parametre / this->mm_rev; // set speed in mm/s
break;
}
}
/*
* this function return the speed of the motor
*
* char mode: type of speed
* 0: step/s
* 1: °/s
* 2: rotation/s
* 3: mm/s
*/
int Stepper::getSpeed(char mode) {
switch (mode) {
case 0:
return this->maxSpeed; // return speed in step/s
break;
case 1:
return this->maxSpeed / (this->rotation / 360); // return speed in °/s
break;
case 2:
return this->maxSpeed / this->rotation; // return speed in rotition/s
break;
case 3:
return (this->maxSpeed / this->rotation) * this->mm_rev; // return speed in mm/s
break;
}
}
/*
* this function is called in every timer interrution
* that make stepper motor turn smothly
*/
void Stepper::interrupt(){
if ( this->stepCount < this->totalSteps ) {
// pulse the step pin to turn one steppe
digitalWrite(this->step_pin, 1); // set step pin to high
digitalWrite(this->step_pin, 0); // set step pin to low
// increase the step counter
this->stepCount++; // increase step counter
}
else {
this->movementDone = true; // set movementDone flag to True
TIMSK1 &= ~(1 << OCIE1A); // disable Compare A Match Interrupt
this->d = this->maxSpeed; // set delay value to maxspeed
}
// -------------------- ramp up phase --------------------
if ( this->rampUpStepCount == 0 ) {
this->n++;
this->d = this->d - (2 * this->d) / (4 * this->n + 1);
if ( this->d <= this->maxSpeed ) { // reached max speed
this->d = this->maxSpeed; // set delay value to maxspeed
this->rampUpStepCount = this->stepCount; // set rampup phase counter to step counter value
}
// ---------------- reached halfway point ----------------
if ( this->stepCount >= this->totalSteps / 2 ) {
this->rampUpStepCount = this->stepCount; // set rampup phase counter to step counter value
}
}
// -------------------- ramp down phase --------------------
else if ( this->stepCount >= this->totalSteps - this->rampUpStepCount ) {
this->n--; //
this->d = (this->d * (4 * this->n + 1)) / (4 * this->n + 1 - 2); // set delay value to
}
OCR1A = this->maxSpeed + this->d; // set the TIMER1 Comparator to maxspeed + delay value calculated
}