forked from AllYarnsAreBeautiful/ayab-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoders.cpp
More file actions
144 lines (115 loc) · 3.33 KB
/
encoders.cpp
File metadata and controls
144 lines (115 loc) · 3.33 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
// encoders.cpp
/*
This file is part of AYAB.
AYAB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AYAB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AYAB. If not, see <http://www.gnu.org/licenses/>.
Copyright 2013-2015 Christian Obersteiner, Andreas Müller
http://ayab-knitting.com
*/
#include "Arduino.h"
#include "./encoders.h"
Encoders::Encoders() {
m_direction = NoDirection;
m_hallActive = NoDirection;
m_beltShift = Unknown;
m_carriage = NoCarriage;
m_encoderPos = 0x00;
}
void Encoders::encA_interrupt() {
m_hallActive = NoDirection;
static bool _oldState = false;
bool _curState = digitalRead(ENC_PIN_A);
if (!_oldState && _curState) {
encA_rising();
} else if (_oldState && !_curState) {
encA_falling();
}
_oldState = _curState;
}
/*
* PRIVATE METHODS
*/
void Encoders::encA_rising() {
// Direction only decided on rising edge of encoder A
m_direction = digitalRead(ENC_PIN_B) ? Right : Left;
// Update carriage position
if (Right == m_direction) {
if (m_encoderPos < END_RIGHT) {
m_encoderPos++;
}
}
// In front of Left Hall Sensor?
uint16 hallValue = analogRead(EOL_PIN_L);
if (hallValue < FILTER_L_MIN
|| hallValue > FILTER_L_MAX) {
m_hallActive = Left;
// TODO(chris): Verify these decisions!
if (hallValue < FILTER_L_MIN) {
if (m_carriage == K /*&& m_encoderPos == ?? */) {
m_carriage = G;
} else {
m_carriage = L;
}
} else if (hallValue > FILTER_L_MAX) {
m_carriage = K;
}
// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) ? Regular : Shifted;
// Known position of the carriage -> overwrite position
m_encoderPos = END_LEFT + 28;
}
}
void Encoders::encA_falling() {
// Update carriage position
if (Left == m_direction) {
if (m_encoderPos > END_LEFT) {
m_encoderPos--;
}
}
// In front of Right Hall Sensor?
uint16 hallValue = analogRead(EOL_PIN_R);
if (hallValue < FILTER_R_MIN
|| hallValue > FILTER_R_MAX) {
m_hallActive = Right;
if (hallValue < FILTER_R_MIN) {
m_carriage = K;
}
// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) ? Shifted : Regular;
// Known position of the carriage -> overwrite position
m_encoderPos = END_RIGHT - 28;
}
}
byte Encoders::getPosition() {
return m_encoderPos;
}
Beltshift_t Encoders::getBeltshift() {
return m_beltShift;
}
Direction_t Encoders::getDirection() {
return m_direction;
}
Direction_t Encoders::getHallActive() {
return m_hallActive;
}
Carriage_t Encoders::getCarriage() {
return m_carriage;
}
uint16 Encoders::getHallValue(Direction_t pSensor) {
switch (pSensor) {
case Left:
return analogRead(EOL_PIN_L);
case Right:
return analogRead(EOL_PIN_R);
default:
return 0;
}
}