Skip to content

Commit

Permalink
needle selection WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanperret committed Oct 16, 2024
1 parent 849cd02 commit 358cab4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/mocks/knitting_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ void KnittingMachine::moveCarriageRight() {
moveBeltRight();
}
++m_carriagePosition;

updateNeedles();
}

void KnittingMachine::moveCarriageLeft() {
if (carriageEngagesBelt()) {
moveBeltLeft();
}
--m_carriagePosition;

updateNeedles();
}

int KnittingMachine::beltPeriod() const {
Expand All @@ -138,4 +142,40 @@ bool KnittingMachine::moveCarriageCenterTowardsNeedle(int position) {
moveCarriageLeft();
}
return true;
}

void KnittingMachine::setNeedleCount(int count) {
m_needles.clear();
for (int n = 0; n < count; n++) {
m_needles.push_back(Needle{.index = n, .position = NeedlePosition::A});
}
}

void KnittingMachine::setNeedlePosition(int needle, NeedlePosition position) {
m_needles[needle].position = position;
}

KnittingMachine::NeedlePosition KnittingMachine::getNeedlePosition(int needle) {
return m_needles[needle].position;
}

void KnittingMachine::setSolenoid(int solenoid, bool state) {
(void)state;
(void)solenoid;
}

void KnittingMachine::updateNeedles() {
for (auto& needle : m_needles) {
needle.update();
}
}

void KnittingMachine::Needle::update() {
switch (position) {
case B:
position = D;
break;
default:
break;
}
}
66 changes: 66 additions & 0 deletions test/mocks/knitting_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,45 @@ class KnittingMachine {
*/
bool moveCarriageCenterTowardsNeedle(int position);

/**
* Initialize the needle bed with the given needle count
*
* \param count Count of (programmable) needles
*/
void setNeedleCount(int count);

/**
* Needle positions
*
* Position "C" is not indicated on the machine nor referenced
* in the manual but is useful to represent an intermediate
* position of the needle during the selection process.
*/
enum NeedlePosition { A = 0, B, C, D, E };

/**
* Set a needle's position
*
* \param needle the needle index (e.g. 0 to 199 on a KH-9xx)
* \param position the needle position (A, B…)
*/
void setNeedlePosition(int needle, NeedlePosition position);

/**
* Get a needle's position
*
* \param needle the needle index (e.g. 0 to 199 on a KH-9xx)
* \returns the needle's position (A, B…)
*/
NeedlePosition getNeedlePosition(int needle);

/**
* Set a solenoid's state
*
* \param state the solenoid's power state (true = powered)
*/
void setSolenoid(int solenoid, bool state);

private:
static constexpr int STEPS_PER_NEEDLE = 4;
static constexpr float POSITION_SENSOR_LOW_VOLTAGE = 0.2f;
Expand Down Expand Up @@ -251,6 +290,33 @@ class KnittingMachine {
* center and their polarity. \see addCarriageMagnet()
*/
std::vector<std::pair<float, bool>> m_carriageMagnets;

/**
* A single needle
*/
struct Needle {
/**
* Needle index (0 to needle count - 1)
*/
int index;
/**
* Current needle position (A, B…)
*/
NeedlePosition position;

void update();
};

/**
* Needles on the bed
*/
std::vector<Needle> m_needles;

/**
* Update the state of all the needles.
* Called after every carriage movement.
*/
void updateNeedles();
};

#endif // KNITTINGMACHINE_H_
25 changes: 25 additions & 0 deletions test/test_knitting_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,29 @@ TEST(KnittingMachine, KCarriageScanBed) {

km.addCarriageMagnet(0, true);
doBedScan(km);
}

TEST(KnittingMachine, NeedleSelection) {
KnittingMachine km;

const int testNeedle = 47;
const int testSolenoid = 0;

km.setNeedleCount(200);
EXPECT_EQ(km.getNeedlePosition(testNeedle), KnittingMachine::A);

km.setNeedlePosition(testNeedle, KnittingMachine::B);
EXPECT_EQ(km.getNeedlePosition(testNeedle), KnittingMachine::B);
km.setSolenoid(testSolenoid, true);
km.putCarriageCenterInFrontOfNeedle(0);
while (km.moveCarriageCenterTowardsNeedle(100)) {
}

for (int n = 0; n < 100; n++) {
if (n == testNeedle) {
EXPECT_EQ(km.getNeedlePosition(n), KnittingMachine::D);
} else {
EXPECT_EQ(km.getNeedlePosition(n), KnittingMachine::A);
}
}
}

0 comments on commit 358cab4

Please sign in to comment.