Gen 3 inverter converter control software

Topics concerning the Toyota and Lexus inverter drop in boards
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

I replaced the resistors with 10 K... the calibration of the sensors is loaded-But! After a few seconds, Atmega freezes!!! Parameters in the menu are not displayed, etc. After the reset to manual, the atmega comes to life for a while and freezes again((( what do we do now?
User avatar
Bigpie
Posts: 1585
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 74 times
Been thanked: 299 times

Re: Gen 3 inverter converter control software

Post by Bigpie »

Not sure, try rolling back to celeron555s original with hardcoded values and see if you can get that charging first?

Not sure why you've replaced resistors. I had to replace one because it was dead, I always had 1023 from.calibration.
VW Beetle 2003
Outlander front generator
Prius Gen 3 inverter (EVBMW logic board)
Outlander charger
3x Golf GTE batteries
Chademo Charging
Outlander water heater
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

I'm wondering if what I'm trying to repeat works for you) I replaced the resistors according to the description in the script for the experiment, and with 4.7 k there was the same story))) I repeat the problem again:- with the current sensors not connected, the readings are output to the web interface...Once you connect the sensor connector, the parameter readings are no longer displayed in the interface.... atmega freezes and you can only get it out of the hang by rebooting .... so I wonder if your creation works or not.Write in more detail about your development, draw a diagram of some kind - maybe the process will go faster and more efficiently. Thank you in advance for your help..))
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

Leo M wrote: Sun Feb 21, 2021 6:40 pm I'm wondering if what I'm trying to repeat works for you) I replaced the resistors according to the description in the script for the experiment, and with 4.7 k there was the same story))) I repeat the problem again:- with the current sensors not connected, the readings are output to the web interface...Once you connect the sensor connector, the parameter readings are no longer displayed in the interface.... atmega freezes and you can only get it out of the hang by rebooting .... so I wonder if your creation works or not.Write in more detail about your development, draw a diagram of some kind - maybe the process will go faster and more efficiently. Thank you in advance for your help..))
What is your output on the serial from 328? Does the 328 SW ok the current sensor calibration?
Maybe, as Bigpie suggests, go back to celeron55 original code and check this.
User avatar
Jack Bauer
Posts: 3563
Joined: Wed Dec 12, 2018 5:24 pm
Location: Ireland
Has thanked: 1 time
Been thanked: 87 times
Contact:

Re: Gen 3 inverter converter control software

Post by Jack Bauer »

So a few things to note here : The 5v regulator on the Yaris board cannot keep up with the extra load from the wemos. I bet if you check it is hovering around 4.5v which is causing the 328p to drop in and out of brownout. This will also cause the -5v rail to run low or not at all and hence the current sensors read garbage. When you disconnect the current sensors the 5v rail recovers and everything seems to work fine. So what I would do is remove the inverter wifi and see what happens or try supporting the 5v with an external psu for testing.

In light of this I have redesigned the Yaris board with the following modifications :
-2A 5V psu
-AMS117 3v3 reg
-MAX735 switch mode -5v regulator in place of the cap pump version.
-add filtering to inverter wifi module
-add option for a wemos d1 mini for charger wifi

Currently have prototypes in production at JLC. As soon as I verify correct operation they will be available in the webshop and design files on Github.
Attachments
Produce_DanZhi.SMT_Snapshot.Top.2707872A_Y97.SMT0210220146644.png
I'm going to need a hacksaw
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

Great news) I also thought that the reason for the failure of the 5V power supply. We will work on this further and I think in the end we will win !!! I would like to know how Demin's tests went.
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

MAX735 ? Maybe there's a problem here? A separate 5V power supply did not solve the problem ((Atmega is still hanging... it turns out that on all the boards previously released, the charger circuit is not working? Or is it still possible to revive it?
Attachments
E2A31586-45C8-41F7-9918-81903FD47CE4.jpeg
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

