forked from AllYarnsAreBeautiful/ayab-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knitter.cpp
374 lines (310 loc) · 9.03 KB
/
knitter.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// knitter.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 "./knitter.h"
Knitter::Knitter() {}
Knitter::Knitter(SLIPPacketSerial* packetSerial) {
Knitter();
m_packetSerial = packetSerial;
m_opState = s_init;
m_startNeedle = 0;
m_stopNeedle = 0;
m_currentLineNumber = 0;
m_lineRequested = false;
m_solenoids.init();
}
void Knitter::isr() {
// Update machine state data
m_encoders.encA_interrupt();
m_position = m_encoders.getPosition();
m_direction = m_encoders.getDirection();
m_hallActive = m_encoders.getHallActive();
m_beltshift = m_encoders.getBeltshift();
m_carriage = m_encoders.getCarriage();
}
void Knitter::fsm() {
switch (m_opState) {
case s_init:
state_init();
break;
case s_ready:
state_ready();
break;
case s_operate:
state_operate();
break;
case s_test:
state_test();
break;
default:
break;
}
}
bool Knitter::startOperation(byte startNeedle,
byte stopNeedle,
bool continuousReportingEnabled,
byte(*line)) {
if (startNeedle >= 0
&& stopNeedle < NUM_NEEDLES
&& startNeedle < stopNeedle) {
if (s_ready == m_opState) {
// Proceed to next state
m_opState = s_operate;
// Assign image width
m_startNeedle = startNeedle;
m_stopNeedle = stopNeedle;
// Continuous Reporting enabled?
m_continuousReportingEnabled = continuousReportingEnabled;
// Set pixel data source
m_lineBuffer = line;
// Reset variables to start conditions
m_currentLineNumber = 255; // because counter will
// be increased before request
m_lineRequested = false;
m_lastLineFlag = false;
m_lastLinesCountdown = 2;
m_beeper.ready();
return true;
}
}
return false;
}
bool Knitter::startTest() {
if (s_init == m_opState
|| s_ready == m_opState) {
m_opState = s_test;
return true;
}
return false;
}
bool Knitter::setNextLine(byte lineNumber) {
if (m_lineRequested) {
// Is there even a need for a new line?
if (lineNumber == m_currentLineNumber) {
m_lineRequested = false;
m_beeper.finishedLine();
return true;
} else {
// line numbers didnt match -> request again
reqLine(m_currentLineNumber);
}
}
return false;
}
void Knitter::setLastLine() {
// lastLineFlag is evaluated in s_operate
m_lastLineFlag = true;
}
/*
* PRIVATE METHODS
*/
void Knitter::state_init() {
static bool _ready = false;
#ifdef DBG_NOMACHINE
static bool _prevState = false;
bool state = digitalRead(DBG_BTN_PIN);
// TODO Check if debounce is needed
if (_prevState && !state) {
_ready = true;
}
_prevState = state;
#else
// Machine is initialized when left hall sensor is passed in Right direction
if (Right == m_direction
&& Left == m_hallActive) {
_ready = true;
}
#endif // DBG_NOMACHINE
if (_ready) {
m_opState = s_ready;
m_solenoids.setSolenoids(0xFFFF);
indState(true);
return;
}
m_opState = s_init;
}
void Knitter::state_ready() {
digitalWrite(LED_PIN_A, 0);
// This state is left when the startOperation() method
// is called successfully by main()
}
void Knitter::state_operate() {
digitalWrite(LED_PIN_A, 1);
static bool _firstRun = true;
static byte _sOldPosition = 0;
static bool _workedOnLine = false;
if (true == _firstRun) {
_firstRun = false;
// Optimize Delay for various Arduino Models
delay(2000);
m_beeper.finishedLine();
reqLine(++m_currentLineNumber);
}
#ifdef DBG_NOMACHINE
static bool _prevState = false;
bool state = digitalRead(DBG_BTN_PIN);
// TODO Check if debounce is needed
if (_prevState && !state) {
if (!m_lineRequested) {
reqLine(++m_currentLineNumber);
}
}
_prevState = state;
return;
#else
if (_sOldPosition != m_position) {
// Only act if there is an actual change of position
// Store current Encoder position for next call of this function
_sOldPosition = m_position;
if (m_continuousReportingEnabled) {
// Send current position to GUI
indState(true);
}
if (!calculatePixelAndSolenoid()) {
// No valid/useful position calculated
return;
}
if ((m_pixelToSet >= m_startNeedle-END_OF_LINE_OFFSET_L)
&& (m_pixelToSet <= m_stopNeedle+END_OF_LINE_OFFSET_R)) {
if ((m_pixelToSet >= m_startNeedle) && (m_pixelToSet <= m_stopNeedle)) {
_workedOnLine = true;
}
// Find the right byte from the currentLine array,
// then read the appropriate Pixel(/Bit) for the current needle to set
int _currentByte = (int)(m_pixelToSet/8);
bool _pixelValue = bitRead(m_lineBuffer[_currentByte],
m_pixelToSet-(8*_currentByte));
// Write Pixel state to the appropriate needle
m_solenoids.setSolenoid(m_solenoidToSet, _pixelValue);
} else { // Outside of the active needles
// digitalWrite(LED_PIN_B, 0);
// Reset Solenoids when out of range
m_solenoids.setSolenoid(m_solenoidToSet, true);
if (_workedOnLine) {
// already worked on the current line -> finished the line
_workedOnLine = false;
if (!m_lineRequested && !m_lastLineFlag) {
// request new Line from Host
reqLine(++m_currentLineNumber);
} else if (m_lastLineFlag) {
m_beeper.endWork();
m_opState = s_ready;
m_solenoids.setSolenoids(0xFFFF);
m_beeper.finishedLine();
}
}
}
}
#endif // DBG_NOMACHINE
}
void Knitter::state_test() {
static byte _sOldPosition = 0;
if (_sOldPosition != m_position) {
// Only act if there is an actual change of position
// Store current Encoder position for next call of this function
_sOldPosition = m_position;
calculatePixelAndSolenoid();
indState();
}
}
bool Knitter::calculatePixelAndSolenoid() {
switch (m_direction) {
// Calculate the solenoid and pixel to be set
// Implemented according to machine manual
// Magic numbers result from machine manual
case Right:
if (m_position >= getStartOffset(Left)) {
m_pixelToSet = m_position - getStartOffset(Left);
if (Regular == m_beltshift) {
m_solenoidToSet = m_position % 16;
} else if (Shifted == m_beltshift) {
m_solenoidToSet = (m_position-8) % 16;
}
if (L == m_carriage) {
m_pixelToSet = m_pixelToSet + 8;
}
} else {
return false;
}
break;
case Left:
if (m_position <= (END_RIGHT - getStartOffset(Right))) {
m_pixelToSet = m_position - getStartOffset(Right);
if (Regular == m_beltshift) {
m_solenoidToSet = (m_position+8) % 16;
} else if (Shifted == m_beltshift) {
m_solenoidToSet = m_position % 16;
}
if (L == m_carriage) {
m_pixelToSet = m_pixelToSet - 16;
}
} else {
return false;
}
break;
default:
return false;
break;
}
return true;
}
byte Knitter::getStartOffset(Direction_t direction) {
switch (direction) {
case Left:
if (m_carriage == G) {
// G carriage
return 8;
} else {
// K and L carriage
return 40;
}
break;
case Right:
if (m_carriage == G) {
// G carriage
return 32;
} else {
// K and L carriage
return 16;
}
break;
default:
return 0;
}
}
void Knitter::reqLine(byte lineNumber) {
uint8_t payload[2];
payload[0] = reqLine_msgid;
payload[1] = lineNumber;
m_packetSerial->send(payload, 2);
m_lineRequested = true;
}
void Knitter::indState(bool initState) {
uint8_t payload[9];
payload[0] = indState_msgid;
payload[1] = (byte)initState;
uint16 hallValue = m_encoders.getHallValue(Left);
payload[2] = (byte)(hallValue >> 8) & 0xFF;
payload[3] = (byte)hallValue & 0xFF;
hallValue = m_encoders.getHallValue(Right);
payload[4] = (byte)(hallValue >> 8) & 0xFF;
payload[5] = (byte)hallValue & 0xFF;
payload[6] = (byte)m_carriage;
payload[7] = (byte)m_position;
payload[8] = (byte)m_encoders.getDirection();
m_packetSerial->send(payload, 9);
}