ESP_IOT v2.5
IOT ESP Coding
StepperDriver.cpp
Go to the documentation of this file.
1#include "Arduino.h"
2#include "StepperDriver.h"
3#ifdef ATOM_STEPPER_MOTOR_MODULE
4
5
6StepperDriver::StepperDriver(int number_of_steps, int step_division, int en_pin, int dir_pin, int step_pin)
7{
8 this->number_of_steps = number_of_steps;
9 this->step_division = step_division;
10 this->step_interval = 10000;
11 this->last_step_time = 0;
12 this->target_step_time1 = 0;
13 this->target_step_time2 = 0;
14
15 // Arduino pins for the motor control connection:
16 this->en_pin = en_pin;
17 this->dir_pin = dir_pin;
18 this->step_pin = step_pin;
19
20 // setup the pins on the microcontroller:
21 pinMode(en_pin, OUTPUT);
22 pinMode(dir_pin, OUTPUT);
23 pinMode(step_pin, OUTPUT);
24}
25
26/*
27 Sets the speed in revs per minute
28*/
29void StepperDriver::setSpeed(float rpm)
30{
31 step_interval = 60000000L / (number_of_steps * rpm * step_division);
32}
33
34
35
36void StepperDriver::powerEnable(bool ena)
37{
38 if (ena) {
39 digitalWrite(en_pin, LOW);
40 } else {
41 digitalWrite(en_pin, HIGH);
42 }
43}
44
45
46/*
47 Moves the motor steps_to_move steps. If the number is negative,
48 the motor moves in the reverse direction.
49 */
50void StepperDriver::step(long steps_to_move)
51{
52 steps_to_move *= step_division;
53 setDirection(steps_to_move);
54 last_step_time = micros();
55
56 for (long i = abs(steps_to_move); i > 0; i--) {
57 move();
58 }
59}
60
61
62
63void StepperDriver::step(long steps_to_move, long steps_acc, long steps_dec)
64{
65 steps_to_move *= step_division;
66 steps_acc *= step_division;
67 steps_dec *= step_division;
68 setDirection(steps_to_move);
69 last_step_time = micros();
70
71 if (steps_acc > 0) {
72 for (long i = 1; i <= steps_acc; i++) {
73 dynamicMove( i , steps_acc );
74 }
75 }
76
77 for (long i = (abs(steps_to_move) - abs(steps_acc) - abs(steps_dec)); i > 0; i--) {
78 move();
79 }
80
81 if (steps_dec > 0) {
82 for (long i = (steps_dec - 1); i >= 0; i--) {
83 dynamicMove( i , steps_dec );
84 }
85 }
86}
87
88
89
90void StepperDriver::setDirection(long steps_to_move)
91{
92 if (steps_to_move < 0) {
93 digitalWrite(dir_pin, HIGH);
94 }
95 else {
96 digitalWrite(dir_pin, LOW);
97 }
98}
99
100
101
102void StepperDriver::move()
103{
104 digitalWrite(step_pin, HIGH);
105 moveInterval(step_interval);
106}
107
108
109
110void StepperDriver::dynamicMove(int s1, int s2)
111{
112 digitalWrite(step_pin, HIGH);
113 double r1 = (double)s1 / (double)s2;
114 double r2 = 0.1 + 0.2*r1 + 2.2*r1*r1 - 1.5*r1*r1*r1;
115 moveInterval( (unsigned long)(step_interval / r2) );
116}
117
118
119
120void StepperDriver::moveInterval(unsigned long target_delay)
121{
122 target_step_time1 = last_step_time + (target_delay / 2);
123 target_step_time2 = last_step_time + target_delay;
124
125 if (target_step_time1 >= last_step_time) {
126 while (micros() < target_step_time1) {}
127 }
128 else {
129 while ((long)(micros()) < (long)target_step_time1) {}
130 }
131
132 digitalWrite(step_pin, LOW);
133
134 if (target_step_time2 >= last_step_time) {
135 while (micros() < target_step_time2) {}
136 }
137 else {
138 while ((long)(micros()) < (long)target_step_time2) {}
139 }
140 last_step_time = micros();
141}
142
143#endif //ATOM_STEPPER_MOTOR_MODULE
144