m.art.y wrote: Tue Feb 02, 2021 2:59 pm
bexander wrote: Tue Jan 19, 2021 6:37 pm Ok, great. Thank you!
Did you manage to get it working? 😊 Would you share how it went?
Ok, now I have tested it and it works!
I can get the charger to start using CAN-messages between BMS, Inverter (STM32) and Charger(328p).
celeron55 wrote: Tue Jan 19, 2021 6:07 pm
However the output current is set by BMS to 5A and it is very unstable during charging, see attached pdf.
BMS2_3_9.pdf
(85.45 KiB) Downloaded 176 times
This is the current measured by the BMS, every 250ms. The serialPrint from the charger gives a similar result but I have no capture of it.
Have I done anything wrong is this what I can expect at this low charging current? Using 10kHz and a battery pack voltage of 375V, supplying from 400V AC 3-phase (565V DC rectified).

Also, when doing the boost precharge the voltage overshoots a bit. Limit set to 550V but can read 610V on serial monitor?
CHG2_1_2.txt
(5.25 KiB) Downloaded 159 times
"wanted_output_voltage = 550.00 V"
"AC side precharge FINISHED at 610V, AC contactor CLOSED"

Any ideas on to how I can decrease the overshoot?
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

I just relized I have decreased the CAN speed to 250k (500k), to work with the rest of the car. Could this have messed up some timing in the PWM loop making it unstable?
User avatar
Jack Bauer
Posts: 3563
Joined: Wed Dec 12, 2018 5:24 pm
Location: Ireland
Has thanked: 1 time
Been thanked: 87 times
Contact:

Re: Gen 3 inverter converter control software

Post by Jack Bauer »

Of course the charger circuit works. How else would celeron55 have developed the code. If there is a problem I'll remedy it. Just a little busy with 100 other things right now.
I'm going to need a hacksaw
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

We are waiting for the tests of the new replica of Yaris... it would be good to look at the update scheme . I could try running updates on the circuit board...
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: Gen 3 inverter converter control software

Post by celeron55 »

bexander wrote:...
The unstability is probably not because you have anything wrong in your setup and probably mostly a feedback loop tuning (or complete feedback loop rewrite) problem.

There are at least three reasons for the unstability to not be very surprising: 1) the charger mcu only has input current measurement designed for hundreds of amps at its disposal. 5A 375V output will be about 3A 3 phase input, which is barely measurable. 2) the current sensors are on the AC side, making things even worse and my code regarding that isn't the greatest (makes for a really slow feedback loop). 3) I designed and tested it for a specific need: charging my 260V battery fast at 22kW AC charging stations (sucking in 30A at 550V and putting out 65A at 280V), so anything outside that ballpark, while probably safe, is definitely untested and untuned.

EDIT: By the way, nice to hear it (somewhat) working in another actual charging setup without breaking an inverter. I'm sure you can already feel it begging for the full 20 (27?) kW...
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

celeron55 wrote: Sat Feb 27, 2021 7:08 pm
bexander wrote:...
The unstability is probably not because you have anything wrong in your setup and probably mostly a feedback loop tuning (or complete feedback loop rewrite) problem.

There are at least three reasons for the unstability to not be very surprising: 1) the charger mcu only has input current measurement designed for hundreds of amps at its disposal. 5A 375V output will be about 3A 3 phase input, which is barely measurable. 2) the current sensors are on the AC side, making things even worse and my code regarding that isn't the greatest (makes for a really slow feedback loop). 3) I designed and tested it for a specifical need: charging my 260V battery fast at 22kW AC charging stations (sucking in 30A at 550V and putting out 65A at 280V), so anything outside that ballpark, while probably safe, is definitely untested and untuned.

EDIT: By the way, nice to hear it (somewhat) working in another actual charging setup without breaking an inverter. I'm sure you can already feel it begging for the full 20 (27?) kW...
Ok, so I'm just beeing chicken with the current then... :)

