Skip to content

Commit

Permalink
Finally works
Browse files Browse the repository at this point in the history
  • Loading branch information
clholgat committed Aug 25, 2024
1 parent f273377 commit 1d0d6cf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 72 deletions.
42 changes: 32 additions & 10 deletions src/ayab/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ void Encoders::init(Machine_t machineType) {
m_beltShift = BeltShift::Unknown;
m_carriage = Carriage_t::NoCarriage;
m_oldState = false;
m_passedLeft = false;
m_passedRight = false;
}

/*!
Expand Down Expand Up @@ -133,8 +135,13 @@ void Encoders::encA_rising() {
m_direction = digitalRead(ENC_PIN_B) != 0 ? Direction_t::Right : Direction_t::Left;

// Update carriage position
if ((Direction_t::Right == m_direction)) {
if (Direction_t::Right == m_direction) {
m_position = m_position + (uint8_t) 1;

// Reset carriage passed state when we know all magnets have cleared the turn mark.
if (m_position > ALL_MAGNETS_CLEARED_LEFT[static_cast<uint8_t>(m_machineType)]) {
m_passedLeft = false;
}
}

// In front of Left Hall Sensor?
Expand All @@ -143,8 +150,13 @@ void Encoders::encA_rising() {
(hallValue > FILTER_L_MAX[static_cast<uint8_t>(m_machineType)])) {
m_hallActive = Direction_t::Left;

// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Regular : BeltShift::Shifted;
// Only set the belt shift the first time a magnet passes the turn mark.
// Headed to the right.
if (!m_passedLeft && Direction_t::Right == m_direction) {
// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Regular : BeltShift::Shifted;
m_passedLeft = true;
}

// The garter carriage has a second set of magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
Expand Down Expand Up @@ -179,8 +191,8 @@ void Encoders::encA_rising() {
} else if (m_carriage != detected_carriage && m_position > start_position) {
m_carriage = Carriage_t::Garter;

//start_position = start_position + 6 - 2;
//start_position = 16;
// Belt shift and start position were set when the first magnet passed
// the sensor and we assumed we were working with a standard carriage.
return;
} else {
m_carriage = detected_carriage;
Expand All @@ -203,8 +215,13 @@ void Encoders::encA_falling() {
m_direction = digitalRead(ENC_PIN_B) ? Direction_t::Left : Direction_t::Right;

// Update carriage position
if ((Direction_t::Left == m_direction)) {
if (Direction_t::Left == m_direction) {
m_position = m_position - (uint8_t) 1;

// Reset carriage passed state when we know all magnets have cleared the turn mark.
if (m_position < ALL_MAGNETS_CLEARED_RIGHT[static_cast<uint8_t>(m_machineType)]) {
m_passedRight = false;
}
}

// In front of Right Hall Sensor?
Expand All @@ -219,10 +236,15 @@ void Encoders::encA_falling() {
if (hallValueSmall || hallValue > FILTER_R_MAX[static_cast<uint8_t>(m_machineType)]) {
m_hallActive = Direction_t::Right;

// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Shifted : BeltShift::Regular;
// Only set the belt shift when the first magnet passes the turn mark.
// Headed to the left.
if (!m_passedRight && Direction_t::Left == m_direction) {
// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) != 0 ? BeltShift::Shifted : BeltShift::Regular;
m_passedRight = true;
}

// The garter carriage has a second set of magnets that are going to
// The garter carriage has extra magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
// continue.
if (m_carriage == Carriage_t::Garter) {
Expand All @@ -234,6 +256,6 @@ void Encoders::encA_falling() {
}

// Known position of the carriage -> overwrite position
//m_position = END_RIGHT_MINUS_OFFSET[static_cast<uint8_t>(m_machineType)];
m_position = END_RIGHT_MINUS_OFFSET[static_cast<uint8_t>(m_machineType)];
}
}
9 changes: 7 additions & 2 deletions src/ayab/encoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ constexpr uint8_t END_OFFSET[NUM_MACHINES] = {28U, 28U, 5U};
constexpr uint8_t END_LEFT_PLUS_OFFSET[NUM_MACHINES] = {28U, 28U, 5U};
constexpr uint8_t END_RIGHT_MINUS_OFFSET[NUM_MACHINES] = {227U, 227U, 135U};

constexpr uint8_t ALL_MAGNETS_CLEARED_LEFT[NUM_MACHINES] = {56U, 56U, 10U};
constexpr uint8_t ALL_MAGNETS_CLEARED_RIGHT[NUM_MACHINES] = {199U, 199U, 130U};

// The garter slop is needed to determine whether or not we have a garter carriage.
// If we didn't have it, we'd decide which carriage we had when the first magnet passed the sensor.
// For the garter carriage we need to see both magnets.
Expand All @@ -83,8 +86,8 @@ constexpr uint8_t START_OFFSET[NUM_MACHINES][NUM_DIRECTIONS][NUM_CARRIAGES] = {
// KH910
{
// K, L, G
{40U, 32U, 34U/*16U*/}, // Left
{16U, 32U, 44U/*40U*/} // Right
{40U, 32U, 32U/*16U*/}, // Left
{16U, 32U, 50U/*40U*/} // Right
},
// KH930
{
Expand Down Expand Up @@ -182,6 +185,8 @@ class Encoders : public EncodersInterface {
volatile Direction_t m_hallActive;
volatile uint8_t m_position;
volatile bool m_oldState;
volatile bool m_passedLeft;
volatile bool m_passedRight;

void encA_rising();
void encA_falling();
Expand Down
74 changes: 14 additions & 60 deletions src/ayab/knitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,38 +271,15 @@ void Knitter::knit() {

if (!calculatePixelAndSolenoid()) {
// no valid/useful position calculated
//GlobalBeeper::finishedLine();
return;
}

/*if (m_position > 240) {
GlobalBeeper::finishedLine();
}
if (m_position < 15) {
GlobalBeeper::finishedLine();
}*/

if (m_pixelToSet == 0) {
GlobalBeeper::finishedLine();
}

if (m_pixelToSet == 200) {
GlobalBeeper::finishedLine();
return;
}

// Desktop software is setting flanking needles so we need to set
// these even outside of the working needles.
// find the right byte from the currentLine array,
// then read the appropriate Pixel(/Bit) for the current needle to set
uint8_t currentByte = m_pixelToSet >> 3;
if (currentByte > 24) {
//GlobalBeeper::finishedLine();
}

if (currentByte < 1) {
//GlobalBeeper::finishedLine();
}
bool pixelValue =
bitRead(m_lineBuffer[currentByte], m_pixelToSet & 0x07);
// write Pixel state to the appropriate needle
Expand Down Expand Up @@ -421,47 +398,24 @@ bool Knitter::calculatePixelAndSolenoid() {
case Direction_t::Right:
startOffset = getStartOffset(Direction_t::Left);

// We have to start setting pixels earlier when the lace carriage is selected because we shift
// the lace pixel selection up HALF_SOLENOIDS_NUM in this direction. Doesn't matter going back
// the other way.
if (Carriage_t::Lace == m_carriage) {
laceOffset = HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
}

//if (m_position >= startOffset - laceOffset) {
m_pixelToSet = m_position - startOffset;

if ((BeltShift::Shifted == m_beltShift) || (m_machineType == Machine_t::Kh270) || (Carriage_t::Lace == m_carriage)) {
m_solenoidToSet = m_pixelToSet % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
} else if (BeltShift::Regular == m_beltShift) {
m_solenoidToSet = (m_pixelToSet + HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)]) % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
}
m_pixelToSet = m_position - startOffset;

//} else {
// return false;
//}
if ((BeltShift::Regular == m_beltShift)) {
m_solenoidToSet = m_pixelToSet % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
} else if (BeltShift::Shifted == m_beltShift) {
m_solenoidToSet = (m_pixelToSet + HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)]) % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
}
break;

case Direction_t::Left:
startOffset = getStartOffset(Direction_t::Right);
//if (m_position <= (END_RIGHT[static_cast<uint8_t>(m_machineType)] - startOffset)) {
m_pixelToSet = m_position - startOffset;

if (((BeltShift::Regular == m_beltShift) || (m_machineType == Machine_t::Kh270)) && (Carriage_t::Lace != m_carriage)) {
m_solenoidToSet = (m_pixelToSet + HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)]) % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
} else {
m_solenoidToSet = m_pixelToSet % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
}


// THIS WORKS with offset at 16
// But it's not clear why
//m_pixelToSet = m_pixelToSet - SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)] - HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)] - 8;

//m_pixelToSet = m_pixelToSet;
//} else {
// return false;
//}
m_pixelToSet = m_position - startOffset;

if (BeltShift::Regular == m_beltShift) {
m_solenoidToSet = (m_pixelToSet + HALF_SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)]) % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
} else if (BeltShift::Shifted == m_beltShift) {
m_solenoidToSet = m_pixelToSet % SOLENOIDS_NUM[static_cast<uint8_t>(m_machineType)];
}
break;

default:
Expand Down

0 comments on commit 1d0d6cf

Please sign in to comment.