Skip to content

Commit

Permalink
Merge pull request #507 from madcowswe/move-thermistors
Browse files Browse the repository at this point in the history
Change thermistors from top-level to Motor
  • Loading branch information
samuelsadok authored Oct 21, 2020
2 parents ad1b34b + fd848f2 commit 1683657
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 101 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Please add a note of your changes below this heading if you make a Pull Request.

### Changed

* Moved thermistors from being a top level object to belonging to Motor objects. Also changed errors: thermistor errors rolled into motor errors
* Use DMA for DRV8301 setup
* Make NVM configuration code more dynamic so that the layout doesn't have to be known at compile time.
* GPIO initialization logic was changed. GPIOs now need to be explicitly set to the mode corresponding to the feature that they are used by. See `<odrv>.config.gpioX_mode`.
* Previously, if two components used the same interrupt pin (e.g. step input for axis0 and axis1) then the one that was configured later would override the other one. Now this is no longer the case (the old component remains the owner of the pin).

### API Miration Notes
### API Migration Notes

* `odrive.axis.fet_thermistor`, `odrive.axis.motor_thermistor` moved to `odrive.axis.motor` object
* `enable_uart` and `uart_baudrate` were renamed to `enable_uart0` and `uart0_baudrate`.
* `enable_i2c_instead_of_can` was replaced by the separate settings `enable_i2c0` and `enable_can0`.
* `<axis>.motor.gate_driver` was moved to `<axis>.gate_driver`.
Expand Down
15 changes: 8 additions & 7 deletions Firmware/Board/v3/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,26 @@ OnboardThermistorCurrentLimiter fet_thermistors[AXIS_COUNT] = {
}
};

OffboardThermistorCurrentLimiter motor_thermistors[AXIS_COUNT];

Motor motors[AXIS_COUNT] = {
{
&htim1, // timer
TIM_1_8_PERIOD_CLOCKS, // control_deadline
1.0f / SHUNT_RESISTANCE, // shunt_conductance [S]
m0_gate_driver, // gate_driver
m0_gate_driver // opamp
m0_gate_driver, // opamp
fet_thermistors[0],
motor_thermistors[0]
},
{
&htim8, // timer
(3 * TIM_1_8_PERIOD_CLOCKS) / 2, // control_deadline
1.0f / SHUNT_RESISTANCE, // shunt_conductance [S]
m1_gate_driver, // gate_driver
m1_gate_driver // opamp
m1_gate_driver, // opamp
fet_thermistors[1],
motor_thermistors[1]
}
};

Expand Down Expand Up @@ -101,7 +107,6 @@ MechanicalBrake mechanical_brakes[AXIS_COUNT];
SensorlessEstimator sensorless_estimators[AXIS_COUNT];
Controller controllers[AXIS_COUNT];
TrapezoidalTrajectory trap[AXIS_COUNT];
OffboardThermistorCurrentLimiter motor_thermistors[AXIS_COUNT];