Seriously, my wish is to use it for charging with 2kW up to 22kW but even 22kW will only draw 32A AC so it might be a good idea to look into a better suited current sensor for these currents then. I will see what I can find.
I will test with some higher power as well, to check if it will be more stable. I still suspect it will be hard for control loop at the end of charge CV phase.
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: Gen 3 inverter converter control software

Post by celeron55 »

Voltage measurement is fast and accurate, so the V part is easier for it I think.

Anyway, the PCB has a connector ready for an additional output current sensor (GND, signal, 5V) and a pin for this was allocated from the atmega328.
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

Well, I've looked into the current measurement and it can be improved a bit if only charging is required (as in my case).
Present and suggested schematic changes attached.
MG1_IL_Amp.pdf
(21.01 KiB) Downloaded 190 times
If I change from the original wiring as per left in the schematic to the right solution it should result in an increase by 10 times in current measure resolution as seen by the 328p, i.e. from 1A/bit to 0.1A/bit.
I can look into this further but I don't think this is the main problem.

The PWM duty changes faster than the current measurement can catch.
I think the current measurement is very slow as you have to average it to get a stable value over the sine period.
My guess is that the PWM duty changes can be made a lot slower. When charging a battery there should never be any fast load changes so the "regulator" can be very slow.
I'm not a programmer but I will try and look into the code and see what can be done.

Celeron55: Are you willing to help with code upgrades if I suggest what might be changed?

EDIT, replaced the circuit diagram with an imporved version.
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: Gen 3 inverter converter control software

Post by celeron55 »

bexander wrote: Sun Feb 28, 2021 8:13 am Celeron55: Are you willing to help with code upgrades if I suggest what might be changed?
Go ahead.
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

I put the max charge current to 10A and it is still very unstable.
CHG2_1_2_10A.txt
(13.56 KiB) Downloaded 163 times
Could it be I'm requesting a max DC output current? Maybe better to limit AC input current?
When you tested it, was it this unstable for you as well?
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

I'm trying to understand the code and how the 328p is setup.

F_CPU / 2 / 10000 = 800 (ICR1)
no prescaler => CLK I/O = 31,2kHz => T = 32us
SIGNAL(TIMER1_CAPT_vect) is triggered every 800x32us = 25,6ms
every 8:th time (case 0-6 + default) or every 205ms, the current is calculated from max of 1 or 2, averaged over the last 10 measurements
then if all is ok every 4:th time (820ms) the pwm is allowed to step up or down 1 step

Is this correct?

EDIT:
No, this is not correct...
The PWM frequency is 10kHz (T = 100us) and hence the SIGNAL(TIMER1_CAPT_vect) should triggered at this interval (every 100us), right?
every 8:th time the current is calculated, every 800us and then PWM change every 3,2ms if close to desired value
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

Did a quick test by setting max AC current to 5A but no luck there, starts to oscillate after a while.
Strangely the AC current seem to be ignored as the inverter sets 7A or seem to be regulating at 10A max dc instead?
CHG2_1_2_5AAC.txt
Serial print from charger
(9.38 KiB) Downloaded 152 times
I will focus on making a small circuit board to patch in to get better current readouts. At least for some testing.
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: Gen 3 inverter converter control software

Post by celeron55 »

Try making the PWM adjustment slower first? I'll take a look once I'm back at my desk later tonight. But I think you were right in that it's trying to adjust faster than the slow current feedback reacts.
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

Ok, I'm struggeling with understanding the full algorithm of the PWM and current measurement so the following is guesswork.

Measuring AC current with samples and averaging. Shouldn't it be average over a whole period of 20ms to get a RMS similar value?
If so, average over 25 cycles should be closer to 20ms.
Also, if the AC current resolution is 1bit/A and I use, say 3A that means the instant current goes up to 4, maybe 5 bits and that gives kind of poor average. Here I think, better current resolution really would help. As the current feedback is per standard there is a 2,7 times dampening to easy get the zero level down to 2,5V. This is, for my purposes, not suitable so if I can get this to no dampening it would be good but it will, to my knowledge, require a opamp and then why not put in some gain to fully utilize the 5V of the ADC. Will of course introduce some noise as well but I think the gains in measuring resolution will be worth it. It would still be possible to use 32A input current (Itop = 45,3A).

