Skip to content

Commit 8ce9993

Browse files
Simplify KH270 carriage detection and fix offsets
1 parent 13d5000 commit 8ce9993

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

src/ayab/encoders.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,30 @@ void Encoders::encA_rising() {
193193
return;
194194
}
195195

196-
// For KH-270, if the carriage is already set, ignore the rest.
197-
if (Machine_t::Kh270 == m_machineType && Carriage_t::Knit == m_carriage) {
198-
return;
199-
}
200-
201-
// Only set the belt shift the first time a magnet passes the turn mark.
202-
// Headed to the right.
203-
if (!m_passedLeft && Direction_t::Right == m_direction) {
204-
// Belt shift signal only decided in front of hall sensor
205-
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Shifted : BeltShift::Regular;
206-
m_passedLeft = true;
196+
// The KH270 carriage also has two magnets, but:
197+
// - the sensors and magnets are set up such that the carriage center
198+
// is in front of the first/last needle when the carriage's outermost
199+
// magnet is in front of the sensor;
200+
// - we will always see the outermost magnet last and reset the position
201+
// to a correct value, so it's not a problem if we detect the innermost
202+
// first, even though that gives us a wrong position for a few needles
203+
// (since we're not selecting anything at that point this has no impact)
204+
// So there's no need to special-case position detection for the KH270 carriage.
205+
206+
// KH270 has no belt shift
207+
if (m_machineType != Machine_t::Kh270) {
208+
// Only set the belt shift the first time a magnet passes the turn mark.
209+
// Headed to the right.
210+
if (!m_passedLeft && Direction_t::Right == m_direction) {
211+
// Belt shift signal only decided in front of hall sensor
212+
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Shifted : BeltShift::Regular;
213+
m_passedLeft = true;
214+
}
207215
}
208216

209217
uint8_t start_position = END_LEFT_PLUS_OFFSET[static_cast<uint8_t>(m_machineType)];
210218

211-
if (m_machineType == Machine_t::Kh270) {
212-
m_carriage = Carriage_t::Knit;
213-
214-
// Assume the rightmost magnet was detected
215-
start_position = start_position + MAGNET_DISTANCE_270;
216-
} else if (m_carriage == Carriage_t::Lace &&
219+
if (m_carriage == Carriage_t::Lace &&
217220
detected_carriage == Carriage_t::Knit &&
218221
m_position > start_position) {
219222
m_carriage = Carriage_t::Garter;
@@ -281,29 +284,32 @@ void Encoders::encA_falling() {
281284
return;
282285
}
283286

284-
// For KH-270, if the carriage is already set, ignore the rest.
285-
if (Machine_t::Kh270 == m_machineType && Carriage_t::Knit == m_carriage) {
286-
return;
287-
}
288-
289-
// Only set the belt shift the first time a magnet passes the turn mark.
290-
// Headed to the left.
291-
if (!m_passedRight && Direction_t::Left == m_direction) {
292-
// Belt shift signal only decided in front of hall sensor
293-
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Regular : BeltShift::Shifted;
294-
m_passedRight = true;
295-
296-
// Shift doesn't need to be swapped for the g-carriage in this direction.
287+
// The KH270 carriage also has two magnets, but:
288+
// - the sensors and magnets are set up such that the carriage center
289+
// is in front of the first/last needle when the carriage's outermost
290+
// magnet is in front of the sensor;
291+
// - we will always see the outermost magnet last and reset the position
292+
// to a correct value, so it's not a problem if we detect the innermost
293+
// first, even though that gives us a wrong position for a few needles
294+
// (since we're not selecting anything at that point this has no impact)
295+
// So there's no need to special-case position detection for the KH270 carriage.
296+
297+
// KH270 has no belt shift
298+
if (m_machineType != Machine_t::Kh270) {
299+
// Only set the belt shift the first time a magnet passes the turn mark.
300+
// Headed to the left.
301+
if (!m_passedRight && Direction_t::Left == m_direction) {
302+
// Belt shift signal only decided in front of hall sensor
303+
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Regular : BeltShift::Shifted;
304+
m_passedRight = true;
305+
306+
// Shift doesn't need to be swapped for the g-carriage in this direction.
307+
}
297308
}
298309

299310
uint8_t start_position = END_RIGHT_MINUS_OFFSET[static_cast<uint8_t>(m_machineType)];
300311

301-
if (m_machineType == Machine_t::Kh270) {
302-
m_carriage = Carriage_t::Knit;
303-
304-
// Assume the leftmost magnet was detected
305-
start_position = start_position; // FIXME
306-
} else if (m_carriage == Carriage_t::Lace &&
312+
if (m_carriage == Carriage_t::Lace &&
307313
detected_carriage == Carriage_t::Knit &&
308314
m_position < start_position) {
309315
m_carriage = Carriage_t::Garter;

src/ayab/encoders.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ constexpr uint8_t END_OF_LINE_OFFSET_L[NUM_MACHINES] = {12U, 12U, 6U};
6666
constexpr uint8_t END_OF_LINE_OFFSET_R[NUM_MACHINES] = {12U, 12U, 6U};
6767

6868
constexpr uint8_t END_LEFT[NUM_MACHINES] = {0U, 0U, 0U};
69-
constexpr uint8_t END_RIGHT[NUM_MACHINES] = {255U, 255U, 140U};
70-
constexpr uint8_t END_OFFSET[NUM_MACHINES] = {28U, 28U, 5U};
69+
constexpr uint8_t END_RIGHT[NUM_MACHINES] = {255U, 255U, 167U};
70+
constexpr uint8_t END_OFFSET[NUM_MACHINES] = {28U, 28U, 28U};
7171

7272
// The following two arrays are created by combining, respectively,
7373
// the arrays END_LEFT and END_RIGHT with END_OFFSET
74-
constexpr uint8_t END_LEFT_PLUS_OFFSET[NUM_MACHINES] = {28U, 28U, 5U};
75-
constexpr uint8_t END_RIGHT_MINUS_OFFSET[NUM_MACHINES] = {227U, 227U, 135U};
74+
constexpr uint8_t END_LEFT_PLUS_OFFSET[NUM_MACHINES] = {28U, 28U, 28U};
75+
constexpr uint8_t END_RIGHT_MINUS_OFFSET[NUM_MACHINES] = {227U, 227U, 139U};
7676

77-
constexpr uint8_t ALL_MAGNETS_CLEARED_LEFT[NUM_MACHINES] = {56U, 56U, 10U};
78-
constexpr uint8_t ALL_MAGNETS_CLEARED_RIGHT[NUM_MACHINES] = {199U, 199U, 130U};
77+
constexpr uint8_t ALL_MAGNETS_CLEARED_LEFT[NUM_MACHINES] = {56U, 56U, 38U};
78+
constexpr uint8_t ALL_MAGNETS_CLEARED_RIGHT[NUM_MACHINES] = {199U, 199U, 129U};
7979

8080
// The garter slop is needed to determine whether or not we have a garter carriage.
8181
// If we didn't have it, we'd decide which carriage we had when the first magnet passed the sensor.
@@ -102,8 +102,8 @@ constexpr uint8_t START_OFFSET[NUM_MACHINES][NUM_DIRECTIONS][NUM_CARRIAGES] = {
102102
// KH270
103103
{
104104
// K
105-
{28U, 0U, 0U}, // Left
106-
{16U, 0U, 0U} // Right
105+
{28U + 6U, 0U, 0U}, // Left
106+
{28U - 6U, 0U, 0U} // Right
107107
}};
108108

109109
// Should be calibrated to each device
@@ -117,8 +117,6 @@ constexpr uint16_t FILTER_R_MAX[NUM_MACHINES] = {1023U, 600U, 600U};
117117

118118
constexpr uint16_t SOLENOIDS_BITMASK = 0xFFFFU;
119119

120-
constexpr uint8_t MAGNET_DISTANCE_270 = 12U;
121-
122120
/*!
123121
* \brief Encoder interface.
124122
*

0 commit comments

Comments
 (0)