forked from m4k3r-org/simple-stepper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleStepper.h
89 lines (72 loc) · 2.52 KB
/
SimpleStepper.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
/* vim: set ft=c : */
/**
* Library to handle 28byj-48 motor steppers.
*
* https://github.com/minirobots/simple-stepper
*
* Gear Reduction ratio: 1 / 64 (Not really exact: probably 63.68395.:1 )
* Step Angle (Full Step sequence: Internal Motor alone): 11.25° (32 steps per revolution)
* Step Angle (Half Step sequence: Internal Motor alone): 5.625° (64 steps per revolution)
* SO: it takes 32*64 = 2048 steps per output shaft revolution.. In Full Step sequence.
* SO: it takes 64*64 = 4096 steps per output shaft revolution.. In Half Step sequence.
*
* Useful links:
* - 28BYJ-48 Stepper Motor and ULN2003 Driver Intro
* https://youtu.be/B86nqDRskVU
* - 28BYJ-48: Small Stepper Motor and Driver Board
* https://arduino-info.wikispaces.com/SmallSteppers
*
*
* Author: Leo Vidarte <http://nerdlabs.com.ar>
*
* This is free software:
* you can redistribute it and/or modify it
* under the terms of the GPL version 3
* as published by the Free Software Foundation.
*/
#include <Arduino.h>
#ifndef SimpleStepper_h
#define SimpleStepper_h
#define STEPS_PER_MOTOR_REVOLUTION_FS 32
#define STEPS_PER_OUTPUT_REVOLUTION_FS 2038 // ~(32 * 63.68395)
#define STEPS_PER_MOTOR_REVOLUTION_HS 64
#define STEPS_PER_OUTPUT_REVOLUTION_HS 4076 // ~(64 * 63.68395)
class SimpleStepper
{
public:
// secuences
static const uint8_t WAKEDRIVE = 0;
static const uint8_t FULLSTEP = 1;
static const uint8_t HALFSTEP = 2;
SimpleStepper(uint8_t sequence, bool clockwise,
uint8_t pin1, uint8_t pin2,
uint8_t pin3, uint8_t pin4);
void setDirection(bool clockwise) { _clockwise = clockwise; };
bool getDirection() { return _clockwise; };
void setStep(int steps);
int getStep() { return _positive? _steps : -_steps; };
void step();
void offCoils();
bool ready();
int getFullRotationSteps();
private:
void _writeMotor(int step);
int _steps = 0; // absolute steps
uint8_t _actualStep = 0; // values: 0 - 7
uint8_t _pins[4]; // motor pins
uint8_t _sequence; // the motor sequence
bool _clockwise; // direction of rotation when _positive is true
bool _positive; // sign of _steps
const uint8_t _motorSequences[3][8] = {
// wake drive
{B01000, B00100, B00010, B00001,
B01000, B00100, B00010, B00001},
// full step
{B01100, B00110, B00011, B01001,
B01100, B00110, B00011, B01001},
// half step
{B01000, B01100, B00100, B00110,
B00010, B00011, B00001, B01001}
};
};
#endif