Advice on Current Sensing for new board design

Some great points given. Jlcpcb has some sense resistors. Will take a look at those. I’ve got a stm32g4 disco, didn’t realise it has opamps. Interesting. A bit of a concern is how easy these would be to configure from stm32duino core. I’ll look at Vc capacitor.

I’m wondering whether i should be looking at a smarter driver. I’ve been meaning to ask @David_Gonzalez about how he is doing current sensing on his dagor controller. This may mean dropping l6234 for a drv83xx chip and adding mosfets. Another area I know little about!

Nevermind, i got the problem. this helped a lot:

My suggestion can even be deadly for the build in ADCs of the STM32g4, since the voltage drop during the on state of the low side mosfet is negative due to the free-wheeling current.

No problems, it is easy to understand :slight_smile:
You can see below a schematic of a low side current sensing architecture.

In a low side current sensing topology the current flow through the shunt resistor only when the low side transistor of the branch is switched on. If your ADC sample the shunt voltage when T3* is not conductive then no current flow through the shunt resistor and you will not get the current flowing through the coil.

Another problem: If your ADC have a minimum sample time of 25us (for example) how do you sample the current when T3* is switched on for 25us or less? The answer is simple: you can’t…

From my understanding I have to measure the current in at least two phases and use the Clark transformation in order to calculate the corresponding vectors and set the PWM signal for the 3 half bridges.
You are right, in a middle performance brushless driver you can assume that i1+I2+i3 = 0 so if you sample only 2 current then you can easily compute i3 without actually measuring it.

In some design, you will find one shunt resistor on each phase of the driver. That’s can be useful when a branch have a low side commutation time lower than your ADC sample time… You can use the measurement of the two other phase (with a long low side commutation time) for computing the current is this unmeasurable branch.

Selecting which branch have the longest low side commutation time have to be done in software which bring complexity to it …

This is Nice, i love good arguments.

Considering the dev. on steppers. If you are going for inline sensing and 4 bridges, you can measure the current flow in both directions. The MAX ic is bidirectional.

Hi Owen,

DRV82xx use low side or high side current sensing. Each will need synchronization between PWM and ADC…

In my opinion, the simplest architecture for easy integration with simple FOC is in-line current sensing:

This topology need dedicated amplifiers with wide input common mode voltage but the current can be sampled without PWM synchronization and only two branch have to be sampled so it will keep the simple FOC library efficient.

@Owen_Williams I have lost many nights over this, and I’m still not sure how I will approach current sensing with my current hardware.

@robin2906 explained it perfectly. With the DRV8305 I’m doing low-side current sensing and this is straight from the ADC:

If I can’t figure out a way to sync the measurements with the PWM signal I’ll probably use a low-pass filter and use current sensing to apply/find a voltage limit in terms of current, and to detect collisions.

If somebody has any suggestions on how to do this with the ESP32 please let me know over: ESP32 Brushless controller - Dagor (work in progress)

@David_Gonzalez I’m happy for this thread to drift into general strategies/ideas on current sensing.

Can you explain the graphs a bit (I’m being a bit slow!). Is your drv8305 actually measuring the current, amplifying, and putting the results on an an analog pin for mcu to measure via adc?

I don’t understand why the sinusoidal waves have those cuts in them. Are they 50% duty? Are you only suppose to measure each phase when low or high gate is in use? I’d understand the graphs if they were pwm only (not sinusoidal) as I’d expect real current to be pwm.

I take it you want current readings for reading torque and not for sensorless position. Perhaps taking a moving average is good enough (or even correct!)

What we’re seeing on the graph is the voltage drop across the sense resistor after a 10x amplification on the DRV8305 measured directly from the ESP32’s ADC. If my understanding is correct the fact that we’re seeing sinusoidal signals is that current doesn’t behave like voltage and even if you have a PWM (pulsating voltage) signal you don’t necessarily see pulsating current.

The reason that we are seeing the cuts in the waves is the fact that the sense resistors are located after the low side FET so when the FET is off the voltage across the sense resistor will ultimately be 0. This wouldn’t be an issue if you do in-line current sensing as @robin2906 suggested.

You’re correct :slight_smile: I want current sensing to do torque control and to detect collisions in direct or quasi-direct drive systems.

Awesome project! I’m working on a similar design, and happy to exchange information if you want.

For the current sensing: as some people have already mentioned, you need to size the shunt resistor so that:
a) your max power dissipation (given by the voltage drop on the shunt x the max current, probably 5A for a 6234 driver) must be well within the power rating for the shunt, or it will burn up
b) your voltage drop is as large as possible (makes readings more accurate and less noisy)
So you basically pick the largest value that (with some safety margin) doesn’t burn

Since you’re using a single shunt sensing topology, you probably don’t want to use the current sensing to drive the FOC algorithm in terms of motor position, so you will still need an accurate encoder. But you can use the current sensing to drive motors with lower winding resistances by implementing current limiting. Note that this is currently not implemented in the SimpleFOC library, I think.

For a 1 Ohm winding resistance, I think your shunt value will need to be quite low, and you will definitely need to amplify the voltage. For this an op amp is what you need, but I would strongly recommend using a current sense amp (a ready made IC integrating most of the stuff you need as well as filtering circuitry etc). This is where it starts to get quite complicated, because you will need to choose the op-amp or current sense amp according to the needs of the application. So in your case you need a common mode that can go (at least a little bit) negative, 16(?)V input, fast response times, and can map the positive/negative voltages you get from the shunt to a positive only voltage the microcontroller can work with. You may also need to use some kind of voltage reference to provide a stable reference voltage to the amp. Take a look at the INA*** current sense amps, they have some useful models.

