nanoframework / Home

:house: The landing page for .NET nanoFramework repositories.
https://www.nanoframework.net
MIT License
863 stars 78 forks source link

Phase Shifted Center Aligned? #883

Closed jhancock4d closed 2 years ago

jhancock4d commented 3 years ago

nanoFramework area: (PWM Library)

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

Hi, I'm looking at controlling a half-bridge for DC/DC synchronous buck/boost. I'm trying to figure out how to use the PWM code to get 2 pins, set pin 2 180 degrees offset from pin 1, center aligned and insert dead time so that pin 2 has "shoulders" from pin 1. so that shoot through doesn't occur on the mosfets.

How would one go about doing this? In Arduino code, we have to drop down to low level logic, and take 2 pins from the same timer, then set the pin to be inverted, and then add the dead time. I can't find any example or even any indication that this is possible in nanoframework.

The same requirement occurs for all ESC motor control for BLDC, linear motors, actuators, and any 3 phase control, so I was hoping that there would just be some way to create some sort of paired PWM pin controller that would allow pulling 2 pints from the same timer and just took a dead time, and frequency and would fail if the hardware didn't support it, but otherwise would set it. I.e. on an ESP32, the resolution decreases the faster you set the frequency, so it's a trade off. You have different trade offs on ATMega chips.

Suggestions on how to do this? Is it even possible?

Describe alternatives you've considered

Additional context

Submit a PR with the implementation

jhancock4d commented 3 years ago

What templates? Pretty easy to have no open issues when they get closed automatically because of failure to use a template that doesn't exist, isn't filled in like every other github repo....

josesimoes commented 3 years ago

Hey @jhancock4d. On the message above, that's an error at our end. All our issues are centralized in the Home repository. This a recent library and accidentally the github issues was left enabled. I'll move this one to Home and we can take it from there.

jhancock4d commented 3 years ago

@josesimoes That explains it! Thank you! How do I make this valid? It's a question, but it's also, I suspect, a feature request.

networkfusion commented 2 years ago

@jhancock4d did you find a workable solution for this?

jhancock4d commented 2 years ago

@networkfusion I didn't. I suspect that this would require interop to make work, which means that a library that does this needs to be brought in and coded against. The FOC project for BLDC motors has the code, it's a matter of adapting it. This project having ESC with FOC and Phase Shifting, and alignment on PWM would be key to further adoption because it's one of the major use cases for these microcontrollers.

networkfusion commented 2 years ago

@jhancock4d why not something like:

            PwmChannel pwmPin1 = PwmChannel.CreateFromPin(18, 40000);
            PwmChannel pwmPin2 = PwmChannel.CreateFromPin(19, 40000);
            // You can check then if it has created a valid one:
            if (pwmPin1 != null && pwmPin2 != null)
            {
                // You have a valid one
                double dc = 0.42; //between 0.0 and 1.0
                double inverteddc; //between 0.0 and 1.0 180 deg out of phase (math maybe wrong)

                if (dc == 0.50)
                    inverteddc = 0.50; // i.e. 180deg out of phase
                else
                {
                    if (dc > 0.5) inverteddc = dc - 0.50;
                    else inverteddc = dc + 0.50;
                }

                pwmPin1.DutyCycle = dc;
                Thread.Sleep(100); //the delay needed
                pwmPin2.DutyCycle = inverteddc;
            }
networkfusion commented 2 years ago

https://github.com/nanoframework/nanoFramework.IoT.Device/blob/79a350be3978312f9b8ed60679b0b6bddfda0a4b/devices/Uln2003/README.md

and

https://github.com/nanoframework/nanoFramework.IoT.Device/blob/79a350be3978312f9b8ed60679b0b6bddfda0a4b/devices/ServoMotor/README.md

may also help?

jhancock4d commented 2 years ago

@networkfusion Sadly they don't. Steppers don't use Phased PWM. If there was BLDC control code then I could extract that or add to that to create a standard half-bridge. (in fact BLDC motor control uses half-bridges by definition)

Anyone have any BLDC motor control code? If not, it definitely should be added. Basically this is what needs to be done: https://github.com/simplefoc/Arduino-FOC or mapped to, but the half-bridge needs to be broken out too instead of being hidden.

Ellerbach commented 2 years ago

I guess this may not help niether? https://github.com/nanoframework/nanoFramework.IoT.Device/blob/develop/devices/DCMotor/README.md

jhancock4d commented 2 years ago

Sadly no. The H-Bridge in this case (which is a full bridge synchronous buck if used for power) is on the board that the ESP32 or whatever controls. The board is doing the work with an IC.

For Arduino, you'd see code something like this (This is pin 9 and 10 acting for PWM with PIN 10 being inverted and center aligned which allows dead time)

  TCCR1B = ((TCCR1B & 0b11111000) | 0x01); // set prescaler to 1
  TCCR1A = 0xA2;
  TCCR1B = 0x11;
  ICR1 = 0x01FF;
  DDRB = 0x06;
  TCCR1A = (TCCR1A & 0x0F) | 0xB0; // set pin 10 inverted

And then to control it you'd use something like this:

  OCR1A = dutyCycle - DEAD_TIME; //Pin 9
  OCR1B = dutyCycle;
  TCCR1A = 0b10110000 | (TCCR1A & 0b00001111);

This is simplified code, because the dead time actually needs to scale versus the duty cycle for maximum performance, and of course in your best case scenario you'll also be monitoring for output voltage, output amperage, input voltage, input amperage and ZVS which will determine your dead time dynamically.

