This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CNCAxis.cpp
66 lines (59 loc) · 1.48 KB
/
CNCAxis.cpp
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
#include "CNCAxis.h"
CNCAxis::CNCAxis()
{}
CNCAxis::CNCAxis(int encoderPinA, int encoderPinB, int pinDir, int pinStep, int pinEndstop, int pinEnable)
{
_pinDir = pinDir;
_pinStep = pinStep;
_pinEndstop = pinEndstop;
_pinEnable = pinEnable;
_encoder = new Encoder(encoderPinA, encoderPinB);
pinMode(_pinDir, OUTPUT);
pinMode(_pinStep, OUTPUT);
pinMode(_pinEndstop, INPUT);
pinMode(_pinEnable, OUTPUT);
}
void CNCAxis::Enable()
{
digitalWrite(_pinEnable, HIGH);
}
void CNCAxis::Disable()
{
digitalWrite(_pinEnable, LOW);
}
long CNCAxis::GetPosition(AccelStepper * _stepper)
{
#ifdef useEncoders
return _encoder->read();
#else
return _stepper->currentPosition();
#endif
}
void CNCAxis::StepForward(AccelStepper * _stepper)
{
Enable();
long pos = _stepper->currentPosition();
digitalWrite(_pinDir, HIGH);
while (pos == _stepper->currentPosition()) {
digitalWrite(_pinStep, HIGH);
delayMicroseconds(_stepper->getMinPulseWidth());
digitalWrite(_pinStep, LOW);
#ifndef useEncoders
_stepper->setCurrentPosition(_stepper->currentPosition() + 1);
#endif
}
}
void CNCAxis::StepBackward(AccelStepper * _stepper)
{
Enable();
long pos = _stepper->currentPosition();
digitalWrite(_pinDir, LOW);
while (pos == _stepper->currentPosition()) {
digitalWrite(_pinStep, HIGH);
delayMicroseconds(_stepper->getMinPulseWidth());
digitalWrite(_pinStep, LOW);
#ifndef useEncoders
_stepper->setCurrentPosition(_stepper->currentPosition() - 1);
#endif
}
}