Skip to content

Instantly share code, notes, and snippets.

@samuelsadok
Created June 10, 2020 09:18
Show Gist options
  • Save samuelsadok/f1dbc3e2598d56127a6ff1464e2a2019 to your computer and use it in GitHub Desktop.
Save samuelsadok/f1dbc3e2598d56127a6ff1464e2a2019 to your computer and use it in GitHub Desktop.
// Context: https://github.com/madcowswe/ODrive/pull/419
#include <stdint.h>
struct HwConfig_t {
// [polynomial and ADC channel]
};
class ThermistorCurrentLimiter {
public:
virtual void update() = 0;
float get_current_limit(float user_limit) {
// [...]
}
float temp_;
};
class OnboardThermistorCurrentLimiter : public ThermistorCurrentLimiter {
public:
struct Config_t {
// [...]
// no fields for polynomial and gpio
};
OnboardThermistorCurrentLimiter(HwConfig_t hw_config)
: hw_config_(hw_config) {}
void update() override {
const float voltage = get_adc_voltage_channel(hw_config_.adc_channel_);
const float normalized_voltage = voltage / adc_full_scale;
temp_ = horner_fma(normalized_voltage,
hw_config_.thermistor_poly_coeffs.data(),
hw_config_.thermistor_poly_coeffs.size());
}
HwConfig_t hw_config_;
Config_t config_;
auto make_protocol_definitions() {
// [...]
}
};
class OffboardThermistorCurrentLimiter : public ThermistorCurrentLimiter {
public:
struct Config_t {
// [...]
// includes fields for polynomial and gpio
};
OffboardThermistorCurrentLimiter(Config_t& config)
: config_(config) {}
void update() override {
const float voltage = get_adc_voltage(config_.gpio_pin);
const float normalized_voltage = voltage / adc_full_scale;
temp_ = horner_fma(normalized_voltage,
config_.thermistor_poly_coeffs.data(),
config_.thermistor_poly_coeffs.size());
}
Config_t config_;
auto make_protocol_definitions() {
// [...]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment