Skip to content

Commit

Permalink
ignore beltshift for KH270
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mpr1c3 committed Sep 29, 2023
1 parent 1a8ce58 commit fd6c7ad
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 41 deletions.
24 changes: 15 additions & 9 deletions src/ayab/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ void Encoders::encA_rising() {
return;
}

// In front of Left Hall Sensor?
// Hall value is used to detect whether carriage is in front of Left Hall sensor
uint16_t hallValue = analogRead(EOL_PIN_L);

if ((hallValue < FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) ||
(hallValue > FILTER_L_MAX[static_cast<uint8_t>(m_machineType)])) {
// In front of Left Hall Sensor
m_hallActive = Direction_t::Left;

Carriage detected_carriage = Carriage_t::NoCarriage;
Expand All @@ -192,10 +194,11 @@ void Encoders::encA_rising() {
return;
} else {
m_carriage = detected_carriage;
}

// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) ? BeltShift::Regular : BeltShift::Shifted;
// Belt shift signal only decided in front of Hall sensor
// Belt shift is ignored for KH270
m_beltShift = digitalRead(ENC_PIN_C) ? BeltShift::Regular : BeltShift::Shifted;
}

// Known position of the carriage -> overwrite position
m_position = start_position;
Expand All @@ -218,27 +221,30 @@ void Encoders::encA_falling() {
m_position = m_position - 1;
}

// In front of Right Hall Sensor?
// Hall value is used to detect whether carriage is in front of Right Hall sensor
uint16_t hallValue = analogRead(EOL_PIN_R);

// Avoid 'comparison of unsigned expression < 0 is always false'
// by being explicit about that behaviour being expected.
bool hallValueSmall = false;

hallValueSmall = (hallValue < FILTER_R_MIN[static_cast<uint8_t>(m_machineType)]);