Would it be possible to adjust PWM change rate of 1 increment once every full AC cycle (20ms)? Will that be to slow?

EDIT:
What is the theory behind using max of abs(I1) and abs(I2)? To me it makes more sense to use only one of them the whole time, either abs(I1) or abs(I2). Or possible calculate the third current as well and use max of the abs(I1), abs(I2) and abs(I3)(calculated).
User avatar
celeron55
Posts: 774
Joined: Thu Jul 04, 2019 3:04 pm
Location: Finland
Has thanked: 27 times
Been thanked: 110 times
Contact:

Re: Gen 3 inverter converter control software

Post by celeron55 »

bexander wrote: Sun Feb 28, 2021 7:10 pm Measuring AC current with samples and averaging. Shouldn't it be average over a whole period of 20ms to get a RMS similar value?
bexander wrote: Sun Feb 28, 2021 7:10 pm Would it be possible to adjust PWM change rate of 1 increment once every full AC cycle (20ms)? Will that be to slow?
It should be possible and might be wise to modify the code to work like that, averaging exactly one AC cycle and then updating everything based n that. It would make it more predictable and tunable, and faster as looks like I ended up averaging over 2 full faves probably just to work around some noisiness. Reacting to overvoltage measurements can be done separately as they come in.
bexander wrote: Sun Feb 28, 2021 7:10 pm What is the theory behind using max of abs(I1) and abs(I2)? To me it makes more sense to use only one of them the whole time, either abs(I1) or abs(I2). Or possible calculate the third current as well and use max of the abs(I1), abs(I2) and abs(I3)(calculated).
I guess I was thinking I want to use all data I possibly can, as early as I can. I think using only one sensor is a bit risky as you might miss something, but calculating the third current sounds like a better idea. It's obviously how the sensors are meant to be used.
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

celeron55 wrote: Sun Feb 28, 2021 9:15 pm
bexander wrote: Sun Feb 28, 2021 7:10 pm Measuring AC current with samples and averaging. Shouldn't it be average over a whole period of 20ms to get a RMS similar value?
bexander wrote: Sun Feb 28, 2021 7:10 pm Would it be possible to adjust PWM change rate of 1 increment once every full AC cycle (20ms)? Will that be to slow?
It should be possible and might be wise to modify the code to work like that, averaging exactly one AC cycle and then updating everything based n that. It would make it more predictable and tunable, and faster as looks like I ended up averaging over 2 full faves probably just to work around some noisiness. Reacting to overvoltage measurements can be done separately as they come in.
bexander wrote: Sun Feb 28, 2021 7:10 pm What is the theory behind using max of abs(I1) and abs(I2)? To me it makes more sense to use only one of them the whole time, either abs(I1) or abs(I2). Or possible calculate the third current as well and use max of the abs(I1), abs(I2) and abs(I3)(calculated).
I guess I was thinking I want to use all data I possibly can, as early as I can. I think using only one sensor is a bit risky as you might miss something, but calculating the third current sounds like a better idea. It's obviously how the sensors are meant to be used.
After a good night sleep, I have a theory...
Assuming SIGNAL(TIMER1_CAPT_vect) is triggered every PWM pulse i.e. 1/f the result is for 10kHz 100us and for 4kHz 250us.
Then current calculation every 8th call, so for 10kHz every 800us and for 4kHz every 2ms.
Now averaging this over 10 calculated samples gives for 10kHz 8ms and for 4kHz 20ms.
Please confirm the timing of this is correct.
Now you have mostly tested using 4kHz (3,9kHz) with ok results. Makes sense as the average is roughly over one complete AC cycle. Should give a fairly consistent current reading.
Then if changing the frequency to 10kHz then the average is no longer over a complete or even half a complete cycle witch should result in a current reading varying with time and hence the PWM will always chase the varying current reading.
I can try and test this theory by changing the frequency to 4kHz and see if this is more stable.

