I am using the B-G431B-ESC1, and I’ve been able to get closed-loop control to work decently well. While trying to make it better, I wanted to implement current sensing and the foc_current torque controller. Here is my code:
#include <SimpleFOC.h>
// C:\Users\[user name]\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.5.0\platform.txt
// Changed build.extra_flags= to build.extra_flags=-D HAL_OPAMP_MODULE_ENABLED
MagneticSensorI2C as5600 = MagneticSensorI2C(0x36, 12, 0x0E, 4);
BLDCMotor motor = BLDCMotor(7);
BLDCDriver6PWM driver = BLDCDriver6PWM(A_PHASE_UH, A_PHASE_UL, A_PHASE_VH, A_PHASE_VL, A_PHASE_WH, A_PHASE_WL);
LowsideCurrentSense currentSense = LowsideCurrentSense(0.003f, -64.0f/7.0f, A_OP1_OUT, A_OP2_OUT, A_OP3_OUT);
float targetSpeed = 0;
Commander command = Commander(Serial);
void doMotor(char* cmd) { command.motor(&motor, cmd); }
void setup() {
// init magnetic sensor hardware
as5600.init();
motor.linkSensor(&as5600);
driver.voltage_power_supply = 16;
driver.init();
motor.linkDriver(&driver);
// link current sense and the driver
currentSense.linkDriver(&driver);
// current sensing
currentSense.init();
// no need for aligning
currentSense.skip_align = true;
motor.linkCurrentSense(¤tSense);
// aligning voltage [V]
motor.voltage_sensor_align = 3;
// index search velocity [rad/s]
motor.velocity_index_search = 3;
// Control loop setup
motor.controller = MotionControlType::angle;
motor.torque_controller = TorqueControlType::foc_current;
//motor.motion_downsample
// velocity PI controller parameters
motor.PID_velocity.P = 0.2;
motor.PID_velocity.I = 20;
// default voltage_power_supply
motor.voltage_limit = 8;
// jerk control using voltage voltage ramp
// default value is 300 volts per sec ~ 0.3V per millisecond
motor.PID_velocity.output_ramp = 1000;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.01;
// angle P controller
motor.P_angle.P = 20;
// maximal velocity of the position control
motor.current_limit = 30; // Amps
motor.velocity_limit = 4; // [rad/s] 5 rad/s cca 50rpm
Serial.begin(115200);
command.add('M',doMotor,"motor");
motor.useMonitoring(Serial);
motor.init();
motor.initFOC();
Serial.println(motor.zero_electric_angle);
Serial.println("Motor ready!");
_delay(1000);
}
void loop() {
as5600.update();
float pot = (float)analogRead(A_POTENTIOMETER);
float target = ((-(pot-(1023/2))/100)*3)/5;
motor.loopFOC();
motor.move(target);
motor.monitor();
command.run();
}
Even when I comment out everything besides the initialization line LowsideCurrentSense currentSense = LowsideCurrentSense(0.003f, -64.0f/7.0f, A_OP1_OUT, A_OP2_OUT, A_OP3_OUT);
, I still get the following error:
c:/users/jb1ds/appdata/local/arduino15/packages/stmicroelectronics/tools/xpack-arm-none-eabi-gcc/12.2.1-1.2/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\jb1ds\AppData\Local\Temp\arduino\sketches\B5591D0D06726E0579F61F27F8B05ECE\libraries\Simple_FOC\current_sense\hardware_specific\stm32\b_g431\objs.a(b_g431_mcu.cpp.o): in function `_configureOPAMP(OPAMP_HandleTypeDef*, OPAMP_TypeDef*)':
b_g431_mcu.cpp:(.text._Z15_configureOPAMPP19OPAMP_HandleTypeDefP13OPAMP_TypeDef+0x22): undefined reference to `HAL_OPAMP_Init'
c:/users/jb1ds/appdata/local/arduino15/packages/stmicroelectronics/tools/xpack-arm-none-eabi-gcc/12.2.1-1.2/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\jb1ds\AppData\Local\Temp\arduino\sketches\B5591D0D06726E0579F61F27F8B05ECE\libraries\Simple_FOC\current_sense\hardware_specific\stm32\b_g431\objs.a(b_g431_mcu.cpp.o): in function `_configureADCLowSide(void const*, int, int, int)':
b_g431_mcu.cpp:(.text._Z20_configureADCLowSidePKviii+0x68): undefined reference to `HAL_OPAMP_Start'
c:/users/jb1ds/appdata/local/arduino15/packages/stmicroelectronics/tools/xpack-arm-none-eabi-gcc/12.2.1-1.2/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: b_g431_mcu.cpp:(.text._Z20_configureADCLowSidePKviii+0x6e): undefined reference to `HAL_OPAMP_Start'
c:/users/jb1ds/appdata/local/arduino15/packages/stmicroelectronics/tools/xpack-arm-none-eabi-gcc/12.2.1-1.2/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: b_g431_mcu.cpp:(.text._Z20_configureADCLowSidePKviii+0x74): undefined reference to `HAL_OPAMP_Start'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
As noted in the comment in the third line of my code, I went to C:\Users\jb1ds\AppData\Local\Arduino15\packages\STMicroelectronics\hardware\stm32\2.5.0\platform.txt
and changed build.extra_flags=
to build.extra_flags=-D HAL_OPAMP_MODULE_ENABLED
.
Any help is greatly appreciated, thank you,
Joseph