if (hallValueSmall || (hallValue > FILTER_R_MAX[static_cast<uint8_t>(m_machineType)])) {
// In front of Right Hall Sensor
m_hallActive = Direction_t::Right;

// The garter carriage has a second set of magnets that are going to
// The Garter carriage has a second set of magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
// continue.
if (hallValueSmall && (m_carriage != Carriage_t::Garter)) {
m_carriage = Carriage_t::Knit;
}

// Belt shift signal only decided in front of hall sensor
m_beltShift = digitalRead(ENC_PIN_C) ? BeltShift::Shifted : BeltShift::Regular;
// Belt shift is ignored for KH270
if (m_machineType != Machine_t::Kh270) {
// Belt shift signal only decided in front of Hall sensor
m_beltShift = digitalRead(ENC_PIN_C) ? BeltShift::Shifted : BeltShift::Regular;
}

// Known position of the carriage -> overwrite position
m_position = END_RIGHT_MINUS_OFFSET[static_cast<uint8_t>(m_machineType)];
Expand Down
8 changes: 3 additions & 5 deletions src/ayab/encoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ constexpr uint8_t START_OFFSET[NUM_MACHINES][NUM_DIRECTIONS][NUM_CARRIAGES] = {
{
// K, L, G
{40U, 40U, 32U}, // Left
{16U, 16U, 56U} // Right
{16U, 16U, 56U} // Right
},
// KH930
{
// K, L, G
{40U, 40U, 8U}, // Left
{16U, 16U, 32U} // Right
{16U, 16U, 32U} // Right
},
// KH270
{
// K
{28U, 0U, 0U}, // Left: x % 12 == 4
{16U, 0U, 0U} // Right: (x + 6) % 12 == 10
{16U, 0U, 0U} // Right: (x + 6) % 12 == 10
}};

// Should be calibrated to each device
Expand All @@ -108,8 +108,6 @@ constexpr uint16_t FILTER_L_MAX[NUM_MACHINES] = { 600U, 600U, 600U};
constexpr uint16_t FILTER_R_MIN[NUM_MACHINES] = { 200U, 0U, 0U};
constexpr uint16_t FILTER_R_MAX[NUM_MACHINES] = {1023U, 600U, 600U};

constexpr uint16_t SOLENOIDS_BITMASK = 0xFFFFU;

constexpr uint8_t MAGNET_DISTANCE_270 = 12U;

/*!
Expand Down
4 changes: 2 additions & 2 deletions src/ayab/opKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void OpKnit::encodePosition() {
// only act if there is an actual change of position
// store current encoder position for next call of this function
m_sOldPosition = position;
calculatePixelAndSolenoid();
(void) calculatePixelAndSolenoid();
GlobalCom::send_indState(Err_t::Unspecified_failure); // FIXME is this the right error code?
}
}
Expand Down Expand Up @@ -333,8 +333,8 @@ void OpKnit::reqLine(uint8_t lineNumber) {
bool OpKnit::calculatePixelAndSolenoid() {
uint8_t startOffset = 0;

auto direction = GlobalController::getDirection();
auto position = GlobalController::getPosition();
auto direction = GlobalController::getDirection();
auto beltShift = GlobalController::getBeltShift();
auto carriage = GlobalController::getCarriage();
auto machineType = GlobalController::getMachineType();
Expand Down
1 change: 1 addition & 0 deletions src/ayab/solenoids.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ constexpr uint8_t SOLENOIDS_NUM[NUM_MACHINES] = {16U, 16U, 12U};
constexpr uint8_t HALF_SOLENOIDS_NUM[NUM_MACHINES] = {8U, 8U, 6U};
constexpr uint8_t SOLENOIDS_I2C_ADDRESS_MASK = 0x20U;
constexpr uint8_t SOLENOID_BUFFER_SIZE = 16U;
constexpr uint16_t SOLENOIDS_BITMASK = 0xFFFFU;

class SolenoidsInterface {
public:
Expand Down
61 changes: 48 additions & 13 deletions test/test_OpKnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ class OpKnitTest : public ::testing::Test {
controller->cacheEncoders();
}

void expected_cacheISR(uint16_t pos, Direction_t dir, BeltShift_t belt, Carriage_t carriage) {
expect_cacheISR(pos, dir, Direction_t::NoDirection, belt, carriage);
controller->cacheEncoders();
}

void expect_cacheISR(Direction_t dir, Direction_t hall) {
expect_cacheISR(1, dir, hall, BeltShift::Regular, Carriage_t::Knit);
}
Expand Down Expand Up @@ -646,57 +651,87 @@ TEST_F(OpKnitTest, test_calculatePixelAndSolenoid) {

// No direction
// Lace carriage, no direction, need to change position to enter test
expected_cacheISR(100, Direction_t::NoDirection, Direction_t::Right, BeltShift::Shifted, Carriage_t::Lace);
expected_cacheISR(100, Direction_t::NoDirection, BeltShift::Shifted, Carriage_t::Lace);
ASSERT_FALSE(opKnit->calculatePixelAndSolenoid());
// opKnit->pixelToSet not set
// opKnit->m_solenoidToSet not set

// Right direction
// Lace carriage, no belt on Right, have not reached offset
expected_cacheISR(39, Direction_t::Right, Direction_t::Left, BeltShift::Unknown, Carriage_t::Lace);
expected_cacheISR(39, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace);
ASSERT_FALSE(opKnit->calculatePixelAndSolenoid());
// opKnit->pixelToSet not set
// opKnit->m_solenoidToSet not set

// Lace carriage, no belt on Right, need to change position to enter test
expected_cacheISR(100, Direction_t::Right, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace);
expected_cacheISR(100, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8);
// opKnit->m_solenoidToSet not set

// Lace carriage, regular belt on Right
expected_cacheISR(100, Direction_t::Right, Direction_t::Right, BeltShift::Regular, Carriage_t::Lace);
expected_cacheISR(100, Direction_t::Right, BeltShift::Regular, Carriage_t::Lace);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8);
ASSERT_EQ(opKnit->m_solenoidToSet, 100 % 16);

// Lace carriage, shifted belt on Right, new position, active Hall
expected_cacheISR(100, Direction_t::Right, Direction_t::Right, BeltShift::Shifted, Carriage_t::Lace);
// Lace carriage, shifted belt on Right
expected_cacheISR(100, Direction_t::Right, BeltShift::Shifted, Carriage_t::Lace);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, (100 - 40) + 8);
ASSERT_EQ(opKnit->m_solenoidToSet, (100 - 8) % 16);

// Left direction
// Lace carriage, no belt on Left, off of Right end, position is changed
expected_cacheISR(END_RIGHT[static_cast<uint8_t>(Machine_t::Kh910)] - 15, Direction_t::Left, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace);
expected_cacheISR(END_RIGHT[static_cast<uint8_t>(Machine_t::Kh910)] - 15, Direction_t::Left, BeltShift::Unknown, Carriage_t::Lace);
ASSERT_FALSE(opKnit->calculatePixelAndSolenoid());
// opKnit->pixelToSet not set
// opKnit->m_solenoidToSet not set

// Lace carriage, no belt on Left
expected_cacheISR(100, Direction_t::Left, Direction_t::Right, BeltShift::Unknown, Carriage_t::Lace);
expected_cacheISR(100, Direction_t::Left, BeltShift::Unknown, Carriage_t::Lace);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, 100 - 16 - 16);
// opKnit->m_solenoidToSet not set

// Lace carriage, regular belt on Left
expected_cacheISR(100, Direction_t::Left, BeltShift::Regular, Carriage_t::Lace);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, 100 - 16 - 16);
ASSERT_EQ(opKnit->m_solenoidToSet, (100 + 8) % 16);

// Garter Carriage, no belt on Left, need to change position to enter test
expected_cacheISR(101, Direction_t::Left, Direction_t::Right, BeltShift::Unknown, Carriage_t::Garter);
expected_cacheISR(100, Direction_t::Left, BeltShift::Unknown, Carriage_t::Garter);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56);
// opKnit->m_solenoidToSet not set

// Garter carriage, regular belt on Left, need to change position to enter test
expected_cacheISR(101, Direction_t::Left, Direction_t::Right, BeltShift::Regular, Carriage_t::Garter);
expected_cacheISR(100, Direction_t::Left, BeltShift::Regular, Carriage_t::Garter);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56);
ASSERT_EQ(opKnit->m_solenoidToSet, (100 + 8) % 16);

// Garter carriage, shifted belt on Left, need to change position to enter test
expected_cacheISR(100, Direction_t::Left, Direction_t::Right, BeltShift::Shifted, Carriage_t::Garter);
expected_cacheISR(100, Direction_t::Left, BeltShift::Shifted, Carriage_t::Garter);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, 100 - 56);
ASSERT_EQ(opKnit->m_solenoidToSet, 100 % 16);

// KH270
controller->setMachineType(Machine_t::Kh270);

// K carriage, no belt on Left
expected_cacheISR(0, Direction_t::Left, Direction_t::Right, BeltShift::Unknown, Carriage_t::Knit);
expected_cacheISR(0, Direction_t::Left, BeltShift::Unknown, Carriage_t::Knit);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, static_cast<uint8_t>(0 - 16));
ASSERT_EQ(opKnit->m_solenoidToSet, static_cast<uint8_t>(0 + 6) % 12 + 3);

// K carriage, no belt on Right
expected_cacheISR(END_RIGHT[static_cast<uint8_t>(Machine_t::Kh270)], Direction_t::Right, Direction_t::Left, BeltShift::Unknown, Carriage_t::Knit);
expected_cacheISR(END_RIGHT[static_cast<uint8_t>(Machine_t::Kh270)], Direction_t::Right, BeltShift::Unknown, Carriage_t::Knit);
ASSERT_TRUE(opKnit->calculatePixelAndSolenoid());
ASSERT_EQ(opKnit->m_pixelToSet, (140 - 28));
ASSERT_EQ(opKnit->m_solenoidToSet, 140 % 12 + 3);

// Test expectations without destroying instance
ASSERT_TRUE(Mock::VerifyAndClear(solenoidsMock));
Expand Down
36 changes: 24 additions & 12 deletions test/test_encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,38 @@ TEST_F(EncodersTest, test_encA_rising_in_front_KH270) {
// In front of Left Hall Sensor
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L))
.WillOnce(Return(FILTER_L_MIN[static_cast<int8_t>(Machine_t::Kh270)] - 1));
// BeltShift is regular
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).WillOnce(Return(true));
// BeltShift is ignored
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0);
encoders->isr();

ASSERT_EQ(encoders->getDirection(), Direction_t::Right);
ASSERT_EQ(encoders->getHallActive(), Direction_t::Left);
ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast<int8_t>(Machine_t::Kh270)]);
ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit);
ASSERT_EQ(encoders->getBeltShift(), BeltShift::Regular);
ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown);

// Create a falling edge, then a rising edge:
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false)).WillOnce(Return(true));
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false)).WillOnce(Return(true));
// Not in front of Left Hall Sensor
// Enter falling function
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(false));
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(false));
// In front of Left Hall Sensor
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_R))
.WillOnce(Return(FILTER_R_MIN[static_cast<int8_t>(encoders->getMachineType())]));
.WillOnce(Return(FILTER_R_MIN[static_cast<int8_t>(encoders->getMachineType())] - 1));
// BeltShift is ignored
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0);
encoders->isr();

ASSERT_EQ(encoders->getDirection(), Direction_t::Right);
ASSERT_EQ(encoders->getHallActive(), Direction_t::Right);
ASSERT_EQ(encoders->getPosition(), END_RIGHT_MINUS_OFFSET[static_cast<int8_t>(Machine_t::Kh270)]);
ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit);
ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown);

// Create a rising edge
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_A)).WillOnce(Return(true));
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_B)).WillOnce(Return(true));
// We will not enter the rising function
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L)).Times(0);
encoders->isr();
encoders->isr();
}

TEST_F(EncodersTest, test_encA_rising_after_KH270) {
Expand All @@ -193,15 +205,15 @@ TEST_F(EncodersTest, test_encA_rising_after_KH270) {
// After Left Hall Sensor
EXPECT_CALL(*arduinoMock, analogRead(EOL_PIN_L))
.WillOnce(Return(FILTER_L_MAX[static_cast<int8_t>(encoders->getMachineType())] + 1));
// BeltShift is regular
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).WillOnce(Return(true));
// BeltShift is ignored
EXPECT_CALL(*arduinoMock, digitalRead(ENC_PIN_C)).Times(0);
encoders->isr();

ASSERT_EQ(encoders->getDirection(), Direction_t::Right);
ASSERT_EQ(encoders->getHallActive(), Direction_t::Left);
ASSERT_EQ(encoders->getPosition(), END_LEFT_PLUS_OFFSET[static_cast<int8_t>(Machine_t::Kh270)] + MAGNET_DISTANCE_270);
ASSERT_EQ(encoders->getCarriage(), Carriage_t::Knit);
ASSERT_EQ(encoders->getBeltShift(), BeltShift::Regular);
ASSERT_EQ(encoders->getBeltShift(), BeltShift::Unknown);
}

TEST_F(EncodersTest, test_G_carriage) {
Expand Down

0 comments on commit fd6c7ad

Please sign in to comment.