Commit 41d26978 authored by MagoKimbra's avatar MagoKimbra

Update 4.0.3

parent 1e446da3
......@@ -5,16 +5,9 @@
#include "Marlin.h"
#ifdef BLINKM
#if (ARDUINO >= 100)
# include "Arduino.h"
#else
# include "WProgram.h"
#endif
#include "BlinkM.h"
void SendColors(byte red, byte grn, byte blu)
{
void SendColors(byte red, byte grn, byte blu) {
Wire.begin();
Wire.beginTransmission(0x09);
Wire.write('o'); //to disable ongoing script, only needs to be used once
......
......@@ -2,13 +2,12 @@
BlinkM.h
Library header file for BlinkM library
*/
#if (ARDUINO >= 100)
# include "Arduino.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
# include "WProgram.h"
#include "WProgram.h"
#endif
#include "Wire.h"
void SendColors(byte red, byte grn, byte blu);
......@@ -20,7 +20,7 @@
// User-specified version info of this build to display in [Pronterface, etc] terminal window during
// startup. Implementation of an idea by Prof Braino to inform user that any changes made to this
// build by the user have been successfully uploaded into firmware.
#define STRING_VERSION "4.0.2"
#define STRING_VERSION " 4.0.3"
#define STRING_URL "reprap.org"
#define STRING_VERSION_CONFIG_H __DATE__ " " __TIME__ // build date and time
#define STRING_CONFIG_H_AUTHOR "(none, default config)" // Who made the changes.
......
......@@ -5,39 +5,36 @@
#include "ultralcd.h"
#include "ConfigurationStore.h"
void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size)
{
void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
uint8_t c;
while(size--)
{
while(size--) {
eeprom_write_byte((unsigned char*)pos, *value);
c = eeprom_read_byte((unsigned char*)pos);
if (c != *value)
{
if (c != *value) {
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
}
pos++;
value++;
}
};
}
void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size)
{
do
{
void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size) {
do {
*value = eeprom_read_byte((unsigned char*)pos);
pos++;
value++;
} while (--size);
}
#define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
#define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
//======================================================================================
#define DUMMY_PID_VALUE 3000.0f
#define EEPROM_OFFSET 100
// IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
// in the functions below, also increment the version number. This makes sure that
// the default values are used whenever there is a change to the data, to prevent
......@@ -47,8 +44,7 @@ void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size)
#define EEPROM_VERSION "V13"
#ifdef EEPROM_SETTINGS
void Config_StoreSettings()
{
void Config_StoreSettings() {
float dummy = 0.0f;
char ver[4] = "000";
int i = EEPROM_OFFSET;
......
#ifndef CONFIG_STORE_H
#define CONFIG_STORE_H
#ifndef __CONFIGURATIONSTORE_H
#define __CONFIGURATIONSTORE_H
#include "Configuration.h"
......@@ -19,4 +19,4 @@ void Config_ResetDefault();
FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); }
#endif
#endif //CONFIG_STORE_H
#endif //__CONFIGURATIONSTORE_H
// Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
// License: GPL
#ifndef MARLIN_H
#define MARLIN_H
#ifndef __MARLIN_H
#define __MARLIN_H
#define FORCE_INLINE __attribute__((always_inline)) inline
......@@ -68,6 +68,7 @@
#define SERIAL_PROTOCOLLN(x) (MYSERIAL.print(x),MYSERIAL.write('\n'))
#define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x)),MYSERIAL.write('\n'))
extern const char errormagic[] PROGMEM;
extern const char echomagic[] PROGMEM;
......@@ -143,8 +144,8 @@ void manage_inactivity(bool ignore_stepper_queue=false);
#define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
#endif
#else
#define enable_z();
#define disable_z();
#define enable_z() ;
#define disable_z() ;
#endif
#if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
......@@ -181,8 +182,12 @@ void manage_inactivity(bool ignore_stepper_queue=false);
#define disable_e() {disable_e0(); disable_e1(); disable_e2(); disable_e3();}
enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3};
#ifdef COREXY
enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
//X_HEAD and Y_HEAD is used for systems that don't have a 1:1 relationship between X_AXIS and X Head movement, like CoreXY bots.
#else
enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3};
#endif
void FlushSerialRequestResend();
void ClearToSend();
......@@ -342,4 +347,4 @@ void FirmwareTest();
extern void calculate_volumetric_multipliers();
#endif //MARLIN_H
\ No newline at end of file
#endif //__MARLIN_H
\ No newline at end of file
......@@ -20,7 +20,7 @@
#ifdef ENABLE_AUTO_BED_LEVELING
#if Z_MIN_PIN == -1
#error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature!!! Z_MIN_PIN must point to a valid hardware pin."
#error "You must have a Z_MIN endstop to enable Auto Bed Leveling feature. Z_MIN_PIN must point to a valid hardware pin."
#endif
#include "vector_3.h"
#ifdef AUTO_BED_LEVELING_GRID
......@@ -613,8 +613,7 @@ void setup_pausepin()
void setup_photpin()
{
#if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
SET_OUTPUT(PHOTOGRAPH_PIN);
WRITE(PHOTOGRAPH_PIN, LOW);
OUT_WRITE(PHOTOGRAPH_PIN, LOW);
#endif
}
......@@ -629,25 +628,21 @@ void setup_statledpin()
void setup_laserbeampin()
{
#ifdef LASERBEAM
SET_OUTPUT(LASER_PWR_PIN);
WRITE(LASER_PWR_PIN, LOW);
SET_OUTPUT(LASER_TTL_PIN);
WRITE(LASER_TTL_PIN, LOW);
OUT_WRITE(LASER_PWR_PIN, LOW);
OUT_WRITE(LASER_TTL_PIN, LOW);
#endif
}
void setup_powerhold()
{
#if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, HIGH);
OUT_WRITE(SUICIDE_PIN, HIGH);
#endif
#if defined(PS_ON_PIN) && PS_ON_PIN > -1
SET_OUTPUT(PS_ON_PIN);
#if defined(PS_DEFAULT_OFF)
WRITE(PS_ON_PIN, PS_ON_ASLEEP);
OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
#else
WRITE(PS_ON_PIN, PS_ON_AWAKE);
OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
#endif
#endif
}
......@@ -655,8 +650,7 @@ void setup_powerhold()
void suicide()
{
#if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, LOW);
OUT_WRITE(SUICIDE_PIN, LOW);
#endif
}
......@@ -758,8 +752,7 @@ void setup()
digipot_i2c_init();
#endif
#ifdef Z_PROBE_SLED
SET_OUTPUT(SERVO0_PIN);
WRITE(SERVO0_PIN, LOW); // turn it off
OUT_WRITE(SERVO0_PIN, LOW); // turn it off
#endif // Z_PROBE_SLED
setup_homepin();
#ifdef FIRMWARE_TEST
......@@ -1055,8 +1048,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
#endif //DUAL_X_CARRIAGE
#if defined(CARTESIAN) || defined(COREXY) || defined(SCARA)
static void axis_is_at_home(int axis)
{
static void axis_is_at_home(int axis) {
#ifdef DUAL_X_CARRIAGE
if (axis == X_AXIS)
{
......@@ -1082,10 +1074,8 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
float homeposition[3];
char i;
if (axis < 2)
{
for (i=0; i<3; i++)
{
if (axis < 2) {
for (i=0; i<3; i++) {
homeposition[i] = base_home_pos(i);
}
// SERIAL_ECHOPGM("homeposition[x]= "); SERIAL_ECHO(homeposition[0]);
......@@ -1097,8 +1087,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
// SERIAL_ECHOPGM("base Theta= "); SERIAL_ECHO(delta[X_AXIS]);
// SERIAL_ECHOPGM(" base Psi+Theta="); SERIAL_ECHOLN(delta[Y_AXIS]);
for (i=0; i<2; i++)
{
for (i=0; i<2; i++) {
delta[i] -= add_homing[i];
}
......@@ -1134,8 +1123,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
#ifdef ENABLE_AUTO_BED_LEVELING
#ifdef AUTO_BED_LEVELING_GRID
static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
{
static void set_bed_level_equation_lsq(double *plane_equation_coefficients) {
vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
planeNormal.debug("planeNormal");
plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
......@@ -1157,8 +1145,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
}
#else // not AUTO_BED_LEVELING_GRID
static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3)
{
static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
plan_bed_level_matrix.set_to_identity();
vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
......@@ -1184,8 +1171,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
#endif // AUTO_BED_LEVELING_GRID
static void run_z_probe()
{
static void run_z_probe() {
plan_bed_level_matrix.set_to_identity();
feedrate = homing_feedrate[Z_AXIS];
......@@ -1214,8 +1200,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
}
static void do_blocking_move_to(float x, float y, float z)
{
static void do_blocking_move_to(float x, float y, float z) {
float oldFeedRate = feedrate;
feedrate = homing_feedrate[Z_AXIS];
......@@ -1233,13 +1218,11 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
feedrate = oldFeedRate;
}
static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z)
{
static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
}
static void setup_for_endstop_move()
{
static void setup_for_endstop_move() {
saved_feedrate = feedrate;
saved_feedmultiply = feedmultiply;
feedmultiply = 100;
......@@ -1247,8 +1230,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
enable_endstops(true);
}
static void clean_up_after_endstop_move()
{
static void clean_up_after_endstop_move() {
#ifdef ENDSTOPS_ONLY_FOR_HOMING
enable_endstops(false);
#endif
......@@ -1257,8 +1239,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
refresh_cmd_timeout();
}
static void engage_z_probe()
{
static void engage_z_probe() {
// Engage Z Servo endstop if enabled
#if defined(SERVO_ENDSTOPS) && (NUM_SERVOS > 0)
if (servo_endstops[Z_AXIS] > -1)
......@@ -1275,8 +1256,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
#endif
}
static void retract_z_probe()
{
static void retract_z_probe() {
// Retract Z Servo endstop if enabled
#if defined(SERVO_ENDSTOPS) && (NUM_SERVOS > 0)
if (servo_endstops[Z_AXIS] > -1)
......@@ -1294,8 +1274,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
}
/// Probe bed height at position (x,y), returns the measured z value
static float probe_pt(float x, float y, float z_before, int retract_action=0)
{
static float probe_pt(float x, float y, float z_before, int retract_action=0) {
// move to right place
do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
......@@ -1319,10 +1298,10 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
SERIAL_PROTOCOLPGM("\n");
return measured_z;
}
#endif // #ifdef ENABLE_AUTO_BED_LEVELING
static void homeaxis(int axis)
{
static void homeaxis(int axis) {
#define HOMEAXIS_DO(LETTER) \
((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
......@@ -1890,10 +1869,7 @@ XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
}
#endif //DELTA
void refresh_cmd_timeout(void)
{
previous_millis_cmd = millis();
}
void refresh_cmd_timeout(void) { previous_millis_cmd = millis(); }
#ifdef FWRETRACT
void retract(bool retracting, bool swapretract = false)
......@@ -1971,26 +1947,22 @@ void refresh_cmd_timeout(void)
// dock[in] If true, move to MAX_X and engage the electromagnet
// offset[in] The additional distance to move to adjust docking location
//
static void dock_sled(bool dock, int offset=0)
{
static void dock_sled(bool dock, int offset=0) {
int z_loc;
if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS])))
{
if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
return;
}
if (dock)
{
if (dock) {
do_blocking_move_to(X_MAX_POS + SLED_DOCKING_OFFSET + offset, current_position[Y_AXIS], current_position[Z_AXIS]);
// turn off magnet
digitalWrite(SERVO0_PIN, LOW);
}
else
{
else {
if (current_position[Z_AXIS] < (Z_RAISE_BEFORE_PROBING + 5))
z_loc = Z_RAISE_BEFORE_PROBING;
else
......@@ -2002,8 +1974,7 @@ void refresh_cmd_timeout(void)
}
#endif //Z_PROBE_SLED
inline void lcd_beep(int number_beep=3)
{
inline void lcd_beep(int number_beep=3) {
#ifdef LCD_USE_I2C_BUZZER
#if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
for(int8_t i=0;i<3;i++) {
......@@ -2017,16 +1988,14 @@ inline void lcd_beep(int number_beep=3)
#elif defined(BEEPER) && BEEPER > -1
SET_OUTPUT(BEEPER);
#if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
for(int8_t i=0;i<number_beep;i++)
{
for(int8_t i=0;i<number_beep;i++) {
WRITE(BEEPER,HIGH);
delay(100);
WRITE(BEEPER,LOW);
delay(100);
}
#else
for(int8_t i=0;i<number_beep;i++)
{
for(int8_t i=0;i<number_beep;i++) {
WRITE(BEEPER,HIGH);
delay(1000000 / LCD_FEEDBACK_FREQUENCY_HZ / 2);
WRITE(BEEPER,LOW);
......@@ -2036,8 +2005,7 @@ inline void lcd_beep(int number_beep=3)
#endif
}
inline void wait_heater()
{
inline void wait_heater() {
setWatch();
unsigned long codenum = millis();
......@@ -2056,8 +2024,7 @@ inline void wait_heater()
while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)))
#endif //TEMP_RESIDENCY_TIME
{
if ((millis() - codenum) > 1000UL)
{
if ((millis() - codenum) > 1000UL) {
//Print Temp Reading and remaining time every 1 second while heating up/cooling down
SERIAL_PROTOCOLPGM("T:");
SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
......@@ -2065,13 +2032,11 @@ inline void wait_heater()
SERIAL_PROTOCOL((int)tmp_extruder);
#ifdef TEMP_RESIDENCY_TIME
SERIAL_PROTOCOLPGM(" W:");
if (residencyStart > -1)
{
if (residencyStart > -1) {
codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
SERIAL_PROTOCOLLN( codenum );
}
else
{
else {
SERIAL_PROTOCOLLN( "?" );
}
#else
......@@ -2098,16 +2063,13 @@ inline void wait_heater()
refresh_cmd_timeout();
}
inline void wait_bed()
{
inline void wait_bed() {
unsigned long codenum = millis();
cancel_heatup = false;
target_direction = isHeatingBed(); // true if heating, false if cooling
while ((target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)))
{
if ((millis() - codenum) > 1000) //Print Temp Reading every 1 second while heating up.
{
while ((target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false))) {
if ((millis() - codenum) > 1000) { //Print Temp Reading every 1 second while heating up.
float tt=degHotend(active_extruder);
SERIAL_PROTOCOLPGM("T:");
SERIAL_PROTOCOL(tt);
......@@ -2169,8 +2131,8 @@ inline void gcode_G4() {
if (code_seen('S')) codenum = code_value_long() * 1000; // seconds to wait
st_synchronize();
codenum += millis(); // keep track of when we started waiting
previous_millis_cmd = millis();
codenum += previous_millis_cmd; // keep track of when we started waiting
while(millis() < codenum) {
manage_heater();
manage_inactivity();
......@@ -2208,7 +2170,7 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
enable_endstops(true);
for(int8_t i=0; i < NUM_AXIS; i++) destination[i] = current_position[i];
for (int i=0; i < NUM_AXIS; i++) destination[i] = current_position[i];
feedrate = 0.0;
......@@ -2232,7 +2194,7 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
for (int i = X_AXIS; i <= Z_AXIS; i++) current_position[i] = 0;
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
for (int i = X_AXIS; i <= Z_AXIS; i++) destination[i] = 3 * max_length[Z_AXIS];
for (int i = X_AXIS; i <= Z_AXIS; i++) destination[i] = 3 * Z_MAX_LENGTH;
feedrate = 1.732 * homing_feedrate[X_AXIS];
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder, active_driver);
st_synchronize();
......@@ -2503,31 +2465,143 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
}
#ifdef ENABLE_AUTO_BED_LEVELING
// G29: Detailed Z-Probe, probes the bed at 3 or more points.
// Will fail if the printer has not been homed with G28.
// Override probing area by providing [F]ront [B]ack [L]eft [R]ight Grid[P]oints values
/**
* G29: Detailed Z-Probe, probes the bed at 3 or more points.
* Will fail if the printer has not been homed with G28.
*
* Enhanced G29 Auto Bed Leveling Probe Routine
*
* Parameters With AUTO_BED_LEVELING_GRID:
*
* P Set the size of the grid that will be probed (P x P points).
* Example: "G29 P4"
*
* V Set the verbose level (0-4). Example: "G29 V3"
*
* T Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
* This is useful for manual bed leveling and finding flaws in the bed (to
* assist with part placement).
*
* F Set the Front limit of the probing grid
* B Set the Back limit of the probing grid
* L Set the Left limit of the probing grid
* R Set the Right limit of the probing grid
*
* Global Parameters:
*
* E/e By default G29 engages / disengages the probe for each point.
* Include "E" to engage and disengage the probe just once.
* There's no extra effect if you have a fixed probe.
* Usage: "G29 E" or "G29 e"
*
*/
enum ProbeAction { ProbeStay, ProbeEngage, ProbeRetract, ProbeEngageRetract };
// Use one of these defines to specify the origin
// for a topographical map to be printed for your bed.
enum { OriginBackLeft, OriginFrontLeft, OriginBackRight, OriginFrontRight };
#define TOPO_ORIGIN OriginFrontLeft
inline void gcode_G29() {
float x_tmp, y_tmp, z_tmp, real_z;
// Prevent user from running a G29 without first homing in X and Y
if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS])) {
if (!axis_known_position[X_AXIS] || !axis_known_position[Y_AXIS]) {
LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
return;
}
bool enhanced_g29 = code_seen('E') || code_seen('e');
#ifdef AUTO_BED_LEVELING_GRID
// Example Syntax: G29 N4 V2 E T
int verbose_level = 1;
bool topo_flag = code_seen('T') || code_seen('t');
if (code_seen('V') || code_seen('v')) {
verbose_level = code_value();
if (verbose_level < 0 || verbose_level > 4) {
SERIAL_PROTOCOLPGM("?(V)erbose Level is implausible (0-4).\n");
return;
}
if (verbose_level > 0) {
SERIAL_PROTOCOLPGM("G29 Enhanced Auto Bed Leveling Code:\n");
if (verbose_level > 2) topo_flag = true;
}
}
int auto_bed_leveling_grid_points = code_seen('P') ? code_value_long() : AUTO_BED_LEVELING_GRID_POINTS;
if (auto_bed_leveling_grid_points < 2) {
SERIAL_PROTOCOLPGM("?Number of probed (P)oints is implausible (2 minimum).\n");
return;
}
// Define the possible boundaries for probing based on the set limits.
// Code above (in G28) might have these limits wrong, or I am wrong here.
#define MIN_PROBE_EDGE 10 // Edges of the probe square can be no less
const int min_probe_x = max(X_MIN_POS, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
max_probe_x = min(X_MAX_POS, X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
min_probe_y = max(Y_MIN_POS, Y_MIN_POS + Y_PROBE_OFFSET_FROM_EXTRUDER),
max_probe_y = min(Y_MAX_POS, Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER);
int left_probe_bed_position = code_seen('L') ? code_value_long() : LEFT_PROBE_BED_POSITION,
right_probe_bed_position = code_seen('R') ? code_value_long() : RIGHT_PROBE_BED_POSITION,
front_probe_bed_position = code_seen('F') ? code_value_long() : FRONT_PROBE_BED_POSITION,
back_probe_bed_position = code_seen('B') ? code_value_long() : BACK_PROBE_BED_POSITION;
bool left_out_l = left_probe_bed_position < min_probe_x,
left_out_r = left_probe_bed_position > right_probe_bed_position - MIN_PROBE_EDGE,
left_out = left_out_l || left_out_r,
right_out_r = right_probe_bed_position > max_probe_x,
right_out_l =right_probe_bed_position < left_probe_bed_position + MIN_PROBE_EDGE,
right_out = right_out_l || right_out_r,
front_out_f = front_probe_bed_position < min_probe_y,
front_out_b = front_probe_bed_position > back_probe_bed_position - MIN_PROBE_EDGE,
front_out = front_out_f || front_out_b,
back_out_b = back_probe_bed_position > max_probe_y,
back_out_f = back_probe_bed_position < front_probe_bed_position + MIN_PROBE_EDGE,
back_out = back_out_f || back_out_b;
if (left_out || right_out || front_out || back_out) {
if (left_out) {
SERIAL_PROTOCOLPGM("?Probe (L)eft position out of range.\n");
left_probe_bed_position = left_out_l ? min_probe_x : right_probe_bed_position - MIN_PROBE_EDGE;
}
if (right_out) {
SERIAL_PROTOCOLPGM("?Probe (R)ight position out of range.\n");
right_probe_bed_position = right_out_r ? max_probe_x : left_probe_bed_position + MIN_PROBE_EDGE;
}
if (front_out) {
SERIAL_PROTOCOLPGM("?Probe (F)ront position out of range.\n");
front_probe_bed_position = front_out_f ? min_probe_y : back_probe_bed_position - MIN_PROBE_EDGE;
}
if (back_out) {
SERIAL_PROTOCOLPGM("?Probe (B)ack position out of range.\n");
back_probe_bed_position = back_out_b ? max_probe_y : front_probe_bed_position + MIN_PROBE_EDGE;
}
return;
}
#endif // AUTO_BED_LEVELING_GRID
#ifdef Z_PROBE_SLED
dock_sled(false);
#endif //Z_PROBE_SLED
dock_sled(false); // engage (un-dock) the probe
#endif
st_synchronize();
// make sure the bed_level_rotation_matrix is identity or the planner will get it incorrectly
// make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
//vector_3 corrected_position = plan_get_position_mm();
//corrected_position.debug("position before G29");
plan_bed_level_matrix.set_to_identity();
vector_3 uncorrected_position = plan_get_position();
//uncorrected_position.debug("position during G29");
//uncorrected_position.debug("position durring G29");
current_position[X_AXIS] = uncorrected_position.x;
current_position[Y_AXIS] = uncorrected_position.y;
current_position[Z_AXIS] = uncorrected_position.z;
......@@ -2537,27 +2611,10 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
feedrate = homing_feedrate[Z_AXIS];
#ifdef AUTO_BED_LEVELING_GRID
int right_probe_bed_position = RIGHT_PROBE_BED_POSITION;
int left_probe_bed_position = LEFT_PROBE_BED_POSITION;
int front_probe_bed_position = FRONT_PROBE_BED_POSITION;
int back_probe_bed_position = BACK_PROBE_BED_POSITION;
int auto_bed_leveling_points = AUTO_BED_LEVELING_GRID_POINTS;
if (code_seen('R')) right_probe_bed_position = code_value();
if (code_seen('L')) left_probe_bed_position = code_value();
if (code_seen('F')) front_probe_bed_position = code_value();
if (code_seen('B')) back_probe_bed_position = code_value();
if (code_seen('A')) auto_bed_leveling_points = code_value();
if((front_probe_bed_position == back_probe_bed_position) || (right_probe_bed_position == left_probe_bed_position)) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_EMPTY_PLANE);
return;
}
// probe at the points of a lattice grid
int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_points-1);
int yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_points-1);
int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
int yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
// solve the plane equation ax + by + d = z
// A is the matrix with rows [x y 1] for all the probed points
......@@ -2565,87 +2622,154 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
// the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
// so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
// "A" matrix of the linear system of equations
double eqnAMatrix[auto_bed_leveling_points*auto_bed_leveling_points*3];
// "B" vector of Z points
double eqnBVector[auto_bed_leveling_points*auto_bed_leveling_points];
int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points;
double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
eqnBVector[abl2], // "B" vector of Z points
mean = 0.0;
int probePointCounter = 0;
bool zig = true;
for (int yProbe=front_probe_bed_position; yProbe <= back_probe_bed_position; yProbe += yGridSpacing) {
for (int yProbe = front_probe_bed_position; yProbe <= back_probe_bed_position; yProbe += yGridSpacing) {
int xProbe, xInc;
if (zig) { //zig
xProbe = left_probe_bed_position;
//xEnd = RIGHT_PROBE_BED_POSITION;
xInc = xGridSpacing;
zig = false;
}
else { //zag
xProbe = right_probe_bed_position;
//xEnd = LEFT_PROBE_BED_POSITION;
xInc = -xGridSpacing;
zig = true;
}
for (int xCount=0; xCount < auto_bed_leveling_points; xCount++) {
float z_before;
if (probePointCounter == 0) {
// raise before probing
z_before = Z_RAISE_BEFORE_PROBING;
}
else {
if (zig)
xProbe = left_probe_bed_position, xInc = xGridSpacing;
else
xProbe = right_probe_bed_position, xInc = -xGridSpacing;
// If topo_flag is set then don't zig-zag. Just scan in one direction.
// This gets the probe points in more readable order.
if (!topo_flag) zig = !zig;
for (int xCount = 0; xCount < auto_bed_leveling_grid_points; xCount++) {
// raise extruder
z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
float measured_z,
z_before = probePointCounter == 0 ? Z_RAISE_BEFORE_PROBING : current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
// Enhanced G29 - Do not retract servo between probes
ProbeAction act;
if (enhanced_g29) {
if (yProbe == front_probe_bed_position && xCount == 0)
act = ProbeEngage;
else if (yProbe == front_probe_bed_position + (yGridSpacing * (auto_bed_leveling_grid_points - 1)) && xCount == auto_bed_leveling_grid_points - 1)
act = ProbeRetract;
else
act = ProbeStay;
}
else
act = ProbeEngageRetract;
measured_z = probe_pt(xProbe, yProbe, z_before, act);
mean += measured_z;
float measured_z = probe_pt(xProbe, yProbe, z_before);
eqnBVector[probePointCounter] = measured_z;
eqnAMatrix[probePointCounter + 0 * auto_bed_leveling_points * auto_bed_leveling_points] = xProbe;
eqnAMatrix[probePointCounter + 1 * auto_bed_leveling_points * auto_bed_leveling_points] = yProbe;
eqnAMatrix[probePointCounter + 2 * auto_bed_leveling_points * auto_bed_leveling_points] = 1;
eqnAMatrix[probePointCounter + 0 * abl2] = xProbe;
eqnAMatrix[probePointCounter + 1 * abl2] = yProbe;
eqnAMatrix[probePointCounter + 2 * abl2] = 1;
probePointCounter++;
xProbe += xInc;
}
}
} //xProbe
} //yProbe
clean_up_after_endstop_move();
// solve lsq problem
double *plane_equation_coefficients = qr_solve(auto_bed_leveling_points * auto_bed_leveling_points, 3, eqnAMatrix, eqnBVector);
double *plane_equation_coefficients = qr_solve(abl2, 3, eqnAMatrix, eqnBVector);
mean /= abl2;
if (verbose_level) {
SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
SERIAL_PROTOCOL(plane_equation_coefficients[0]);
SERIAL_PROTOCOLPGM(" b: ");
SERIAL_PROTOCOL(plane_equation_coefficients[1]);
SERIAL_PROTOCOLPGM(" d: ");
SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
if (verbose_level > 2) {
SERIAL_PROTOCOLPGM("Mean of sampled points: ");
SERIAL_PROTOCOL_F(mean, 6);
SERIAL_PROTOCOLPGM(" \n");
}
}
if (topo_flag) {
int xx, yy;
SERIAL_PROTOCOLPGM(" \nBed Height Topography: \n");
#if TOPO_ORIGIN == OriginFrontLeft
for (yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--)
#else
for (yy = 0; yy < auto_bed_leveling_grid_points; yy++)
#endif
{
#if TOPO_ORIGIN == OriginBackRight
for (xx = auto_bed_leveling_grid_points - 1; xx >= 0; xx--)
#else
for (xx = 0; xx < auto_bed_leveling_grid_points; xx++)
#endif
{
int ind =
#if TOPO_ORIGIN == OriginBackRight || TOPO_ORIGIN == OriginFrontLeft
yy * auto_bed_leveling_grid_points + xx
#elif TOPO_ORIGIN == OriginBackLeft
xx * auto_bed_leveling_grid_points + yy
#elif TOPO_ORIGIN == OriginFrontRight
abl2 - xx * auto_bed_leveling_grid_points - yy - 1
#endif
;
float diff = eqnBVector[ind] - mean;
if (diff >= 0.0)
SERIAL_PROTOCOLPGM(" +"); // Watch column alignment in Pronterface
else
SERIAL_PROTOCOLPGM(" -");
SERIAL_PROTOCOL_F(diff, 5);
} // xx
SERIAL_PROTOCOLPGM("\n");
} // yy
SERIAL_PROTOCOLPGM("\n");
} //topo_flag
set_bed_level_equation_lsq(plane_equation_coefficients);
free(plane_equation_coefficients);
#else // AUTO_BED_LEVELING_GRID not defined
#else // !AUTO_BED_LEVELING_GRID
// Probe at 3 arbitrary points
// probe 1
float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
// probe 2
float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
// probe 3
float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
float z_at_pt_1, z_at_pt_2, z_at_pt_3;
if (enhanced_g29) {
// Basic Enhanced G29
z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING, ProbeEngage);
z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeStay);
z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeRetract);
}
else {
z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
}
clean_up_after_endstop_move();
set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
#endif // AUTO_BED_LEVELING_GRID
#endif // !AUTO_BED_LEVELING_GRID
st_synchronize();
if (verbose_level > 0)
plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
// The following code correct the Z height difference from z-probe position and hotend tip position.
// The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
// When the bed is uneven, this height must be corrected.
real_z = float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane)
real_z = float(st_get_position(Z_AXIS)) / axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane)
x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
z_tmp = current_position[Z_AXIS];
......@@ -2653,9 +2777,10 @@ inline void gcode_G28(boolean home_x=false, boolean home_y=false) {
apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
#ifdef Z_PROBE_SLED
dock_sled(true, -SLED_DOCKING_OFFSET); // correct for over travel.
#endif // Z_PROBE_SLED
dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
#endif
}
#ifndef Z_PROBE_SLED
......@@ -3304,11 +3429,33 @@ inline void gcode_G92() {
#endif //LASERBEAM
#if defined(ENABLE_AUTO_BED_LEVELING) && defined(Z_PROBE_REPEATABILITY_TEST)
//M49: Z-Probe repeatability
inline void gcode_M49() {
#if Z_MIN_PIN == -1
#error "You must have a Z_MIN endstop in order to enable calculation of Z-Probe repeatability."
#endif
/**
* M49: Z-Probe repeatability measurement function.
*
* Usage:
* M48 <n#> <X#> <Y#> <V#> <E> <L#>
* n = Number of samples (4-50, default 10)
* X = Sample X position
* Y = Sample Y position
* V = Verbose level (0-4, default=1)
* E = Engage probe for each reading
* L = Number of legs of movement before probe
*
* This function assumes the bed has been homed. Specificaly, that a G28 command
* as been issued prior to invoking the M48 Z-Probe repeatability measurement function.
* Any information generated by a prior G29 Bed leveling command will be lost and need to be
* regenerated.
*
* The number of samples will default to 10 if not specified. You can use upper or lower case
* letters for any of the options EXCEPT n. n must be in lower case because Marlin uses a capital
* N for its communication protocol and will get horribly confused if you send it a capital N.
*/
inline void gcode_M49() {
double sum = 0.0, mean = 0.0, sigma = 0.0, sample_set[50];
int verbose_level = 1, n = 0, j, n_samples = 10, n_legs = 0, engage_probe_for_each_reading = 0;
......@@ -3324,8 +3471,7 @@ inline void gcode_G92() {
}
if (verbose_level > 0) {
SERIAL_PROTOCOLPGM("M49 Z-Probe Repeatability test. Version 2.00\n");
SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
SERIAL_PROTOCOLPGM("M49 Z-Probe Repeatability test.\n");
}
if (code_seen('n')) {
......@@ -3376,7 +3522,7 @@ inline void gcode_G92() {
st_synchronize();
plan_bed_level_matrix.set_to_identity();
plan_buffer_line( X_current, Y_current, Z_start_location, ext_position, homing_feedrate[Z_AXIS]/60, active_extruder, active_driver);
plan_buffer_line(X_current, Y_current, Z_start_location, ext_position, homing_feedrate[Z_AXIS]/60, active_extruder, active_driver);
st_synchronize();
//
......@@ -3387,7 +3533,7 @@ inline void gcode_G92() {
if (verbose_level > 2)
SERIAL_PROTOCOL("Positioning probe for the test.\n");
plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, ext_position, homing_feedrate[X_AXIS]/60, active_extruder, active_driver);
plan_buffer_line(X_probe_location, Y_probe_location, Z_start_location, ext_position, homing_feedrate[X_AXIS]/60, active_extruder, active_driver);
st_synchronize();
current_position[X_AXIS] = X_current = st_get_position_mm(X_AXIS);
......@@ -3408,7 +3554,7 @@ inline void gcode_G92() {
current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
Z_start_location = st_get_position_mm(Z_AXIS) + Z_RAISE_BEFORE_PROBING;
plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, ext_position, homing_feedrate[X_AXIS]/60, active_extruder, active_driver);
plan_buffer_line(X_probe_location, Y_probe_location, Z_start_location, ext_position, homing_feedrate[X_AXIS]/60, active_extruder, active_driver);
st_synchronize();
current_position[Z_AXIS] = Z_current = st_get_position_mm(Z_AXIS);
......@@ -3498,7 +3644,7 @@ inline void gcode_G92() {
if (verbose_level > 0)
SERIAL_PROTOCOLPGM("\n");
plan_buffer_line( X_probe_location, Y_probe_location, Z_start_location, current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder, active_driver);
plan_buffer_line(X_probe_location, Y_probe_location, Z_start_location, current_position[E_AXIS], homing_feedrate[Z_AXIS]/60, active_extruder, active_driver);
st_synchronize();
if (engage_probe_for_each_reading) {
......@@ -3522,7 +3668,7 @@ inline void gcode_G92() {
SERIAL_PROTOCOL_F(sigma, 6);
SERIAL_PROTOCOLPGM("\n\n");
}
#endif //defined(ENABLE_AUTO_BED_LEVELING) && defined(Z_PROBE_REPEATABILITY_TEST)
#endif // ENABLE_AUTO_BED_LEVELING && Z_PROBE_REPEATABILITY_TEST
#ifdef FILAMENTCHANGEENABLE
//M600: Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
......@@ -6279,11 +6425,9 @@ void Stop()
bool IsStopped() { return Stopped; };
#ifdef FAST_PWM_FAN
void setPwmFrequency(uint8_t pin, int val)
{
void setPwmFrequency(uint8_t pin, int val) {
val &= 0x07;
switch(digitalPinToTimer(pin))
{
switch(digitalPinToTimer(pin)) {
#if defined(TCCR0A)
case TIMER0A:
case TIMER0B:
......@@ -6347,17 +6491,13 @@ bool IsStopped() { return Stopped; };
}
#endif //FAST_PWM_FAN
bool setTargetedHotend(int code)
{
bool setTargetedHotend(int code) {
tmp_extruder = active_extruder;
if(code_seen('T'))
{
if(code_seen('T')) {
tmp_extruder = code_value();
if(tmp_extruder >= EXTRUDERS)
{
if(tmp_extruder >= EXTRUDERS) {
SERIAL_ECHO_START;
switch(code)
{
switch(code) {
case 104:
SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
break;
......
......@@ -7,256 +7,193 @@
#ifdef SDSUPPORT
CardReader::CardReader()
{
CardReader::CardReader() {
filesize = 0;
sdpos = 0;
sdprinting = false;
cardOK = false;
saving = false;
logging = false;
autostart_atmillis=0;
workDirDepth = 0;
file_subcall_ctr=0;
file_subcall_ctr = 0;
memset(workDirParents, 0, sizeof(workDirParents));
autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
autostart_index=0;
autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
autostart_index = 0;
//power to SD reader
#if SDPOWER > -1
SET_OUTPUT(SDPOWER);
WRITE(SDPOWER,HIGH);
OUT_WRITE(SDPOWER, HIGH);
#endif //SDPOWER
autostart_atmillis=millis()+5000;
autostart_atmillis = millis() + 5000;
}
char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
{
char *pos=buffer;
for (uint8_t i = 0; i < 11; i++)
{
if (p.name[i] == ' ')continue;
if (i == 8)
{
*pos++='.';
char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
char *pos = buffer;
for (uint8_t i = 0; i < 11; i++) {
if (p.name[i] == ' ') continue;
if (i == 8) *pos++ = '.';
*pos++ = p.name[i];
}
*pos++=p.name[i];
}
*pos++=0;
*pos++ = 0;
return buffer;
}
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/)
{
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
dir_t p;
uint8_t cnt=0;
while (parent.readDir(p, longFilename) > 0)
{
if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
{
uint8_t cnt = 0;
while (parent.readDir(p, longFilename) > 0) {
if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // hence LS_SerialPrint
char path[FILENAME_LENGTH*2];
char lfilename[FILENAME_LENGTH];
createFilename(lfilename,p);
createFilename(lfilename, p);
path[0]=0;
if(prepend[0]==0) //avoid leading / if already in prepend
{
strcat(path,"/");
}
strcat(path,prepend);
strcat(path,lfilename);
strcat(path,"/");
path[0] = 0;
if (prepend[0] == 0) strcat(path, "/"); //avoid leading / if already in prepend
strcat(path, prepend);
strcat(path, lfilename);
strcat(path, "/");
//Serial.print(path);
SdFile dir;
if(!dir.open(parent,lfilename, O_READ))
{
if(lsAction==LS_SerialPrint)
{
if (!dir.open(parent, lfilename, O_READ)) {
if (lsAction == LS_SerialPrint) {
SERIAL_ECHO_START;
SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
SERIAL_ECHOLN(lfilename);
}
}
lsDive(path,dir);
lsDive(path, dir);
//close done automatically by destructor of SdFile
}
else
{
else {
char pn0 = p.name[0];
if (pn0 == DIR_NAME_FREE) break;
if (pn0 == DIR_NAME_DELETED || pn0 == '.' || pn0 == '_') continue;
if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
char lf0 = longFilename[0];
if (lf0 == '.' || lf0 == '_') continue;
if (lf0 == '.') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
filenameIsDir=DIR_IS_SUBDIR(&p);
filenameIsDir = DIR_IS_SUBDIR(&p);
if(!filenameIsDir)
{
if(p.name[8]!='G') continue;
if(p.name[9]=='~') continue;
}
//if(cnt++!=nr) continue;
createFilename(filename,p);
if(lsAction==LS_SerialPrint)
{
if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
//if (cnt++ != nr) continue;
createFilename(filename, p);
if (lsAction == LS_SerialPrint) {
SERIAL_PROTOCOL(prepend);
SERIAL_PROTOCOLLN(filename);
}
else if(lsAction==LS_Count)
{
else if (lsAction == LS_Count) {
nrFiles++;
}
else if(lsAction==LS_GetFilename)
{
else if (lsAction == LS_GetFilename) {
if (match != NULL) {
if (strcasecmp(match, filename) == 0) return;
}
else if (cnt == nrFiles) return;
cnt++;
}
}
}
}
void CardReader::ls()
{
lsAction=LS_SerialPrint;
if(lsAction==LS_Count)
nrFiles=0;
void CardReader::ls() {
lsAction = LS_SerialPrint;
root.rewind();
lsDive("",root);
lsDive("", root);
}
void CardReader::initsd()
{
void CardReader::initsd() {
cardOK = false;
if(root.isOpen())
root.close();
#ifdef SDSLOW
if (!card.init(SPI_HALF_SPEED,SDSS)
#if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
&& !card.init(SPI_HALF_SPEED,LCD_SDSS)
if (root.isOpen()) root.close();
#ifdef SDSLOW
#define SPI_SPEED SPI_HALF_SPEED
#else
#define SPI_SPEED SPI_FULL_SPEED
#endif
)
#else
if (!card.init(SPI_FULL_SPEED,SDSS)
if (!card.init(SPI_SPEED,SDSS)
#if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
&& !card.init(SPI_FULL_SPEED,LCD_SDSS)
&& !card.init(SPI_SPEED, LCD_SDSS)
#endif
)
#endif
{
) {
//if (!card.init(SPI_HALF_SPEED,SDSS))
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
}
else if (!volume.init(&card))
{
else if (!volume.init(&card)) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
}
else if (!root.openRoot(&volume))
{
else if (!root.openRoot(&volume)) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
}
else
{
else {
cardOK = true;
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
}
workDir=root;
curDir=&root;
workDir = root;
curDir = &root;
/*
if(!workDir.openRoot(&volume))
{
if (!workDir.openRoot(&volume)) {
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
}
*/
}
void CardReader::setroot()
{
/*if(!workDir.openRoot(&volume))
{
void CardReader::setroot() {
/*if (!workDir.openRoot(&volume)) {
SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
}*/
workDir=root;
curDir=&workDir;
workDir = root;
curDir = &workDir;
}
void CardReader::release()
{
void CardReader::release() {
sdprinting = false;
cardOK = false;
}
void CardReader::startFileprint()
{
if(cardOK)
{
void CardReader::startFileprint() {
if (cardOK) {
sdprinting = true;
}
}
void CardReader::pauseSDPrint()
{
if(sdprinting)
{
sdprinting = false;
}
void CardReader::pauseSDPrint() {
if (sdprinting) sdprinting = false;
}
void CardReader::openLogFile(char* name)
{
void CardReader::openLogFile(char* name) {
logging = true;
openFile(name, false);
}
void CardReader::getAbsFilename(char *t)
{
uint8_t cnt=0;
*t='/';t++;cnt++;
for(uint8_t i=0;i<workDirDepth;i++)
{
void CardReader::getAbsFilename(char *t) {
uint8_t cnt = 0;
*t = '/'; t++; cnt++;
for (uint8_t i = 0; i < workDirDepth; i++) {
workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
while(*t!=0 && cnt< MAXPATHNAMELENGTH)
{t++;cnt++;} //crawl counter forward.
while(*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
}
if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
if (cnt < MAXPATHNAMELENGTH - FILENAME_LENGTH)
file.getFilename(t);
else
t[0]=0;
t[0] = 0;
}
void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
{
if(!cardOK)
return;
if(file.isOpen()) //replacing current file by new file, or subfile call
{
if(!replace_current)
{
if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
{
void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) {
if (!cardOK) return;
if (file.isOpen()) { //replacing current file by new file, or subfile call
if (!replace_current) {
if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
SERIAL_ERROR_START;
SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
......@@ -275,79 +212,67 @@ void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
SERIAL_ECHO(filenames[file_subcall_ctr]);
SERIAL_ECHOPGM("\" pos");
SERIAL_ECHOLN(sdpos);
filespos[file_subcall_ctr]=sdpos;
filespos[file_subcall_ctr] = sdpos;
file_subcall_ctr++;
}
else
{
else {
SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now doing file: ");
SERIAL_ECHOLN(name);
}
file.close();
}
else //opening fresh file
{
file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
else { //opening fresh file
file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure
SERIAL_ECHO_START;
SERIAL_ECHOPGM("Now fresh file: ");
SERIAL_ECHOLN(name);
}
sdprinting = false;
SdFile myDir;
curDir=&root;
char *fname=name;
char *dirname_start,*dirname_end;
if(name[0]=='/')
{
dirname_start=strchr(name,'/')+1;
while(dirname_start>0)
{
dirname_end=strchr(dirname_start,'/');
//SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
if(dirname_end>0 && dirname_end>dirname_start)
{
curDir = &root;
char *fname = name;
char *dirname_start, *dirname_end;
if (name[0] == '/') {
dirname_start = &name[1];
while(dirname_start > 0) {
dirname_end = strchr(dirname_start, '/');
//SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
if (dirname_end > 0 && dirname_end > dirname_start) {
char subdirname[FILENAME_LENGTH];
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
subdirname[dirname_end-dirname_start]=0;
strncpy(subdirname, dirname_start, dirname_end - dirname_start);
subdirname[dirname_end - dirname_start] = 0;
SERIAL_ECHOLN(subdirname);
if(!myDir.open(curDir,subdirname,O_READ))
{
if (!myDir.open(curDir, subdirname, O_READ)) {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(subdirname);
SERIAL_PROTOCOLLNPGM(".");
return;
}
else
{
else {
//SERIAL_ECHOLN("dive ok");
}
curDir=&myDir;
dirname_start=dirname_end+1;
curDir = &myDir;
dirname_start = dirname_end + 1;
}
else // the reminder after all /fsa/fdsa/ is the filename
{
fname=dirname_start;
//SERIAL_ECHOLN("remaider");
else { // the remainder after all /fsa/fdsa/ is the filename
fname = dirname_start;
//SERIAL_ECHOLN("remainder");
//SERIAL_ECHOLN(fname);
break;
}
}
}
else //relative path
{
curDir=&workDir;
else { //relative path
curDir = &workDir;
}
if(read)
{
if (file.open(curDir, fname, O_READ))
{
if (read) {
if (file.open(curDir, fname, O_READ)) {
filesize = file.fileSize();
SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
SERIAL_PROTOCOL(fname);
......@@ -359,124 +284,105 @@ void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
getfilename(0, fname);
lcd_setstatus(longFilename[0] ? longFilename : fname);
}
else
{
else {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLLNPGM(".");
}
}
else
{ //write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
{
else { //write
if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLLNPGM(".");
}
else
{
else {
saving = true;
SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
SERIAL_PROTOCOLLN(name);
lcd_setstatus(fname);
}
}
}
void CardReader::removeFile(char* name)
{
if(!cardOK)
return;
void CardReader::removeFile(char* name) {
if (!cardOK) return;
file.close();
sdprinting = false;
SdFile myDir;
curDir=&root;
char *fname=name;
char *dirname_start,*dirname_end;
if(name[0]=='/')
{
dirname_start=strchr(name,'/')+1;
while(dirname_start>0)
{
dirname_end=strchr(dirname_start,'/');
//SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
if(dirname_end>0 && dirname_end>dirname_start)
{
curDir = &root;
char *fname = name;
char *dirname_start, *dirname_end;
if (name[0] == '/') {
dirname_start = strchr(name, '/') + 1;
while (dirname_start > 0) {
dirname_end = strchr(dirname_start, '/');
//SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
//SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end - name));
if (dirname_end > 0 && dirname_end > dirname_start) {
char subdirname[FILENAME_LENGTH];
strncpy(subdirname, dirname_start, dirname_end-dirname_start);
subdirname[dirname_end-dirname_start]=0;
strncpy(subdirname, dirname_start, dirname_end - dirname_start);
subdirname[dirname_end - dirname_start] = 0;
SERIAL_ECHOLN(subdirname);
if(!myDir.open(curDir,subdirname,O_READ))
{
if (!myDir.open(curDir, subdirname, O_READ)) {
SERIAL_PROTOCOLPGM("open failed, File: ");
SERIAL_PROTOCOL(subdirname);
SERIAL_PROTOCOLLNPGM(".");
return;
}
else
{
else {
//SERIAL_ECHOLN("dive ok");
}
curDir=&myDir;
dirname_start=dirname_end+1;
curDir = &myDir;
dirname_start = dirname_end + 1;
}
else // the reminder after all /fsa/fdsa/ is the filename
{
fname=dirname_start;
//SERIAL_ECHOLN("remaider");
else { // the remainder after all /fsa/fdsa/ is the filename
fname = dirname_start;
//SERIAL_ECHOLN("remainder");
//SERIAL_ECHOLN(fname);
break;
}
}
}
else //relative path
{
curDir=&workDir;
else { // relative path
curDir = &workDir;
}
if (file.remove(curDir, fname))
{
if (file.remove(curDir, fname)) {
SERIAL_PROTOCOLPGM("File deleted:");
SERIAL_PROTOCOLLN(fname);
sdpos = 0;
}
else
{
else {
SERIAL_PROTOCOLPGM("Deletion failed, File: ");
SERIAL_PROTOCOL(fname);
SERIAL_PROTOCOLLNPGM(".");
}
}
void CardReader::getStatus()
{
if(cardOK){
void CardReader::getStatus() {
if (cardOK) {
SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
SERIAL_PROTOCOL(sdpos);
SERIAL_PROTOCOLPGM("/");
SERIAL_PROTOCOLLN(filesize);
}
else{
else {
SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
}
}
void CardReader::write_command(char *buf)
{
void CardReader::write_command(char *buf) {
char* begin = buf;
char* npos = 0;
char* end = buf + strlen(buf) - 1;
file.writeError = false;
if((npos = strchr(buf, 'N')) != NULL)
{
if ((npos = strchr(buf, 'N')) != NULL) {
begin = strchr(npos, ' ') + 1;
end = strchr(npos, '*') - 1;
}
......@@ -484,162 +390,129 @@ void CardReader::write_command(char *buf)
end[2] = '\n';
end[3] = '\0';
file.write(begin);
if (file.writeError)
{
if (file.writeError) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
}
}
void CardReader::checkautostart(bool force)
{
if(!force)
{
if(!autostart_stilltocheck)
return;
if(autostart_atmillis<millis())
void CardReader::checkautostart(bool force) {
if (!force && (!autostart_stilltocheck || autostart_atmillis < millis()))
return;
}
autostart_stilltocheck=false;
if(!cardOK)
{
autostart_stilltocheck = false;
if (!cardOK) {
initsd();
if(!cardOK) //fail
return;
if (!cardOK) return; // fail
}
char autoname[30];
sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
autoname[i]=tolower(autoname[i]);
for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
dir_t p;
root.rewind();
bool found=false;
while (root.readDir(p, NULL) > 0)
{
for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
p.name[i]=tolower(p.name[i]);
//Serial.print((char*)p.name);
//Serial.print(" ");
//Serial.println(autoname);
if(p.name[9]!='~') //skip safety copies
if(strncmp((char*)p.name,autoname,5)==0)
{
bool found = false;
while (root.readDir(p, NULL) > 0) {
for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
char cmd[30];
sprintf_P(cmd, PSTR("M23 %s"), autoname);
enquecommand(cmd);
enquecommands_P(PSTR("M24"));
found=true;
found = true;
}
}
if(!found)
autostart_index=-1;
if (!found)
autostart_index = -1;
else
autostart_index++;
}
void CardReader::closefile(bool store_location)
{
void CardReader::closefile(bool store_location) {
file.sync();
file.close();
saving = false;
logging = false;
saving = logging = false;
if(store_location)
{
if (store_location) {
//future: store printer state, filename and position for continuing a stopped print
// so one can unplug the printer and continue printing the next day.
}
}
void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
{
curDir=&workDir;
lsAction=LS_GetFilename;
nrFiles=nr;
/**
* Get the name of a file in the current directory by index
*/
void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
curDir = &workDir;
lsAction = LS_GetFilename;
nrFiles = nr;
curDir->rewind();
lsDive("",*curDir,match);
lsDive("", *curDir, match);
}
uint16_t CardReader::getnrfilenames()
{
curDir=&workDir;
lsAction=LS_Count;
nrFiles=0;
uint16_t CardReader::getnrfilenames() {
curDir = &workDir;
lsAction = LS_Count;
nrFiles = 0;
curDir->rewind();
lsDive("",*curDir);
lsDive("", *curDir);
//SERIAL_ECHOLN(nrFiles);
return nrFiles;
}
void CardReader::chdir(const char * relpath)
{
void CardReader::chdir(const char * relpath) {
SdFile newfile;
SdFile *parent=&root;
SdFile *parent = &root;
if(workDir.isOpen())
parent=&workDir;
if (workDir.isOpen()) parent = &workDir;
if(!newfile.open(*parent,relpath, O_READ))
{
if (!newfile.open(*parent, relpath, O_READ)) {
SERIAL_ECHO_START;
SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
SERIAL_ECHOLN(relpath);
}
else
{
else {
if (workDirDepth < MAX_DIR_DEPTH) {
for (int d = ++workDirDepth; d--;)
workDirParents[d+1] = workDirParents[d];
workDirParents[0]=*parent;
++workDirDepth;
for (int d = workDirDepth; d--;) workDirParents[d + 1] = workDirParents[d];
workDirParents[0] = *parent;
}
workDir=newfile;
workDir = newfile;
}
}
void CardReader::updir()
{
if(workDirDepth > 0)
{
void CardReader::updir() {
if (workDirDepth > 0) {
--workDirDepth;
workDir = workDirParents[0];
int d;
for (int d = 0; d < workDirDepth; d++)
workDirParents[d] = workDirParents[d+1];
}
}
void CardReader::printingHasFinished()
{
void CardReader::printingHasFinished() {
st_synchronize();
if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
{
if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
file.close();
file_subcall_ctr--;
openFile(filenames[file_subcall_ctr],true,true);
openFile(filenames[file_subcall_ctr], true, true);
setIndex(filespos[file_subcall_ctr]);
startFileprint();
}
else
{
else {
quickStop();
file.close();
sdprinting = false;
if(SD_FINISHED_STEPPERRELEASE)
{
if (SD_FINISHED_STEPPERRELEASE) {
//finishAndDisableSteppers();
enquecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
}
autotempShutdown();
}
}
#endif //SDSUPPORT
#ifndef CARDREADER_H
#define CARDREADER_H
#ifndef __CARDREADER_H
#define __CARDREADER_H
#ifdef SDSUPPORT
#define MAX_DIR_DEPTH 10
#define MAX_DIR_DEPTH 10 // Maximum folder depth
#include "SdFile.h"
enum LsAction {LS_SerialPrint,LS_Count,LS_GetFilename};
class CardReader
{
enum LsAction { LS_SerialPrint, LS_Count, LS_GetFilename };
class CardReader {
public:
CardReader();
......@@ -33,7 +33,6 @@ public:
void getAbsFilename(char *t);
void ls();
void chdir(const char * relpath);
void updir();
......@@ -41,56 +40,52 @@ public:
FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
FORCE_INLINE bool eof() { return sdpos>=filesize ;};
FORCE_INLINE int16_t get() { sdpos = file.curPosition();return (int16_t)file.read();};
FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);};
FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
FORCE_INLINE bool eof() { return sdpos >= filesize; }
FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
FORCE_INLINE void setIndex(long index) { sdpos = index; file.seekSet(index); }
FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
public:
bool saving;
bool logging;
bool sdprinting;
bool cardOK;
char filename[FILENAME_LENGTH];
char longFilename[LONG_FILENAME_LENGTH];
bool filenameIsDir;
bool saving, logging, sdprinting, cardOK, filenameIsDir;
char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
int autostart_index;
private:
SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
uint16_t workDirDepth;
Sd2Card card;
SdVolume volume;
SdFile file;
#define SD_PROCEDURE_DEPTH 1
#define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
#define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
uint8_t file_subcall_ctr;
uint32_t filespos[SD_PROCEDURE_DEPTH];
char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
uint32_t filesize;
//int16_t n;
unsigned long autostart_atmillis;
uint32_t sdpos ;
uint32_t sdpos;
bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
LsAction lsAction; //stored for recursion.
int16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
uint16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
char* diveDirName;
void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
};
extern CardReader card;
#define IS_SD_PRINTING (card.sdprinting)
#if (SDCARDDETECT > -1)
# ifdef SDCARDDETECTINVERTED
# define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
# else
# define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
# endif //SDCARDTETECTINVERTED
#ifdef SDCARDDETECTINVERTED
#define IS_SD_INSERTED (READ(SDCARDDETECT) != 0)
#else
#define IS_SD_INSERTED (READ(SDCARDDETECT) == 0)
#endif
#else
//If we don't have a card detect line, aways asume the card is inserted
# define IS_SD_INSERTED true
//No card detect line? Assume the card is inserted.
#define IS_SD_INSERTED true
#endif
#else
......@@ -98,4 +93,5 @@ extern CardReader card;
#define IS_SD_PRINTING (false)
#endif //SDSUPPORT
#endif
#endif //__CARDREADER_H
#include "Configuration.h"
#ifdef DIGIPOT_I2C
#include "Stream.h"
#include "utility/twi.h"
#include "Wire.h"
// Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
#if MB(5DPRINT)
#define DIGIPOT_I2C_FACTOR 117.96
#define DIGIPOT_I2C_MAX_CURRENT 1.736
#define DIGIPOT_I2C_FACTOR 117.96
#define DIGIPOT_I2C_MAX_CURRENT 1.736
#else
#define DIGIPOT_I2C_FACTOR 106.7
#define DIGIPOT_I2C_MAX_CURRENT 2.5
#define DIGIPOT_I2C_FACTOR 106.7
#define DIGIPOT_I2C_MAX_CURRENT 2.5
#endif
static byte current_to_wiper( float current ){
static byte current_to_wiper(float current) {
return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
}
static void i2c_send(byte addr, byte a, byte b)
{
static void i2c_send(byte addr, byte a, byte b) {
Wire.beginTransmission(addr);
Wire.write(a);
Wire.write(b);
......@@ -27,33 +27,32 @@ static void i2c_send(byte addr, byte a, byte b)
}
// This is for the MCP4451 I2C based digipot
void digipot_i2c_set_current( int channel, float current )
{
void digipot_i2c_set_current(int channel, float current) {
current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
// these addresses are specific to Azteeg X3 Pro, can be set to others,
// In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
byte addr= 0x2C; // channel 0-3
if(channel >= 4) {
addr= 0x2E; // channel 4-7
channel-= 4;
byte addr = 0x2C; // channel 0-3
if (channel >= 4) {
addr = 0x2E; // channel 4-7
channel -= 4;
}
// Initial setup
i2c_send( addr, 0x40, 0xff );
i2c_send( addr, 0xA0, 0xff );
i2c_send(addr, 0x40, 0xff);
i2c_send(addr, 0xA0, 0xff);
// Set actual wiper value
byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
i2c_send( addr, addresses[channel], current_to_wiper(current) );
i2c_send(addr, addresses[channel], current_to_wiper(current));
}
void digipot_i2c_init()
{
void digipot_i2c_init() {
const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
Wire.begin();
// setup initial currents as defined in Configuration_adv.h
for(int i=0;i<=sizeof(digipot_motor_current)/sizeof(float);i++) {
for(int i = 0; i <= sizeof(digipot_motor_current) / sizeof(float); i++) {
digipot_i2c_set_current(i, digipot_motor_current[i]);
}
}
#endif
#endif //DIGIPOT_I2C
......@@ -14,24 +14,20 @@
#ifndef DOGM_LCD_IMPLEMENTATION_H
#define DOGM_LCD_IMPLEMENTATION_H
#define MARLIN_VERSION "4.0.2"
#define MARLIN_VERSION " 4.0.3"
/**
* Implementation of the LCD display routines for a DOGM128 graphic display. These are common LCD 128x64 pixel graphic displays.
**/
#ifdef ULTIPANEL
#define BLEN_A 0
#define BLEN_B 1
#define BLEN_C 2
#define EN_A (1<<BLEN_A)
#define EN_B (1<<BLEN_B)
#define EN_C (1<<BLEN_C)
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#define LCD_CLICKED (buttons&EN_C)
#define BLEN_A 0
#define BLEN_B 1
#define BLEN_C 2
#define EN_A (1<<BLEN_A)
#define EN_B (1<<BLEN_B)
#define EN_C (1<<BLEN_C)
#define LCD_CLICKED (buttons&EN_C)
#endif
#include <U8glib.h>
......
......@@ -83,6 +83,9 @@
/// check if pin is an timer wrapper
#define GET_TIMER(IO) _GET_TIMER(IO)
/// Shorthand
#define OUT_WRITE(IO, v) { SET_OUTPUT(IO); WRITE(IO, v); }
/*
ports and functions
......
#ifndef LANGUAGE_H
#define LANGUAGE_H
#ifndef __LANGUAGE_H
#define __LANGUAGE_H
// NOTE: IF YOU CHANGE LANGUAGE FILES OR MERGE A FILE WITH CHANGES
//
......@@ -149,6 +149,7 @@
#define MSG_ERR_EEPROM_WRITE "Error writing to EEPROM!"
// temperature.cpp strings
#define MSG_PID_AUTOTUNE "PID Autotune"
#define MSG_PID_AUTOTUNE_START MSG_PID_AUTOTUNE " start"
#define MSG_PID_AUTOTUNE_FAILED MSG_PID_AUTOTUNE " failed!"
......@@ -185,6 +186,7 @@
#define MSG_MAXTEMP_EXTRUDER_OFF ": Extruder" MSG_SWITCHED_OFF_MAX
#define MSG_MAXTEMP_BED_OFF "Heated bed" MSG_SWITCHED_OFF_MAX
// LCD Menu Messages
// Add your own character. Reference: https://www.sparkfun.com/datasheets/LCD/HD44780.pdf page 17-18
#ifdef DOGLCD
#define STR_Ae "\304" // 'Ä' U8glib
......
......@@ -473,12 +473,6 @@
//Cheaptronic v1.0 does not use this port
#define SDCARDDETECT -1
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif // CHEAPTRONIC
/****************************************************************************************/
......@@ -643,13 +637,6 @@
#define BLEN_C 2
#define BLEN_B 1
#define BLEN_A 0
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif //RA_CONTROL_PANEL
#ifdef RA_DISCO
......@@ -1743,11 +1730,6 @@
#define SDCARDDETECT 81 // Ramps does not use this port
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#else //old style panel with shift register
//arduino pin witch triggers an piezzo beeper
#define BEEPER 33 No Beeper added
......@@ -1765,12 +1747,6 @@
#define LCD_PINS_D6 27
#define LCD_PINS_D7 29
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
//bits in the shift register that carry the buttons for:
// left up center down right red
#define BL_LE 7
......@@ -2715,13 +2691,8 @@
#define BLEN_A 0
#define SDCARDDETECT -1 // Ramps does not use this port
#endif //NEWPANEL
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif
#endif //ULTRA_LCD
#endif // MEGATRONICS
......@@ -2836,12 +2807,6 @@
#define SDCARDDETECT -1 // Megatronics does not use this port
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif // MEGATRONICS_2
/****************************************************************************************/
......@@ -2939,12 +2904,6 @@
#define SDCARDDETECT -1 // Megatronics does not use this port
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif // MEGATRONICS_1
/****************************************************************************************/
......@@ -3070,12 +3029,6 @@
#define SDCARDDETECT -1 // Megatronics does not use this port
//encoder rotation values
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif // MEGATRONICS_3
/****************************************************************************************/
......@@ -4424,6 +4377,21 @@ DaveX plan for Teensylu/printrboard-type pinouts (ref teensylu & sprinter) for a
************************************* FEATURE *******************************************
/****************************************************************************************/
#ifdef SINGLENOZZLE
#undef HEATER_1_PIN
#undef HEATER_2_PIN
#undef HEATER_3_PIN
#define HEATER_1_PIN -1
#define HEATER_2_PIN -1
#define HEATER_3_PIN -1
#undef TEMP_1_PIN
#undef TEMP_2_PIN
#undef TEMP_3_PIN
#define TEMP_1_PIN -1
#define TEMP_2_PIN -1
#define TEMP_3_PIN -1
#endif //SINGLENOZZLE
#ifdef MKR4
#if (EXTRUDERS == 2) && (DRIVER_EXTRUDERS==1) // Use this for one driver and two extruder
#define E0E1_CHOICE_PIN 5
......@@ -4488,21 +4456,27 @@ DaveX plan for Teensylu/printrboard-type pinouts (ref teensylu & sprinter) for a
#endif
#if X_HOME_DIR > 0 //Home X to MAX
#undef X_MIN_PIN
#define X_MIN_PIN -1
#elif X_HOME_DIR < 0 //Home X to MIN
#undef X_MAX_PIN
#define X_MAX_PIN -1
#endif //X_HOME_DIR > 0
#if Y_HOME_DIR > 0 //Home Y to MAX
#undef Y_MIN_PIN
#define Y_MIN_PIN -1
#elif Y_HOME_DIR < 0 //Home Y to MIN
#undef Y_MAX_PIN
#define Y_MAX_PIN -1
#endif //Y_HOME_DIR > 0
#ifndef DELTA
#if Z_HOME_DIR > 0 //Home Z to MAX
#undef Z_MIN_PIN
#define Z_MIN_PIN -1
#elif Z_HOME_DIR < 0 //Home Z to MIN
#undef Z_MAX_PIN
#define Z_MAX_PIN -1
#endif //Z_HOME_DIR > 0
#endif //!DELTA
......
......@@ -2,5 +2,42 @@
//==================== Change PIN width Configurator Tool ====================
//============================================================================
//X axis pins
#define ORIG_X_STEP_PIN X_STEP_PIN
#define ORIG_X_DIR_PIN X_DIR_PIN
#define ORIG_X_ENABLE_PIN X_ENABLE_PIN
//Y axis pins
#define ORIG_Y_STEP_PIN Y_STEP_PIN
#define ORIG_Y_DIR_PIN Y_DIR_PIN
#define ORIG_Y_ENABLE_PIN Y_ENABLE_PIN
//Z axis pins
#define ORIG_Z_STEP_PIN Z_STEP_PIN
#define ORIG_Z_DIR_PIN Z_DIR_PIN
#define ORIG_Z_ENABLE_PIN Z_ENABLE_PIN
//E axis pins
#define ORIG_E0_STEP_PIN E0_STEP_PIN
#define ORIG_E0_DIR_PIN E0_DIR_PIN
#define ORIG_E0_ENABLE_PIN E0_ENABLE_PIN
#define ORIG_E1_STEP_PIN E1_STEP_PIN
#define ORIG_E1_DIR_PIN E1_DIR_PIN
#define ORIG_E1_ENABLE_PIN E1_ENABLE_PIN
#define ORIG_E2_STEP_PIN E2_STEP_PIN
#define ORIG_E2_DIR_PIN E2_DIR_PIN
#define ORIG_E2_ENABLE_PIN E2_ENABLE_PIN
#define ORIG_E3_STEP_PIN E3_STEP_PIN
#define ORIG_E3_DIR_PIN E3_DIR_PIN
#define ORIG_E3_ENABLE_PIN E3_ENABLE_PIN
//FAN pin
#define ORIG_FAN_PIN FAN_PIN
//============================================================================
//============================================================================
\ No newline at end of file
......@@ -669,7 +669,15 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
{
block->direction_bits |= (1<<Y_AXIS);
}
#else
#else //COREXY
if (target[X_AXIS] < position[X_AXIS])
{
block->direction_bits |= (1<<X_HEAD);
}
if (target[Y_AXIS] < position[Y_AXIS])
{
block->direction_bits |= (1<<Y_HEAD);
}
if ((target[X_AXIS]-position[X_AXIS]) + (target[Y_AXIS]-position[Y_AXIS]) < 0)
{
block->direction_bits |= (1<<X_AXIS);
......@@ -678,7 +686,7 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
{
block->direction_bits |= (1<<Y_AXIS);
}
#endif
#endif //COREXY
if (target[Z_AXIS] < position[Z_AXIS])
{
block->direction_bits |= (1<<Z_AXIS);
......@@ -692,15 +700,14 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
//enable active axes
#ifdef COREXY
if((block->steps_x != 0) || (block->steps_y != 0))
{
if((block->steps_x != 0) || (block->steps_y != 0)) {
enable_x();
enable_y();
}
#else
#else //NO COREXY
if(block->steps_x != 0) enable_x();
if(block->steps_y != 0) enable_y();
#endif // COREXY
#endif //NOCOREXY
#ifndef Z_LATE_ENABLE
if(block->steps_z != 0) enable_z();
#endif
......@@ -770,14 +777,24 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
if(feed_rate<minimumfeedrate) feed_rate=minimumfeedrate;
}
float delta_mm[4];
/* This part of the code calculates the total length of the movement.
For cartesian bots, the X_AXIS is the real X movement and same for Y_AXIS.
But for corexy bots, that is not true. The "X_AXIS" and "Y_AXIS" motors (that should be named to A_AXIS
and B_AXIS) cannot be used for X and Y length, because A=X+Y and B=X-Y.
So we need to create other 2 "AXIS", named X_HEAD and Y_HEAD, meaning the real displacement of the Head.
Having the real displacement of the head, we can calculate the total movement length and apply the desired speed.
*/
#ifndef COREXY
float delta_mm[4];
delta_mm[X_AXIS] = (target[X_AXIS]-position[X_AXIS])/axis_steps_per_unit[X_AXIS];
delta_mm[Y_AXIS] = (target[Y_AXIS]-position[Y_AXIS])/axis_steps_per_unit[Y_AXIS];
#else
float delta_mm[6];
delta_mm[X_HEAD] = (target[X_AXIS]-position[X_AXIS])/axis_steps_per_unit[X_AXIS];
delta_mm[Y_HEAD] = (target[Y_AXIS]-position[Y_AXIS])/axis_steps_per_unit[Y_AXIS];
delta_mm[X_AXIS] = ((target[X_AXIS]-position[X_AXIS]) + (target[Y_AXIS]-position[Y_AXIS]))/axis_steps_per_unit[X_AXIS];
delta_mm[Y_AXIS] = ((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-position[Y_AXIS]))/axis_steps_per_unit[Y_AXIS];
#endif // COREXY
#endif
delta_mm[Z_AXIS] = (target[Z_AXIS]-position[Z_AXIS])/axis_steps_per_unit[Z_AXIS];
delta_mm[E_AXIS] = ((target[E_AXIS]-position[E_AXIS])/axis_steps_per_unit[active_extruder+3])*volumetric_multiplier[active_extruder]*extruder_multiplier[active_extruder]/100.0;
if ( block->steps_x <=dropsegments && block->steps_y <=dropsegments && block->steps_z <=dropsegments )
......@@ -786,7 +803,11 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
}
else
{
#ifndef COREXY
block->millimeters = sqrt(square(delta_mm[X_AXIS]) + square(delta_mm[Y_AXIS]) + square(delta_mm[Z_AXIS]));
#else
block->millimeters = sqrt(square(delta_mm[X_HEAD]) + square(delta_mm[Y_HEAD]) + square(delta_mm[Z_AXIS]));
#endif
}
float inverse_millimeters = 1.0/block->millimeters; // Inverse millimeters to remove multiple divides
......@@ -809,9 +830,9 @@ block->steps_y = labs((target[X_AXIS]-position[X_AXIS]) - (target[Y_AXIS]-positi
if (segment_time < minsegmenttime)
{ // buffer is draining, add extra time. The amount of time added increases if the buffer is still emptied more.
inverse_second=1000000.0/(segment_time+lround(2*(minsegmenttime-segment_time)/moves_queued));
#ifdef XY_FREQUENCY_LIMIT
#ifdef XY_FREQUENCY_LIMIT
segment_time = lround(1000000.0/inverse_second);
#endif // XY_FREQUENCY_LIMIT
#endif
}
}
#endif // SLOWDOWN
......@@ -1136,17 +1157,12 @@ uint8_t movesplanned()
}
#ifdef PREVENT_DANGEROUS_EXTRUDE
void set_extrude_min_temp(float temp)
{
extrude_min_temp=temp;
}
void set_extrude_min_temp(float temp) { extrude_min_temp = temp; }
#endif
// Calculate the steps/s^2 acceleration rates, based on the mm/s^s
void reset_acceleration_rates()
{
for(int8_t i=0; i < 3 + EXTRUDERS; i++)
{
void reset_acceleration_rates() {
for(int8_t i=0; i < 3 + EXTRUDERS; i++) {
axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
}
}
......@@ -41,12 +41,12 @@ typedef struct {
long acceleration_rate; // The acceleration rate used for acceleration calculation
unsigned char direction_bits; // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
unsigned char active_driver; // Selects the active driver
#ifdef ADVANCE
#ifdef ADVANCE
long advance_rate;
volatile long initial_advance;
volatile long final_advance;
float advance;
#endif
#endif
// Fields used by the motion planner to manage acceleration
// float speed_x, speed_y, speed_z, speed_e; // Nominal mm/sec for each axis
......@@ -64,13 +64,13 @@ typedef struct {
unsigned long final_rate; // The minimal rate at exit
unsigned long acceleration_st; // acceleration steps/sec^2
unsigned long fan_speed;
#ifdef BARICUDA
#ifdef BARICUDA
unsigned long valve_pressure;
unsigned long e_to_p_pressure;
#endif
#ifdef LASERBEAM
#endif
#ifdef LASERBEAM
unsigned long laser_ttlmodulation;
#endif
#endif
volatile char busy;
} block_t;
......
......@@ -245,7 +245,7 @@ void checkHitEndstops()
SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
}
SERIAL_ECHOLN("");
SERIAL_EOL;
endstop_x_hit=false;
endstop_y_hit=false;
endstop_z_hit=false;
......@@ -403,15 +403,14 @@ ISR(TIMER1_COMPA_vect)
// Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
out_bits = current_block->direction_bits;
// Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
if((out_bits & (1<<X_AXIS))!=0){
if((out_bits & (1<<X_AXIS))!=0) {
#ifdef DUAL_X_CARRIAGE
if (extruder_duplication_enabled){
if (extruder_duplication_enabled) {
WRITE(X_DIR_PIN, INVERT_X_DIR);
WRITE(X2_DIR_PIN, INVERT_X_DIR);
}
else{
else {
if (current_block->active_driver != 0)
WRITE(X2_DIR_PIN, INVERT_X_DIR);
else
......@@ -422,13 +421,13 @@ ISR(TIMER1_COMPA_vect)
#endif
count_direction[X_AXIS]=-1;
}
else{
else {
#ifdef DUAL_X_CARRIAGE
if (extruder_duplication_enabled){
if (extruder_duplication_enabled) {
WRITE(X_DIR_PIN, !INVERT_X_DIR);
WRITE(X2_DIR_PIN, !INVERT_X_DIR);
}
else{
else {
if (current_block->active_driver != 0)
WRITE(X2_DIR_PIN, !INVERT_X_DIR);
else
......@@ -439,42 +438,37 @@ ISR(TIMER1_COMPA_vect)
#endif
count_direction[X_AXIS]=1;
}
if((out_bits & (1<<Y_AXIS))!=0){
if((out_bits & (1<<Y_AXIS))!=0) {
WRITE(Y_DIR_PIN, INVERT_Y_DIR);
#ifdef Y_DUAL_STEPPER_DRIVERS
WRITE(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
#endif
count_direction[Y_AXIS]=-1;
}
else{
else {
WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
#ifdef Y_DUAL_STEPPER_DRIVERS
WRITE(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
#endif
count_direction[Y_AXIS]=1;
}
// Set direction en check limit switches
CHECK_ENDSTOPS { // check X and Y Endstops
#ifndef COREXY
if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
if ((out_bits & (1<<X_AXIS)) != 0) // stepping along -X axis (regular cartesians bot)
#else
if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
if (!((current_block->steps_x == current_block->steps_y) && ((out_bits & (1<<X_AXIS))>>X_AXIS != (out_bits & (1<<Y_AXIS))>>Y_AXIS))) // AlexBorro: If DeltaX == -DeltaY, the movement is only in Y axis
if ((out_bits & (1<<X_HEAD)) != 0)
#endif
CHECK_ENDSTOPS
{
{ // -direction
#ifdef DUAL_X_CARRIAGE
// with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
if ((current_block->active_driver == 0 && X_HOME_DIR == -1)
|| (current_block->active_driver != 0 && X2_HOME_DIR == -1))
if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
#endif
{
#if defined(X_MIN_PIN) && X_MIN_PIN > -1
bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
if (x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
endstop_x_hit=true;
step_events_completed = current_block->step_event_count;
......@@ -483,19 +477,15 @@ ISR(TIMER1_COMPA_vect)
#endif
}
}
}
else { // +direction
CHECK_ENDSTOPS
{
#ifdef DUAL_X_CARRIAGE
// with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
if ((current_block->active_driver == 0 && X_HOME_DIR == 1)
|| (current_block->active_driver != 0 && X2_HOME_DIR == 1))
if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
#endif
{
#if defined(X_MAX_PIN) && X_MAX_PIN > -1
bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)) {
endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
endstop_x_hit=true;
step_events_completed = current_block->step_event_count;
......@@ -504,15 +494,14 @@ ISR(TIMER1_COMPA_vect)
#endif
}
}
}
#ifndef COREXY
if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
if ((out_bits & (1<<Y_AXIS)) != 0) // -direction
#else
if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
if (!((current_block->steps_x == current_block->steps_y) && ((out_bits & (1<<X_AXIS))>>X_AXIS == (out_bits & (1<<Y_AXIS))>>Y_AXIS))) // AlexBorro: If DeltaX == DeltaY, the movement is only in X axis
if ((out_bits & (1<<Y_HEAD)) != 0)
#endif
CHECK_ENDSTOPS
{
{ // -direction
#if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
......@@ -523,13 +512,10 @@ ISR(TIMER1_COMPA_vect)
old_y_min_endstop = y_min_endstop;
#endif
}
}
else { // +direction
CHECK_ENDSTOPS
{
#if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)) {
endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
endstop_y_hit=true;
step_events_completed = current_block->step_event_count;
......@@ -541,14 +527,12 @@ ISR(TIMER1_COMPA_vect)
if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
WRITE(Z_DIR_PIN,INVERT_Z_DIR);
#ifdef Z_DUAL_STEPPER_DRIVERS
WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
#endif
count_direction[Z_AXIS]=-1;
CHECK_ENDSTOPS
{
CHECK_ENDSTOPS {
#if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
......@@ -562,14 +546,12 @@ ISR(TIMER1_COMPA_vect)
}
else { // +direction
WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
#ifdef Z_DUAL_STEPPER_DRIVERS
WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
#endif
count_direction[Z_AXIS]=1;
CHECK_ENDSTOPS
{
CHECK_ENDSTOPS {
#if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
bool z_max_endstop=(READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
......@@ -587,11 +569,10 @@ ISR(TIMER1_COMPA_vect)
REV_E_DIR();
count_direction[E_AXIS]=-1;
#ifdef NPR2
CHECK_ENDSTOPS
{
CHECK_ENDSTOPS {
#if defined(E_MIN_PIN) && E_MIN_PIN > -1
bool e_min_endstop=(READ(E_MIN_PIN) != E_MIN_ENDSTOP_INVERTING);
if(e_min_endstop && old_e_min_endstop && (current_block->steps_e > 0)) {
if (e_min_endstop && old_e_min_endstop && (current_block->steps_e > 0)) {
endstops_trigsteps[E_AXIS] = count_position[E_AXIS];
endstop_e_hit=true;
step_events_completed = current_block->step_event_count;
......@@ -607,8 +588,6 @@ ISR(TIMER1_COMPA_vect)
}
#endif //!ADVANCE
for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
#ifndef AT90USB
MSerial.checkRx(); // Check for serial chars.
......@@ -628,7 +607,7 @@ ISR(TIMER1_COMPA_vect)
#endif //ADVANCE
counter_x += current_block->steps_x;
#ifdef CONFIG_STEPPERS_TOSHIBA
#ifdef CONFIG_STEPPERS_TOSHIBA
/* The toshiba stepper controller require much longer pulses
* tjerfore we 'stage' decompose the pulses between high, and
* low instead of doing each in turn. The extra tests add enough
......@@ -679,7 +658,7 @@ ISR(TIMER1_COMPA_vect)
WRITE_E_STEP(LOW);
}
#endif //!ADVANCE
#else
#else
if (counter_x > 0) {
#ifdef DUAL_X_CARRIAGE
if (extruder_duplication_enabled){
......@@ -698,7 +677,7 @@ ISR(TIMER1_COMPA_vect)
counter_x -= current_block->step_event_count;
count_position[X_AXIS]+=count_direction[X_AXIS];
#ifdef DUAL_X_CARRIAGE
if (extruder_duplication_enabled){
if (extruder_duplication_enabled) {
WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
WRITE(X2_STEP_PIN, INVERT_X_STEP_PIN);
}
......@@ -716,15 +695,12 @@ ISR(TIMER1_COMPA_vect)
counter_y += current_block->steps_y;
if (counter_y > 0) {
WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
#ifdef Y_DUAL_STEPPER_DRIVERS
WRITE(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
#endif
counter_y -= current_block->step_event_count;
count_position[Y_AXIS]+=count_direction[Y_AXIS];
WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
#ifdef Y_DUAL_STEPPER_DRIVERS
WRITE(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
#endif
......@@ -733,15 +709,12 @@ ISR(TIMER1_COMPA_vect)
counter_z += current_block->steps_z;
if (counter_z > 0) {
WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
#ifdef Z_DUAL_STEPPER_DRIVERS
WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
#endif
counter_z -= current_block->step_event_count;
count_position[Z_AXIS]+=count_direction[Z_AXIS];
WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
#ifdef Z_DUAL_STEPPER_DRIVERS
WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
#endif
......@@ -756,7 +729,7 @@ ISR(TIMER1_COMPA_vect)
WRITE_E_STEP(INVERT_E_STEP_PIN);
}
#endif //!ADVANCE
#endif // CONFIG_STEPPERS_TOSHIBA
#endif // CONFIG_STEPPERS_TOSHIBA
step_events_completed += 1;
if(step_events_completed >= current_block->step_event_count) break;
}
......@@ -784,7 +757,6 @@ ISR(TIMER1_COMPA_vect)
// Do E steps + advance steps
e_steps[current_block->active_driver] += ((advance >>8) - old_advance);
old_advance = advance >>8;
#endif
}
else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
......@@ -988,16 +960,13 @@ void st_init()
//Choice E0-E1 or E0-E2 or E1-E3 pin
#if defined(E0E1_CHOICE_PIN) && (E0E1_CHOICE_PIN > -1)
SET_OUTPUT(E0E1_CHOICE_PIN);
WRITE(E0E1_CHOICE_PIN,LOW);
OUT_WRITE(E0E1_CHOICE_PIN,LOW);
#endif
#if defined(E0E2_CHOICE_PIN) && (E0E2_CHOICE_PIN > -1)
SET_OUTPUT(E0E2_CHOICE_PIN);
WRITE(E0E2_CHOICE_PIN,LOW);
OUT_WRITE(E0E2_CHOICE_PIN,LOW);
#endif
#if defined(E1E3_CHOICE_PIN) && (E1E3_CHOICE_PIN > -1)
SET_OUTPUT(E1E3_CHOICE_PIN);
WRITE(E1E3_CHOICE_PIN,LOW);
OUT_WRITE(E1E3_CHOICE_PIN,LOW);
#endif
//endstops and pullups
......@@ -1054,51 +1023,41 @@ void st_init()
//Initialize Step Pins
#if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
SET_OUTPUT(X_STEP_PIN);
WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
OUT_WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
disable_x();
#endif
#if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
SET_OUTPUT(X2_STEP_PIN);
WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
OUT_WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
disable_x();
#endif
#if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
SET_OUTPUT(Y_STEP_PIN);
WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
OUT_WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
#if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
SET_OUTPUT(Y2_STEP_PIN);
WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
OUT_WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
#endif
disable_y();
#endif
#if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
SET_OUTPUT(Z_STEP_PIN);
WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
OUT_WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
#if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
SET_OUTPUT(Z2_STEP_PIN);
WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
OUT_WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
#endif
disable_z();
#endif
#if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
SET_OUTPUT(E0_STEP_PIN);
WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
OUT_WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
disable_e0();
#endif
#if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
SET_OUTPUT(E1_STEP_PIN);
WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
OUT_WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
disable_e1();
#endif
#if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
SET_OUTPUT(E2_STEP_PIN);
WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
OUT_WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
disable_e2();
#endif
#if defined(E3_STEP_PIN) && (E3_STEP_PIN > -1)
SET_OUTPUT(E3_STEP_PIN);
WRITE(E3_STEP_PIN,INVERT_E_STEP_PIN);
OUT_WRITE(E3_STEP_PIN,INVERT_E_STEP_PIN);
disable_e3();
#endif
......
......@@ -38,6 +38,43 @@
#include "Sd2PinMap.h"
//===========================================================================
//================================== macros =================================
//===========================================================================
#ifndef SINGLENOZZLE
#if EXTRUDERS > 4
#error Unsupported number of extruders
#elif EXTRUDERS > 3
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
#elif EXTRUDERS > 2
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3 }
#elif EXTRUDERS > 1
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2 }
#else
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
#endif
#else
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
#endif //SINGLENOZZLE
#define HAS_TEMP_0 (defined(TEMP_0_PIN) && TEMP_0_PIN >= 0)
#define HAS_TEMP_1 (defined(TEMP_1_PIN) && TEMP_1_PIN >= 0)
#define HAS_TEMP_2 (defined(TEMP_2_PIN) && TEMP_2_PIN >= 0)
#define HAS_TEMP_3 (defined(TEMP_3_PIN) && TEMP_3_PIN >= 0)
#define HAS_TEMP_BED (defined(TEMP_BED_PIN) && TEMP_BED_PIN >= 0)
#define HAS_FILAMENT_SENSOR (defined(FILAMENT_SENSOR) && defined(FILWIDTH_PIN) && FILWIDTH_PIN >= 0)
#define HAS_HEATER_0 (defined(HEATER_0_PIN) && HEATER_0_PIN >= 0)
#define HAS_HEATER_1 (defined(HEATER_1_PIN) && HEATER_1_PIN >= 0)
#define HAS_HEATER_2 (defined(HEATER_2_PIN) && HEATER_2_PIN >= 0)
#define HAS_HEATER_3 (defined(HEATER_3_PIN) && HEATER_3_PIN >= 0)
#define HAS_HEATER_BED (defined(HEATER_BED_PIN) && HEATER_BED_PIN >= 0)
#define HAS_AUTO_FAN_0 (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN >= 0)
#define HAS_AUTO_FAN_1 (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN >= 0)
#define HAS_AUTO_FAN_2 (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN >= 0)
#define HAS_AUTO_FAN_3 (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN >= 0)
#define HAS_AUTO_FAN HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3
#define HAS_FAN (defined(FAN_PIN) && FAN_PIN >= 0)
//===========================================================================
//============================= public variables ============================
......@@ -155,22 +192,6 @@ static volatile bool temp_meas_ready = false;
static unsigned long extruder_autofan_last_check;
#endif
#ifndef SINGLENOZZLE
#if EXTRUDERS > 4
#error Unsupported number of extruders
#elif EXTRUDERS > 3
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
#elif EXTRUDERS > 2
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3 }
#elif EXTRUDERS > 1
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2 }
#else
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
#endif
#else
#define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
#endif
// Init min and max temp with extreme values to prevent false errors during startup
#ifndef SINGLENOZZLE
static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
......@@ -187,7 +208,7 @@ static volatile bool temp_meas_ready = false;
#endif
#ifdef BED_MAXTEMP
static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
#endif
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
......@@ -230,33 +251,26 @@ static void updateTemperaturesFromRawValues();
void PID_autotune(float temp, int extruder, int ncycles) {
float input = 0.0;
int cycles=0;
int cycles = 0;
bool heating = true;
unsigned long temp_millis = millis();
unsigned long t1 = temp_millis;
unsigned long t2 = temp_millis;
long t_high = 0;
long t_low = 0;
unsigned long temp_millis = millis(), t1 = temp_millis, t2 = temp_millis;
long t_high = 0, t_low = 0;
long bias, d;
float Ku, Tu;
float Kp, Ki, Kd;
float max = 0, min = 10000;
#if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
unsigned long extruder_autofan_last_check = millis();
#if HAS_AUTO_FAN
unsigned long extruder_autofan_last_check = temp_millis;
#endif
#if (TEMP_BED_PIN <= -1)
if ((extruder >= EXTRUDERS) || (extruder < 0))
#else
if (extruder >= EXTRUDERS)
if (extruder >= EXTRUDERS
#if !HAS_TEMP_BED
|| extruder < 0
#endif
{
) {
SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
return;
}
......@@ -266,71 +280,65 @@ void PID_autotune(float temp, int extruder, int ncycles) {
disable_heater(); // switch off all heaters.
if (extruder < 0)
{
soft_pwm_bed = (MAX_BED_POWER)/2;
bias = d = (MAX_BED_POWER)/2;
}
soft_pwm_bed = bias = d = MAX_BED_POWER / 2;
else
{
soft_pwm[extruder] = (PID_MAX)/2;
bias = d = (PID_MAX)/2;
}
soft_pwm[extruder] = bias = d = PID_MAX / 2;
// PID Tuning loop
for(;;) {
if(temp_meas_ready == true) { // temp sample ready
unsigned long ms = millis();
if (temp_meas_ready == true) { // temp sample ready
updateTemperaturesFromRawValues();
input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
max = max(max,input);
min = min(min,input);
max = max(max, input);
min = min(min, input);
#if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
if(millis() - extruder_autofan_last_check > 2500) {
#if HAS_AUTO_FAN
if (ms > extruder_autofan_last_check + 2500) {
checkExtruderAutoFans();
extruder_autofan_last_check = millis();
extruder_autofan_last_check = ms;
}
#endif
if(heating == true && input > temp) {
if(millis() - t2 > 5000) {
heating=false;
if (extruder<0)
if (heating == true && input > temp) {
if (ms - t2 > 5000) {
heating = false;
if (extruder < 0)
soft_pwm_bed = (bias - d) >> 1;
else
soft_pwm[extruder] = (bias - d) >> 1;
t1=millis();
t_high=t1 - t2;
max=temp;
}
}
if(heating == false && input < temp) {
if(millis() - t1 > 5000) {
heating=true;
t2=millis();
t_low=t2 - t1;
if(cycles > 0) {
t1 = ms;
t_high = t1 - t2;
max = temp;
}
}
if (heating == false && input < temp) {
if (ms - t1 > 5000) {
heating = true;
t2 = ms;
t_low = t2 - t1;
if (cycles > 0) {
long max_pow = extruder < 0 ? MAX_BED_POWER : PID_MAX;
bias += (d*(t_high - t_low))/(t_low + t_high);
bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
else d = bias;
bias = constrain(bias, 20, max_pow - 20);
d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
SERIAL_PROTOCOLPGM(MSG_MIN); SERIAL_PROTOCOL(min);
SERIAL_PROTOCOLPGM(MSG_MAX); SERIAL_PROTOCOLLN(max);
if(cycles > 2) {
Ku = (4.0*d)/(3.14159*(max-min)/2.0);
Tu = ((float)(t_low + t_high)/1000.0);
if (cycles > 2) {
Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
Tu = ((float)(t_low + t_high) / 1000.0);
SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
Kp = 0.6*Ku;
Ki = 2*Kp/Tu;
Kd = Kp*Tu/8;
Kp = 0.6 * Ku;
Ki = 2 * Kp / Tu;
Kd = Kp * Tu / 8;
SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(Kp);
SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(Ki);
......@@ -353,7 +361,7 @@ void PID_autotune(float temp, int extruder, int ncycles) {
*/
}
}
if (extruder<0)
if (extruder < 0)
soft_pwm_bed = (bias + d) >> 1;
else
soft_pwm[extruder] = (bias + d) >> 1;
......@@ -362,18 +370,19 @@ void PID_autotune(float temp, int extruder, int ncycles) {
}
}
}
if(input > (temp + 20)) {
if (input > temp + 20) {
SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
return;
}
if(millis() - temp_millis > 2000) {
// Every 2 seconds...
if (ms > temp_millis + 2000) {
int p;
if (extruder<0) {
p=soft_pwm_bed;
if (extruder < 0) {
p = soft_pwm_bed;
SERIAL_PROTOCOLPGM(MSG_OK_B);
}
else {
p=soft_pwm[extruder];
p = soft_pwm[extruder];
SERIAL_PROTOCOLPGM(MSG_OK_T);
}
......@@ -381,13 +390,14 @@ void PID_autotune(float temp, int extruder, int ncycles) {
SERIAL_PROTOCOLPGM(MSG_AT);
SERIAL_PROTOCOLLN(p);
temp_millis = millis();
}
if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
temp_millis = ms;
} // every 2 seconds
// Over 2 minutes?
if (((ms - t1) + (ms - t2)) > (10L*60L*1000L*2L)) {
SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
return;
}
if(cycles > ncycles) {
if (cycles > ncycles) {
SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
return;
}
......@@ -411,17 +421,12 @@ void updatePID() {
}
int getHeaterPower(int heater) {
if (heater<0)
return soft_pwm_bed;
return soft_pwm[heater];
return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
}
#if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
#if HAS_AUTO_FAN
#if defined(FAN_PIN) && FAN_PIN > -1
#if HAS_FAN
#if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
#error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
#endif
......@@ -448,13 +453,13 @@ int getHeaterPower(int heater) {
uint8_t fanState = 0;
// which fan pins need to be turned on?
#if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_0
if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
fanState |= 1;
#endif
#ifndef SINGLENOZZLE
#if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_1
if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1;
......@@ -462,7 +467,7 @@ int getHeaterPower(int heater) {
fanState |= 2;
}
#endif
#if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_2
if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1;
......@@ -472,7 +477,7 @@ int getHeaterPower(int heater) {
fanState |= 4;
}
#endif
#if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_3
if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE) {
if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
fanState |= 1;
......@@ -487,21 +492,21 @@ int getHeaterPower(int heater) {
#endif // !SINLGENOZZE
// update extruder auto fan states
#if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_0
setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
#endif
#ifndef SINGLENOZZLE
#if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_1
if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
#endif
#if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_2
if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
&& EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
#endif
#if defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1
#if HAS_AUTO_FAN_3
if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
&& EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
&& EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
......@@ -511,24 +516,64 @@ int getHeaterPower(int heater) {
}
#endif // any extruder auto fan pins set
//
// Error checking and Write Routines
//
#if EXTRUDERS > 0
#if !HAS_HEATER_0
#error HEATER_0_PIN not defined for this board
#endif
#define WRITE_HEATER_0P(v) WRITE(HEATER_0_PIN, v)
#endif
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1 || defined(HEATERS_PARALLEL)
#if !HAS_HEATER_1
#error HEATER_1_PIN not defined for this board
#endif
#define WRITE_HEATER_1(v) WRITE(HEATER_1_PIN, v)
#if EXTRUDERS > 2
#if !HAS_HEATER_2
#error HEATER_2_PIN not defined for this board
#endif
#define WRITE_HEATER_2(v) WRITE(HEATER_2_PIN, v)
#if EXTRUDERS > 3
#if !HAS_HEATER_3
#error HEATER_3_PIN not defined for this board
#endif
#define WRITE_HEATER_3(v) WRITE(HEATER_3_PIN, v)
#endif
#endif
#endif
#endif //SINGLENOZZLE
#ifdef HEATERS_PARALLEL
#define WRITE_HEATER_0(v) { WRITE_HEATER_0P(v); WRITE_HEATER_1(v); }
#else
#define WRITE_HEATER_0(v) WRITE_HEATER_0P(v)
#endif
#if HAS_HEATER_BED
#define WRITE_HEATER_BED(v) WRITE(HEATER_BED_PIN, v)
#endif
#if HAS_FAN
#define WRITE_FAN(v) WRITE(FAN_PIN, v)
#endif
void manage_heater() {
static float pid_input;
static float pid_output;
if(temp_meas_ready != true) //better readability
return;
if (!temp_meas_ready) return;
float pid_input, pid_output;
updateTemperaturesFromRawValues();
#ifdef HEATER_0_USES_MAX6675
if (current_temperature[0] > 1023 || current_temperature[0] > HEATER_0_MAXTEMP) {
max_temp_error(0);
}
if (current_temperature[0] == 0 || current_temperature[0] < HEATER_0_MINTEMP) {
min_temp_error(0);
}
float ct = current_temperature[0];
if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
#endif //HEATER_0_USES_MAX6675
unsigned long ms = millis();
// Loop through all extruders
#ifndef SINGLENOZZLE
for(int e = 0; e < EXTRUDERS; e++)
#else
......@@ -545,16 +590,16 @@ void manage_heater() {
#ifndef PID_OPENLOOP
pid_error[e] = target_temperature[e] - pid_input;
if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
pid_output = BANG_MAX;
pid_reset[e] = true;
}
else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
pid_output = 0;
pid_reset[e] = true;
}
else {
if(pid_reset[e] == true) {
if (pid_reset[e] == true) {
temp_iState[e] = 0.0;
pid_reset[e] = false;
}
......@@ -565,15 +610,15 @@ void manage_heater() {
//K1 defined in Configuration.h in the PID settings
#define K2 (1.0-K1)
dTerm[e] = (Kd[e] * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
dTerm[e] = (Kd[e] * (pid_input - temp_dState[e])) * K2 + (K1 * dTerm[e]);
pid_output = pTerm[e] + iTerm[e] - dTerm[e];
if (pid_output > PID_MAX) {
if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
pid_output=PID_MAX;
if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
pid_output = PID_MAX;
}
else if (pid_output < 0) {
if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
pid_output=0;
if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
pid_output = 0;
}
}
temp_dState[e] = pid_input;
......@@ -596,7 +641,9 @@ void manage_heater() {
SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
SERIAL_ECHOLN(dTerm[e]);
#endif //PID_DEBUG
#else //NO PIDTEMP
#else /* PID off */
pid_output = 0;
if (current_temperature[e] < target_temperature[e]) pid_output = PID_MAX;
#endif //PIDTEMP
......@@ -605,8 +652,8 @@ void manage_heater() {
soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
#ifdef WATCH_TEMP_PERIOD
if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD) {
if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE) {
if (watchmillis[e] && ms > watchmillis[e] + WATCH_TEMP_PERIOD) {
if (degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE) {
setTargetHotend(0, e);
LCD_MESSAGEPGM(MSG_HEATING_FAILED_LCD);
SERIAL_ECHO_START;
......@@ -619,9 +666,9 @@ void manage_heater() {
#endif //WATCH_TEMP_PERIOD
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
disable_heater();
if(IsStopped() == false) {
if (IsStopped() == false) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_EXTRUDER_SWITCHED_OFF);
LCD_ALERTMESSAGEPGM(MSG_ERR_REDUNDANT_TEMP);
......@@ -633,29 +680,27 @@ void manage_heater() {
#endif //TEMP_SENSOR_1_AS_REDUNDANT
} //End extruder for loop
#if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1) || \
(defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN > -1)
if(millis() - extruder_autofan_last_check > 2500) { // only need to check fan state very infrequently
#if HAS_AUTO_FAN
if (ms > extruder_autofan_last_check + 2500) { // only need to check fan state very infrequently
checkExtruderAutoFans();
extruder_autofan_last_check = millis();
extruder_autofan_last_check = ms;
}
#endif
#ifndef PIDTEMPBED
if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
return;
previous_millis_bed_heater = millis();
#endif
if (ms < previous_millis_bed_heater + BED_CHECK_INTERVAL) return;
previous_millis_bed_heater = ms;
#endif //PIDTEMPBED
#if TEMP_SENSOR_BED != 0
#if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
#endif
#ifdef PIDTEMPBED
pid_input = current_temperature_bed;
#ifndef PID_OPENLOOP
pid_error_bed = target_temperature_bed - pid_input;
pTerm_bed = bedKp * pid_error_bed;
......@@ -665,17 +710,17 @@ void manage_heater() {
//K1 defined in Configuration.h in the PID settings
#define K2 (1.0-K1)
dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
dTerm_bed = (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
temp_dState_bed = pid_input;
pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
if (pid_output > MAX_BED_POWER) {
if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
pid_output=MAX_BED_POWER;
if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
pid_output = MAX_BED_POWER;
}
else if (pid_output < 0) {
if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
pid_output=0;
if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
pid_output = 0;
}
#else
......@@ -697,62 +742,44 @@ void manage_heater() {
SERIAL_ECHOLN(dTerm_bed);
#endif //PID_BED_DEBUG
if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP)) {
soft_pwm_bed = (int)pid_output >> 1;
}
else {
soft_pwm_bed = 0;
}
soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
#elif !defined(BED_LIMIT_SWITCHING)
// Check if temperature is within the correct range
if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP)) {
if(current_temperature_bed >= target_temperature_bed) {
soft_pwm_bed = 0;
}
else {
soft_pwm_bed = MAX_BED_POWER>>1;
}
if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
}
else {
soft_pwm_bed = 0;
WRITE(HEATER_BED_PIN,LOW);
WRITE_HEATER_BED(LOW);
}
#else //#ifdef BED_LIMIT_SWITCHING
// Check if temperature is within the correct band
if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP)) {
if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS) {
if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
soft_pwm_bed = 0;
}
else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS) {
soft_pwm_bed = MAX_BED_POWER>>1;
}
else if (current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
soft_pwm_bed = MAX_BED_POWER >> 1;
}
else {
soft_pwm_bed = 0;
WRITE(HEATER_BED_PIN,LOW);
WRITE_HEATER_BED(LOW);
}
#endif //BED_LIMIT_SWITCHING
#endif
#endif //TEMP_SENSOR_BED != 0
//code for controlling the extruder rate based on the width sensor
// Control the extruder rate based on the width sensor
#ifdef FILAMENT_SENSOR
if(filament_sensor) {
meas_shift_index=delay_index1-meas_delay_cm;
if(meas_shift_index<0)
meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
if (filament_sensor) {
meas_shift_index = delay_index1 - meas_delay_cm;
if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
//get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
//then square it to get an area
if(meas_shift_index<0)
meas_shift_index=0;
else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
meas_shift_index=MAX_MEASUREMENT_DELAY;
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
// Get the delayed info and add 100 to reconstitute to a percent of
// the nominal filament diameter then square it to get an area
meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
if (vm < 0.01) vm = 0.01;
volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
}
#endif //FILAMENT_SENSOR
}
......@@ -762,9 +789,9 @@ void manage_heater() {
// For hot end temperature measurement.
static float analog2temp(int raw, uint8_t e) {
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
if(e > EXTRUDERS)
if (e > EXTRUDERS)
#else
if(e >= EXTRUDERS)
if (e >= EXTRUDERS)
#endif
{
SERIAL_ERROR_START;
......@@ -837,9 +864,9 @@ static void updateTemperaturesFromRawValues() {
#endif
#ifndef SINGLENOZZLE
for(uint8_t e=0;e<EXTRUDERS;e++)
for(int e=0;e<EXTRUDERS;e++)
#else
uint8_t e=0;
int e=0;
#endif // !SINGLENOZZLE
{
current_temperature[e] = analog2temp(current_temperature_raw[e], e);
......@@ -848,7 +875,7 @@ static void updateTemperaturesFromRawValues() {
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
redundant_temperature = analog2temp(redundant_temperature_raw, 1);
#endif
#if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
#if HAS_FILAMENT_SENSOR
filament_width_meas = analog2widthFil();
#endif
//Reset the watchdog after we know we have a temperature measurement.
......@@ -859,23 +886,23 @@ static void updateTemperaturesFromRawValues() {
CRITICAL_SECTION_END;
}
// For converting raw Filament Width to milimeters
#ifdef FILAMENT_SENSOR
// Convert raw Filament Width to millimeters
float analog2widthFil() {
return current_raw_filwidth/16383.0*5.0;
return current_raw_filwidth / 16383.0 * 5.0;
//return current_raw_filwidth;
}
// For converting raw Filament Width to a ratio
// Convert raw Filament Width to a ratio
int widthFil_to_size_ratio() {
float temp;
temp=filament_width_meas;
if(filament_width_meas<MEASURED_LOWER_LIMIT)
temp=filament_width_nominal; //assume sensor cut out
else if (filament_width_meas>MEASURED_UPPER_LIMIT)
temp= MEASURED_UPPER_LIMIT;
return(filament_width_nominal/temp*100);
float temp = filament_width_meas;
if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
return filament_width_nominal / temp * 100;
}
#endif
void tp_init() {
......@@ -887,9 +914,9 @@ void tp_init() {
// Finish init of mult extruder arrays
#ifndef SINGLENOZZLE
for (uint8_t e = 0; e < EXTRUDERS; e++)
for (int e = 0; e < EXTRUDERS; e++)
#else
uint8_t e = 0;
int e = 0;
#endif // !SINGLENOZZLE
{
// populate with the first value
......@@ -904,27 +931,24 @@ void tp_init() {
#endif //PIDTEMPBED
}
#if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
#if HAS_HEATER_0
SET_OUTPUT(HEATER_0_PIN);
#endif
#ifndef SINGLENOZZLE
#if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
#if HAS_HEATER_1
SET_OUTPUT(HEATER_1_PIN);
#endif
#if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
#if HAS_HEATER_2
SET_OUTPUT(HEATER_2_PIN);
#endif
#if defined(HEATER_3_PIN) && (HEATER_3_PIN > -1)
#if HAS_HEATER_3
SET_OUTPUT(HEATER_3_PIN);
#endif
#endif // !SINGLENOZZLE
#if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
#if HAS_HEATER_BED
SET_OUTPUT(HEATER_BED_PIN);
#endif
#if defined(FAN_PIN) && (FAN_PIN > -1)
#if HAS_FAN
SET_OUTPUT(FAN_PIN);
#ifdef FAST_PWM_FAN
setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
......@@ -935,6 +959,7 @@ void tp_init() {
#endif
#ifdef HEATER_0_USES_MAX6675
#ifndef SDSUPPORT
SET_OUTPUT(SCK_PIN);
WRITE(SCK_PIN,0);
......@@ -953,60 +978,35 @@ void tp_init() {
WRITE(MAX6675_SS,1);
#endif //HEATER_0_USES_MAX6675
#ifdef DIDR2
#define ANALOG_SELECT(pin) do{ if (pin < 8) DIDR0 |= 1 << pin; else DIDR2 |= 1 << (pin - 8); }while(0)
#else
#define ANALOG_SELECT(pin) do{ DIDR0 |= 1 << pin; }while(0)
#endif
// Set analog inputs
ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
DIDR0 = 0;
#ifdef DIDR2
DIDR2 = 0;
#endif
#if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
#if TEMP_0_PIN < 8
DIDR0 |= 1 << TEMP_0_PIN;
#else
DIDR2 |= 1<<(TEMP_0_PIN - 8);
#endif
#endif
#ifndef SINGLENOZZLE
#if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
#if TEMP_1_PIN < 8
DIDR0 |= 1<<TEMP_1_PIN;
#else
DIDR2 |= 1<<(TEMP_1_PIN - 8);
#endif
#if HAS_TEMP_0
ANALOG_SELECT(TEMP_0_PIN);
#endif
#if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
#if TEMP_2_PIN < 8
DIDR0 |= 1 << TEMP_2_PIN;
#else
DIDR2 |= 1<<(TEMP_2_PIN - 8);
#endif
#endif
#if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
#if TEMP_3_PIN < 8
DIDR0 |= 1 << TEMP_3_PIN;
#else
DIDR2 |= 1<<(TEMP_3_PIN - 8);
#endif
#endif
#endif // !SINGLENOZZLE
#if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
#if TEMP_BED_PIN < 8
DIDR0 |= 1<<TEMP_BED_PIN;
#else
DIDR2 |= 1<<(TEMP_BED_PIN - 8);
#if HAS_TEMP_1
ANALOG_SELECT(TEMP_1_PIN);
#endif
#if HAS_TEMP_2
ANALOG_SELECT(TEMP_2_PIN);
#endif
//Added for Filament Sensor
#ifdef FILAMENT_SENSOR
#if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
#if FILWIDTH_PIN < 8
DIDR0 |= 1<<FILWIDTH_PIN;
#else
DIDR2 |= 1<<(FILWIDTH_PIN - 8);
#if HAS_TEMP_3
ANALOG_SELECT(TEMP_3_PIN);
#endif
#if HAS_TEMP_BED
ANALOG_SELECT(TEMP_BED_PIN);
#endif
#if HAS_FILAMENT_SENSOR
ANALOG_SELECT(FILWIDTH_PIN);
#endif
// Use timer0 for temperature measurement
......@@ -1017,130 +1017,97 @@ void tp_init() {
// Wait for temperature measurement to settle
delay(250);
#ifdef HEATER_0_MINTEMP
minttemp[0] = HEATER_0_MINTEMP;
while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
#if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
minttemp_raw[0] += OVERSAMPLENR;
#else
minttemp_raw[0] -= OVERSAMPLENR;
#endif
}
#endif //MINTEMP
#ifdef HEATER_0_MAXTEMP
maxttemp[0] = HEATER_0_MAXTEMP;
while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
#if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
maxttemp_raw[0] -= OVERSAMPLENR;
#else
maxttemp_raw[0] += OVERSAMPLENR;
#endif
#define TEMP_MIN_ROUTINE(NR) \
minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
minttemp_raw[NR] += OVERSAMPLENR; \
else \
minttemp_raw[NR] -= OVERSAMPLENR; \
}
#endif //MAXTEMP
#ifndef SINGLENOZZLE
#if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
minttemp[1] = HEATER_1_MINTEMP;
while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
#if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
minttemp_raw[1] += OVERSAMPLENR;
#else
minttemp_raw[1] -= OVERSAMPLENR;
#endif
#define TEMP_MAX_ROUTINE(NR) \
maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
maxttemp_raw[NR] -= OVERSAMPLENR; \
else \
maxttemp_raw[NR] += OVERSAMPLENR; \
}
#endif // MINTEMP 1
#if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
maxttemp[1] = HEATER_1_MAXTEMP;
while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
#if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
maxttemp_raw[1] -= OVERSAMPLENR;
#else
maxttemp_raw[1] += OVERSAMPLENR;
#endif
}
#endif //MAXTEMP 1
#if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
minttemp[2] = HEATER_2_MINTEMP;
while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
#if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
minttemp_raw[2] += OVERSAMPLENR;
#else
minttemp_raw[2] -= OVERSAMPLENR;
#endif
}
#endif //MINTEMP 2
#if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
maxttemp[2] = HEATER_2_MAXTEMP;
while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
#if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
maxttemp_raw[2] -= OVERSAMPLENR;
#else
maxttemp_raw[2] += OVERSAMPLENR;
#endif
}
#endif //MAXTEMP 2
#ifdef HEATER_0_MINTEMP
TEMP_MIN_ROUTINE(0);
#endif
#ifdef HEATER_0_MAXTEMP
TEMP_MAX_ROUTINE(0);
#endif
#if (EXTRUDERS > 3) && defined(HEATER_3_MINTEMP)
minttemp[3] = HEATER_3_MINTEMP;
while(analog2temp(minttemp_raw[3], 3) < HEATER_3_MINTEMP) {
#if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
minttemp_raw[3] += OVERSAMPLENR;
#else
minttemp_raw[3] -= OVERSAMPLENR;
#endif
}
#endif //MINTEMP 3
#if (EXTRUDERS > 3) && defined(HEATER_3_MAXTEMP)
maxttemp[3] = HEATER_3_MAXTEMP;
while(analog2temp(maxttemp_raw[3], 3) > HEATER_3_MAXTEMP) {
#if HEATER_3_RAW_LO_TEMP < HEATER_3_RAW_HI_TEMP
maxttemp_raw[3] -= OVERSAMPLENR;
#else
maxttemp_raw[3] += OVERSAMPLENR;
#endif
}
#endif // MAXTEMP 3
#endif // !SINGLENOZZLE
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
#ifdef HEATER_1_MINTEMP
TEMP_MIN_ROUTINE(1);
#endif
#ifdef HEATER_1_MAXTEMP
TEMP_MAX_ROUTINE(1);
#endif
#if EXTRUDERS > 2
#ifdef HEATER_2_MINTEMP
TEMP_MIN_ROUTINE(2);
#endif
#ifdef HEATER_2_MAXTEMP
TEMP_MAX_ROUTINE(2);
#endif
#if EXTRUDERS > 3
#ifdef HEATER_3_MINTEMP
TEMP_MIN_ROUTINE(3);
#endif
#ifdef HEATER_3_MAXTEMP
TEMP_MAX_ROUTINE(3);
#endif
#endif // EXTRUDERS > 3
#endif // EXTRUDERS > 2
#endif // EXTRUDERS > 1
#endif //SINGLENOZZLE
#ifdef BED_MINTEMP
#ifdef BED_MINTEMP
/* No bed MINTEMP error implemented?!? */ /*
while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
bed_minttemp_raw += OVERSAMPLENR;
#else
#else
bed_minttemp_raw -= OVERSAMPLENR;
#endif
#endif
}
*/
#endif //BED_MINTEMP
#ifdef BED_MAXTEMP
#endif //BED_MINTEMP
#ifdef BED_MAXTEMP
while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
#if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
bed_maxttemp_raw -= OVERSAMPLENR;
#else
#else
bed_maxttemp_raw += OVERSAMPLENR;
#endif
#endif
}
#endif //BED_MAXTEMP
#endif //BED_MAXTEMP
}
void setWatch() {
#ifdef WATCH_TEMP_PERIOD
unsigned long ms = millis();
#ifndef SINGLENOZZLE
for (uint8_t e = 0; e < EXTRUDERS; e++)
for (int e = 0; e < EXTRUDERS; e++)
#else
uint8_t e = 0;
int e = 0;
#endif // !SINGLENOZZLE
{
if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2)) {
if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2)) {
watch_start_temp[e] = degHotend(e);
watchmillis[e] = millis();
watchmillis[e] = ms;
}
}
#endif //WATCH_TEMP_PERIOD
}
#if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
#if defined(THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
/*
SERIAL_ECHO_START;
......@@ -1173,14 +1140,14 @@ void setWatch() {
case 2: // "Temperature Stable" state
if (temperature >= (target_temperature - hysteresis_degc))
{
*timer = millis();
*timer = ms;
}
else if ( (millis() - *timer) > ((unsigned long) period_seconds) * 1000)
else if ( (ms - *timer) > ((unsigned long) period_seconds) * 1000)
{
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
SERIAL_ERRORLN((int)heater_id);
LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
thermal_runaway = true;
while(1)
{
......@@ -1203,51 +1170,41 @@ void setWatch() {
void disable_heater() {
#ifndef SINGLENOZZLE
for(uint8_t i=0;i<EXTRUDERS;i++)
for (int e = 0; e < EXTRUDERS; e++)
#else
uint8_t i=0;
int e = 0;
#endif // !SINGLENOZZLE
setTargetHotend(0,i);
setTargetHotend(0, e);
setTargetBed(0);
#if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
#if HAS_TEMP_0
target_temperature[0] = 0;
soft_pwm[0] = 0;
#if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
WRITE(HEATER_0_PIN,LOW);
#endif
WRITE_HEATER_0P(LOW); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
#endif
#ifndef SINGLENOZZLE
#if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
#if EXTRUDERS > 1 && HAS_TEMP_1
target_temperature[1] = 0;
soft_pwm[1] = 0;
#if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
WRITE(HEATER_1_PIN,LOW);
#endif
WRITE_HEATER_1(LOW);
#endif
#if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
#if EXTRUDERS > 2 && HAS_TEMP_2
target_temperature[2] = 0;
soft_pwm[2] = 0;
#if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
WRITE(HEATER_2_PIN,LOW);
#endif
WRITE_HEATER_2(LOW);
#endif
#if defined(TEMP_3_PIN) && TEMP_3_PIN > -1 && EXTRUDERS > 3
#if EXTRUDERS > 3 && HAS_TEMP_3
target_temperature[3] = 0;
soft_pwm[3] = 0;
#if defined(HEATER_3_PIN) && HEATER_3_PIN > -1
WRITE(HEATER_3_PIN,LOW);
WRITE_HEATER_3(LOW);
#endif
#endif
#endif // !SINGLENOZZLE
#if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
#if HAS_TEMP_BED
target_temperature_bed = 0;
soft_pwm_bed = 0;
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
WRITE(HEATER_BED_PIN,LOW);
#if HAS_HEATER_BED
WRITE_HEATER_BED(LOW);
#endif
#endif
}
......@@ -1257,8 +1214,8 @@ void max_temp_error(uint8_t e) {
if(IsStopped() == false) {
SERIAL_ERROR_START;
SERIAL_ERRORLN((int)e);
SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
SERIAL_ERRORLNPGM(MSG_MAXTEMP_EXTRUDER_OFF);
LCD_ALERTMESSAGEPGM(MSG_ERR_MAXTEMP);
}
#ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
Stop();
......@@ -1279,10 +1236,10 @@ void min_temp_error(uint8_t e) {
}
void bed_max_temp_error(void) {
#if HEATER_BED_PIN > -1
WRITE(HEATER_BED_PIN, 0);
#if HAS_HEATER_BED
WRITE_HEATER_BED(0);
#endif
if(IsStopped() == false) {
if (IsStopped() == false) {
SERIAL_ERROR_START;
SERIAL_ERRORLNPGM(MSG_MAXTEMP_BED_OFF);
LCD_ALERTMESSAGEPGM(MSG_ERR_MAXTEMP_BED);
......@@ -1298,10 +1255,12 @@ void bed_max_temp_error(void) {
int max6675_temp = 2000;
static int read_max6675() {
if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
unsigned long ms = millis();
if (ms < max6675_previous_millis + MAX6675_HEAT_INTERVAL)
return max6675_temp;
max6675_previous_millis = millis();
max6675_previous_millis = ms;
max6675_temp = 0;
#ifdef PRR
......@@ -1340,10 +1299,27 @@ void bed_max_temp_error(void) {
else {
max6675_temp = max6675_temp >> 3;
}
return max6675_temp;
}
#endif //HEATER_0_USES_MAX6675
//Stages in the ISR loop
enum TempState {
PrepareTemp_0,
MeasureTemp_0,
PrepareTemp_BED,
MeasureTemp_BED,
PrepareTemp_1,
MeasureTemp_1,
PrepareTemp_2,
MeasureTemp_2,
PrepareTemp_3,
MeasureTemp_3,
Prepare_FILWIDTH,
Measure_FILWIDTH,
StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
};
// Timer 0 is shared with millies
ISR(TIMER0_COMPB_vect) {
......@@ -1354,340 +1330,179 @@ ISR(TIMER0_COMPB_vect) {
static unsigned long raw_temp_2_value = 0;
static unsigned long raw_temp_3_value = 0;
static unsigned long raw_temp_bed_value = 0;
static unsigned char temp_state = 12;
static TempState temp_state = StartupDelay;
static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
static unsigned char soft_pwm_0;
// Static members for each heater
#ifdef SLOW_PWM_HEATERS
static unsigned char slow_pwm_count = 0;
static unsigned char state_heater_0 = 0;
static unsigned char state_timer_heater_0 = 0;
#define ISR_STATICS(n) \
static unsigned char soft_pwm_ ## n; \
static unsigned char state_heater_ ## n = 0; \
static unsigned char state_timer_heater_ ## n = 0
#else
#define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
#endif
#ifndef SINGLENOZZLE
// Statics per heater
ISR_STATICS(0);
#ifndef SINGLENOZZLE
#if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
static unsigned char soft_pwm_1;
#ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_1 = 0;
static unsigned char state_timer_heater_1 = 0;
#endif //SLOW_PWM_HEATERS
#endif //(EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
ISR_STATICS(1);
#if EXTRUDERS > 2
static unsigned char soft_pwm_2;
#ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_2 = 0;
static unsigned char state_timer_heater_2 = 0;
#endif //SLOW_PWM_HEATERS
#endif //EXTRUDERS > 2
ISR_STATICS(2);
#if EXTRUDERS > 3
static unsigned char soft_pwm_3;
#ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_3 = 0;
static unsigned char state_timer_heater_3 = 0;
#endif //LOW_PWM_HEATERS
#endif //EXTRUDERS > 3
#endif // !SINGLENOZZLE
#if HEATER_BED_PIN > -1
static unsigned char soft_pwm_b;
#ifdef SLOW_PWM_HEATERS
static unsigned char state_heater_b = 0;
static unsigned char state_timer_heater_b = 0;
#endif //SLOW_PWM_HEATERS
#endif //HEATER_BED_PIN > -1
ISR_STATICS(3);
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
ISR_STATICS(BED);
#endif
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
static unsigned long raw_filwidth_value = 0; //added for filament width sensor
#if HAS_FILAMENT_SENSOR
static unsigned long raw_filwidth_value = 0;
#endif
#ifndef SLOW_PWM_HEATERS
/*
#ifndef SLOW_PWM_HEATERS
/**
* standard PWM modulation
*/
if(pwm_count == 0){
if (pwm_count == 0) {
soft_pwm_0 = soft_pwm[0];
if(soft_pwm_0 > 0) {
WRITE(HEATER_0_PIN,1);
#ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN,1);
#endif
} else WRITE(HEATER_0_PIN,0);
if (soft_pwm_0 > 0) {
WRITE_HEATER_0(1);
}
else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
#if EXTRUDERS > 1
soft_pwm_1 = soft_pwm[1];
if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
#endif
#if EXTRUDERS > 2
WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
#if EXTRUDERS > 2
soft_pwm_2 = soft_pwm[2];
if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
#endif
#if EXTRUDERS > 3
WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
#if EXTRUDERS > 3
soft_pwm_3 = soft_pwm[3];
if(soft_pwm_3 > 0) WRITE(HEATER_3_PIN,1); else WRITE(HEATER_3_PIN,0);
#endif
#endif // !SINGLENOZZLE
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
soft_pwm_b = soft_pwm_bed;
if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
#endif
#ifdef FAN_SOFT_PWM
WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
soft_pwm_BED = soft_pwm_bed;
WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
#endif
#ifdef FAN_SOFT_PWM
soft_pwm_fan = fanSpeedSoftPwm / 2;
if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
#endif
}
if(soft_pwm_0 < pwm_count) {
WRITE(HEATER_0_PIN,0);
#ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN,0);
#endif
WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
#endif
}
if (soft_pwm_0 < pwm_count) { WRITE_HEATER_0(0); }
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
#endif
#if EXTRUDERS > 2
if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
#endif
#if EXTRUDERS > 3
if(soft_pwm_3 < pwm_count) WRITE(HEATER_3_PIN,0);
#endif
#endif // !SINGLENOZZLE
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
#endif
#ifdef FAN_SOFT_PWM
if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
#endif
#if EXTRUDERS > 1
if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
#if EXTRUDERS > 2
if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
#if EXTRUDERS > 3
if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
#endif
#ifdef FAN_SOFT_PWM
if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
#endif
pwm_count += (1 << SOFT_PWM_SCALE);
pwm_count &= 0x7f;
#else //ifndef SLOW_PWM_HEATERS
#else // SLOW_PWM_HEATERS
/*
* SLOW PWM HEATERS
*
* for heaters drived by relay
*/
#ifndef MIN_STATE_TIME
#define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
#endif
if (slow_pwm_count == 0) {
// EXTRUDER 0
soft_pwm_0 = soft_pwm[0];
if (soft_pwm_0 > 0) {
// turn ON heather only if the minimum time is up
if (state_timer_heater_0 == 0) {
// if change state set timer
if (state_heater_0 == 0) {
state_timer_heater_0 = MIN_STATE_TIME;
}
state_heater_0 = 1;
WRITE(HEATER_0_PIN, 1);
#ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN, 1);
#endif
}
} else {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_0 == 0) {
// if change state set timer
if (state_heater_0 == 1) {
state_timer_heater_0 = MIN_STATE_TIME;
}
state_heater_0 = 0;
WRITE(HEATER_0_PIN, 0);
#ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN, 0);
#endif
}
}
#if EXTRUDERS > 1
// EXTRUDER 1
soft_pwm_1 = soft_pwm[1];
if (soft_pwm_1 > 0) {
// turn ON heather only if the minimum time is up
if (state_timer_heater_1 == 0) {
// if change state set timer
if (state_heater_1 == 0) {
state_timer_heater_1 = MIN_STATE_TIME;
}
state_heater_1 = 1;
WRITE(HEATER_1_PIN, 1);
}
} else {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_1 == 0) {
// if change state set timer
if (state_heater_1 == 1) {
state_timer_heater_1 = MIN_STATE_TIME;
}
state_heater_1 = 0;
WRITE(HEATER_1_PIN, 0);
}
}
#endif
#if EXTRUDERS > 2
// EXTRUDER 2
soft_pwm_2 = soft_pwm[2];
if (soft_pwm_2 > 0) {
// turn ON heather only if the minimum time is up
if (state_timer_heater_2 == 0) {
// if change state set timer
if (state_heater_2 == 0) {
state_timer_heater_2 = MIN_STATE_TIME;
}
state_heater_2 = 1;
WRITE(HEATER_2_PIN, 1);
}
} else {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_2 == 0) {
// if change state set timer
if (state_heater_2 == 1) {
state_timer_heater_2 = MIN_STATE_TIME;
}
state_heater_2 = 0;
WRITE(HEATER_2_PIN, 0);
}
}
#endif
#if EXTRUDERS > 3
// EXTRUDER 3
soft_pwm_3 = soft_pwm[3];
if (soft_pwm_3 > 0) {
// turn ON heather only if the minimum time is up
if (state_timer_heater_3 == 0) {
// if change state set timer
if (state_heater_3 == 0) {
state_timer_heater_3 = MIN_STATE_TIME;
}
state_heater_3 = 1;
WRITE(HEATER_3_PIN, 1);
}
} else {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_3 == 0) {
// if change state set timer
if (state_heater_3 == 1) {
state_timer_heater_3 = MIN_STATE_TIME;
}
state_heater_3 = 0;
WRITE(HEATER_3_PIN, 0);
}
}
#endif
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// BED
soft_pwm_b = soft_pwm_bed;
if (soft_pwm_b > 0) {
// turn ON heather only if the minimum time is up
if (state_timer_heater_b == 0) {
// if change state set timer
if (state_heater_b == 0) {
state_timer_heater_b = MIN_STATE_TIME;
}
state_heater_b = 1;
WRITE(HEATER_BED_PIN, 1);
}
} else {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_b == 0) {
// if change state set timer
if (state_heater_b == 1) {
state_timer_heater_b = MIN_STATE_TIME;
}
state_heater_b = 0;
WRITE(HEATER_BED_PIN, 0);
}
}
#endif
} // if (slow_pwm_count == 0)
// EXTRUDER 0
if (soft_pwm_0 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_0 == 0) {
// if change state set timer
if (state_heater_0 == 1) {
state_timer_heater_0 = MIN_STATE_TIME;
}
state_heater_0 = 0;
WRITE(HEATER_0_PIN, 0);
#ifdef HEATERS_PARALLEL
WRITE(HEATER_1_PIN, 0);
#endif
}
#ifndef MIN_STATE_TIME
#define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
#endif
// Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
#define _SLOW_PWM_ROUTINE(NR, src) \
soft_pwm_ ## NR = src; \
if (soft_pwm_ ## NR > 0) { \
if (state_timer_heater_ ## NR == 0) { \
if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
state_heater_ ## NR = 1; \
WRITE_HEATER_ ## NR(1); \
} \
} \
else { \
if (state_timer_heater_ ## NR == 0) { \
if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
state_heater_ ## NR = 0; \
WRITE_HEATER_ ## NR(0); \
} \
}
#define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
#define PWM_OFF_ROUTINE(NR) \
if (soft_pwm_ ## NR < slow_pwm_count) { \
if (state_timer_heater_ ## NR == 0) { \
if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
state_heater_ ## NR = 0; \
WRITE_HEATER_ ## NR (0); \
} \
}
#if EXTRUDERS > 1
// EXTRUDER 1
if (soft_pwm_1 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_1 == 0) {
// if change state set timer
if (state_heater_1 == 1) {
state_timer_heater_1 = MIN_STATE_TIME;
}
state_heater_1 = 0;
WRITE(HEATER_1_PIN, 0);
}
}
#endif
if (slow_pwm_count == 0) {
#if EXTRUDERS > 2
// EXTRUDER 2
if (soft_pwm_2 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_2 == 0) {
// if change state set timer
if (state_heater_2 == 1) {
state_timer_heater_2 = MIN_STATE_TIME;
}
state_heater_2 = 0;
WRITE(HEATER_2_PIN, 0);
}
}
#endif
SLOW_PWM_ROUTINE(0); // EXTRUDER 0
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
SLOW_PWM_ROUTINE(1); // EXTRUDER 1
#if EXTRUDERS > 2
SLOW_PWM_ROUTINE(2); // EXTRUDER 2
#if EXTRUDERS > 3
SLOW_PWM_ROUTINE(3); // EXTRUDER 3
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
_SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
#endif
#if EXTRUDERS > 3
// EXTRUDER 3
if (soft_pwm_3 < slow_pwm_count) {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_3 == 0) {
// if change state set timer
if (state_heater_3 == 1) {
state_timer_heater_3 = MIN_STATE_TIME;
}
state_heater_3 = 0;
WRITE(HEATER_3_PIN, 0);
}
}
#endif
} // slow_pwm_count == 0
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// BED
if (soft_pwm_b < slow_pwm_count) {
// turn OFF heather only if the minimum time is up
if (state_timer_heater_b == 0) {
// if change state set timer
if (state_heater_b == 1) {
state_timer_heater_b = MIN_STATE_TIME;
}
state_heater_b = 0;
WRITE(HEATER_BED_PIN, 0);
}
}
#endif
PWM_OFF_ROUTINE(0); // EXTRUDER 0
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
PWM_OFF_ROUTINE(1); // EXTRUDER 1
#if EXTRUDERS > 2
PWM_OFF_ROUTINE(2); // EXTRUDER 2
#if EXTRUDERS > 3
PWM_OFF_ROUTINE(3); // EXTRUDER 3
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
PWM_OFF_ROUTINE(BED); // BED
#endif
#ifdef FAN_SOFT_PWM
if (pwm_count == 0){
#ifdef FAN_SOFT_PWM
if (pwm_count == 0) {
soft_pwm_fan = fanSpeedSoftPwm / 2;
if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
}
if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
#endif
if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
#endif //FAN_SOFT_PWM
pwm_count += (1 << SOFT_PWM_SCALE);
pwm_count &= 0x7f;
......@@ -1697,201 +1512,153 @@ ISR(TIMER0_COMPB_vect) {
slow_pwm_count++;
slow_pwm_count &= 0x7f;
// Extruder 0
if (state_timer_heater_0 > 0) {
state_timer_heater_0--;
}
#if EXTRUDERS > 1
// Extruder 1
if (state_timer_heater_1 > 0)
state_timer_heater_1--;
#endif
#if EXTRUDERS > 2
// Extruder 2
if (state_timer_heater_2 > 0)
state_timer_heater_2--;
#endif
#if EXTRUDERS > 3
// Extruder 3
if (state_timer_heater_3 > 0)
state_timer_heater_3--;
#endif
#if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
// Bed
if (state_timer_heater_b > 0)
state_timer_heater_b--;
#endif
} //if ((pwm_count % 64) == 0) {
// EXTRUDER 0
if (state_timer_heater_0 > 0) state_timer_heater_0--;
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1 // EXTRUDER 1
if (state_timer_heater_1 > 0) state_timer_heater_1--;
#if EXTRUDERS > 2 // EXTRUDER 2
if (state_timer_heater_2 > 0) state_timer_heater_2--;
#if EXTRUDERS > 3 // EXTRUDER 3
if (state_timer_heater_3 > 0) state_timer_heater_3--;
#endif
#endif
#endif
#endif //SINGLENOZZLE
#if HAS_HEATER_BED
if (state_timer_heater_BED > 0) state_timer_heater_BED--;
#endif
} // (pwm_count % 64) == 0
#endif //ifndef SLOW_PWM_HEATERS
#endif // SLOW_PWM_HEATERS
switch(temp_state) {
case 0: // Prepare TEMP_0
#if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
#if TEMP_0_PIN > 7
ADCSRB = 1<<MUX5;
#define SET_ADMUX_ADCSRA(pin) ADMUX = (1 << REFS0) | (pin & 0x07); ADCSRA |= 1<<ADSC
#ifdef MUX5
#define START_ADC(pin) if (pin > 7) ADCSRB = 1 << MUX5; else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
#else
ADCSRB = 0;
#define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
#endif
ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
switch(temp_state) {
case PrepareTemp_0:
#if HAS_TEMP_0
START_ADC(TEMP_0_PIN);
#endif
lcd_buttons_update();
temp_state = 1;
temp_state = MeasureTemp_0;
break;
case 1: // Measure TEMP_0
#if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
case MeasureTemp_0:
#if HAS_TEMP_0
raw_temp_0_value += ADC;
#endif
temp_state = 2;
temp_state = PrepareTemp_BED;
break;
case 2: // Prepare TEMP_BED
#if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
#if TEMP_BED_PIN > 7
ADCSRB = 1<<MUX5;
#else
ADCSRB = 0;
#endif
ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
case PrepareTemp_BED:
#if HAS_TEMP_BED
START_ADC(TEMP_BED_PIN);
#endif
lcd_buttons_update();
temp_state = 3;
temp_state = MeasureTemp_BED;
break;
case 3: // Measure TEMP_BED
#if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
case MeasureTemp_BED:
#if HAS_TEMP_BED
raw_temp_bed_value += ADC;
#endif
temp_state = 4;
temp_state = PrepareTemp_1;
break;
case 4: // Prepare TEMP_1
#if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
#if TEMP_1_PIN > 7
ADCSRB = 1<<MUX5;
#else
ADCSRB = 0;
#endif
ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
case PrepareTemp_1:
#if HAS_TEMP_1
START_ADC(TEMP_1_PIN);
#endif
lcd_buttons_update();
temp_state = 5;
temp_state = MeasureTemp_1;
break;
case 5: // Measure TEMP_1
#if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
case MeasureTemp_1:
#if HAS_TEMP_1
raw_temp_1_value += ADC;
#endif
temp_state = 6;
temp_state = PrepareTemp_2;
break;
case 6: // Prepare TEMP_2
#if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
#if TEMP_2_PIN > 7
ADCSRB = 1<<MUX5;
#else
ADCSRB = 0;
#endif
ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
case PrepareTemp_2:
#if HAS_TEMP_2
START_ADC(TEMP_2_PIN);
#endif
lcd_buttons_update();
temp_state = 7;
temp_state = MeasureTemp_2;
break;
case 7: // Measure TEMP_2
#if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
case MeasureTemp_2:
#if HAS_TEMP_2
raw_temp_2_value += ADC;
#endif
temp_state = 8;
temp_state = PrepareTemp_3;
break;
case 8: // Prepare TEMP_3
#if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
#if TEMP_3_PIN > 7
ADCSRB = 1<<MUX5;
#else
ADCSRB = 0;
#endif
ADMUX = ((1 << REFS0) | (TEMP_3_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
case PrepareTemp_3:
#if HAS_TEMP_3
START_ADC(TEMP_3_PIN);
#endif
lcd_buttons_update();
temp_state = 9;
temp_state = MeasureTemp_3;
break;
case 9: // Measure TEMP_3
#if defined(TEMP_3_PIN) && (TEMP_3_PIN > -1)
case MeasureTemp_3:
#if HAS_TEMP_3
raw_temp_3_value += ADC;
#endif
temp_state = 10; //change so that Filament Width is also measured
temp_state = Prepare_FILWIDTH;
break;
case 10: //Prepare FILWIDTH
#if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
#if FILWIDTH_PIN>7
ADCSRB = 1<<MUX5;
#else
ADCSRB = 0;
#endif
ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
ADCSRA |= 1<<ADSC; // Start conversion
case Prepare_FILWIDTH:
#if HAS_FILAMENT_SENSOR
START_ADC(FILWIDTH_PIN);
#endif
lcd_buttons_update();
temp_state = 11;
temp_state = Measure_FILWIDTH;
break;
case 11: //Measure FILWIDTH
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
//raw_filwidth_value += ADC; //remove to use an IIR filter approach
if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
{
raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
case Measure_FILWIDTH:
#if HAS_FILAMENT_SENSOR
// raw_filwidth_value += ADC; //remove to use an IIR filter approach
if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128
raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading
}
#endif
temp_state = 0;
temp_state = PrepareTemp_0;
temp_count++;
break;
case 12: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
temp_state = 0;
case StartupDelay:
temp_state = PrepareTemp_0;
break;
// default:
// SERIAL_ERROR_START;
// SERIAL_ERRORLNPGM("Temp measurement error!");
// break;
}
if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
{
if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
{
#ifndef HEATER_0_USES_MAX6675
// default:
// SERIAL_ERROR_START;
// SERIAL_ERRORLNPGM("Temp measurement error!");
// break;
} // switch(temp_state)
if (temp_count >= OVERSAMPLENR) { // 12 * 16 * 1/(16000000/64/256) = 164ms.
if (!temp_meas_ready) { //Only update the raw values if they have been read. Else we could be updating them during reading.
#ifndef HEATER_0_USES_MAX6675
current_temperature_raw[0] = raw_temp_0_value;
#endif
#endif
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
#if EXTRUDERS > 1
current_temperature_raw[1] = raw_temp_1_value;
#endif
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
redundant_temperature_raw = raw_temp_1_value;
#endif
#if EXTRUDERS > 2
#if EXTRUDERS > 2
current_temperature_raw[2] = raw_temp_2_value;
#endif
#if EXTRUDERS > 3
#if EXTRUDERS > 3
current_temperature_raw[3] = raw_temp_3_value;
#endif
#endif // !SINGLENOZZLE
#endif
#endif
#endif
#endif //SINGLENOZZLE
#ifdef TEMP_SENSOR_1_AS_REDUNDANT
redundant_temperature_raw = raw_temp_1_value;
#endif
current_temperature_bed_raw = raw_temp_bed_value;
}
//Add similar code for Filament Sensor - can be read any time since IIR filtering is used
#if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
#endif
} //!temp_meas_ready
// Filament Sensor - can be read any time since IIR filtering is used
#if HAS_FILAMENT_SENSOR
current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
#endif
temp_meas_ready = true;
temp_count = 0;
......@@ -1901,131 +1668,52 @@ ISR(TIMER0_COMPB_vect) {
raw_temp_3_value = 0;
raw_temp_bed_value = 0;
#if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
if(current_temperature_raw[0] <= maxttemp_raw[0]) {
#else
if(current_temperature_raw[0] >= maxttemp_raw[0]) {
#endif
#ifndef HEATER_0_USES_MAX6675
max_temp_error(0);
#endif
}
#if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
if(current_temperature_raw[0] >= minttemp_raw[0]) {
#else
if(current_temperature_raw[0] <= minttemp_raw[0]) {
#endif
#ifndef HEATER_0_USES_MAX6675
min_temp_error(0);
#endif
}
#if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
#define MAXTEST <=
#define MINTEST >=
#else
#define MAXTEST >=
#define MINTEST <=
#endif
#ifndef SINGLENOZZLE
#if EXTRUDERS > 1
#if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
if(current_temperature_raw[1] <= maxttemp_raw[1]) {
#else
if(current_temperature_raw[1] >= maxttemp_raw[1]) {
#endif
max_temp_error(1);
}
#if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
if(current_temperature_raw[1] >= minttemp_raw[1]) {
#else
if(current_temperature_raw[1] <= minttemp_raw[1]) {
#endif
min_temp_error(1);
}
#endif
#if EXTRUDERS > 2
#if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
if(current_temperature_raw[2] <= maxttemp_raw[2]) {
#else
if(current_temperature_raw[2] >= maxttemp_raw[2]) {
#endif
max_temp_error(2);
}
#if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
if(current_temperature_raw[2] >= minttemp_raw[2]) {
#else
if(current_temperature_raw[2] <= minttemp_raw[2]) {
#endif
min_temp_error(2);
}
#endif
#if EXTRUDERS > 3
#if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
if(current_temperature_raw[3] <= maxttemp_raw[3]) {
#else
if(current_temperature_raw[3] >= maxttemp_raw[3]) {
#endif
max_temp_error(3);
}
#if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
if(current_temperature_raw[3] >= minttemp_raw[3]) {
#else
if(current_temperature_raw[3] <= minttemp_raw[3]) {
#endif
min_temp_error(3);
#ifndef SINGLENOZZLE
for (int i = 0; i < EXTRUDERS; i++)
#else
int i = 0;
#endif
{
if (current_temperature_raw[i] MAXTEST maxttemp_raw[i]) max_temp_error(i);
else if (current_temperature_raw[i] MINTEST minttemp_raw[i]) min_temp_error(i);
}
#endif
#endif // !SINGLENOZZLE
/* No bed MINTEMP error? */
#if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
# if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
if(current_temperature_bed_raw <= bed_maxttemp_raw) {
#else
if(current_temperature_bed_raw >= bed_maxttemp_raw) {
#endif
#if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
if (current_temperature_bed_raw MAXTEST bed_maxttemp_raw) {
target_temperature_bed = 0;
bed_max_temp_error();
}
#endif
}
#endif
} // temp_count >= OVERSAMPLENR
#ifdef BABYSTEPPING
for(uint8_t axis=0;axis<3;axis++)
{
#ifdef BABYSTEPPING
for (uint8_t axis=X_AXIS; axis<=Z_AXIS; axis++) {
int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
if(curTodo>0)
{
if (curTodo > 0) {
babystep(axis,/*fwd*/true);
babystepsTodo[axis]--; //less to do next time
}
else
if(curTodo<0)
{
else if(curTodo < 0) {
babystep(axis,/*fwd*/false);
babystepsTodo[axis]++; //less to do next time
}
}
#endif //BABYSTEPPING
#endif //BABYSTEPPING
}
#ifdef PIDTEMP
// Apply the scale factors to the PID values
float scalePID_i(float i)
{
return i*PID_dT;
}
float unscalePID_i(float i)
{
return i/PID_dT;
}
float scalePID_d(float d)
{
return d/PID_dT;
}
float unscalePID_d(float d)
{
return d*PID_dT;
}
// Apply the scale factors to the PID values
float scalePID_i(float i) { return i * PID_dT; }
float unscalePID_i(float i) { return i / PID_dT; }
float scalePID_d(float d) { return d / PID_dT; }
float unscalePID_d(float d) { return d * PID_dT; }
#endif //PIDTEMP
......@@ -96,63 +96,51 @@ FORCE_INLINE float degHotend(uint8_t extruder) {
#else
return current_temperature[0];
#endif
};
}
#ifdef SHOW_TEMP_ADC_VALUES
FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
#ifndef SINGLENOZZLE
FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
#ifndef SINGLENOZZLE
return current_temperature_raw[extruder];
#else
#else
return current_temperature_raw[0];
#endif
};
#endif
}
FORCE_INLINE float rawBedTemp() {
return current_temperature_bed_raw;
};
#endif
FORCE_INLINE float rawBedTemp() { return current_temperature_bed_raw; }
#endif //SHOW_TEMP_ADC_VALUES
FORCE_INLINE float degBed() {
return current_temperature_bed;
};
FORCE_INLINE float degBed() { return current_temperature_bed; }
FORCE_INLINE float degTargetHotend(uint8_t extruder) {
#ifndef SINGLENOZZLE
#ifndef SINGLENOZZLE
return target_temperature[extruder];
#else
#else
return target_temperature[0];
#endif
};
FORCE_INLINE float degTargetBed() {
return target_temperature_bed;
};
#endif
}
FORCE_INLINE float degTargetBed() { return target_temperature_bed; }
FORCE_INLINE void setTargetHotend(const float &celsius, uint8_t extruder) {
#ifndef SINGLENOZZLE
#ifndef SINGLENOZZLE
target_temperature[extruder] = celsius;
#else
#else
if (extruder == active_extruder) {
target_temperature[0] = celsius;
}
#endif
};
FORCE_INLINE void setTargetBed(const float &celsius) {
target_temperature_bed = celsius;
};
#endif
}
FORCE_INLINE void setTargetBed(const float &celsius) { target_temperature_bed = celsius; }
FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
#ifndef SINGLENOZZLE
FORCE_INLINE bool isHeatingHotend(uint8_t extruder) {
#ifndef SINGLENOZZLE
return target_temperature[extruder] > current_temperature[extruder];
#else
#else
return target_temperature[0] > current_temperature[0];
#endif
};
#endif
}
FORCE_INLINE bool isHeatingBed() {
return target_temperature_bed > current_temperature_bed;
};
FORCE_INLINE bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
#ifndef SINGLENOZZLE
......@@ -162,9 +150,7 @@ FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
#endif
};
FORCE_INLINE bool isCoolingBed() {
return target_temperature_bed < current_temperature_bed;
};
FORCE_INLINE bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
#define degHotend0() degHotend(0)
#define degTargetHotend0() degTargetHotend(0)
......@@ -172,38 +158,36 @@ FORCE_INLINE bool isCoolingBed() {
#define isHeatingHotend0() isHeatingHotend(0)
#define isCoolingHotend0() isCoolingHotend(0)
#if EXTRUDERS > 1 && !defined(SINGLENOZZLE)
#define degHotend1() degHotend(1)
#define degTargetHotend1() degTargetHotend(1)
#define setTargetHotend1(_celsius) setTargetHotend((_celsius), 1)
#define isHeatingHotend1() isHeatingHotend(1)
#define isCoolingHotend1() isCoolingHotend(1)
#define degHotend1() degHotend(1)
#define degTargetHotend1() degTargetHotend(1)
#define setTargetHotend1(_celsius) setTargetHotend((_celsius), 1)
#define isHeatingHotend1() isHeatingHotend(1)
#define isCoolingHotend1() isCoolingHotend(1)
#else
#define setTargetHotend1(_celsius) do{}while(0)
#define setTargetHotend1(_celsius) do{}while(0)
#endif
#if EXTRUDERS > 2 && !defined(SINGLENOZZLE)
#define degHotend2() degHotend(2)
#define degTargetHotend2() degTargetHotend(2)
#define setTargetHotend2(_celsius) setTargetHotend((_celsius), 2)
#define isHeatingHotend2() isHeatingHotend(2)
#define isCoolingHotend2() isCoolingHotend(2)
#define degHotend2() degHotend(2)
#define degTargetHotend2() degTargetHotend(2)
#define setTargetHotend2(_celsius) setTargetHotend((_celsius), 2)
#define isHeatingHotend2() isHeatingHotend(2)
#define isCoolingHotend2() isCoolingHotend(2)
#else
#define setTargetHotend2(_celsius) do{}while(0)
#define setTargetHotend2(_celsius) do{}while(0)
#endif
#if EXTRUDERS > 3 && !defined(SINGLENOZZLE)
#define degHotend3() degHotend(3)
#define degTargetHotend3() degTargetHotend(3)
#define setTargetHotend3(_celsius) setTargetHotend((_celsius), 3)
#define isHeatingHotend3() isHeatingHotend(3)
#define isCoolingHotend3() isCoolingHotend(3)
#define degHotend3() degHotend(3)
#define degTargetHotend3() degTargetHotend(3)
#define setTargetHotend3(_celsius) setTargetHotend((_celsius), 3)
#define isHeatingHotend3() isHeatingHotend(3)
#define isCoolingHotend3() isCoolingHotend(3)
#else
#define setTargetHotend3(_celsius) do{}while(0)
#define setTargetHotend3(_celsius) do{}while(0)
#endif
#if EXTRUDERS > 4
#error Invalid number of extruders
#error Invalid number of extruders
#endif
int getHeaterPower(int heater);
void disable_heater();
void setWatch();
......@@ -220,15 +204,14 @@ static bool thermal_runaway = false;
#endif
#endif
FORCE_INLINE void autotempShutdown(){
#ifdef AUTOTEMP
if(autotemp_enabled)
{
autotemp_enabled=false;
if(degTargetHotend(active_extruder)>autotemp_min)
setTargetHotend(0,active_extruder);
FORCE_INLINE void autotempShutdown() {
#ifdef AUTOTEMP
if (autotemp_enabled) {
autotemp_enabled = false;
if (degTargetHotend(active_extruder) > autotemp_min)
setTargetHotend(0, active_extruder);
}
#endif
#endif
}
void PID_autotune(float temp, int extruder, int ncycles);
......
......@@ -1096,13 +1096,26 @@ const short temptable_1047[][2] PROGMEM = {
#endif
#if (THERMISTORHEATER_0 == 999) || (THERMISTORHEATER_1 == 999) || (THERMISTORHEATER_2 == 999) || (THERMISTORHEATER_3 == 999) || (THERMISTORBED == 999) //User defined table
// Dummy Thermistor table.. It will ALWAYS read 25C.
const short temptable_999[][2] PROGMEM = {
{1*OVERSAMPLENR, 25},
{1023*OVERSAMPLENR, 25}
// Dummy Thermistor table.. It will ALWAYS read a fixed value.
#ifndef DUMMY_THERMISTOR_999_VALUE
#define DUMMY_THERMISTOR_999_VALUE 25
#endif
const short temptable_999[][2] PROGMEM = {
{1*OVERSAMPLENR, DUMMY_THERMISTOR_999_VALUE},
{1023*OVERSAMPLENR, DUMMY_THERMISTOR_999_VALUE}
};
#endif
#if (THERMISTORHEATER_0 == 998) || (THERMISTORHEATER_1 == 998) || (THERMISTORHEATER_2 == 998) || (THERMISTORHEATER_3 == 998) || (THERMISTORBED == 998) //User defined table
// Dummy Thermistor table.. It will ALWAYS read a fixed value.
#ifndef DUMMY_THERMISTOR_998_VALUE
#define DUMMY_THERMISTOR_998_VALUE 25
#endif
const short temptable_998[][2] PROGMEM = {
{1*OVERSAMPLENR, DUMMY_THERMISTOR_998_VALUE},
{1023*OVERSAMPLENR, DUMMY_THERMISTOR_998_VALUE}
};
#endif
#define _TT_NAME(_N) temptable_ ## _N
......
......@@ -802,7 +802,7 @@ static void lcd_control_menu() {
MENU_ITEM(submenu, MSG_VOLUMETRIC, lcd_control_volumetric_menu);
#ifdef DOGLCD
// MENU_ITEM_EDIT(int3, MSG_CONTRAST, &lcd_contrast, 0, 63);
//MENU_ITEM_EDIT(int3, MSG_CONTRAST, &lcd_contrast, 0, 63);
MENU_ITEM(submenu, MSG_CONTRAST, lcd_set_contrast);
#endif
#ifdef FWRETRACT
......@@ -869,9 +869,9 @@ static void lcd_control_temperature_menu() {
#if TEMP_SENSOR_3 != 0
MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_NOZZLE " 4", &target_temperature[3], 0, HEATER_3_MAXTEMP - 15);
#endif
#endif
#endif
#endif
#endif //EXTRUDERS > 3
#endif //EXTRUDERS > 2
#endif //EXTRUDERS > 1
#if TEMP_SENSOR_BED != 0
MENU_MULTIPLIER_ITEM_EDIT(int3, MSG_BED, &target_temperature_bed, 0, BED_MAXTEMP - 15);
#endif
......@@ -1037,6 +1037,7 @@ static void lcd_control_volumetric_menu()
#endif //EXTRUDERS > 2
#endif //EXTRUDERS > 1
}
END_MENU();
}
......@@ -1495,6 +1496,16 @@ void lcd_reset_alert_level() { lcd_status_message_level = 0; }
#ifdef ULTIPANEL
// Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
// These values are independent of which pins are used for EN_A and EN_B indications
// The rotary encoder part is also independent to the chipset used for the LCD
#if defined(EN_A) && defined(EN_B)
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif
/* Warning: This function is called from interrupt context */
void lcd_buttons_update() {
#ifdef NEWPANEL
......@@ -1887,20 +1898,20 @@ char *ftostr52(const float &x)
// grab the PID i value out of the temp variable; scale it; then update the PID driver
void copy_and_scalePID_i()
{
#ifdef PIDTEMP
#ifdef PIDTEMP
Ki[active_extruder] = scalePID_i(raw_Ki);
updatePID();
#endif
#endif
}
// Callback for after editing PID d value
// grab the PID d value out of the temp variable; scale it; then update the PID driver
void copy_and_scalePID_d()
{
#ifdef PIDTEMP
#ifdef PIDTEMP
Kd[active_extruder] = scalePID_d(raw_Kd);
updatePID();
#endif
#endif
}
#endif //ULTRA_LCD
......@@ -123,17 +123,6 @@
#define LCD_CLICKED (buttons&(B_MI|B_ST))
#endif
////////////////////////
// Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
// These values are independent of which pins are used for EN_A and EN_B indications
// The rotary encoder part is also independent to the chipset used for the LCD
#if defined(EN_A) && defined(EN_B)
#define encrot0 0
#define encrot1 2
#define encrot2 3
#define encrot3 1
#endif
#endif //ULTIPANEL
////////////////////////////////////
......
......@@ -47,12 +47,9 @@ uint8_t u8g_dev_rrd_st7920_128x64_fn(u8g_t *u8g, u8g_dev_t *dev, uint8_t msg, vo
{
case U8G_DEV_MSG_INIT:
{
SET_OUTPUT(ST7920_CS_PIN);
WRITE(ST7920_CS_PIN,0);
SET_OUTPUT(ST7920_DAT_PIN);
WRITE(ST7920_DAT_PIN,0);
SET_OUTPUT(ST7920_CLK_PIN);
WRITE(ST7920_CLK_PIN,1);
OUT_WRITE(ST7920_CS_PIN,LOW);
OUT_WRITE(ST7920_DAT_PIN,LOW);
OUT_WRITE(ST7920_CLK_PIN,HIGH);
ST7920_CS();
u8g_Delay(120); //initial delay for boot up
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment