fix opamp instance

fix voltage scale in utils.cpp
fix switch case for pins in _readADCVoltageInline

added commented out lines for my motor config, changed PID values, added global monitor variables near loop
This commit is contained in:
copper280z
2023-11-15 08:52:30 -05:00
parent 37d5e4b64c
commit fef9087a00
4 changed files with 45 additions and 23 deletions

View File

@@ -26,6 +26,10 @@ void MX_ADC1_Init(void)
hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE; hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16;
hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_4;
hadc1.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc1.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE;
if (HAL_ADC_Init(&hadc1) != HAL_OK) if (HAL_ADC_Init(&hadc1) != HAL_OK)
SIMPLEFOC_DEBUG("HAL ADC1 Init fail."); SIMPLEFOC_DEBUG("HAL ADC1 Init fail.");
@@ -74,6 +78,10 @@ void MX_ADC2_Init(void)
hadc2.Init.DMAContinuousRequests = ENABLE; hadc2.Init.DMAContinuousRequests = ENABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE; hadc2.Init.OversamplingMode = DISABLE;
hadc2.Init.Oversampling.Ratio = ADC_OVERSAMPLING_RATIO_16;
hadc2.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_4;
hadc2.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc2.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE;
if (HAL_ADC_Init(&hadc2) != HAL_OK) if (HAL_ADC_Init(&hadc2) != HAL_OK)
SIMPLEFOC_DEBUG("HAL ADC2 init failed!"); SIMPLEFOC_DEBUG("HAL ADC2 init failed!");

View File

@@ -26,7 +26,7 @@ void configureOPAMPs(void)
if(HAL_OPAMP_Init(&hopamp2) != HAL_OK) if(HAL_OPAMP_Init(&hopamp2) != HAL_OK)
SIMPLEFOC_DEBUG("OPAMP2 init failed."); SIMPLEFOC_DEBUG("OPAMP2 init failed.");
hopamp3.Instance = OPAMP2; hopamp3.Instance = OPAMP3;
hopamp3.Init.PowerMode = OPAMP_POWERMODE_NORMALSPEED; hopamp3.Init.PowerMode = OPAMP_POWERMODE_NORMALSPEED;
hopamp3.Init.Mode = OPAMP_FOLLOWER_MODE; hopamp3.Init.Mode = OPAMP_FOLLOWER_MODE;
hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO1; hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO1;

View File