By focusing on adding this hierarchically starting with half bridges and then expanding up to H bridges, and then 3 way and then 6 way with FOC into a simple package, there is a major win to be had here for Nanoframework. This is the hardest thing to do in power electronics and if we can make it a few lines of code and you're done, it will be huge.

Ellerbach commented 2 years ago

So this would need to be done on the native side and exposed regarding the precision required. If you have to control in a very precise way 2 PWM I guess, there are not too many options. Still, the higher level can be exposed in a very simple way. Would you have some graph showing the states of both pins over the time?

jhancock4d commented 2 years ago

@Ellerbach

DC Power Converters

Here's a full bridge hard switched power supply. Pictures in the doc: (half bridge is literally just half of it) https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/786872/ucd3138hsfbevm-029-the-ucd3138-based-full-bridge-hard-switching-power-supply-adds-synchronous-rectification-and-the-waveform-is-abnormal-and-the-input-current-continues-to-rise

A half bridge is for buck or boost, a full bridge is buck AND boost. In this schematic there will be a voltage and current sense analog pin as well that will have a voltage divider and then a connection back as feedback. It can work in constant current or constant voltage mode. If in constant current, then the user would specify the pin pairs (i.e. on an uno there are 3 pairs that can be used, on esp32 there are 6. These pairs always have to be used together in microcontrollers to get inverted (180 degree phase shifted), center aligned PWM at high frequency (typically anywhere from 50kHz to 3mHz) They would also specify the analog feedback pins for each of constant current and constant voltage (optional on both because they might not care about one or the other). In a fully functioning version of this the end user would specify the target current or target voltage and be able to change it by calling a function to set them and then the algorithm uses PID to automatically adjust the PWM to maintain the voltage or current. There is a 3rd setting for doing MPPT which would tell the system to use a shift and test method to optimize Wattage based on PWM regardless of voltage or amperage. And of course ZVS is your ideal goal here. That is, it switches right at the moment that the inductor has no more voltage and the system is being fed from the capacitor.

Here's the possible applications for this:

  1. Synchronous Buck/boost/both with hard switching and ZVS (often using GanFets) Typically this will also use a half of full bridge driver to drive the fets.
  2. Flyback converter (SMPS) with transformer. Typically this will use an isolated flyback IC driver that drives the fets on both sides of the transformer.
  3. Forward converter (High power isolated power supplies like in PSUs). This will also use an isolated IC that is driven from the PWM.
  4. LiON/LiFEPo4 battery chargers (constant current/constant voltage are required here)
  5. MPPT Solar charge controllers (they can be used for other cases as well). This can also be used with a synchronous boost/buck to output to a DC Grid at a constant voltage. Basically this optimizes the wattage coming out of the solar panels using the PWM signal and then optionally has a second stage that normalizes the voltage from that. So in this, it tracks the wattage using P = I * V and shifts the PWM on a regular basis and takes a reading and optimizes using PID from those readings.

DC Brushed Motor H Bridges

Here's what a DC Brushed motor H bridge looks like (this is much simplier): https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.researchgate.net%2Ffigure%2FThree-level-shifted-carrier-PWM-H-bridge-structure-11-with-related-control-signals_fig2_282604574&psig=AOvVaw2fFYbPGNfk3JzoAhyY2M26&ust=1639581177131000&source=images&cd=vfe&ved=0CAwQjhxqFwoTCJCKgK3K4_QCFQAAAAAdAAAAABAJ

DC Brushless Motors (BLDC, AxialFlux, SWM (Tesla)), Linear Actuators, Linear Pumps

And here's what a 3 phase BLDC motor looks like: https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.researchgate.net%2Ffigure%2FA-typical-commutation-scheme-for-BLDC-motors-with-high-side-PWM-where-A-represents-the_fig5_253772458&psig=AOvVaw2feq2xt2v97KoPYF3_tWFE&ust=1639581320087000&source=images&cd=vfe&ved=0CAwQjhxqFwoTCPjO8cbK4_QCFQAAAAAdAAAAABAD

And here's the library that does the 3 phase BLDC motor control already: https://github.com/simplefoc/Arduino-FOC/issues/132#issuecomment-965037384

The code there can be adapted to support the H bridge, synchronous half bridge and full bridges for all of the other cases listed above.

By having high level functionality for all of these cases, nanoframework would become the goto for all of these cases because you'll have made it trivial to implement all of these things. And they're all just variations on the same theme of a high and low side (or multiple) FETs being switched in opposition with dead time (to prevent shoot through) and optimizing for ZVS in most cases. If you setup the basis for one (i.e. setting up the PWM pairs, getting center aligned, getting maximum frequencies allowed, and inverted) for each controller type supported, then the rest is a trivial exercise in C# code to write once you have those, because it's all about feedback from other pins at that point so it would be straight forward to wrap those PWM pairs for the rest.

Note that there is also left and right aligned PWM that you might want to support in this as well although less common and not needed for any of the use cases above.

I'd be happy to contribute the functionality for the synchronous half and full bridge tested and working and add additional methods for constant current, constant voltage, and maximized wattage once the base PWM code for the pairing is done. (This is my primary focus) I can probably do the H bridge motor driver as well as I have one around that I could test it on a bread board. And eventually I could do the BLDC etc. control as well, it's just down the road for me.

josesimoes commented 2 years ago

@jhancock4d is this being used (to be used) in a comercial application?

jhancock4d commented 2 years ago

@josesimoes It isn't at the moment, but if I can prove it out, it will be going into a complete IoT infrastructure with various commercial components. For the moment, this is me testing theories.

I'm also suggesting that this is a good idea for you, because micro-controllers are disproportionately used for driving motors.

(and so would integration with ESPHome built in, but that's a different topic)

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.