From da7f7192833e0f02501378e1505989ed3fa3a137 Mon Sep 17 00:00:00 2001 From: Samuel Sadok Date: Thu, 14 May 2020 11:02:36 +0200 Subject: [PATCH] improve naming consistency between code and fibre Consistent naming makes code autogeneration easier. This commit does not claim that the exported names of the variables were more sensible than the in-code names. However changing the exported names can break external tools and needs to happen in a controlled and documented way. --- Firmware/MotorControl/axis.cpp | 16 ++++----- Firmware/MotorControl/axis.hpp | 30 ++++++++-------- Firmware/MotorControl/controller.cpp | 34 +++++++++---------- Firmware/MotorControl/controller.hpp | 32 ++++++++--------- Firmware/MotorControl/encoder.cpp | 2 +- Firmware/MotorControl/encoder.hpp | 8 ++--- Firmware/MotorControl/low_level.cpp | 2 +- Firmware/MotorControl/motor.cpp | 2 +- Firmware/MotorControl/motor.hpp | 8 ++--- .../MotorControl/sensorless_estimator.hpp | 6 ++-- Firmware/Tests/test_can.cpp | 6 ++-- Firmware/communication/ascii_protocol.cpp | 8 ++--- Firmware/communication/can_simple.cpp | 10 +++--- Firmware/communication/interface_can.cpp | 12 +++---- Firmware/communication/interface_can.hpp | 12 +++---- docs/commands.md | 8 ++--- docs/getting-started.md | 8 ++--- docs/hoverboard.md | 2 +- docs/input_modes.md | 18 +++++----- tools/odrive/enums.py | 8 ++--- tools/odrive/tests/can_test.py | 4 +-- tools/odrive/tests/closed_loop_test.py | 8 ++--- tools/odrive/tests/endstop_test.py | 2 +- tools/odrive/tests/uart_ascii_test.py | 8 ++--- tools/setup_hall_as_index.py | 2 +- 25 files changed, 128 insertions(+), 128 deletions(-) diff --git a/Firmware/MotorControl/axis.cpp b/Firmware/MotorControl/axis.cpp index b68ef01b..c1d11a4b 100644 --- a/Firmware/MotorControl/axis.cpp +++ b/Firmware/MotorControl/axis.cpp @@ -25,7 +25,7 @@ Axis::Axis(int axis_num, sensorless_estimator_(sensorless_estimator), controller_(controller), motor_(motor), - trap_(trap), + trap_traj_(trap), min_endstop_(min_endstop), max_endstop_(max_endstop) { @@ -33,7 +33,7 @@ Axis::Axis(int axis_num, sensorless_estimator_.axis_ = this; controller_.axis_ = this; motor_.axis_ = this; - trap_.axis_ = this; + trap_traj_.axis_ = this; min_endstop_.axis_ = this; max_endstop_.axis_ = this; decode_step_dir_pins(); @@ -173,7 +173,7 @@ bool Axis::do_checks() { // controller_.do_checks(); // Check for endstop presses - bool vel_dependent_stopping = (current_state_ == AXIS_STATE_HOMING) && (controller_.config_.control_mode >= Controller::CTRL_MODE_VELOCITY_CONTROL); + bool vel_dependent_stopping = (current_state_ == AXIS_STATE_HOMING) && (controller_.config_.control_mode >= Controller::CONTROL_MODE_VELOCITY_CONTROL); if (min_endstop_.config_.enabled && min_endstop_.get_state() && (!vel_dependent_stopping || controller_.vel_setpoint_ < 0.0f)) { error_ |= ERROR_MIN_ENDSTOP_PRESSED; } else if (max_endstop_.config_.enabled && max_endstop_.get_state() && (!vel_dependent_stopping || controller_.vel_setpoint_ > 0.0f)) { @@ -331,8 +331,8 @@ bool Axis::run_closed_loop_control_loop() { // Slowly drive in the negative direction at homing_speed until the min endstop is pressed // When pressed, set the linear count to the offset (default 0), and then go to position 0 bool Axis::run_homing() { - Controller::ControlMode_t stored_control_mode = controller_.config_.control_mode; - Controller::InputMode_t stored_input_mode = controller_.config_.input_mode; + Controller::ControlMode stored_control_mode = controller_.config_.control_mode; + Controller::InputMode stored_input_mode = controller_.config_.input_mode; // TODO: theoretically this check should be inside the update loop, // otherwise someone could disable the endstop while homing is in progress. @@ -340,7 +340,7 @@ bool Axis::run_homing() { return error_ |= ERROR_HOMING_WITHOUT_ENDSTOP, false; } - controller_.config_.control_mode = Controller::CTRL_MODE_VELOCITY_CONTROL; + controller_.config_.control_mode = Controller::CONTROL_MODE_VELOCITY_CONTROL; controller_.config_.input_mode = Controller::INPUT_MODE_VEL_RAMP; controller_.input_pos_ = 0.0f; @@ -381,7 +381,7 @@ bool Axis::run_homing() { // Set our current position in encoder counts to make control more logical encoder_.set_linear_count(static_cast(controller_.pos_setpoint_)); - controller_.config_.control_mode = Controller::CTRL_MODE_POSITION_CONTROL; + controller_.config_.control_mode = Controller::CONTROL_MODE_POSITION_CONTROL; controller_.config_.input_mode = Controller::INPUT_MODE_TRAP_TRAJ; controller_.input_pos_ = 0.0f; @@ -499,7 +499,7 @@ void Axis::run_state_machine_loop() { case AXIS_STATE_LOCKIN_SPIN: { if (!motor_.is_calibrated_ || motor_.config_.direction==0) goto invalid_state_label; - status = run_lockin_spin(config_.lockin); + status = run_lockin_spin(config_.general_lockin); } break; case AXIS_STATE_SENSORLESS_CONTROL: { diff --git a/Firmware/MotorControl/axis.hpp b/Firmware/MotorControl/axis.hpp index 5b489a93..63925753 100644 --- a/Firmware/MotorControl/axis.hpp +++ b/Firmware/MotorControl/axis.hpp @@ -7,7 +7,7 @@ class Axis { public: - enum Error_t { + enum Error { ERROR_NONE = 0x00, ERROR_INVALID_STATE = 0x01, //error_ |= Axis::ERROR_CONTROLLER_FAILED; } @@ -50,11 +50,11 @@ bool Controller::select_encoder(size_t encoder_num) { } void Controller::move_to_pos(float goal_point) { - axis_->trap_.planTrapezoidal(goal_point, pos_setpoint_, vel_setpoint_, - axis_->trap_.config_.vel_limit, - axis_->trap_.config_.accel_limit, - axis_->trap_.config_.decel_limit); - axis_->trap_.t_ = 0.0f; + axis_->trap_traj_.planTrapezoidal(goal_point, pos_setpoint_, vel_setpoint_, + axis_->trap_traj_.config_.vel_limit, + axis_->trap_traj_.config_.accel_limit, + axis_->trap_traj_.config_.decel_limit); + axis_->trap_traj_.t_ = 0.0f; trajectory_done_ = false; } @@ -90,7 +90,7 @@ bool Controller::anticogging_calibration(float pos_estimate, float vel_estimate) config_.anticogging.cogging_map[std::clamp(config_.anticogging.index++, 0, 3600)] = vel_integrator_current_; } if (config_.anticogging.index < 3600) { - config_.control_mode = CTRL_MODE_POSITION_CONTROL; + config_.control_mode = CONTROL_MODE_POSITION_CONTROL; input_pos_ = config_.anticogging.index * axis_->encoder_.getCoggingRatio(); input_vel_ = 0.0f; input_current_ = 0.0f; @@ -98,7 +98,7 @@ bool Controller::anticogging_calibration(float pos_estimate, float vel_estimate) return false; } else { config_.anticogging.index = 0; - config_.control_mode = CTRL_MODE_POSITION_CONTROL; + config_.control_mode = CONTROL_MODE_POSITION_CONTROL; input_pos_ = 0.0f; // Send the motor home input_vel_ = 0.0f; input_current_ = 0.0f; @@ -200,19 +200,19 @@ bool Controller::update(float* current_setpoint_output) { if (trajectory_done_) break; - if (axis_->trap_.t_ > axis_->trap_.Tf_) { + if (axis_->trap_traj_.t_ > axis_->trap_traj_.Tf_) { // Drop into position control mode when done to avoid problems on loop counter delta overflow - config_.control_mode = CTRL_MODE_POSITION_CONTROL; + config_.control_mode = CONTROL_MODE_POSITION_CONTROL; pos_setpoint_ = input_pos_; vel_setpoint_ = 0.0f; current_setpoint_ = 0.0f; trajectory_done_ = true; } else { - TrapezoidalTrajectory::Step_t traj_step = axis_->trap_.eval(axis_->trap_.t_); + TrapezoidalTrajectory::Step_t traj_step = axis_->trap_traj_.eval(axis_->trap_traj_.t_); pos_setpoint_ = traj_step.Y; vel_setpoint_ = traj_step.Yd; current_setpoint_ = traj_step.Ydd * config_.inertia; - axis_->trap_.t_ += current_meas_period; + axis_->trap_traj_.t_ += current_meas_period; } anticogging_pos = pos_setpoint_; // FF the position setpoint instead of the pos_estimate } break; @@ -227,7 +227,7 @@ bool Controller::update(float* current_setpoint_output) { // TODO Decide if we want to use encoder or pll position here float gain_scheduling_multiplier = 1.0f; float vel_des = vel_setpoint_; - if (config_.control_mode >= CTRL_MODE_POSITION_CONTROL) { + if (config_.control_mode >= CONTROL_MODE_POSITION_CONTROL) { float pos_err; if (!pos_estimate_src) { set_error(ERROR_INVALID_ESTIMATE); @@ -292,12 +292,12 @@ bool Controller::update(float* current_setpoint_output) { // Anti-cogging is enabled after calibration // We get the current position and apply a current feed-forward // ensuring that we handle negative encoder positions properly (-1 == motor->encoder.encoder_cpr - 1) - if (anticogging_valid_ && config_.anticogging.enable) { + if (anticogging_valid_ && config_.anticogging.anticogging_enabled) { Iq += config_.anticogging.cogging_map[std::clamp(mod(static_cast(anticogging_pos), 3600), 0, 3600)]; } float v_err = 0.0f; - if (config_.control_mode >= CTRL_MODE_VELOCITY_CONTROL) { + if (config_.control_mode >= CONTROL_MODE_VELOCITY_CONTROL) { if (!vel_estimate_src) { set_error(ERROR_INVALID_ESTIMATE); return false; @@ -311,7 +311,7 @@ bool Controller::update(float* current_setpoint_output) { } // Velocity limiting in current mode - if (config_.control_mode < CTRL_MODE_VELOCITY_CONTROL && config_.enable_current_vel_limit) { + if (config_.control_mode < CONTROL_MODE_VELOCITY_CONTROL && config_.enable_current_mode_vel_limit) { if (!vel_estimate_src) { set_error(ERROR_INVALID_ESTIMATE); return false; @@ -334,7 +334,7 @@ bool Controller::update(float* current_setpoint_output) { } // Velocity integrator (behaviour dependent on limiting) - if (config_.control_mode < CTRL_MODE_VELOCITY_CONTROL) { + if (config_.control_mode < CONTROL_MODE_VELOCITY_CONTROL) { // reset integral if not in use vel_integrator_current_ = 0.0f; } else { diff --git a/Firmware/MotorControl/controller.hpp b/Firmware/MotorControl/controller.hpp index 56a60020..293b892a 100644 --- a/Firmware/MotorControl/controller.hpp +++ b/Firmware/MotorControl/controller.hpp @@ -7,7 +7,7 @@ class Controller { public: - enum Error_t { + enum Error { ERROR_NONE = 0, ERROR_OVERSPEED = 0x01, ERROR_INVALID_INPUT_MODE = 0x02, @@ -19,14 +19,14 @@ public: // Note: these should be sorted from lowest level of control to // highest level of control, to allow "<" style comparisons. - enum ControlMode_t{ - CTRL_MODE_VOLTAGE_CONTROL = 0, - CTRL_MODE_CURRENT_CONTROL = 1, - CTRL_MODE_VELOCITY_CONTROL = 2, - CTRL_MODE_POSITION_CONTROL = 3 + enum ControlMode{ + CONTROL_MODE_VOLTAGE_CONTROL = 0, + CONTROL_MODE_CURRENT_CONTROL = 1, + CONTROL_MODE_VELOCITY_CONTROL = 2, + CONTROL_MODE_POSITION_CONTROL = 3 }; - enum InputMode_t{ + enum InputMode{ INPUT_MODE_INACTIVE, INPUT_MODE_PASSTHROUGH, INPUT_MODE_VEL_RAMP, @@ -45,12 +45,12 @@ public: float calib_pos_threshold = 1.0f; float calib_vel_threshold = 1.0f; float cogging_ratio = 1.0f; - bool enable = true; + bool anticogging_enabled = true; } Anticogging_t; struct Config_t { - ControlMode_t control_mode = CTRL_MODE_POSITION_CONTROL; //see: ControlMode_t - InputMode_t input_mode = INPUT_MODE_PASSTHROUGH; //see: InputMode_t + ControlMode control_mode = CONTROL_MODE_POSITION_CONTROL; //see: ControlMode + InputMode input_mode = INPUT_MODE_PASSTHROUGH; //see: InputMode float pos_gain = 20.0f; // [(counts/s) / counts] float vel_gain = 5.0f / 10000.0f; // [A/(counts/s)] // float vel_gain = 5.0f / 200.0f, // [A/(rad/s)] @@ -68,7 +68,7 @@ public: bool enable_gain_scheduling = false; bool enable_vel_limit = true; bool enable_overspeed_error = true; - bool enable_current_vel_limit = true; // enable velocity limit in current control mode (requires a valid velocity estimator) + bool enable_current_mode_vel_limit = true; // enable velocity limit in current control mode (requires a valid velocity estimator) uint8_t axis_to_mirror = -1; float mirror_ratio = 1.0f; uint8_t load_encoder_axis = -1; // default depends on Axis number and is set in load_configuration() @@ -76,7 +76,7 @@ public: explicit Controller(Config_t& config); void reset(); - void set_error(Error_t error); + void set_error(Error error); void input_pos_updated(); bool select_encoder(size_t encoder_num); @@ -95,7 +95,7 @@ public: Config_t& config_; Axis* axis_ = nullptr; // set by Axis constructor - Error_t error_ = ERROR_NONE; + Error error_ = ERROR_NONE; float* pos_estimate_src_ = nullptr; bool* pos_estimate_valid_src_ = nullptr; @@ -138,7 +138,7 @@ public: make_protocol_property("gain_scheduling_width", &config_.gain_scheduling_width), make_protocol_object("config", make_protocol_property("enable_vel_limit", &config_.enable_vel_limit), - make_protocol_property("enable_current_mode_vel_limit", &config_.enable_current_vel_limit), + make_protocol_property("enable_current_mode_vel_limit", &config_.enable_current_mode_vel_limit), make_protocol_property("enable_gain_scheduling", &config_.enable_gain_scheduling), make_protocol_property("enable_overspeed_error", &config_.enable_overspeed_error), make_protocol_property("control_mode", &config_.control_mode), @@ -164,13 +164,13 @@ public: make_protocol_property("calib_pos_threshold", &config_.anticogging.calib_pos_threshold), make_protocol_property("calib_vel_threshold", &config_.anticogging.calib_vel_threshold), make_protocol_ro_property("cogging_ratio", &config_.anticogging.cogging_ratio), - make_protocol_property("anticogging_enabled", &config_.anticogging.enable))), + make_protocol_property("anticogging_enabled", &config_.anticogging.anticogging_enabled))), make_protocol_function("move_incremental", *this, &Controller::move_incremental, "displacement", "from_goal_point"), make_protocol_function("start_anticogging_calibration", *this, &Controller::start_anticogging_calibration) ); } }; -DEFINE_ENUM_FLAG_OPERATORS(Controller::Error_t) +DEFINE_ENUM_FLAG_OPERATORS(Controller::Error) #endif // __CONTROLLER_HPP diff --git a/Firmware/MotorControl/encoder.cpp b/Firmware/MotorControl/encoder.cpp index 77b809aa..c21683e7 100644 --- a/Firmware/MotorControl/encoder.cpp +++ b/Firmware/MotorControl/encoder.cpp @@ -35,7 +35,7 @@ void Encoder::setup() { } } -void Encoder::set_error(Error_t error) { +void Encoder::set_error(Error error) { vel_estimate_valid_ = false; pos_estimate_valid_ = false; error_ |= error; diff --git a/Firmware/MotorControl/encoder.hpp b/Firmware/MotorControl/encoder.hpp index 28830c38..77ad84b1 100644 --- a/Firmware/MotorControl/encoder.hpp +++ b/Firmware/MotorControl/encoder.hpp @@ -7,7 +7,7 @@ class Encoder { public: - enum Error_t { + enum Error { ERROR_NONE = 0, ERROR_UNSTABLE_GAIN = 0x01, ERROR_CPR_POLEPAIRS_MISMATCH = 0x02, @@ -59,7 +59,7 @@ public: Config_t& config, const Motor::Config_t& motor_config); void setup(); - void set_error(Error_t error); + void set_error(Error error); bool do_checks(); void enc_index_cb(); @@ -81,7 +81,7 @@ public: Config_t& config_; Axis* axis_ = nullptr; // set by Axis constructor - Error_t error_ = ERROR_NONE; + Error error_ = ERROR_NONE; bool index_found_ = false; bool is_ready_ = false; int32_t shadow_count_ = 0; @@ -171,6 +171,6 @@ public: } }; -DEFINE_ENUM_FLAG_OPERATORS(Encoder::Error_t) +DEFINE_ENUM_FLAG_OPERATORS(Encoder::Error) #endif // __ENCODER_HPP diff --git a/Firmware/MotorControl/low_level.cpp b/Firmware/MotorControl/low_level.cpp index fda04cf4..f0e32f0c 100644 --- a/Firmware/MotorControl/low_level.cpp +++ b/Firmware/MotorControl/low_level.cpp @@ -84,7 +84,7 @@ static uint16_t GPIO_port_samples [2][num_GPIO]; */ // @brief Floats ALL phases immediately and disarms both motors and the brake resistor. -void low_level_fault(Motor::Error_t error) { +void low_level_fault(Motor::Error error) { // Disable all motors NOW! for (size_t i = 0; i < AXIS_COUNT; ++i) { safety_critical_disarm_motor_pwm(axes[i]->motor_); diff --git a/Firmware/MotorControl/motor.cpp b/Firmware/MotorControl/motor.cpp index 223aba1c..6b8c8d0f 100644 --- a/Firmware/MotorControl/motor.cpp +++ b/Firmware/MotorControl/motor.cpp @@ -137,7 +137,7 @@ bool Motor::check_DRV_fault() { return true; } -void Motor::set_error(Motor::Error_t error){ +void Motor::set_error(Motor::Error error){ error_ |= error; axis_->error_ |= Axis::ERROR_MOTOR_FAILED; safety_critical_disarm_motor_pwm(*this); diff --git a/Firmware/MotorControl/motor.hpp b/Firmware/MotorControl/motor.hpp index 8c627aa7..67a0751d 100644 --- a/Firmware/MotorControl/motor.hpp +++ b/Firmware/MotorControl/motor.hpp @@ -9,7 +9,7 @@ class Motor { public: - enum Error_t { + enum Error { ERROR_NONE = 0, ERROR_PHASE_RESISTANCE_OUT_OF_RANGE = 0x0001, ERROR_PHASE_INDUCTANCE_OUT_OF_RANGE = 0x0002, @@ -128,7 +128,7 @@ public: void update_current_controller_gains(); void DRV8301_setup(); bool check_DRV_fault(); - void set_error(Error_t error); + void set_error(Error error); bool do_checks(); float get_inverter_temp(); bool update_thermal_limits(float fet_temp); @@ -163,7 +163,7 @@ public: uint16_t timing_log_[TIMING_LOG_NUM_SLOTS] = { 0 }; // variables exposed on protocol - Error_t error_ = ERROR_NONE; + Error error_ = ERROR_NONE; // Do not write to this variable directly! // It is for exclusive use by the safety_critical_... functions. ArmedState_t armed_state_ = ARMED_STATE_DISARMED; @@ -279,6 +279,6 @@ public: } }; -DEFINE_ENUM_FLAG_OPERATORS(Motor::Error_t) +DEFINE_ENUM_FLAG_OPERATORS(Motor::Error) #endif // __MOTOR_HPP diff --git a/Firmware/MotorControl/sensorless_estimator.hpp b/Firmware/MotorControl/sensorless_estimator.hpp index e47db893..dd48c705 100644 --- a/Firmware/MotorControl/sensorless_estimator.hpp +++ b/Firmware/MotorControl/sensorless_estimator.hpp @@ -3,7 +3,7 @@ class SensorlessEstimator { public: - enum Error_t { + enum Error { ERROR_NONE = 0, ERROR_UNSTABLE_GAIN = 0x01, }; @@ -22,7 +22,7 @@ public: Config_t& config_; // TODO: expose on protocol - Error_t error_ = ERROR_NONE; + Error error_ = ERROR_NONE; float phase_ = 0.0f; // [rad] float pll_pos_ = 0.0f; // [rad] float vel_estimate_ = 0.0f; // [rad/s] @@ -51,6 +51,6 @@ public: } }; -DEFINE_ENUM_FLAG_OPERATORS(SensorlessEstimator::Error_t) +DEFINE_ENUM_FLAG_OPERATORS(SensorlessEstimator::Error) #endif /* __SENSORLESS_ESTIMATOR_HPP */ diff --git a/Firmware/Tests/test_can.cpp b/Firmware/Tests/test_can.cpp index ef2ffecf..5a0db2fe 100644 --- a/Firmware/Tests/test_can.cpp +++ b/Firmware/Tests/test_can.cpp @@ -5,7 +5,7 @@ #include "communication/can_helpers.hpp" -enum InputMode_t { +enum InputMode { INPUT_MODE_INACTIVE, INPUT_MODE_PASSTHROUGH, INPUT_MODE_VEL_RAMP, @@ -84,7 +84,7 @@ TEST_SUITE("CAN Functions") { can_Message_t rxmsg; rxmsg.buf[0] = INPUT_MODE_MIX_CHANNELS; rxmsg.buf[1] = INPUT_MODE_PASSTHROUGH; - CHECK(static_cast(can_getSignal(rxmsg, 0, 8, true, 1, 0)) == INPUT_MODE_MIX_CHANNELS); - CHECK(static_cast(can_getSignal(rxmsg, 8, 8, true, 1, 0)) == INPUT_MODE_PASSTHROUGH); + CHECK(static_cast(can_getSignal(rxmsg, 0, 8, true, 1, 0)) == INPUT_MODE_MIX_CHANNELS); + CHECK(static_cast(can_getSignal(rxmsg, 8, 8, true, 1, 0)) == INPUT_MODE_PASSTHROUGH); } } \ No newline at end of file diff --git a/Firmware/communication/ascii_protocol.cpp b/Firmware/communication/ascii_protocol.cpp index f0ee9f9c..628dea63 100644 --- a/Firmware/communication/ascii_protocol.cpp +++ b/Firmware/communication/ascii_protocol.cpp @@ -97,7 +97,7 @@ void ASCII_protocol_process_line(const uint8_t* buffer, size_t len, StreamSink& respond(response_channel, use_checksum, "invalid motor %u", motor_number); } else { Axis* axis = axes[motor_number]; - axis->controller_.config_.control_mode = Controller::CTRL_MODE_POSITION_CONTROL; + axis->controller_.config_.control_mode = Controller::CONTROL_MODE_POSITION_CONTROL; axis->controller_.input_pos_ = pos_setpoint; if (numscan >= 3) axis->controller_.input_vel_ = vel_feed_forward; @@ -117,7 +117,7 @@ void ASCII_protocol_process_line(const uint8_t* buffer, size_t len, StreamSink& respond(response_channel, use_checksum, "invalid motor %u", motor_number); } else { Axis* axis = axes[motor_number]; - axis->controller_.config_.control_mode = Controller::CTRL_MODE_POSITION_CONTROL; + axis->controller_.config_.control_mode = Controller::CONTROL_MODE_POSITION_CONTROL; axis->controller_.input_pos_ = pos_setpoint; if (numscan >= 3) axis->controller_.config_.vel_limit = vel_limit; @@ -137,7 +137,7 @@ void ASCII_protocol_process_line(const uint8_t* buffer, size_t len, StreamSink& respond(response_channel, use_checksum, "invalid motor %u", motor_number); } else { Axis* axis = axes[motor_number]; - axis->controller_.config_.control_mode = Controller::CTRL_MODE_VELOCITY_CONTROL; + axis->controller_.config_.control_mode = Controller::CONTROL_MODE_VELOCITY_CONTROL; axis->controller_.input_vel_ = vel_setpoint; if (numscan >= 3) axis->controller_.input_current_ = current_feed_forward; @@ -154,7 +154,7 @@ void ASCII_protocol_process_line(const uint8_t* buffer, size_t len, StreamSink& respond(response_channel, use_checksum, "invalid motor %u", motor_number); } else { Axis* axis = axes[motor_number]; - axis->controller_.config_.control_mode = Controller::CTRL_MODE_CURRENT_CONTROL; + axis->controller_.config_.control_mode = Controller::CONTROL_MODE_CURRENT_CONTROL; axis->controller_.input_current_ = current_setpoint; axis->watchdog_feed(); } diff --git a/Firmware/communication/can_simple.cpp b/Firmware/communication/can_simple.cpp index da22bb70..5ff8009a 100644 --- a/Firmware/communication/can_simple.cpp +++ b/Firmware/communication/can_simple.cpp @@ -295,8 +295,8 @@ void CANSimple::set_input_current_callback(Axis* axis, can_Message_t& msg) { } void CANSimple::set_controller_modes_callback(Axis* axis, can_Message_t& msg) { - axis->controller_.config_.control_mode = static_cast(can_getSignal(msg, 0, 32, true)); - axis->controller_.config_.input_mode = static_cast(can_getSignal(msg, 32, 32, true)); + axis->controller_.config_.control_mode = static_cast(can_getSignal(msg, 0, 32, true)); + axis->controller_.config_.input_mode = static_cast(can_getSignal(msg, 32, 32, true)); } void CANSimple::set_vel_limit_callback(Axis* axis, can_Message_t& msg) { @@ -308,12 +308,12 @@ void CANSimple::start_anticogging_callback(Axis* axis, can_Message_t& msg) { } void CANSimple::set_traj_vel_limit_callback(Axis* axis, can_Message_t& msg) { - axis->trap_.config_.vel_limit = can_getSignal(msg, 0, 32, true); + axis->trap_traj_.config_.vel_limit = can_getSignal(msg, 0, 32, true); } void CANSimple::set_traj_accel_limits_callback(Axis* axis, can_Message_t& msg) { - axis->trap_.config_.accel_limit = can_getSignal(msg, 0, 32, true); - axis->trap_.config_.decel_limit = can_getSignal(msg, 32, 32, true); + axis->trap_traj_.config_.accel_limit = can_getSignal(msg, 0, 32, true); + axis->trap_traj_.config_.decel_limit = can_getSignal(msg, 32, 32, true); } void CANSimple::set_traj_A_per_css_callback(Axis* axis, can_Message_t& msg) { diff --git a/Firmware/communication/interface_can.cpp b/Firmware/communication/interface_can.cpp index bf7f9ff0..339ef9ff 100644 --- a/Firmware/communication/interface_can.cpp +++ b/Firmware/communication/interface_can.cpp @@ -57,7 +57,7 @@ static void can_server_thread_wrapper(void *ctx) { bool ODriveCAN::start_can_server() { HAL_StatusTypeDef status; - set_baud_rate(config_.baud); + set_baud_rate(config_.baud_rate); status = HAL_CAN_Init(handle_); @@ -136,25 +136,25 @@ void ODriveCAN::set_baud_rate(uint32_t baudRate) { switch (baudRate) { case CAN_BAUD_125K: handle_->Init.Prescaler = 16; // 21 TQ's - config_.baud = baudRate; + config_.baud_rate = baudRate; reinit_can(); break; case CAN_BAUD_250K: handle_->Init.Prescaler = 8; // 21 TQ's - config_.baud = baudRate; + config_.baud_rate = baudRate; reinit_can(); break; case CAN_BAUD_500K: handle_->Init.Prescaler = 4; // 21 TQ's - config_.baud = baudRate; + config_.baud_rate = baudRate; reinit_can(); break; case CAN_BAUD_1000K: handle_->Init.Prescaler = 2; // 21 TQ's - config_.baud = baudRate; + config_.baud_rate = baudRate; reinit_can(); break; @@ -172,7 +172,7 @@ void ODriveCAN::reinit_can() { status = HAL_CAN_ActivateNotification(handle_, CAN_IT_RX_FIFO0_MSG_PENDING); } -void ODriveCAN::set_error(Error_t error) { +void ODriveCAN::set_error(Error error) { error_ |= error; } // This function is called by each axis. diff --git a/Firmware/communication/interface_can.hpp b/Firmware/communication/interface_can.hpp index ffb29bb7..fca28822 100644 --- a/Firmware/communication/interface_can.hpp +++ b/Firmware/communication/interface_can.hpp @@ -26,11 +26,11 @@ enum CAN_Protocol_t { class ODriveCAN { public: struct Config_t { - uint32_t baud = CAN_BAUD_250K; + uint32_t baud_rate = CAN_BAUD_250K; CAN_Protocol_t protocol = CAN_PROTOCOL_SIMPLE; }; - enum Error_t { + enum Error { ERROR_NONE = 0x00, ERROR_DUPLICATE_CAN_IDS = 0x01 }; @@ -40,7 +40,7 @@ class ODriveCAN { // Thread Relevant Data osThreadId thread_id_; const uint32_t stack_size_ = 1024; // Bytes - Error_t error_ = ERROR_NONE; + Error error_ = ERROR_NONE; volatile bool thread_id_valid_ = false; bool start_can_server(); @@ -48,7 +48,7 @@ class ODriveCAN { void send_heartbeat(Axis *axis); void reinit_can(); - void set_error(Error_t error); + void set_error(Error error); // I/O Functions uint32_t available(); @@ -60,7 +60,7 @@ class ODriveCAN { return make_protocol_member_list( make_protocol_property("error", &error_), make_protocol_object("config", - make_protocol_ro_property("baud_rate", &config_.baud)), + make_protocol_ro_property("baud_rate", &config_.baud_rate)), make_protocol_property("can_protocol", &config_.protocol), make_protocol_function("set_baud_rate", *this, &ODriveCAN::set_baud_rate, "baudRate")); } @@ -73,6 +73,6 @@ class ODriveCAN { }; -DEFINE_ENUM_FLAG_OPERATORS(ODriveCAN::Error_t) +DEFINE_ENUM_FLAG_OPERATORS(ODriveCAN::Error) #endif // __INTERFACE_CAN_HPP diff --git a/docs/commands.md b/docs/commands.md index 33f7eefc..24fb3fdc 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -55,10 +55,10 @@ See [state machine](#state-machine) for a description of each state. The default control mode is position control. If you want a different mode, you can change `.controller.config.control_mode`. Possible values are: -* `CTRL_MODE_POSITION_CONTROL` -* `CTRL_MODE_VELOCITY_CONTROL` -* `CTRL_MODE_CURRENT_CONTROL` -* `CTRL_MODE_VOLTAGE_CONTROL` - this one is not normally used. +* `CONTROL_MODE_POSITION_CONTROL` +* `CONTROL_MODE_VELOCITY_CONTROL` +* `CONTROL_MODE_CURRENT_CONTROL` +* `CONTROL_MODE_VOLTAGE_CONTROL` - this one is not normally used. ### Input Mode The default input mode is `INPUT_MODE_PASSTHROUGH`. diff --git a/docs/getting-started.md b/docs/getting-started.md index 05fd2f94..3287dcab 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -337,20 +337,20 @@ Note that in this mode `encoder.pos_cpr` is used for feedback in stead of `encod If you try to increment the axis with a large step in one go that exceeds `cpr/2` steps, the motor will go to the same angle around the wrong way. This is also the case if there is a large disturbance. If you have an application where you would like to handle larger steps, you can use a virtual CPR that is an integer times larger than your encoder's actual CPR. Set `encoder.config.cpr = N * your_enc_cpr`, where N is some integer. Choose N to give you an appropriate circular space for your application. ### Velocity control -Set `axis.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL`.
+Set `axis.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL`.
You can now control the velocity with `axis.controller.input_vel = 5000` [count/s]. ### Ramped velocity control -Set `axis.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL`.
+Set `axis.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL`.
Set the velocity ramp rate (acceleration): `axis.controller.config.vel_ramp_rate = 2000` [counts/s^2]
Activate the ramped velocity mode: `axis.controller.config.input_mode = INPUT_MODE_VEL_RAMP`.
You can now control the velocity with `axis.controller.input_vel = 5000` [count/s]. ### Current control -Set `axis.controller.config.control_mode = CTRL_MODE_CURRENT_CONTROL`.
+Set `axis.controller.config.control_mode = CONTROL_MODE_CURRENT_CONTROL`.
You can now control the current with `axis.controller.input_current = 3` [A]. -Note: If you exceed `vel_limit` in current control mode, the current is reduced. To disable this, set `axis.controller.enable_current_vel_limit = False`. +Note: If you exceed `vel_limit` in current control mode, the current is reduced. To disable this, set `axis.controller.enable_current_mode_vel_limit = False`. ## Watchdog Timer Each axis has a configurable watchdog timer that can stop the motors if the diff --git a/docs/hoverboard.md b/docs/hoverboard.md index 91b50764..93938613 100644 --- a/docs/hoverboard.md +++ b/docs/hoverboard.md @@ -52,7 +52,7 @@ odrv0.axis0.controller.config.pos_gain = 1 odrv0.axis0.controller.config.vel_gain = 0.02 odrv0.axis0.controller.config.vel_integrator_gain = 0.1 odrv0.axis0.controller.config.vel_limit = 1000 -odrv0.axis0.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL +odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL ``` In the next step we are going to start powering the motor and so we want to make sure that some of the above settings that require a reboot are applied first. diff --git a/docs/input_modes.md b/docs/input_modes.md index 32d7df86..be449e24 100644 --- a/docs/input_modes.md +++ b/docs/input_modes.md @@ -30,10 +30,10 @@ Pass `input_xxx` through to `xxx_setpoint` directly. * `input_current` ### Valid Control modes: -* `CTRL_MODE_VOLTAGE_CONTROL` -* `CTRL_MODE_CURRENT_CONTROL` -* `CTRL_MODE_VELOCITY_CONTROL` -* `CTRL_MODE_POSITION_CONTROL` +* `CONTROL_MODE_VOLTAGE_CONTROL` +* `CONTROL_MODE_CURRENT_CONTROL` +* `CONTROL_MODE_VELOCITY_CONTROL` +* `CONTROL_MODE_POSITION_CONTROL` ## INPUT_MODE_VEL_RAMP Ramps a velocity command from the current value to the target value. @@ -46,7 +46,7 @@ Ramps a velocity command from the current value to the target value. * `input_vel` ### Valid Control Modes: -* `CTRL_MODE_VELOCITY_CONTROL` +* `CONTROL_MODE_VELOCITY_CONTROL` ## INPUT_MODE_POS_FILTER Implements a 2nd order position tracking filter. Inteded for use with step/dir interface, but can also be used with position-only commands. @@ -62,7 +62,7 @@ Result of a step command from 1000 to 0 * `input_pos` ### Valid Control modes: -* `CTRL_MODE_POSITION_CONTROL` +* `CONTROL_MODE_POSITION_CONTROL` ## INPUT_MODE_MIX_CHANNELS Not Implemented. @@ -83,7 +83,7 @@ Implementes an online trapezoidal trajectory planner. * `input_pos` ### Valid Control Modes: -* `CTRL_MODE_POSITION_CONTROL` +* `CONTROL_MODE_POSITION_CONTROL` ## INPUT_MODE_CURRENT_RAMP Ramp a current command from the current value to the target value. @@ -95,7 +95,7 @@ Ramp a current command from the current value to the target value. * `input_current` ### Valid Control Modes: -* `CTRL_MODE_CURRENT_CONTROL` +* `CONTROL_MODE_CURRENT_CONTROL` ## INPUT_MODE_MIRROR Implements "electronic mirroring". This is like electronic camming, but you can only mirror exactly the movements of the other motor, according to a fixed ratio @@ -110,4 +110,4 @@ Implements "electronic mirroring". This is like electronic camming, but you can * None. Inputs are taken directly from the other axis encoder estimates ### Valid Control modes -* `CTRL_MODE_POSITION_CONTROL` +* `CONTROL_MODE_POSITION_CONTROL` diff --git a/tools/odrive/enums.py b/tools/odrive/enums.py index 1198272e..e3d3ebd0 100644 --- a/tools/odrive/enums.py +++ b/tools/odrive/enums.py @@ -75,10 +75,10 @@ MOTOR_TYPE_HIGH_CURRENT = 0 #MOTOR_TYPE_LOW_CURRENT = 1 MOTOR_TYPE_GIMBAL = 2 -CTRL_MODE_VOLTAGE_CONTROL = 0 -CTRL_MODE_CURRENT_CONTROL = 1 -CTRL_MODE_VELOCITY_CONTROL = 2 -CTRL_MODE_POSITION_CONTROL = 3 +CONTROL_MODE_VOLTAGE_CONTROL = 0 +CONTROL_MODE_CURRENT_CONTROL = 1 +CONTROL_MODE_VELOCITY_CONTROL = 2 +CONTROL_MODE_POSITION_CONTROL = 3 INPUT_MODE_INACTIVE = 0 INPUT_MODE_PASSTHROUGH = 1 diff --git a/tools/odrive/tests/can_test.py b/tools/odrive/tests/can_test.py index e69a8bd9..164dd251 100644 --- a/tools/odrive/tests/can_test.py +++ b/tools/odrive/tests/can_test.py @@ -171,13 +171,13 @@ class TestSimpleCAN(): test_assert_eq(axis.controller.input_vel, 2.0, range=0.01) test_assert_eq(axis.controller.input_current, 3.0, range=0.001) - axis.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL + axis.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL my_cmd('set_input_vel', input_vel=-10.0, cur_ff=30.1234) fence() test_assert_eq(axis.controller.input_vel, -10.0, range=0.01) test_assert_eq(axis.controller.input_current, 30.1234, range=0.01) - axis.controller.config.control_mode = CTRL_MODE_CURRENT_CONTROL + axis.controller.config.control_mode = CONTROL_MODE_CURRENT_CONTROL my_cmd('set_input_current', input_current=3.1415) fence() test_assert_eq(axis.controller.input_current, 3.1415, range=0.01) diff --git a/tools/odrive/tests/closed_loop_test.py b/tools/odrive/tests/closed_loop_test.py index cc07ab2a..01cc003a 100644 --- a/tools/odrive/tests/closed_loop_test.py +++ b/tools/odrive/tests/closed_loop_test.py @@ -80,7 +80,7 @@ class TestClosedLoopControl(TestClosedLoopControlBase): nominal_vel = float(enc_ctx.yaml['cpr']) * nominal_rps logger.debug(f'Testing closed loop velocity control at {nominal_rps} rounds/s...') - axis_ctx.handle.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL + axis_ctx.handle.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL axis_ctx.handle.controller.config.input_mode = INPUT_MODE_PASSTHROUGH axis_ctx.handle.controller.input_vel = 0 @@ -107,7 +107,7 @@ class TestClosedLoopControl(TestClosedLoopControlBase): logger.debug(f'Testing closed loop position control...') - axis_ctx.handle.controller.config.control_mode = CTRL_MODE_POSITION_CONTROL + axis_ctx.handle.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL axis_ctx.handle.controller.input_pos = 0 axis_ctx.handle.controller.config.vel_limit = float(enc_ctx.yaml['cpr']) * 5.0 # max 5 rps axis_ctx.handle.encoder.set_linear_count(0) @@ -181,7 +181,7 @@ class TestRegenProtection(TestClosedLoopControlBase): logger.debug(f'Brake control test from {nominal_rps} rounds/s...') axis_ctx.handle.controller.config.vel_limit = float(enc_ctx.yaml['cpr']) * 10.0 # max 10 rps - axis_ctx.handle.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL + axis_ctx.handle.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL axis_ctx.handle.controller.config.input_mode = INPUT_MODE_PASSTHROUGH request_state(axis_ctx, AXIS_STATE_CLOSED_LOOP_CONTROL) @@ -231,7 +231,7 @@ class TestVelLimitInCurrentControl(TestClosedLoopControlBase): axis_ctx.handle.controller.config.vel_limit = max_vel axis_ctx.handle.controller.config.vel_limit_tolerance = inf # disable hard limit on velocity axis_ctx.handle.motor.config.current_lim = max_current - axis_ctx.handle.controller.config.control_mode = CTRL_MODE_CURRENT_CONTROL + axis_ctx.handle.controller.config.control_mode = CONTROL_MODE_CURRENT_CONTROL # Returns the expected limited setpoint for a given velocity and current def get_expected_setpoint(input_setpoint, velocity): diff --git a/tools/odrive/tests/endstop_test.py b/tools/odrive/tests/endstop_test.py index a823549c..fd07ae8d 100644 --- a/tools/odrive/tests/endstop_test.py +++ b/tools/odrive/tests/endstop_test.py @@ -7,7 +7,7 @@ odrv0 = odrive.find_any() print('Odrive found') odrv0.axis1.controller.config.vel_limit = 50000 -odrv0.axis1.controller.config.control_mode = CTRL_MODE_POSITION_CONTROL +odrv0.axis1.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL odrv0.axis1.controller.config.input_mode = INPUT_MODE_PASSTHROUGH odrv0.axis1.encoder.config.cpr = 2400 odrv0.axis1.encoder.config.bandwidth = 1000 diff --git a/tools/odrive/tests/uart_ascii_test.py b/tools/odrive/tests/uart_ascii_test.py index 91d61709..17cf85f8 100644 --- a/tools/odrive/tests/uart_ascii_test.py +++ b/tools/odrive/tests/uart_ascii_test.py @@ -96,7 +96,7 @@ class TestUartAscii(): ser.write(b'c 0 12.5\n') test_assert_eq(ser.readline(), b'') test_assert_eq(odrive.handle.axis0.controller.input_current, 12.5, accuracy=0.001) - test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CTRL_MODE_CURRENT_CONTROL) + test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CONTROL_MODE_CURRENT_CONTROL) odrive.handle.axis0.controller.input_vel = 0 odrive.handle.axis0.controller.input_current = 0 @@ -104,7 +104,7 @@ class TestUartAscii(): test_assert_eq(ser.readline(), b'') test_assert_eq(odrive.handle.axis0.controller.input_vel, 567.8, accuracy=0.001) test_assert_eq(odrive.handle.axis0.controller.input_current, 12.5, accuracy=0.001) - test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CTRL_MODE_VELOCITY_CONTROL) + test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CONTROL_MODE_VELOCITY_CONTROL) odrive.handle.axis0.controller.input_pos = 0 odrive.handle.axis0.controller.input_vel = 0 @@ -114,7 +114,7 @@ class TestUartAscii(): test_assert_eq(odrive.handle.axis0.controller.input_pos, 123.4, accuracy=0.001) test_assert_eq(odrive.handle.axis0.controller.input_vel, 567.8, accuracy=0.001) test_assert_eq(odrive.handle.axis0.controller.input_current, 12.5, accuracy=0.001) - test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CTRL_MODE_POSITION_CONTROL) + test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CONTROL_MODE_POSITION_CONTROL) odrive.handle.axis0.controller.input_pos = 0 odrive.handle.axis0.controller.config.vel_limit = 0 @@ -124,7 +124,7 @@ class TestUartAscii(): test_assert_eq(odrive.handle.axis0.controller.input_pos, 123.4, accuracy=0.001) test_assert_eq(odrive.handle.axis0.controller.config.vel_limit, 567.8, accuracy=0.001) test_assert_eq(odrive.handle.axis0.motor.config.current_lim, 12.5, accuracy=0.001) - test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CTRL_MODE_POSITION_CONTROL) + test_assert_eq(odrive.handle.axis0.controller.config.control_mode, CONTROL_MODE_POSITION_CONTROL) ser.write(b'f 0\n') response = ser.readline().strip() diff --git a/tools/setup_hall_as_index.py b/tools/setup_hall_as_index.py index c01de4ff..3c21f2cb 100644 --- a/tools/setup_hall_as_index.py +++ b/tools/setup_hall_as_index.py @@ -30,7 +30,7 @@ for ax in axes: ax.encoder.config.find_idx_on_lockin_only = True ax.encoder.config.idx_search_unidirectional = True - ax.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL + ax.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL ax.controller.config.vel_limit = 10000 ax.controller.config.vel_gain = 0.002205736003816127 ax.controller.config.vel_integrator_gain = 0.022057360038161278