Permalink
Browse files

Merge branch 'master' into revarLCD

  • Loading branch information...
2 parents 4d16548 + abc3658 commit df880f4ecb6bb7c071fd88e061bdf0f98d2a9c31 Jordan Miller committed May 6, 2010
Showing with 656 additions and 355 deletions.
  1. +0 −2 v2/src/Extruder/DebugPacketProcessor.cc
  2. +7 −0 v2/src/Extruder/EepromMap.cc
  3. +9 −0 v2/src/Extruder/EepromMap.hh
  4. +47 −11 v2/src/Extruder/Heater.cc
  5. +12 −4 v2/src/Extruder/Heater.hh
  6. +23 −2 v2/src/Extruder/Host.cc
  7. +3 −3 v2/src/Extruder/Main.cc
  8. +1 −1 v2/src/Extruder/Main.hh
  9. +18 −5 v2/src/Extruder/MotorController.cc
  10. +5 −1 v2/src/Extruder/MotorController.hh
  11. +1 −1 v2/src/Extruder/SConscript
  12. +3 −1 v2/src/Extruder/TemperatureThread.cc
  13. +5 −3 v2/src/Extruder/Timeout.cc
  14. +4 −2 v2/src/Extruder/Timeout.hh
  15. +1 −1 v2/src/Extruder/Types.hh
  16. +1 −1 v2/src/Extruder/Version.hh
  17. +5 −13 v2/src/Extruder/boards/ecv22/AnalogPin.cc
  18. +0 −2 v2/src/Extruder/boards/ecv22/AnalogPin.hh
  19. +36 −18 v2/src/Extruder/boards/ecv22/ExtruderBoard.cc
  20. +4 −0 v2/src/Extruder/boards/ecv22/ExtruderBoard.hh
  21. +5 −1 v2/src/Extruder/boards/ecv22/ExtruderMotor.cc
  22. +2 −2 v2/src/Extruder/boards/ecv22/Thermistor.hh
  23. +11 −4 v2/src/Extruder/boards/ecv22/UART.cc
  24. +43 −5 v2/src/Motherboard/Host.cc
  25. +18 −7 v2/src/Motherboard/Main.cc
  26. +3 −1 v2/src/Motherboard/Main.hh
  27. +26 −12 v2/src/Motherboard/SDCard.cc
  28. +13 −11 v2/src/Motherboard/Timeout.cc
  29. +7 −5 v2/src/Motherboard/Timeout.hh
  30. +2 −2 v2/src/Motherboard/Tool.cc
  31. +1 −1 v2/src/Motherboard/Types.hh
  32. +1 −1 v2/src/Motherboard/Version.hh
  33. +4 −4 v2/src/shared/AvrPort.cc
  34. +20 −13 v2/src/shared/AvrPort.hh
  35. +2 −0 v2/src/shared/Commands.hh
  36. +33 −7 v2/src/shared/PID.cc
  37. +14 −1 v2/src/shared/PID.hh
  38. +2 −0 v2/tests/T0-Utilities/SConstruct
  39. +172 −0 v2/tests/T0-Utilities/src/T0.2.TimeoutTest.cc
  40. +92 −207 v2/tests/T5-Extruder/src/T5-Extruder-exerciser.cc