std::array<Axis, AXIS_COUNT> axes{{
{
Expand All @@ -112,8 +117,6 @@ std::array<Axis, AXIS_COUNT> axes{{
encoders[0], // encoder
sensorless_estimators[0], // sensorless_estimator
controllers[0], // controller
fet_thermistors[0], // fet_thermistor
motor_thermistors[0], // motor_thermistor
motors[0], // motor
trap[0], // trap
endstops[0], endstops[1], // min_endstop, max_endstop
Expand All @@ -132,8 +135,6 @@ std::array<Axis, AXIS_COUNT> axes{{
encoders[1], // encoder
sensorless_estimators[1], // sensorless_estimator
controllers[1], // controller
fet_thermistors[1], // fet_thermistor
motor_thermistors[1], // motor_thermistor
motors[1], // motor
trap[1], // trap
endstops[2], endstops[3], // min_endstop, max_endstop
Expand Down
22 changes: 3 additions & 19 deletions Firmware/MotorControl/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ Axis::Axis(int axis_num,
Encoder& encoder,
SensorlessEstimator& sensorless_estimator,
Controller& controller,
OnboardThermistorCurrentLimiter& fet_thermistor,
OffboardThermistorCurrentLimiter& motor_thermistor,
Motor& motor,
TrapezoidalTrajectory& trap,
Endstop& min_endstop,
Expand All @@ -28,25 +26,15 @@ Axis::Axis(int axis_num,
encoder_(encoder),
sensorless_estimator_(sensorless_estimator),
controller_(controller),
fet_thermistor_(fet_thermistor),
motor_thermistor_(motor_thermistor),
motor_(motor),
trap_traj_(trap),
min_endstop_(min_endstop),
max_endstop_(max_endstop),
mechanical_brake_(mechanical_brake),
current_limiters_(make_array(
static_cast<CurrentLimiter*>(&fet_thermistor),
static_cast<CurrentLimiter*>(&motor_thermistor))),
thermistors_(make_array(
static_cast<ThermistorCurrentLimiter*>(&fet_thermistor),
static_cast<ThermistorCurrentLimiter*>(&motor_thermistor)))
mechanical_brake_(mechanical_brake)
{
encoder_.axis_ = this;
sensorless_estimator_.axis_ = this;
controller_.axis_ = this;
fet_thermistor_.axis_ = this;
motor_thermistor.axis_ = this;
motor_.axis_ = this;
trap_traj_.axis_ = this;
min_endstop_.axis_ = this;
Expand Down Expand Up @@ -180,9 +168,6 @@ bool Axis::do_checks() {

// Sub-components should use set_error which will propegate to this error_
motor_.effective_current_lim();
for (ThermistorCurrentLimiter* thermistor : thermistors_) {
thermistor->do_checks();
}
motor_.do_checks();
// encoder_.do_checks();
// sensorless_estimator_.do_checks();
Expand All @@ -201,11 +186,10 @@ bool Axis::do_checks() {
// @brief Update all esitmators
bool Axis::do_updates() {
// Sub-components should use set_error which will propegate to this error_
for (ThermistorCurrentLimiter* thermistor : thermistors_) {
thermistor->update();
}
encoder_.update();
sensorless_estimator_.update();
motor_.fet_thermistor_.update();
motor_.motor_thermistor_.update();
min_endstop_.update();
max_endstop_.update();
bool ret = check_for_errors();
Expand Down
9 changes: 0 additions & 9 deletions Firmware/MotorControl/axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class Axis : public ODriveIntf::AxisIntf {
Encoder& encoder,
SensorlessEstimator& sensorless_estimator,
Controller& controller,
OnboardThermistorCurrentLimiter& fet_thermistor,
OffboardThermistorCurrentLimiter& motor_thermistor,
Motor& motor,
TrapezoidalTrajectory& trap,
Endstop& min_endstop,
Expand Down Expand Up @@ -227,19 +225,12 @@ class Axis : public ODriveIntf::AxisIntf {
Encoder& encoder_;
SensorlessEstimator& sensorless_estimator_;
Controller& controller_;
OnboardThermistorCurrentLimiter& fet_thermistor_;
OffboardThermistorCurrentLimiter& motor_thermistor_;
Motor& motor_;
TrapezoidalTrajectory& trap_traj_;
Endstop& min_endstop_;
Endstop& max_endstop_;
MechanicalBrake& mechanical_brake_;

// List of current_limiters and thermistors to
// provide easy iteration.
std::array<CurrentLimiter*, 2> current_limiters_;
std::array<ThermistorCurrentLimiter*, 2> thermistors_;

osThreadId thread_id_;
const uint32_t stack_size_ = 2048; // Bytes
volatile bool thread_id_valid_ = false;
Expand Down
13 changes: 7 additions & 6 deletions Firmware/MotorControl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ static bool config_read_all() {
config_manager.read(&axes[i].max_endstop_.config_) &&
config_manager.read(&axes[i].mechanical_brake_.config_) &&
config_manager.read(&motors[i].config_) &&
config_manager.read(&fet_thermistors[i].config_) &&
config_manager.read(&axes[i].motor_thermistor_.config_) &&
config_manager.read(&motors[i].fet_thermistor_.config_) &&
config_manager.read(&motors[i].motor_thermistor_.config_) &&
config_manager.read(&axes[i].config_);
}
return success;
Expand All @@ -70,8 +70,8 @@ static bool config_write_all() {
config_manager.write(&axes[i].max_endstop_.config_) &&
config_manager.write(&axes[i].mechanical_brake_.config_) &&
config_manager.write(&motors[i].config_) &&
config_manager.write(&fet_thermistors[i].config_) &&
config_manager.write(&axes[i].motor_thermistor_.config_) &&
config_manager.write(&motors[i].fet_thermistor_.config_) &&
config_manager.write(&motors[i].motor_thermistor_.config_) &&
config_manager.write(&axes[i].config_);
}
return success;
Expand All @@ -90,8 +90,8 @@ static void config_clear_all() {
axes[i].max_endstop_.config_ = {};
axes[i].mechanical_brake_.config_ = {};
motors[i].config_ = {};
fet_thermistors[i].config_ = {};
axes[i].motor_thermistor_.config_ = {};
motors[i].fet_thermistor_.config_ = {};
motors[i].motor_thermistor_.config_ = {};
axes[i].clear_config();
}
}
Expand All @@ -104,6 +104,7 @@ static bool config_apply_all() {
&& axes[i].min_endstop_.apply_config()
&& axes[i].max_endstop_.apply_config()
&& motors[i].apply_config()
&& motors[i].motor_thermistor_.apply_config()
&& axes[i].apply_config();
}
return success;
Expand Down
29 changes: 21 additions & 8 deletions Firmware/MotorControl/motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ Motor::Motor(TIM_HandleTypeDef* timer,
uint16_t control_deadline,
float shunt_conductance,
TGateDriver& gate_driver,
TOpAmp& opamp) :
TOpAmp& opamp,
OnboardThermistorCurrentLimiter& fet_thermistor,
OffboardThermistorCurrentLimiter& motor_thermistor) :
timer_(timer),
control_deadline_(control_deadline),
shunt_conductance_(shunt_conductance),
gate_driver_(gate_driver),
opamp_(opamp) {
opamp_(opamp),
fet_thermistor_(fet_thermistor),
motor_thermistor_(motor_thermistor) {
apply_config();
fet_thermistor_.motor_ = this;
motor_thermistor_.motor_ = this;
}

// @brief Arms the PWM outputs that belong to this motor.
Expand Down Expand Up @@ -110,7 +116,16 @@ bool Motor::do_checks() {
set_error(ERROR_DRV_FAULT);
return false;
}

if (!motor_thermistor_.do_checks()) {
axis_->error_ |= Axis::ERROR_MOTOR_FAILED;
set_error(ERROR_MOTOR_THERMISTOR_OVER_TEMP);
return false;
}
if (!fet_thermistor_.do_checks()) {
axis_->error_ |= Axis::ERROR_MOTOR_FAILED;
set_error(ERROR_FET_THERMISTOR_OVER_TEMP);
return false;
}
return true;
}

Expand All @@ -124,11 +139,9 @@ float Motor::effective_current_lim() {
current_lim = std::min(current_lim, axis_->motor_.current_control_.max_allowed_current);
}

// Apply axis current limiters
for (const CurrentLimiter* const limiter : axis_->current_limiters_) {
current_lim = std::min(current_lim, limiter->get_current_limit(config_.current_lim));
}

// Apply thermistor current limiters
current_lim = std::min(current_lim, motor_thermistor_.get_current_limit(config_.current_lim));
current_lim = std::min(current_lim, fet_thermistor_.get_current_limit(config_.current_lim));
effective_current_lim_ = current_lim;

return effective_current_lim_;
Expand Down
6 changes: 5 additions & 1 deletion Firmware/MotorControl/motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ class Motor : public ODriveIntf::MotorIntf {
uint16_t control_deadline,
float shunt_conductance,
TGateDriver& gate_driver,
TOpAmp& opamp);
TOpAmp& opamp,
OnboardThermistorCurrentLimiter& fet_thermistor,
OffboardThermistorCurrentLimiter& motor_thermistor);

bool arm();
void disarm();
Expand Down Expand Up @@ -130,6 +132,8 @@ class Motor : public ODriveIntf::MotorIntf {
const float shunt_conductance_;
TGateDriver& gate_driver_;
TOpAmp& opamp_;
OnboardThermistorCurrentLimiter& fet_thermistor_;
OffboardThermistorCurrentLimiter& motor_thermistor_;

Config_t config_;
Axis* axis_ = nullptr; // set by Axis constructor
Expand Down
11 changes: 7 additions & 4 deletions Firmware/MotorControl/thermistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ ThermistorCurrentLimiter::ThermistorCurrentLimiter(uint16_t adc_channel,
temperature_(NAN),
temp_limit_lower_(temp_limit_lower),
temp_limit_upper_(temp_limit_upper),
enabled_(enabled),
error_(ERROR_NONE)
enabled_(enabled)
{
}

Expand All @@ -27,8 +26,6 @@ void ThermistorCurrentLimiter::update() {

bool ThermistorCurrentLimiter::do_checks() {
if (enabled_ && temperature_ >= temp_limit_upper_ + 5) {
error_ = ERROR_OVER_TEMP;
axis_->error_ |= Axis::ERROR_OVER_TEMP;
return false;
}
return true;
Expand Down Expand Up @@ -70,6 +67,12 @@ OffboardThermistorCurrentLimiter::OffboardThermistorCurrentLimiter() :
decode_pin();
}

bool OffboardThermistorCurrentLimiter::apply_config() {
config_.parent = this;
decode_pin();
return true;
}

void OffboardThermistorCurrentLimiter::decode_pin() {
adc_channel_ = channel_from_gpio(get_gpio(config_.gpio_pin));
}
7 changes: 4 additions & 3 deletions Firmware/MotorControl/thermistor.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __THERMISTOR_HPP
#define __THERMISTOR_HPP

class Axis; // declared in axis.hpp
class Motor; // declared in motor.hpp

#include "current_limiter.hpp"
#include <autogen/interfaces.hpp>
Expand All @@ -28,8 +28,7 @@ class ThermistorCurrentLimiter : public CurrentLimiter, public ODriveIntf::Therm
const float& temp_limit_lower_;
const float& temp_limit_upper_;
const bool& enabled_;
Error error_;
Axis* axis_ = nullptr; // set by Axis constructor
Motor* motor_ = nullptr; // set by Motor::apply_config()
};

class OnboardThermistorCurrentLimiter : public ThermistorCurrentLimiter, public ODriveIntf::OnboardThermistorCurrentLimiterIntf {
Expand Down Expand Up @@ -68,6 +67,8 @@ class OffboardThermistorCurrentLimiter : public ThermistorCurrentLimiter, public

Config_t config_;

bool apply_config();

private:
void decode_pin();
};
Expand Down
Loading

0 comments on commit 1683657

Please sign in to comment.