Skip to content

Commit

Permalink
Support pause propagation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Mayer authored and Adam Mayer committed May 3, 2010
1 parent 0478761 commit c1cce43
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 42 deletions.
12 changes: 8 additions & 4 deletions v2/src/Extruder/Host.cc
Expand Up @@ -64,6 +64,11 @@ inline void handleWriteEeprom(const InPacket& from_host, OutPacket& to_host) {
}
}

inline void handlePause(const InPacket& from_host, OutPacket& to_host) {
MotorController::getController().pause();
to_host.append8(RC_OK);
}

bool do_host_reset = false;

bool processQueryPacket(const InPacket& from_host, OutPacket& to_host) {
Expand Down Expand Up @@ -96,6 +101,9 @@ bool processQueryPacket(const InPacket& from_host, OutPacket& to_host) {
case SLAVE_CMD_WRITE_TO_EEPROM:
handleWriteEeprom(from_host, to_host);
return true;
case SLAVE_CMD_PAUSE_UNPAUSE:
handlePause(from_host, to_host);
return true;
case SLAVE_CMD_SET_MOTOR_1_PWM:
motor.setSpeed(from_host.read8(2));
to_host.append8(RC_OK);
Expand Down Expand Up @@ -156,7 +164,6 @@ void runHostSlice() {
reset();
}
if (in.isStarted() && !in.isFinished()) {
//ExtruderBoard::getBoard().indicateError(1); cycles = 3000;
if (!packet_in_timeout.isActive()) {
// initiate timeout
packet_in_timeout.start(HOST_PACKET_TIMEOUT_MICROS);
Expand All @@ -166,17 +173,14 @@ void runHostSlice() {
}
}
if (in.hasError()) {
//ExtruderBoard::getBoard().indicateError(1); cycles = 3000;
packet_in_timeout.abort();
// REPORTING: report error.
// Reset packet quickly and start handling the next packet.
in.reset();
uart.reset();
}
if (in.isFinished()) {
//ExtruderBoard::getBoard().indicateError(1); cycles = 3000;
out.reset();
ExtruderBoard::getBoard().indicateError(1);
// SPECIAL CASE: we always process debug packets!
if (processDebugPacket(in,out)) {
// okay, processed
Expand Down
1 change: 0 additions & 1 deletion v2/src/Extruder/Main.cc
Expand Up @@ -41,7 +41,6 @@ void reset() {

int main() {
reset();
ExtruderBoard::getBoard().indicateError(1);
while (1) {
// Host interaction thread.
runHostSlice();
Expand Down
9 changes: 8 additions & 1 deletion v2/src/Extruder/MotorController.cc
Expand Up @@ -27,6 +27,7 @@ MotorController::MotorController() {

void MotorController::reset() {
direction = true;
paused = false;
on = false;
speed = 0;
backoff_state = BO_INACTIVE;
Expand Down Expand Up @@ -74,7 +75,7 @@ void MotorController::update() {
}
}
} else {
int new_speed = on?(direction?speed:-speed):0;
int new_speed = (!paused&&on)?(direction?speed:-speed):0;
board.setMotorSpeed(new_speed);
}
}
Expand All @@ -83,6 +84,12 @@ void MotorController::setSpeed(int speed_in) {
speed = speed_in;
}

void MotorController::pause() {
paused = !paused;
//ExtruderBoard::getBoard().indicateError(paused?1:0);

}

void MotorController::setDir(bool dir_in) {
direction = dir_in;
}
Expand Down
2 changes: 2 additions & 0 deletions v2/src/Extruder/MotorController.hh
Expand Up @@ -27,6 +27,7 @@ public:
void setSpeed(int speed);
void setDir(bool dir);
void setOn(bool on);
void pause();
static MotorController& getController() { return motor_controller; }
static void runMotorSlice() { getController().update(); }
// Reset to board-on state
Expand All @@ -37,6 +38,7 @@ private:
bool direction;
bool on;
int speed;
bool paused;
// Backoff instrumentation
bool backoff_enabled;
enum {
Expand Down
4 changes: 2 additions & 2 deletions v2/src/Extruder/boards/ecv22/UART.cc
Expand Up @@ -44,13 +44,13 @@ UART UART::uart;
inline void speak() {
TX_ENABLE_PIN.setValue(true);
RX_ENABLE_PIN.setValue(true);
ExtruderBoard::getBoard().indicateError(0);
ExtruderBoard::getBoard().indicateError(1);
}

inline void listen() {
TX_ENABLE_PIN.setValue(false);
RX_ENABLE_PIN.setValue(false);
//ExtruderBoard::getBoard().indicateError(1);
ExtruderBoard::getBoard().indicateError(0);
}

UART::UART() : enabled(false) {
Expand Down
41 changes: 37 additions & 4 deletions v2/src/Motherboard/Host.cc
Expand Up @@ -203,10 +203,37 @@ inline void handleNextFilename(const InPacket& from_host, OutPacket& to_host) {
to_host.append8(0);
}

inline void handlePause(const InPacket& from_host, OutPacket& to_host) {
command::pause(!command::isPaused());
tool::pause();
to_host.append8(RC_OK);
void doToolPause(OutPacket& to_host) {
Timeout acquire_lock_timeout;
acquire_lock_timeout.start(HOST_TOOL_RESPONSE_TIMEOUT_MS);
while (!tool::getLock()) {
if (acquire_lock_timeout.hasElapsed()) {
to_host.append8(RC_DOWNSTREAM_TIMEOUT);
Motherboard::getBoard().indicateError(ERR_SLAVE_LOCK_TIMEOUT);
return;
}
}
OutPacket& out = tool::getOutPacket();
InPacket& in = tool::getInPacket();
out.reset();
out.append8(0); // TODO: current tool
out.append8(SLAVE_CMD_PAUSE_UNPAUSE);
// Timeouts are handled inside the toolslice code; there's no need
// to check for timeouts on this loop.
tool::startTransaction();
tool::releaseLock();
// WHILE: bounded by tool timeout in runToolSlice
while (!tool::isTransactionDone()) {
tool::runToolSlice();
}
if (in.getErrorCode() == PacketError::PACKET_TIMEOUT) {
to_host.append8(RC_DOWNSTREAM_TIMEOUT);
} else {
// Copy payload back. Start from 0-- we need the response code.
for (int i = 0; i < in.getLength(); i++) {
to_host.append8(in.read8(i));
}
}
}

inline void handleToolQuery(const InPacket& from_host, OutPacket& to_host) {
Expand Down Expand Up @@ -250,6 +277,12 @@ inline void handleToolQuery(const InPacket& from_host, OutPacket& to_host) {
}
}

inline void handlePause(const InPacket& from_host, OutPacket& to_host) {
command::pause(!command::isPaused());
doToolPause(to_host);
to_host.append8(RC_OK);
}

inline void handleIsFinished(const InPacket& from_host, OutPacket& to_host) {
to_host.append8(RC_OK);
ATOMIC_BLOCK(ATOMIC_FORCEON) {
Expand Down
25 changes: 1 addition & 24 deletions v2/src/Motherboard/Tool.cc
Expand Up @@ -57,37 +57,14 @@ bool reset() {
out.append8(0); // TODO: tool index
out.append8(SLAVE_CMD_INIT);
startTransaction();
releaseLock();
// WHILE: bounded by tool timeout
while (!isTransactionDone()) {
runToolSlice();
}
return Motherboard::getBoard().getSlaveUART().in.isFinished();
}

void pause() {
// This code is very lightly modified from handleToolQuery in Host.cc.
// We don't give up if we fail to get a lock; we force it instead.
Timeout acquire_lock_timeout;
acquire_lock_timeout.start(TOOL_PACKET_TIMEOUT_MICROS*2);
while (!tool::getLock()) {
if (acquire_lock_timeout.hasElapsed()) {
return;
}
}
OutPacket& out = getOutPacket();
InPacket& in = getInPacket();
out.reset();
out.append8(0); // TODO: tool index
out.append8(SLAVE_CMD_PAUSE_UNPAUSE);
startTransaction();
// override standard timeout
timeout.start(TOOL_PACKET_TIMEOUT_MICROS*2);
releaseLock();
// WHILE: bounded by tool timeout
while (!isTransactionDone()) {
runToolSlice();
}
return Motherboard::getBoard().getSlaveUART().in.isFinished();
}

/// The tool is considered locked if a transaction is in progress or
Expand Down
6 changes: 0 additions & 6 deletions v2/src/Motherboard/Tool.hh
Expand Up @@ -78,12 +78,6 @@ InPacket& getInPacket();
*/
bool reset();

/**
* Pause or unpause the tool. Practically speaking,
* this means stop or restart the extrusion.
*/
void pause();

}

#endif // TOOL_HH_

0 comments on commit c1cce43

Please sign in to comment.