@@ -48,8 +48,6 @@ enum {
}
-OutPacket debugSlaveOut;
-
/// Identify a debug packet, and process it. If the packet is a debug
/// packet, return true, indicating that no further processing should
/// be done. Otherwise, processing of this packet should drop through
@@ -52,4 +52,11 @@ uint16_t getEeprom16(const uint16_t location, const uint16_t default_value) {
return data;
}
+float getEepromFixed16(const uint16_t location, const float default_value) {
+ uint8_t data[2];
+ eeprom_read_block(data,(const uint8_t*)location,2);
+ if (data[0] == 0xff && data[1] == 0xff) return default_value;
+ return ((float)data[0]) + ((float)data[1])/256.0;
+}
+
} // namespace eeprom
@@ -63,6 +63,14 @@ const static uint16_t BACKOFF_FORWARD_TIME = 0x0008;
/// Backoff trigger time, in ms: 2 bytes
const static uint16_t BACKOFF_TRIGGER_TIME = 0x000A;
+/// Extruder PID P term, in fixed-point: 2 bytes
+const static uint16_t EXTRUDER_PID_P_TERM = 0x000C;
+/// Extruder PID I term, in fixed-point: 2 bytes
+const static uint16_t EXTRUDER_PID_I_TERM = 0x000E;
+/// Extruder PID D term, in fixed-point: 2 bytes
+const static uint16_t EXTRUDER_PID_D_TERM = 0x0010;
+
+
const static uint16_t THERM_R0_OFFSET = 0x00;
const static uint16_t THERM_T0_OFFSET = 0x04;
const static uint16_t THERM_BETA_OFFSET = 0x08;
@@ -78,6 +86,7 @@ void init();
uint8_t getEeprom8(const uint16_t location, const uint8_t default_value);
uint16_t getEeprom16(const uint16_t location, const uint16_t default_value);
+float getEepromFixed16(const uint16_t location, const float default_value);
} // namespace eeprom
@@ -20,30 +20,59 @@
#include "HeatingElement.hh"
#include "Thermistor.hh"
#include "ExtruderBoard.hh"
+#include "EepromMap.hh"
+
+#define DEFAULT_P 7.0
+#define DEFAULT_I 0.325
+#define DEFAULT_D 36.0
+
+// Offset to compensate for range clipping and bleed-off
+#define HEATER_OFFSET_ADJUSTMENT 0
Heater::Heater(TemperatureSensor& sensor_in, HeatingElement& element_in) :
sensor(sensor_in),
- element(element_in),
- current_temperature(0),
- last_update(0)
+ element(element_in)
{
+ reset();
+}
+
+void Heater::reset() {
+ current_temperature = 0;
+
+ float p = eeprom::getEepromFixed16(eeprom::EXTRUDER_PID_P_TERM,DEFAULT_P);
+ float i = eeprom::getEepromFixed16(eeprom::EXTRUDER_PID_I_TERM,DEFAULT_I);
+ float d = eeprom::getEepromFixed16(eeprom::EXTRUDER_PID_D_TERM,DEFAULT_D);
+
pid.reset();
- pid.setPGain(5.0);
- pid.setIGain(0.1);
- pid.setDGain(5.0);
+ if (p == 0 && i == 0 && d == 0) {
+ p = DEFAULT_P; i = DEFAULT_I; d = DEFAULT_D;
+ }
+ pid.setPGain(p);
+ pid.setIGain(i);
+ pid.setDGain(d);
pid.setTarget(0);
- next_read_timeout.start(UPDATE_INTERVAL_MICROS);
+ next_pid_timeout.start(UPDATE_INTERVAL_MICROS);
+ next_sense_timeout.start(SAMPLE_INTERVAL_MICROS);
}
void Heater::set_target_temperature(int temp)
{
pid.setTarget(temp);
}
+// We now define target hysteresis in absolute degrees. The original
+// implementation (+/-5%) was giving us swings of 10% in either direction
+// *before* any artifacts of process instability came in.
+#define TARGET_HYSTERESIS 2
+
bool Heater::hasReachedTargetTemperature()
{
- return (current_temperature > (pid.getTarget() * 0.95)) &&
- (current_temperature < (pid.getTarget() * 1.1));
+ return (current_temperature >= (pid.getTarget() - TARGET_HYSTERESIS)) &&
+ (current_temperature <= (pid.getTarget() + TARGET_HYSTERESIS));
+}
+
+int Heater::get_set_temperature() {
+ return pid.getTarget();
}
/**
@@ -63,13 +92,20 @@ int Heater::get_current_temperature()
*/
void Heater::manage_temperature()
{
- if (next_read_timeout.hasElapsed()) {
+ if (next_sense_timeout.hasElapsed()) {
if (!sensor.update()) return;
- next_read_timeout.start(UPDATE_INTERVAL_MICROS);
+ next_sense_timeout.start(SAMPLE_INTERVAL_MICROS);
+ }
+ if (next_pid_timeout.hasElapsed()) {
+ next_pid_timeout.start(UPDATE_INTERVAL_MICROS);
// update the temperature reading.
current_temperature = get_current_temperature();
int mv = pid.calculate(current_temperature);
+ // offset value to compensate for heat bleed-off.
+ // There are probably more elegant ways to do this,
+ // but this works pretty well.
+ mv += HEATER_OFFSET_ADJUSTMENT;
// clamp value
if (mv < 0) { mv = 0; }
if (mv >255) { mv = 255; }
@@ -30,26 +30,34 @@ class Heater
private:
TemperatureSensor& sensor;
HeatingElement& element;
- Timeout next_read_timeout;
+ Timeout next_pid_timeout;
+ Timeout next_sense_timeout;
int current_temperature;
PID pid;
- const static int UPDATE_INTERVAL_MICROS = 60L * 1000L;
+ // This is the interval between PID calculations. Longer updates are (counterintuitively)
+ // better since we're using discrete D.
+ const static micros_t UPDATE_INTERVAL_MICROS = 500L * 1000L;
+ // This is the interval between sensor samples.
+ const static micros_t SAMPLE_INTERVAL_MICROS = 50L * 1000L;
public:
- micros_t last_update;
Heater(TemperatureSensor& sensor, HeatingElement& element);
- int get_current_temperature();
+ int get_current_temperature();
+ int get_set_temperature();
void set_target_temperature(int temp);
bool hasReachedTargetTemperature();
// Call once each temperature interval
void manage_temperature();
void set_output(uint8_t value);
+
+ // Reset to board-on state
+ void reset();
};
#endif // HEATER_H
@@ -30,8 +30,8 @@
Timeout packet_in_timeout;
-#define HOST_PACKET_TIMEOUT_MS 20
-#define HOST_PACKET_TIMEOUT_MICROS (1000*HOST_PACKET_TIMEOUT_MS)
+#define HOST_PACKET_TIMEOUT_MS 20L
+#define HOST_PACKET_TIMEOUT_MICROS (1000L*HOST_PACKET_TIMEOUT_MS)
inline void handleReadEeprom(const InPacket& from_host, OutPacket& to_host) {
const uint16_t offset = from_host.read16(2);
const uint8_t count = from_host.read8(4);
@@ -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) {
@@ -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);
@@ -113,6 +121,8 @@ bool processQueryPacket(const InPacket& from_host, OutPacket& to_host) {
board.setFan((from_host.read8(2) & 0x01) != 0);
to_host.append8(RC_OK);
return true;
+ case SLAVE_CMD_TOGGLE_VALVE:
+ board.setValve((from_host.read8(2) & 0x01) != 0);
case SLAVE_CMD_IS_TOOL_READY:
to_host.append8(RC_OK);
to_host.append8(board.getExtruderHeater().hasReachedTargetTemperature()?1:0);
@@ -122,14 +132,25 @@ bool processQueryPacket(const InPacket& from_host, OutPacket& to_host) {
to_host.append16(board.getPlatformHeater().get_current_temperature());
return true;
case SLAVE_CMD_SET_PLATFORM_TEMP:
+ board.setUsingPlatform(true);
board.getPlatformHeater().set_target_temperature(from_host.read16(2));
to_host.append8(RC_OK);
return true;
+ case SLAVE_CMD_GET_SP:
+ to_host.append8(RC_OK);
+ to_host.append16(board.getExtruderHeater().get_set_temperature());
+ return true;
+ case SLAVE_CMD_GET_PLATFORM_SP:
+ to_host.append8(RC_OK);
+ to_host.append16(board.getPlatformHeater().get_set_temperature());
+ return true;
}
}
return false;
}
+extern int cycles;
+
void runHostSlice() {
UART& uart = ExtruderBoard::getBoard().getHostUART();
InPacket& in = uart.in;
@@ -30,17 +30,17 @@
void runHostSlice();
void reset() {
+ cli();
// Intialize various modules
initThermistorTables();
eeprom::init();
ExtruderBoard::getBoard().reset();
- MotorController::getController().setOn(false);
+ MotorController::getController().reset();
+ sei();
}
int main() {
reset();
- ExtruderBoard::getBoard().indicateError(0);
- sei();
while (1) {
// Host interaction thread.
runHostSlice();
@@ -19,7 +19,7 @@
#ifndef MAIN_HH_
#define MAIN_HH_
-// Reset the extruder head
+// Reset the extruder head.
void reset();
#endif // MAIN_HH_
@@ -21,9 +21,16 @@
MotorController MotorController::motor_controller;
-MotorController::MotorController() :
- direction(true), on(false), speed(0), backoff_state(BO_INACTIVE)
-{
+MotorController::MotorController() {
+ reset();
+}
+
+void MotorController::reset() {
+ direction = true;
+ paused = false;
+ on = false;
+ speed = 0;
+ backoff_state = BO_INACTIVE;
loadBackoffParameters();
}
@@ -68,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);
}
}
@@ -77,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;
}
@@ -89,7 +102,7 @@ void MotorController::setOn(bool on_in) {
current_operation_timeout.start(halt_ms*1000L);
} else if (on_in) {
if (!on && direction) {
- forward_trigger_timeout.start(trigger_ms);
+ forward_trigger_timeout.start(trigger_ms*1000L);
}
backoff_state = BO_INACTIVE;
}
@@ -27,14 +27,18 @@ public:
void setSpeed(int speed);
void setDir(bool dir);
void setOn(bool on);
- void loadBackoffParameters();
+ void pause();
static MotorController& getController() { return motor_controller; }
static void runMotorSlice() { getController().update(); }
+ // Reset to board-on state
+ void reset();
private:
MotorController();
+ void loadBackoffParameters();
bool direction;
bool on;
int speed;
+ bool paused;
// Backoff instrumentation
bool backoff_enabled;
enum {
@@ -84,7 +84,7 @@ objs = env.Object(srcs)
hex_name = target_name + '.hex'
elf_name = target_name + '.elf'
-env.Append(BUILDERS={'Elf':Builder(action=avr_tools_path+"/avr-gcc -mmcu="+mcu+" -Os -Wl,--gc-sections -o $TARGET $SOURCES")})
+env.Append(BUILDERS={'Elf':Builder(action=avr_tools_path+"/avr-gcc -mmcu="+mcu+" -Wl,-Map=${TARGET}.map -Os -Wl,--gc-sections -o $TARGET $SOURCES")})
env.Append(BUILDERS={'Hex':Builder(action=avr_tools_path+"/avr-objcopy -O ihex -R .eeprom $SOURCES $TARGET")})
env.Elf(elf_name, objs)
env.Hex(hex_name, elf_name)
@@ -21,5 +21,7 @@
void runTempSlice() {
ExtruderBoard& board = ExtruderBoard::getBoard();
board.getExtruderHeater().manage_temperature();
- board.getPlatformHeater().manage_temperature();
+ if (board.isUsingPlatform()) {
+ board.getPlatformHeater().manage_temperature();
+ }
}
@@ -20,15 +20,17 @@
Timeout::Timeout() : active(false), elapsed(false) {}
-void Timeout::start(uint32_t duration_micros) {
+void Timeout::start(micros_t duration_micros_in) {
active = true;
elapsed = false;
- end_stamp_micros = ExtruderBoard::getBoard().getCurrentMicros() + duration_micros;
+ start_stamp_micros = ExtruderBoard::getBoard().getCurrentMicros();
+ duration_micros = duration_micros_in;
}
bool Timeout::hasElapsed() {
if (active && !elapsed) {
- if ((end_stamp_micros - ExtruderBoard::getBoard().getCurrentMicros()) <= 0) {
+ micros_t delta = ExtruderBoard::getBoard().getCurrentMicros() - start_stamp_micros;
+ if (delta >= duration_micros) {
active = false;
elapsed = true;
}
Oops, something went wrong.

0 comments on commit df880f4

Please sign in to comment.