void Space::load_info() { // {{{ loaddebug("loading space %d", id); int t = type; if (t < 0 || t >= NUM_SPACE_TYPES) { debug("invalid current type?! using default type instead"); t = DEFAULT_TYPE; } type = shmem->ints[1]; loaddebug("requested type %d, current is %d", type, t); if (type < 0 || type >= NUM_SPACE_TYPES || (id == 1 && type != TYPE_EXTRUDER) || (id == 2 && type != TYPE_FOLLOWER)) { debug("request for type %d ignored", type); type = t; return; // The rest of the info is not meant for this type, so ignore it. } if (t != type) { loaddebug("setting type to %d", type); space_types[t].free(this); if (!space_types[type].init(this)) { type = DEFAULT_TYPE; reset_pos(this); for (int a = 0; a < num_axes; ++a) axis[a]->settings.current = axis[a]->settings.source; return; // The rest of the info is not meant for DEFAULT_TYPE, so ignore it. } } space_types[type].load(this); reset_pos(this); loaddebug("done loading space"); } // }}}
void Space::load_axis(int a) { // {{{ loaddebug("loading axis %d", a); axis[a]->park_order = shmem->ints[2]; axis[a]->park = shmem->floats[0]; axis[a]->min_pos = shmem->floats[1]; axis[a]->max_pos = shmem->floats[2]; } // }}}
void Space::load_axis(uint8_t a, int32_t &addr) { // {{{ loaddebug("loading axis %d", a); axis[a]->park = read_float(addr); axis[a]->park_order = read_8(addr); axis[a]->min_pos = read_float(addr); axis[a]->max_pos = read_float(addr); } // }}}
void Space::load_motor(int m, int32_t &addr) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; motor[m]->step_pin.read(read_16(addr)); motor[m]->dir_pin.read(read_16(addr)); motor[m]->enable_pin.read(read_16(addr)); motor[m]->limit_min_pin.read(read_16(addr)); motor[m]->limit_max_pin.read(read_16(addr)); motor[m]->steps_per_unit = read_float(addr); motor[m]->home_pos = read_float(addr); motor[m]->limit_v = read_float(addr); motor[m]->limit_a = read_float(addr); motor[m]->home_order = read_8(addr); arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); bool must_move = false; if (!isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !isnan(old_home_pos)) { double ohp = old_home_pos * old_steps_per_unit; double hp = motor[m]->home_pos * motor[m]->steps_per_unit; double diff = hp - ohp; motor[m]->settings.current_pos += diff; //debug("load motor %d %d new home %f add %f", id, m, motor[m]->home_pos, diff); arch_addpos(id, m, diff); must_move = true; } } else { // Axes without a limit switch, including extruders. if (motors_busy && old_steps_per_unit != motor[m]->steps_per_unit) { debug("load motor %d %d new steps no home", id, m); double oldpos = motor[m]->settings.current_pos; double pos = oldpos / old_steps_per_unit; motor[m]->settings.current_pos = pos * motor[m]->steps_per_unit; arch_addpos(id, m, motor[m]->settings.current_pos - oldpos); // Adjust current_pos in all history. for (int h = 0; h < FRAGMENTS_PER_BUFFER; ++h) { oldpos = motor[m]->history[h].current_pos; pos = oldpos / old_steps_per_unit; motor[m]->history[h].current_pos = pos * motor[m]->steps_per_unit; } } } if (must_move) move_to_current(); } // }}}
void Space::load_motor(uint8_t m, int32_t &addr) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; motor[m]->step_pin.read(read_16(addr)); motor[m]->dir_pin.read(read_16(addr)); motor[m]->enable_pin.read(read_16(addr)); motor[m]->limit_min_pin.read(read_16(addr)); motor[m]->limit_max_pin.read(read_16(addr)); motor[m]->sense_pin.read(read_16(addr)); motor[m]->steps_per_unit = read_float(addr); motor[m]->max_steps = read_8(addr); motor[m]->home_pos = read_float(addr); motor[m]->limit_v = read_float(addr); motor[m]->limit_a = read_float(addr); motor[m]->home_order = read_8(addr); arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); SET_INPUT(motor[m]->sense_pin); bool must_move = false; if (!isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !isnan(old_home_pos)) { int32_t hp = motor[m]->home_pos * motor[m]->steps_per_unit + (motor[m]->home_pos > 0 ? .49 : -.49); int32_t ohp = old_home_pos * old_steps_per_unit + (old_home_pos > 0 ? .49 : -.49); int32_t diff = hp - ohp; motor[m]->settings.current_pos += diff; cpdebug(id, m, "load motor new home add %d", diff); arch_addpos(id, m, diff); must_move = true; } } else { // Axes without a limit switch, including extruders. if (motors_busy && old_steps_per_unit != motor[m]->steps_per_unit) { int32_t cp = motor[m]->settings.current_pos; double pos = cp / old_steps_per_unit; cpdebug(id, m, "load motor new steps no home"); motor[m]->settings.current_pos = pos * motor[m]->steps_per_unit; int diff = motor[m]->settings.current_pos - cp; arch_addpos(id, m, diff); } } if (must_move) move_to_current(); } // }}}
void Space::load_motor(int m) { // {{{ loaddebug("loading motor %d", m); uint16_t enable = motor[m]->enable_pin.write(); double old_home_pos = motor[m]->home_pos; double old_steps_per_unit = motor[m]->steps_per_unit; bool old_dir_invert = motor[m]->dir_pin.inverted(); motor[m]->step_pin.read(shmem->ints[2]); motor[m]->dir_pin.read(shmem->ints[3]); motor[m]->enable_pin.read(shmem->ints[4]); motor[m]->limit_min_pin.read(shmem->ints[5]); motor[m]->limit_max_pin.read(shmem->ints[6]); motor[m]->home_order = shmem->ints[7]; motor[m]->steps_per_unit = shmem->floats[0]; if (std::isnan(motor[m]->steps_per_unit)) { debug("Trying to set NaN steps per unit for motor %d %d", id, m); motor[m]->steps_per_unit = old_steps_per_unit; } motor[m]->home_pos = shmem->floats[1]; motor[m]->limit_v = shmem->floats[2]; motor[m]->limit_a = shmem->floats[3]; arch_motors_change(); SET_OUTPUT(motor[m]->enable_pin); if (enable != motor[m]->enable_pin.write()) { if (motors_busy) SET(motor[m]->enable_pin); else { RESET(motor[m]->enable_pin); } } RESET(motor[m]->step_pin); RESET(motor[m]->dir_pin); SET_INPUT(motor[m]->limit_min_pin); SET_INPUT(motor[m]->limit_max_pin); if (motor[m]->dir_pin.inverted() != old_dir_invert) arch_invertpos(id, m); if (!std::isnan(motor[m]->home_pos)) { // Axes with a limit switch. if (motors_busy && (old_home_pos != motor[m]->home_pos || old_steps_per_unit != motor[m]->steps_per_unit) && !std::isnan(old_home_pos)) { double diff = motor[m]->home_pos - old_home_pos * old_steps_per_unit / motor[m]->steps_per_unit; motor[m]->settings.current_pos += diff; //debug("load motor %d %d new home %f add %f", id, m, motor[m]->home_pos, diff); // adjusting the arch pos is not affected by steps/unit. arch_addpos(id, m, motor[m]->home_pos - old_home_pos); } reset_pos(this); } else if (!std::isnan(motor[m]->settings.current_pos)) { // Motors without a limit switch: adjust motor position to match axes. for (int a = 0; a < num_axes; ++a) axis[a]->settings.target = axis[a]->settings.current; space_types[type].xyz2motors(this); double diff = motor[m]->settings.target_pos - motor[m]->settings.current_pos; motor[m]->settings.current_pos += diff; arch_addpos(id, m, diff); } } // }}}
void Space::load_info(int32_t &addr) { // {{{ loaddebug("loading space %d", id); uint8_t t = type; if (t >= NUM_SPACE_TYPES) t = DEFAULT_TYPE; type = read_8(addr); if (type >= NUM_SPACE_TYPES || (id == 1 && type != EXTRUDER_TYPE)) { debug("request for type %d ignored", type); type = t; } bool ok; if (t != type) { loaddebug("setting type to %d", type); space_types[t].free(this); if (!space_types[type].init(this)) { type = DEFAULT_TYPE; space_types[type].reset_pos(this); for (uint8_t a = 0; a < num_axes; ++a) axis[a]->settings.current = axis[a]->settings.source; return; // The rest of the package is not meant for DEFAULT_TYPE, so ignore it. } ok = false; } else { if (computing_move || !motors_busy) ok = false; else ok = true; } space_types[type].load(this, t, addr); if (t != type) { space_types[type].reset_pos(this); loaddebug("resetting current for load space %d", id); for (uint8_t a = 0; a < num_axes; ++a) axis[a]->settings.current = axis[a]->settings.source; } if (ok) move_to_current(); loaddebug("done loading space"); } // }}}
bool Space::setup_nums(int na, int nm) { // {{{ if (na == num_axes && nm == num_motors) return true; loaddebug("new space %d %d %d %d", na, nm, num_axes, num_motors); int old_nm = num_motors; int old_na = num_axes; num_motors = nm; num_axes = na; if (na != old_na) { Axis **new_axes = new Axis *[na]; loaddebug("new axes: %d", na); for (int a = 0; a < min(old_na, na); ++a) new_axes[a] = axis[a]; for (int a = old_na; a < na; ++a) { new_axes[a] = new Axis; new_axes[a]->park = NAN; new_axes[a]->park_order = 0; new_axes[a]->min_pos = -INFINITY; new_axes[a]->max_pos = INFINITY; new_axes[a]->type_data = NULL; new_axes[a]->settings.dist[0] = NAN; new_axes[a]->settings.dist[1] = NAN; new_axes[a]->settings.main_dist = NAN; new_axes[a]->settings.target = NAN; new_axes[a]->settings.source = NAN; new_axes[a]->settings.current = NAN; new_axes[a]->history = setup_axis_history(); } for (int a = na; a < old_na; ++a) { space_types[type].afree(this, a); delete[] axis[a]->history; delete axis[a]; } delete[] axis; axis = new_axes; } if (nm != old_nm) { Motor **new_motors = new Motor *[nm]; loaddebug("new motors: %d", nm); for (int m = 0; m < min(old_nm, nm); ++m) new_motors[m] = motor[m]; for (int m = old_nm; m < nm; ++m) { new_motors[m] = new Motor; new_motors[m]->step_pin.init(); new_motors[m]->dir_pin.init(); new_motors[m]->enable_pin.init(); new_motors[m]->limit_min_pin.init(); new_motors[m]->limit_max_pin.init(); new_motors[m]->steps_per_unit = 100; new_motors[m]->limit_v = INFINITY; new_motors[m]->limit_a = INFINITY; new_motors[m]->home_pos = NAN; new_motors[m]->home_order = 0; new_motors[m]->limit_v = INFINITY; new_motors[m]->limit_a = INFINITY; new_motors[m]->active = false; new_motors[m]->settings.last_v = 0; new_motors[m]->settings.current_pos = 0; new_motors[m]->settings.target_v = NAN; new_motors[m]->settings.target_pos = NAN; new_motors[m]->history = setup_motor_history(); ARCH_NEW_MOTOR(id, m, new_motors); } for (int m = nm; m < old_nm; ++m) { DATA_DELETE(id, m); delete[] motor[m]->history; delete motor[m]; } delete[] motor; motor = new_motors; arch_motors_change(); } return true; } // }}}