From there it needs to go to the microcontroller. I’d be interested what your plan is for this. The STM32 have good ADCs, I think, and I think there are features to connect them to the PWM driving. Some models also have built in op-amps and comparators. I think the problem here is that the switch-off has to happen fairly quickly, probably faster than would happen if you just “read ADC, re-adjust PWM” in the FOC control loop. Many designs I’ve looked at use comparators to detect the over-current condition and switch the EN or IN control signals low using hardware, and not software. Some of the motor driver chips also have dedicated inputs for this purpose, so you can get a current control loop many times faster than your FOC control loop, maybe even considerably faster than your PWM period…

One final note: I’d urgently recommend doing some kind of simpler design as your first assembled PCB. Doing a motor controller with current sensing and microcontroller as your first project is very ambitious, IMHO. For example, why not start with just a part of it, as a breakout board, and use an existing microcontroller board to drive it?

Regards from Vienna,

Richard

Thanks Richard. Some great points! You are right about making my first board a bit simpler.

I’ve parked my custom board and want to see if I can add current sensing and 6PWM (i.e. 3 Drivers each with HIN and LIN) support to SimpleFOC - before returning to my custom board. I’m using the stm32g431 kit for drones to test the above (it comes with 3x shunt resistors).

Where does that board places the shunt resistors? How is the signal amplified?

The board places 3x shunts resistors (0.003 OHM 1% 3W) on the ground side of the lower mosfets. So I’m guessing similar to your Dagor controller @David_Gonzalez. The kit is rated to 40A assuming you have drone propellers blasting madly down at it!

Those vshunt_x+ and vshunt_x- then feed into what looks like a pullup/pulldown circuitry


And the Curr_fdbkX_OPAmp+ and Curr_fdbkX_OPAmp- feed into special pins in the stm32g431 - these pins can be configured to use internal op-amps.

The stm32g431 has 3 internal opamps each with 4 built-in gain values with 1% accuracy. Configuring pins/opams, I suspect, will be tough on stm32duino as any macros/libs that exist will be for STM32CubeG4

Hi Owen,

if somebody knows better, pls proof me wrong. But i think that low side current sensing can be a problem if you can not ensure, that the current in the shunt resistors allways flows in “positive” direction. There might be a case during free wheeling where the voltage drop becomes negative due to inverted current flow. I dont know whether the i/o pins of the stmg32g4 can handle a negative voltage.
Beste regards
Anderson

Thanks @Owen_Williams! I’d like to see a raw graph from the ADC with your setup and compare it to mine.

Hey Guys, very very interesting topics here!

I’ve started working on the current sensing addition to the SimpleFOCShield and I’m not sure what is the best approach to go with at the moment.

Here is the link to the L6234 datasheet (page 3), they used only one shunt resistor and measured only one current.
As opposed to all that that is usually discussed in terms of BLDC motors (three or two current measurement).
Basically with one current (overall current) you cannot know the motor position but you can still control its torque. Limit it and so on…

I am thinking this is actually not a bad idea, at least for SimpleFOCShield.

What do you guys think?

Hmm, could go either way. What the world really needs is an open-source cross-platform firmware that does true FOC (advancing voltage phase to compensate for current lag at high speed) so we can quit duplicating the hard work again and again. But L6234 has such low current capacity that it’s pretty much only usable for gimbal motors, which can’t go fast enough for FOC to make a big difference anyway. So the only reason to do 2 sensors would be to provide a development platform for yourself, but you could just use a different board for that.

Hello,
Anyhow, It also is totally possible to do a FOC with only one shunt measure with a very good performance.

Hi Antun,

Be careful here, with only one shunt resistor you will be screwed…

In the tab below you will find the current you will sample with the single shunt topology depending of the state of the inverter when you sample the voltage drop across the shunt resistor.

With this approach you will never know the total current flowing through the 3 coil of the motor because the coil will never be (hopefully) connected to the shunt resistor at the same time. So you will be unable to make a torque controller or a torque limitation…

If you want make a torque controller with this single shunt topology you will have to:

  • Synchronize your ADC with the state of T1 T2 and T3. (Difficult to archive with Arduino and esp32, possible with stm32)

  • Find which commutation sequence have the highest Ton time for the period and make your ADC ready to convert during this time. (You have to do this because if you Ton is too low regarding your ADC acquisition time you are not able to sample the volatge drop with your ADC). -> This will not be an easy neither fun task to implement this logic… I will pray for you if you go that way :wink:

  • Then acquire 2 current and compute the third current with i1+i2+i3=0 (easy on Arduino, stm32, esp …)

  • Compute your torque controler and power limitation algorithm.

As Marc_O said, it can be done and with good performances, I’m agree with him. BUT the truth is: It have been done and it work well … but on a fixed platform, with a fixed hardware.

Don’t hesitate to ask if it’s not clear !

Best regards,

Hey guys,
What I had in mind was to measure the overall current draw of the L6234. not just one line or phase.
Basically, I would not be able to use this value for foc but it would still be a good value for torque control.

Basically, keep FOC using voltage, but close the outer torque loop using current.
It is not a standard approach, but for gimbal motors it just might be good enough. :smiley:

Hi Antun,

You cannot measure the overall current draw of the L6234 with the low side shunt resistor as in the page 3 of the datasheet of the L6234. Never in the commutation the current flowing through the 3 coils of the motor flow through the low side shunt resistor at the same time.

You can use a high-side shunt resistor in line with the power line but it’s not accurate and you will need an amplifier on your board.

Best regards,