diff --git a/Firmware/MotorControl/axis.cpp b/Firmware/MotorControl/axis.cpp index 3bdb03ce..dc57bdc4 100644 --- a/Firmware/MotorControl/axis.cpp +++ b/Firmware/MotorControl/axis.cpp @@ -321,6 +321,10 @@ void Axis::run_state_machine_loop() { else if (config_.startup_sensorless_control) task_chain_[pos++] = AXIS_STATE_SENSORLESS_CONTROL; task_chain_[pos++] = AXIS_STATE_IDLE; + } else if (requested_state_ == AXIS_STATE_HOMING){ + task_chain_[pos++] = AXIS_STATE_HOMING; + task_chain_[pos++] = AXIS_STATE_CLOSED_LOOP_CONTROL; + task_chain_[pos++] = AXIS_STATE_IDLE; } else if (requested_state_ == AXIS_STATE_FULL_CALIBRATION_SEQUENCE) { task_chain_[pos++] = AXIS_STATE_MOTOR_CALIBRATION; if (encoder_.config_.use_index) @@ -357,6 +361,10 @@ void Axis::run_state_machine_loop() { status = encoder_.run_index_search(); break; + case AXIS_STATE_HOMING: + status = controller_.home_axis(); + break; + case AXIS_STATE_ENCODER_OFFSET_CALIBRATION: status = encoder_.run_offset_calibration(); break; diff --git a/Firmware/MotorControl/axis.hpp b/Firmware/MotorControl/axis.hpp index 8b0095d1..220a2874 100644 --- a/Firmware/MotorControl/axis.hpp +++ b/Firmware/MotorControl/axis.hpp @@ -16,12 +16,14 @@ enum AxisState_t { AXIS_STATE_SENSORLESS_CONTROL = 5, //config_.min_endstop.enabled) { + set_vel_setpoint(-config_.homing_speed, 0.0f); + } else { + return false; + } + + axis_->run_control_loop([&](){ + if(axis_->min_endstop_state_){ + axis_->encoder_.set_linear_count(axis_->config_.min_endstop.offset); + set_pos_setpoint(0.0f, 0.0f, 0.0f); + } + return !axis_->min_endstop_state_; + }); + return true; +} + /* * This anti-cogging implementation iterates through each encoder position, * waits for zero velocity & position error, diff --git a/Firmware/MotorControl/controller.hpp b/Firmware/MotorControl/controller.hpp index f10b6211..abc155ee 100644 --- a/Firmware/MotorControl/controller.hpp +++ b/Firmware/MotorControl/controller.hpp @@ -21,6 +21,7 @@ struct ControllerConfig_t { // float vel_gain = 5.0f / 200.0f, // [A/(rad/s)] float vel_integrator_gain = 10.0f / 10000.0f; // [A/(counts/s * s)] float vel_limit = 20000.0f; // [counts/s] + float homing_speed = 2000.0f; // [counts/s] }; class Controller { @@ -31,6 +32,8 @@ public: void set_pos_setpoint(float pos_setpoint, float vel_feed_forward, float current_feed_forward); void set_vel_setpoint(float vel_setpoint, float current_feed_forward); void set_current_setpoint(float current_setpoint); + + bool home_axis(); // TODO: make this more similar to other calibration loops void start_anticogging_calibration(); @@ -83,7 +86,8 @@ public: make_protocol_property("pos_gain", &config_.pos_gain), make_protocol_property("vel_gain", &config_.vel_gain), make_protocol_property("vel_integrator_gain", &config_.vel_integrator_gain), - make_protocol_property("vel_limit", &config_.vel_limit) + make_protocol_property("vel_limit", &config_.vel_limit), + make_protocol_property("homing_speed", &config_.homing_speed) ), make_protocol_function("set_pos_setpoint", *this, &Controller::set_pos_setpoint, "pos_setpoint", @@ -94,7 +98,8 @@ public: "current_feed_forward"), make_protocol_function("set_current_setpoint", *this, &Controller::set_current_setpoint, "current_setpoint"), - make_protocol_function("start_anticogging_calibration", *this, &Controller::start_anticogging_calibration) + make_protocol_function("start_anticogging_calibration", *this, &Controller::start_anticogging_calibration), + make_protocol_function("home_axis", *this, &Controller::home_axis) ); } };