Skip to content

Instantly share code, notes, and snippets.

@samuelsadok
Created March 14, 2019 20:36
Show Gist options
  • Save samuelsadok/c4d85ed257c711d124445115dde54900 to your computer and use it in GitHub Desktop.
Save samuelsadok/c4d85ed257c711d124445115dde54900 to your computer and use it in GitHub Desktop.
#include <stm32_adc.hpp>
#include <stm32_can.hpp>
#include <stm32_gpio.hpp>
#include <stm32_i2c.hpp>
#include <stm32_spi.hpp>
#include <stm32_usart.hpp>
#include <stm32_usb.hpp>
#include <drv8301.hpp>
const float thermistor_poly_coeffs[] =
{363.93910201f, -462.15369634f, 307.55129571f, -27.72569531f};
const size_t thermistor_num_coeffs = sizeof(thermistor_poly_coeffs)/sizeof(thermistor_poly_coeffs[0]);
#if HW_VERSION_MAJOR == 3
# if HW_VERSION_MINOR <= 3
# define SHUNT_RESISTANCE (675e-6f)
# else
# define SHUNT_RESISTANCE (500e-6f)
# endif
#else
# error "unknown shunt resistance"
#endif
#if HW_VERSION_VOLTAGE == 48
# define VBUS_S_DIVIDER_RATIO 19.0f
# define VBUS_OVERVOLTAGE_LEVEL 52.0f
#elif HW_VERSION_VOLTAGE == 24
# define VBUS_S_DIVIDER_RATIO 11.0f
# define VBUS_OVERVOLTAGE_LEVEL 26.0f
#else
# error "unknown board voltage"
#endif
STM32_ADCChannel_t adc_vbus_sense = adc1_regular.get_channel(&pa6);
VoltageDivider_t vbus_sense(&adc_vbus_sense, VBUS_S_DIVIDER_RATIO);
STM32_GPIO_t* gpios[] = { &pa0, &pa1, &pa2, &pa3, &pc4, &pb2, &pa15, &pb3 };
DRV8301_t gate_driver_m0(
&spi3,
&pc13, // chip select
&pb12, // enable (shared across both motors)
&pd2 // nFault (shared across both motors)
);
DRV8301_t gate_driver_m1(
&spi3,
&pc14, // chip select
&pb12, // enable (shared across both motors)
&pd2 // nFault (shared across both motors)
);
STM32_ADCChannel_t adc_m0_b = adc2_injected.get_channel(&pc0);
STM32_ADCChannel_t adc_m0_c = adc3_injected.get_channel(&pc1);
STM32_ADCChannel_t adc_m1_b = adc2_regular.get_channel(&pc3);
STM32_ADCChannel_t adc_m1_c = adc3_regular.get_channel(&pc2);
Shunt_t current_sensor_m0_b(&adc_m0_b, &gate_driver_m0, 1.0f / SHUNT_RESISTANCE);
Shunt_t current_sensor_m0_c(&adc_m0_c, &gate_driver_m0, 1.0f / SHUNT_RESISTANCE);
DerivedCurrentSensor_t<2> current_sensor_m0_a({&current_sensor_m0_b, &current_sensor_m0_c});
Shunt_t current_sensor_m1_b(&adc_m1_b, &gate_driver_m1, 1.0f / SHUNT_RESISTANCE);
Shunt_t current_sensor_m1_c(&adc_m1_c, &gate_driver_m1, 1.0f / SHUNT_RESISTANCE);
DerivedCurrentSensor_t<2> current_sensor_m1_a({&current_sensor_m1_b, &current_sensor_m1_c});
STM32_ADCChannel_t adc_m0_inv_temp = adc1_regular.get_channel(&pc5);
STM32_ADCChannel_t adc_m1_inv_temp = adc1_regular.get_channel(&pa4);
Thermistor_t temp_sensor_m0_inv(&adc_m0_inv_temp, thermistor_poly_coeffs, thermistor_num_coeffs);
Thermistor_t temp_sensor_m1_inv(&adc_m1_inv_temp, thermistor_poly_coeffs, thermistor_num_coeffs);
Axis axes[2] = {
Axis(
Motor(
&tim1,
&gate_driver_m0, // gate_driver_a
&gate_driver_m0, // gate_driver_b
&gate_driver_m0, // gate_driver_c
&current_sensor_m0_a, // current_sensor_a
&current_sensor_m0_b, // current_sensor_b
&current_sensor_m0_c, // current_sensor_c
temp_sensor_m0_inv // inverter_thermistor
),
Encoder(
&tim3, // counter
&pc9, // index_gpio
&pb4, // hallA_gpio
&pb5, // hallB_gpio
&pc9 // hallC_gpio (same as index pin)
),
SensorlessEstimator(),
Controller(),
TrapezoidalTrajectory(),
1, // default_step_gpio_num
2 // default_dir_gpio_num
),
Axis(
Motor(
&tim8,
&gate_driver_m1, // gate_driver_a
&gate_driver_m1, // gate_driver_b
&gate_driver_m1, // gate_driver_c
&current_sensor_m1_a, // current_sensor_a
&current_sensor_m1_b, // current_sensor_b
&current_sensor_m1_c, // current_sensor_c
temp_sensor_m1_inv // inverter_thermistor
),
Encoder(
&tim4, // counter
&pc15, // index_gpio
&pb6, // hallA_gpio
&pb7, // hallB_gpio
&pc15 // hallC_gpio (same as index pin)
),
SensorlessEstimator(),
Controller(),
TrapezoidalTrajectory(),
#if HW_VERSION_MAJOR == 3 && HW_VERSION_MINOR >= 5
7, // default_step_gpio_num
8 // default_dir_gpio_num
#else
3, // default_step_gpio_num
4 // default_dir_gpio_num
#endif
)
};
constexpr size_t AXIS_COUNT = sizeof(axes) / sizeof(axes[0]);
size_t n_axes = AXIS_COUNT;
GPIO_t* i2c_a0_gpio = gpios[2];
GPIO_t* i2c_a1_gpio = gpios[3];
GPIO_t* i2c_a2_gpio = gpios[4];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment