Skip to content

Commit 10a38a0

Browse files
committed
Fix incorrect belt phase and KH270 implementations
encoder_phase was operating on 4 bits but it should be 6 to offer 64 states - 16 needles (Belt Phase "period") - 4 v1/v2 states for each needle KH270 didn't took into account that: - the first needle was not mapped to solenoid 0 (but 4 when moving to the right) - when moving to the left, solenoid to set has to be shifted by 6
1 parent b6c078e commit 10a38a0

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

examples/board_ayab/ayab.c

+32-10
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,16 @@ static void * avr_run_thread(void * param)
257257

258258
vcd_trace_enable(vcd_enabled);
259259

260-
// Phase overlaps 16 needles/solenoids
261-
unsigned encoder_phase=0;
262260
// {v1, v2} encoding over 4 phases
263261
unsigned phase_map[4] = {0, 1, 3, 2};
264262

265-
encoder_phase = (machine.carriage.position +
266-
(machine.belt_phase == REGULAR) ? 4 : 12) % 16;
263+
// Phase overlaps 16 needles/solenoids (16*4 v1/v2 states)
264+
unsigned encoder_phase;
265+
if (machine.belt_phase == REGULAR) {
266+
encoder_phase = (unsigned)(machine.carriage.position * 4 + 16) % 64;
267+
} else {
268+
encoder_phase = (unsigned)(machine.carriage.position * 4 + 48) % 64;
269+
}
267270

268271
char needles[machine.num_needles];
269272
memset(needles, ' ', machine.num_needles);
@@ -282,7 +285,7 @@ static void * avr_run_thread(void * param)
282285
switch (event)
283286
{
284287
case CARRIAGE_LEFT:
285-
new_phase = (encoder_phase-1)%16;
288+
new_phase = (encoder_phase-1)%64;
286289
if ((new_phase%4) == 0) {
287290
if (machine.carriage.position > -24) {
288291
machine.carriage.position--;
@@ -293,7 +296,7 @@ static void * avr_run_thread(void * param)
293296
}
294297
break;
295298
case CARRIAGE_RIGHT:
296-
new_phase = (encoder_phase+1)%16;
299+
new_phase = (encoder_phase+1)%64;
297300
if ((new_phase%4) == 0) {
298301
if (machine.carriage.position < (machine.num_needles + 24)) {
299302
machine.carriage.position++;
@@ -312,7 +315,6 @@ static void * avr_run_thread(void * param)
312315
fprintf(stderr, "Unexpect event from graphic thread\n");
313316
break;
314317
}
315-
316318
machine.hall_left = 1650;
317319
machine.hall_right = 1650;
318320
uint16_t solenoid_states = (shield.mcp23008[1].reg[MCP23008_REG_OLAT] << 8) + shield.mcp23008[0].reg[MCP23008_REG_OLAT];
@@ -407,15 +409,35 @@ static void * avr_run_thread(void * param)
407409

408410
selected_needle = machine.carriage.position + select_offset;
409411
if ((selected_needle < machine.num_needles) && (selected_needle >= 0)) {
410-
int solenoid_offset = (machine.type == KH270) ? 3 : 0;
411-
needles[selected_needle] = solenoid_states & (1<< (solenoid_offset + selected_needle % machine.num_solenoids)) ? '.' : '|';
412+
int solenoid_index;
413+
switch (machine.type) {
414+
case KH270:
415+
solenoid_index = selected_needle + 4;
416+
// On KH270 solenoid to needle mapping is direction-dependent
417+
if (event == CARRIAGE_LEFT) {
418+
solenoid_index += machine.num_solenoids >> 1;
419+
}
420+
// 12 solenoids mapped to position [3-14]
421+
solenoid_index = 3 + solenoid_index % machine.num_solenoids;
422+
break;
423+
default:
424+
solenoid_index = selected_needle;
425+
// Solenoid to needle mapping is belt phase-dependent
426+
if (machine.belt_phase == SHIFTED) {
427+
solenoid_index += machine.num_solenoids >> 1;
428+
}
429+
solenoid_index = solenoid_index % machine.num_solenoids;
430+
break;
431+
}
432+
433+
needles[selected_needle] = solenoid_states & (1<< solenoid_index) ? '.' : '|';
412434
solenoid_update = 1;
413435
}
414436

415437
encoder_phase = new_phase;
416438
avr_raise_irq(encoder_v2.irq + IRQ_BUTTON_OUT, (phase_map[encoder_phase % 4] & 1) ? 1 : 0);
417439
avr_raise_irq(encoder_v1.irq + IRQ_BUTTON_OUT, (phase_map[encoder_phase % 4] & 2) ? 1 : 0);
418-
avr_raise_irq(encoder_beltPhase.irq + IRQ_BUTTON_OUT, (encoder_phase & 8) ? 1 : 0);
440+
avr_raise_irq(encoder_beltPhase.irq + IRQ_BUTTON_OUT, (encoder_phase & 32) ? 1 : 0);
419441

420442
if ((encoder_phase % 4) == 0) {
421443
int half_num_needles = machine.num_needles / 2;

0 commit comments

Comments
 (0)