Current sensing stspin32g4

Hello! I started dealing with current sensing using my stspin32g4, platformIO, genericSTM32G431VB.json and i have some doubts I hope you can help me with :slight_smile:

I’m using low side sensing, according to stspin32g4 datasheet:

– Op-amp 1: PA1, PA3, PA2 (OPP, OPN, OPO)
– Op-amp 2: PA7, PC5, PA6 (OPP, OPN, OPO)
– Op-amp 3: PB0, PB2, PB1 (OPP, OPN, OPO)

  1. I don’t know actually how to configure OPAMPS, i guess it should be some mode that uses all 3 pins (because I use external resistors working with opamp), but also obviously output should be connected to ADC channel. I dont see mode like that in CubeMX, am I right?

  2. That resistors configuration provides half of voltage on output when there is no current, is it what library expect or is there possibility to write my own callback or sth to send current value?

  3. I tried simply read some analog value and get 10 bits value, stm has 12 bits ADC, what can i do about it?

I think you can configure it… configure the op-amp, and then click on the pin. You should see an option like ‘ADCx INy +’ (with a plus). Choosing this should add the ADC input on top of the OPAMP configuration.

The library will calibrate the 0-offset on initialisation. So you can put it at any voltage you like. You should take care, of course, with the gain, to keep the voltage in range of the STM32s capabilities.

I believe the library will set 12bits, I have to check TBH. The library has its completely own ADC code, separate from Arduino analogRead(). When using Arduino analogRead() you can use setADCResolution(12); first to set the analog resolution.

So what you’re doing makes a lot of sense, but the library’s standard SMT32G4 current sensing code does not support the op-amps. You will have to configure them yourself.
If the current sensing will then just work without problems after you do the configuration, I don’t know, TBH. You will have to try it out, and it might turn out that you have to modify the library code a little bit.

You can also look at our current sensing driver for the B-G431-ESC1. We have a specific driver for this board because it uses the op-amps. Perhaps you can use or adapt this driver code.

Okay, I initialized omamps as standalone and on outputs i see 1,65V, so looks good, although with this mode I cant connect it with ADC using cubemx.

Is it ok?:

LowsideCurrentSense current_sense  = LowsideCurrentSense(0.005, (11.0/1.5+1), PA2, PA6, PB1);

I mean using names PA2 etc, because i got error:

STM32-CS: ERR: Analog pins dont belong to the same ADC!

or maybe it’s a consequence of what I mentioned

actually opamp1 can be connected to different adc than opamp2 and opamp3, but tried also:

LowsideCurrentSense current_sense  = LowsideCurrentSense(0.005, (11.0/1.5+1), _NC, PA6, PB1);

and didn’t help

yes, the driver currently requires the pins to be all on the same ADC :-(. The B-G431-ESC1 driver is different in this regard.

How about this one:

LowsideCurrentSense current_sense = LowsideCurrentSense(0.005, (11.0/1.5+1), PA6, PB1, _NC);

You can then also change the order of the PWM pins to match…

Does this help?

i’m still getting the same error

Actually, I think you’re using the wrong pin combination.

PA2 → ADC1 channel 3
PA6 → ADC2 channel 3
PB1 → ADC1 channel 12

So you have to use pins PA2 and PB1 together, and leave out PA6…

LowsideCurrentSense current_sense = LowsideCurrentSense(0.005, (11.0/1.5+1), PA2, PB1, _NC);

Yeah, my fault, of course PA2 and PB1 are connected to same ADC

now it measures :wink:

1 Like

Would you like to take a look if something is wrong with the code? motor doesn’t move to target, in general behaves werid, sometimes jerks, vibrates, most time doesn’t move :F (velocity open loop works good)

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"
#include "drivers/stspin32g4/STSPIN32G4.h"
#include "stm32g4xx_hal.h"
#include "HardwareSerial.h"
#include "mu150/mu150.h"
#include "opamps/opamps.h"

unsigned long previousMillis = 0;

STSPIN32G4 driver = STSPIN32G4();
BLDCMotor motor = BLDCMotor(7);
GenericSensor sensor = GenericSensor( mu150_read_sdad, mu150_init );
LowsideCurrentSense current_sense = LowsideCurrentSense(0.005, 1/0.03666, PA2, _NC, PB1);

void setup() {

    opamps_init();

    Serial1.begin(115200);
    sensor.init();
    motor.linkSensor(&sensor);
    SimpleFOCDebug::enable(&Serial1);
    driver.voltage_power_supply = 12.0f;
    driver.voltage_limit = 2.0f;
    driver.init();
    current_sense.linkDriver(&driver);
    motor.voltage_limit = driver.voltage_limit/2.0f;

    motor.PID_velocity.P = 0.2;
    motor.PID_velocity.I = 20;
    motor.PID_velocity.D = 0.001;
    motor.PID_velocity.output_ramp = 1000;
    motor.LPF_velocity.Tf = 0.01;

    motor.P_angle.P = 20; 
    motor.P_angle.I = 0;  // usually only P controller is enough 
    motor.P_angle.D = 0;  // usually only P controller is enough 
    motor.P_angle.output_ramp = 10000; // default 1e6 rad/s^2
    motor.LPF_angle.Tf = 0; // default 0
    motor.velocity_limit = 4; // rad/s - default 20

    motor.controller = MotionControlType::angle;
    motor.linkDriver(&driver);
    motor.init();
    current_sense.init();
    motor.linkCurrentSense(&current_sense);
    motor.initFOC();  
    motor.enable();
    motor.useMonitoring(Serial1);
    motor.monitor_variables = 0b1111111;
    pinMode(PC2, OUTPUT);

    motor.target=2;

}

void loop(){

  motor.loopFOC();
  motor.move();
  motor.monitor();

}



How is the movement in torque-voltage mode? Be sure to set a voltage limit when testing!

Have you tuned the PIDs for velocity and angle control? You can’t jump directly from open loop to angle mode, you have to make sure torque-voltage mode is working well (this is the basic closed loop mode) and then the PIDs need to be tuned.
For current control you also need to tune the current PIDs…

Okay, thanks for your answer

I tried with

    motor.torque_controller = TorqueControlType::voltage;
    motor.controller = MotionControlType::torque;

With motor.target=0 motor puts up slight resistance
With motor.taget=1 much bigger (takes 1,5A from supply) and sounds weird, doesn’t move at all


I still have problem with “3,5 estimated PP” (How many pole pairs?/ Checking timers - #3 by DarkMajster) so for test I set 3 PP instead of 7 (that’s not 3,5 but the closest :smiley: )

Then it work like this


What do you think about it? Would right pole pairs problem solve my troubles? After all torque, voltage controll should just rotate my motor right? Isn’t it confusing it looks a little like some angle controll??

looks definately like the PP are wrong. The “locking” up behaviour is typical for that.

Since 3.5PP is impossible, I think we have to assume the sensor is wrong, probably by a factor of 2, and 7 is probably the correct PP number.

Lets solve that problem first, and then get back to the current sensing…