@@ -7,8 +7,8 @@
#include "opamp.h" #include "opamp.h"
float adcResolution = 4096.0f; // 12 bit ADC float adcResolution = 4096.0f; // 12 bit ADC
float voltageScale = 3.3f; // full scale voltage range of ADC float voltageScale = 2.9f; // full scale voltage range of ADC
float adcSens = adcResolution * voltageScale; float adcSens = voltageScale / adcResolution;
volatile uint16_t adc1Result[3] = {0}; volatile uint16_t adc1Result[3] = {0};
volatile uint16_t adc2Result[2] = {0}; volatile uint16_t adc2Result[2] = {0};
@@ -28,24 +28,24 @@ void MX_GPIO_Init(void)
float _readADCVoltageInline(const int pin, const void *cs_params) float _readADCVoltageInline(const int pin, const void *cs_params)
{ {
// SIMPLEFOC_DEBUG("READ ADC VOLTAGE"); // SIMPLEFOC_DEBUG("READ ADC VOLTAGE");
uint32_t rawResult; uint16_t rawResult;
switch (pin) switch (pin)
{ {
case PA2: case PA3:
rawResult = adc1Result[1]; rawResult = adc1Result[0];
break; break;
case PA3: case PB13:
rawResult = adc1Result[0]; // ADC1 CH13 -> Vopamp1 internal output rawResult = adc2Result[0]; // ADC1 CH13 -> Vopamp1 internal output
break; break;
case PB0: case PB0:
rawResult = adc2Result[0]; // ADC2 CH16 -> Vopamp2 internal output rawResult = adc2Result[1]; // ADC2 CH16 -> Vopamp2 internal output
break; break;
case PA13: // case PA13:
rawResult = adc2Result[2]; // ADC2 CH18 -> Vopamp3 internal output // rawResult = adc2Result[2]; // ADC2 CH18 -> Vopamp3 internal output
break; // break;
// case PA2: // case PA2:
// rawResult = adc1Result[1]; // ADC1 CH16 -> not sure what pin should represent this? // rawResult = adc1Result[1]; // ADC1 CH16 -> not sure what pin should represent this?

View File

@@ -61,9 +61,12 @@ STM32HWEncoder enc = STM32HWEncoder(ENC_PPR, ENC_A, ENC_B, ENC_Z);
* The current sense amps have a gain of 90mA/V -> over 1.5A this is 135mA so we need gain of 24 to get full-scale. * The current sense amps have a gain of 90mA/V -> over 1.5A this is 135mA so we need gain of 24 to get full-scale.
* Actually we are limited to powers of 2 for gain. So it should be 16. This gives sensitivity of 1440mV/A. * Actually we are limited to powers of 2 for gain. So it should be 16. This gives sensitivity of 1440mV/A.
* */ * */
InlineCurrentSenseSync currentsense = InlineCurrentSenseSync(1440, ISENSE_U, ISENSE_V); InlineCurrentSenseSync currentsense = InlineCurrentSenseSync(90, ISENSE_U, ISENSE_V, ISENSE_W);
StepperDriver4PWM driver = StepperDriver4PWM(MOT_A1, MOT_A2, MOT_B1, MOT_B2); StepperDriver4PWM driver = StepperDriver4PWM(MOT_A1, MOT_A2, MOT_B1, MOT_B2);
// StepperDriver4PWM driver = StepperDriver4PWM(PA0, PA9, PA1, PA10, PB12);
// StepperDriver4PWM driver = StepperDriver4PWM(MOT_A1, MOT_A2, MOT_B1, MOT_B2);
// StepperMotor motor = StepperMotor(POLEPAIRS, RPHASE, MOTORKV, 0.0045);
StepperMotor motor = StepperMotor(POLEPAIRS); StepperMotor motor = StepperMotor(POLEPAIRS);
Commander commander = Commander(SERIALPORT); Commander commander = Commander(SERIALPORT);
@@ -145,17 +148,26 @@ void setup()
// } // }
} }
DQCurrent_s foc_currents;
float electrical_angle;
PhaseCurrent_s phase_currents;
void loop() void loop()
{ {
motor.loopFOC(); motor.loopFOC();
motor.move(); motor.move();
commander.run(); commander.run();
if(counter == 0){ electrical_angle = motor.electricalAngle();
digitalToggle(LED_GOOD); phase_currents = currentsense.getPhaseCurrents();
Serial.println(adc1Result[0]); foc_currents = currentsense.getFOCCurrents(electrical_angle);
}
if(counter == 0xFFF){
digitalToggle(LED_GOOD);
// Serial.println(adc1Result[0]);
counter = 0;
}
counter++;
#ifdef HAS_MONITOR #ifdef HAS_MONITOR
motor.monitor(); motor.monitor();
#endif #endif
@@ -211,15 +223,17 @@ uint8_t configureFOC(void)
driver.init(); driver.init();
// Motor PID parameters. // Motor PID parameters.
motor.PID_velocity.P = 5; motor.PID_velocity.P = 0.035;
motor.PID_velocity.I = 24; motor.PID_velocity.I = 0.01;
motor.PID_velocity.D = 0.01; motor.PID_velocity.D = 0.000;
motor.LPF_velocity.Tf = 0.004;
motor.PID_velocity.output_ramp = 750; motor.PID_velocity.output_ramp = 750;
motor.PID_velocity.limit = 500; motor.PID_velocity.limit = 500;
motor.LPF_velocity.Tf = 4;
motor.P_angle.P = 600; motor.P_angle.P = 350;
motor.P_angle.limit = 10000; motor.P_angle.I = 8;
motor.P_angle.D = 0.3;
motor.LPF_angle = 0.001;
motor.LPF_angle.Tf = 0; // try to avoid motor.LPF_angle.Tf = 0; // try to avoid
// Motor initialization. // Motor initialization.