My suggestion.
When sampling AC current you need to do it over a full (20ms) or at least a half cycle (10ms) period.
Also I think it is a good idea to calculate the RMS value and not just the mean value of the abs current.
Then compare the RMS current calculated from sensor 1 with sensor 2, they should be very similar if everything is correct.
The RMS value will be the correct value to use for comparing with fuse ratings etc.

Pseudo code suggestion:
from->
if(ch_i == 3){
// Control PWM

l1_current_A = ((int32_t)mg2l1_current_raw * (int32_t)MG1_CURRENT_A_PER_BIT)^2; // the square of the current
average_l1_current = sum_of_last_N_current_square_samples / N; // calculate the mean value of current^2
RMS_l1_current = sqrt(average_l1_current); // The square root of the average current^2 gives the RMS value

Do the same calculation for the l2_current_A

Compare RMS_l1_current with RMS_l2_current, should be close to equal.

Use one of them as input to PWM. Change PWM max once every 20ms or if you will, after complete N number samples as per above.

N should be selected so one half or one full AC cycle is sampled. As the code is written today it will depend on the switch frequency selected which is fine as long as you don't change the switch frequency... maybe add a warning for this.
So 10kHz selected and no other changes to the code N should be 25 if my first assumption is correct.

A small error in the code, "mg2l1_current_raw" should be named "mg1l1_current_raw" instead. Applies to "mg2l2_current_raw" as well.

I hope you find this helpful!
Leo M
Posts: 88
Joined: Fri Oct 30, 2020 9:48 am
Been thanked: 2 times

Re: Gen 3 inverter converter control software

Post by Leo M »

Friends! Please tell me what Fuses you prescribe in Atmega? Maybe I have a mistake in them?
User avatar
bexander
Posts: 834
Joined: Tue Jun 16, 2020 6:00 pm
Location: Gothenburg, Sweden
Has thanked: 63 times
Been thanked: 89 times

Re: Gen 3 inverter converter control software

Post by bexander »

celeron55, one more suggestion.
During boost to precharge before AC contactor, I think adding complementary drive of the IGBT:s would be a more robust solution as then there is a current path in the revers direction, decreasing the risk of voltage overshoot.
I used it during some testing to boost the motor voltage before I did my battery change. Gave a very stable voltage, both at steady-state and while driving using power and regen.

My SW for complementary drive:

Code: Select all

/*
Runs atmega328p buck/boost control on Prius Gen 3 and Yaris/Auris inverters in buck/boost mode to raise battery voltage to run higher motor voltage with current flow in both directions.
Experimental code. Only tested on the bench! Use at your own risk!
*/
// Change log
/*

1_3
* Adjusted setVoltage to 288 (224)

1_2
* Switched to invert PIN10 (PIN9) in setup()
* Switched deadtime addition/subtraction in SetPWM() to match above
* Added constants for PWMDutyMin and PWMDutyMax for easier settings
* Added 10s delay at startup to allow precharge to complete

1_1
* Replaced Metro library with use of millis() 

1_0
* First version

*/

// Include libraries

// Constant declaration
// Limits
const uint16_t setVoltage = 288; // Desired set point voltage
const uint8_t half_dead_time = 2; // Total deadtime = half_dead_time * 2 * 0.5us
const uint8_t PWMDutyMin = 1;
const uint8_t PWMDutyMax = 190; // To limit maximum output voltage VoutMax = Vin * 255 / (255 - PWMDutyMax)

// I/O-PINS
const uint8_t lowSidePIN = 9;
const uint8_t highSidePIN = 10;

// Timing constants
const uint32_t timePeriod2ms = 2; // delay, 2ms
const uint32_t timePeriod200ms = 200; // delay, 200ms


/********
* SETUP *
********/
void setup()
{
	pinMode(lowSidePIN, INPUT);   //PWM is set to input before PWM initialized to prevent start up short circuit
  pinMode(highSidePIN, INPUT);   //PWM is set to input before PWM initialized to prevent start up short circuit
	
  TCCR1A = 0b10110001; // mode 1: phase correct PWM 8-bit, TOP = 0x00FF, active for PIN9 and inv. PIN10
  TCCR1B = 0b00000010; // mode 1: phase correct PWM 8-bit, TOP = 0x00FF; prescaler 8 (frequency to 3.9kHz)
	
	setPWM(0); // Set start condition

  delay(10000); // Delay to allow precharge to complete
    
	pinMode(lowSidePIN, OUTPUT);
  pinMode(highSidePIN, OUTPUT);
  	
	//Serial.begin(115200);
}


/*******
* LOOP *
*******/
void loop()
{
	updatePWMDuty();  //call pwm update routine.
}


/**********************
* Sets PWM duty-cycle *
**********************/
void updatePWMDuty()
{
	static uint32_t lastMillisPWM = millis(); // For timing
  //static uint32_t lastMillisSerial = millis(); // For timing
  	
	static uint8_t PWMDuty = 0; // PWM duty-cycle
  static uint16_t HVLow = 0;
  static uint16_t HVHi = 0;
  
	if(millis() - lastMillisPWM >= timePeriod2ms)
	{
		lastMillisPWM = millis();  // Reset timer
		
		HVLow = (analogRead(A0) * 20 / 37); // Division by 1.85
		HVHi = (analogRead(A1) * 5 / 4); // Multiplication by 1.25
		
		if(HVLow >= 105) // If battery voltage is above 105V start ramping up pwm and regulate to setpoint.
		{
			if(HVHi < setVoltage) PWMDuty++;
			if(HVHi > setVoltage) PWMDuty--;
			if(PWMDuty < PWMDutyMin) PWMDuty = PWMDutyMin;
			if(PWMDuty > PWMDutyMax) PWMDuty = PWMDutyMax;
		}
		
		if(HVLow < 105) // If battery voltage is lower then 105v ramp down pwm
		{
			PWMDuty--;
			if(PWMDuty < 1) PWMDuty = 1;
		}
		
		setPWM(PWMDuty);
	}
	
	/*if(millis() - lastMillisSerial >= timePeriod200ms)
  {
		lastMillisSerial = millis();  // Reset timer
		
		serialOUT(HVLow, HVHi, PWMDuty);
	}*/
}


/*******************
* Sets PWM outputs *
*******************/
void setPWM(uint8_t PWMDuty)
{
 //Limit the pwm_phx so it take the dead time into account so it not will swap over when reach end limit of workable PWM range
 if(PWMDuty > (255 - half_dead_time))
 {
   PWMDuty = (255 - half_dead_time);
 }
 
 if(PWMDuty < half_dead_time)
 {
   PWMDuty = half_dead_time;
 }
 
 //All The PWM will run from 0xFF to 0x00 and up again to TOP 0xFF
 //The OCRnx will automatic update when TCNTn is on TOP 0xFF by hardware
 //Therefor it is sutible to change both OCRnx pair on a place where TCNT not update to ensure that the hardware automatic update NOT occur exact when only one of the two OCRnx have changed by this code
 while(TCNT1 > 0x7F)
 {
 }
 //Now it's safe to update OCR1x pair (connect to same transistor pair)
  OCR1A = (PWMDuty - half_dead_time); // Low side IGBT
  OCR1B = (PWMDuty + half_dead_time); // High side IGBT
}


/**************************************
* Sends information to serial monitor *
**************************************/
void serialOUT(uint16_t HVLow, uint16_t HVHi, uint8_t PWMDuty)
{
  Serial.print("Low Vbus = ");
	Serial.print(HVLow);
	Serial.print("Volts");
	Serial.print("\t High Vbus = ");
	Serial.print(HVHi);
	Serial.print("Volts");
	Serial.print("\t PWMDUTY = ");
	Serial.println(PWMDuty);
}
Post Reply