AC 1b open loop - owntech-foundation/Tutorials GitHub Wiki
- This code has not been tested. I couldn't test it because I dont have the Zephyr librairies used for the GPIO on my version of the code. Starting from Buck 1 tutorial, follow this tutorial to test !
- Test without hwConfig.setLeg2PhaseShift(0); //sets the second leg at the same phase shift as the first
- This version if the full code for 3-phase DC/AC Converter. Need to be split into 2 tutoriels. First 1-phase AC/DC converter then 3-phase.
The goal of this tutorial is to use the O2 as a 1-phase DC/AC converter in open-loop. We will start from the Buck 2 tutorial.
- O2
v_1_1_2
- STLinkV3
- PC 64-bits (windows or linux)
- DC power supply (48 V, 2 A)
- Oscilloscope or multimeter
- Git
- Visual Studio Code with PlatformIO (see Blinky tutorial)
- SerialPlot (see SerialPlot tutorial)
- We will create the project by copying the Buck 1 Task tutorial in a new branch, that will be called
dcac_1
. In the bottom menu, click on the New Terminal icon . This will open a new terminal into which you can write the following commands to create a new git branch.
git status
git add --all
git commit -m "work done on the inverter_1"
git branch inverter_2
git checkout inverter_2
- In the bottom menu, check that you are now in the
inverter_2
branch.
Load some libraries to handle the gpios.
//------------ZEPHYR DRIVERS----------------------
#include "zephyr.h"
#include "console/console.h"
#include <drivers/gpio.h>
#include <device.h>
- Define the variables
In src/main.cpp
, in the section USER VARIABLE DECLARATIONS
, add the variables below. The initial duty cycle is 0.5. The duty cycle steps are set to 0.05. A boolean indicates the state of the PWM (ON or OFF). A variable will also define the period of the control task.
//--------------USER VARIABLES DECLARATIONS----------------------
static float32_t duty_cycle = 0.5; //[-] duty cycle (comm task)
static bool pwm_enable = false; //[bool] state of the PWM (ctrl task)
static uint32_t control_task_period = 50; //[us] period of the control tas
static float32_t Vhigh_value; //store value of Vhigh (app task)
static float32_t V1_low_value; //store value of V1_low (app task)
static float32_t V2_low_value; //store value of V2_low (app task)
static float32_t ihigh_value; //store value of ihigh (app task)
static float32_t i1_low_value; //store value of i1_low (app task)
static float32_t i2_low_value; //store value of i2_low (app task)
static float32_t data_value; //stores the value of the data converted by the ADC
static int cpt_step=0;
static bool pin_state = 0;
static bool old_state = 0;
static uint16_t period = 1000; // nb of points in the lookup table for 360 degrees
static uint16_t phase_shift = 0; // index in the lookup table corresponding to the pahse shift (500/1000 = 180 deg)
static float32_t sin_angle[] = {0,0.00628314396555895,0.0125660398833526,0.0188484397154082,0.0251300954433375,0.0314107590781283,0.0376901826699345,0.0439681183178649,0.0502443181797696,0.0565185344820245,0.0627905195293134,0.0690600257144058,0.0753268055279327,0.0815906115681575,0.0878511965507432,0.0941083133185143,0.100361714851215,0.10661115427526,0.112856384873482,0.11909716009487,0.125333233564304,0.131564359092282,0.137790290684638,0.144010782552252,0.150225589120757,0.156434465040231,0.162637165194884,0.168833444712734,0.175023058975276,0.181205763627137,0.187381314585725,0.19354946805086,0.199709980514407,0.205862608769881,0.212007109922055,0.218143241396543,0.224270760949381,0.230389426676591,0.236498997023725,0.242599230795407,0.248689887164855,0.254770725683382,0.260841506289897,0.266901989320376,0.272951935517325,0.278991106039229,0.285019262469976,0.291036166828272,0.297041581577035,0.303035269632774,0.309016994374947,0.314986519655305,0.320943609807209,0.326888029654942,0.332819544522987,0.338737920245291,0.344642923174517,0.350534320191259,0.356411878713251,0.362275366704546,0.368124552684678,0.3739592057378,0.379779095521801,0.385583992277397,0.391373666837202,0.397147890634781,0.402906435713663,0.408649074736349,0.414375580993284,0.420085728411806,0.425779291565073,0.431456045680959,0.437115766650933,0.442758231038902,0.448383216090032,0.453990499739547,0.459579860621488,0.465151078077458,0.470703932165333,0.476238203667939,0.481753674101715,0.487250125725332,0.492727341548292,0.498185105339491,0.503623201635761,0.509041415750371,0.514439533781506,0.519817342620709,0.525174629961296,0.530511184306734,0.535826794978997,0.541121252126876,0.546394346734269,0.55164587062843,0.556875616488188,0.562083377852131,0.567268949126757,0.572432125594591,0.577572703422268,0.582690479668576,0.587785252292473,0.592856820161059,0.597904983057519,0.602929541689025,0.607930297694605,0.612907053652976,0.617859613090334,0.622787780488113,0.6276913612907,0.632570161913124,0.63742398974869,0.642252653176584,0.647055961569444,0.651833725300879,0.656585755752956,0.661311865323652,0.666011867434252,0.67068557653672,0.675332808121024,0.679953378722419,0.684547105928689,0.689113808387348,0.693653305812805,0.698165418993473,0.702649969798849,0.707106781186547,0.711535677209285,0.715936483021831,0.720309024887907,0.724653130187047,0.728968627421411,0.73325534622256,0.737513117358174,0.741741772738739,0.745941145424182,0.75011106963046,0.754251380736104,0.758361915288722,0.762442511011448,0.76649300680935,0.770513242775789,0.774503060198734,0.778462301567023,0.782390810576588,0.786288432136619,0.79015501237569,0.793990398647835,0.797794439538571,0.801566984870877,0.805307885711122,0.809016994374947,0.812694164433094,0.816339250717184,0.819952109325452,0.823532597628427,0.827080574274562,0.830595899195813,0.834078433613171,0.837528040042142,0.840944582298169,0.844327925502015,0.847677936085083,0.850994481794692,0.854277431699295,0.857526656193652,0.860742027003944,0.863923417192835,0.86707070116449,0.870183754669526,0.87326245480992,0.876306680043864,0.879316310190556,0.882291226434953,0.885231311332455,0.888136448813544,0.891006524188368,0.893841424151264,0.896641036785236,0.899405251566371,0.902133959368203,0.904827052466019,0.907484424541117,0.910105970684996,0.912691587403503,0.915241172620918,0.917754625683981,0.92023184736587,0.922672739870115,0.925077206834458,0.927445153334661,0.929776485888251,0.932071112458211,0.934328942456612,0.936549886748192,0.938733857653874,0.940880768954225,0.942990535892864,0.945063075179805,0.947098304994744,0.949096144990295,0.951056516295154,0.952979341517219,0.954864544746643,0.95671205155883,0.958521789017376,0.960293685676943,0.962027671586086,0.96372367829001,0.965381638833274,0.967001487762435,0.968583161128631,0.970126596490106,0.971631732914674,0.973098510982127,0.974526872786577,0.975916761938747,0.977268123568193,0.978580904325472,0.979855052384247,0.981090517443334,0.982287250728689,0.98344520499533,0.984564334529205,0.985644595148998,0.986685944207868,0.987688340595138,0.988651744737914,0.989576118602651,0.990461425696651,0.991307631069507,0.992114701314478,0.992882604569814,0.993611310520008,0.994300790396999,0.9949510169813,0.99556196460308,0.996133609143173,0.99666592803403,0.997158900260614,0.997612506361225,0.998026728428272,0.998401550108975,0.998736956606017,0.999032934678125,0.999289472640589,0.999506560365732,0.9996841892833,0.999822352380809,0.999921044203816,0.999980260856137,1,0.999980260856137,0.999921044203816,0.999822352380809,0.9996841892833,0.999506560365732,0.999289472640589,0.999032934678125,0.998736956606017,0.998401550108975,0.998026728428272,0.997612506361225,0.997158900260614,0.99666592803403,0.996133609143173,0.99556196460308,0.9949510169813,0.994300790396999,0.993611310520008,0.992882604569814,0.992114701314478,0.991307631069507,0.990461425696651,0.989576118602651,0.988651744737914,0.987688340595138,0.986685944207868,0.985644595148998,0.984564334529205,0.98344520499533,0.982287250728689,0.981090517443334,0.979855052384247,0.978580904325472,0.977268123568193,0.975916761938747,0.974526872786577,0.973098510982127,0.971631732914674,0.970126596490106,0.968583161128631,0.967001487762435,0.965381638833274,0.96372367829001,0.962027671586086,0.960293685676943,0.958521789017376,0.956712051558831,0.954864544746643,0.952979341517219,0.951056516295154,0.949096144990295,0.947098304994744,0.945063075179805,0.942990535892865,0.940880768954225,0.938733857653874,0.936549886748192,0.934328942456612,0.932071112458211,0.929776485888251,0.927445153334661,0.925077206834458,0.922672739870115,0.92023184736587,0.917754625683981,0.915241172620918,0.912691587403503,0.910105970684996,0.907484424541117,0.904827052466019,0.902133959368203,0.899405251566371,0.896641036785236,0.893841424151264,0.891006524188368,0.888136448813545,0.885231311332455,0.882291226434953,0.879316310190556,0.876306680043864,0.87326245480992,0.870183754669526,0.86707070116449,0.863923417192835,0.860742027003944,0.857526656193652,0.854277431699295,0.850994481794692,0.847677936085083,0.844327925502015,0.840944582298169,0.837528040042142,0.834078433613171,0.830595899195813,0.827080574274562,0.823532597628427,0.819952109325452,0.816339250717184,0.812694164433094,0.809016994374947,0.805307885711122,0.801566984870877,0.797794439538571,0.793990398647835,0.790155012375691,0.786288432136619,0.782390810576588,0.778462301567023,0.774503060198734,0.770513242775789,0.76649300680935,0.762442511011448,0.758361915288722,0.754251380736104,0.750111069630459,0.745941145424182,0.741741772738739,0.737513117358174,0.73325534622256,0.728968627421412,0.724653130187047,0.720309024887907,0.715936483021831,0.711535677209285,0.707106781186548,0.702649969798849,0.698165418993473,0.693653305812805,0.689113808387349,0.684547105928689,0.679953378722419,0.675332808121024,0.67068557653672,0.666011867434252,0.661311865323652,0.656585755752956,0.651833725300879,0.647055961569444,0.642252653176585,0.63742398974869,0.632570161913125,0.627691361290701,0.622787780488113,0.617859613090334,0.612907053652977,0.607930297694606,0.602929541689025,0.597904983057519,0.59285682016106,0.587785252292473,0.582690479668576,0.577572703422268,0.572432125594591,0.567268949126756,0.56208337785213,0.556875616488188,0.55164587062843,0.546394346734269,0.541121252126876,0.535826794978997,0.530511184306734,0.525174629961296,0.519817342620709,0.514439533781507,0.509041415750371,0.503623201635761,0.498185105339491,0.492727341548292,0.487250125725332,0.481753674101716,0.476238203667939,0.470703932165333,0.465151078077459,0.459579860621488,0.453990499739547,0.448383216090032,0.442758231038902,0.437115766650933,0.431456045680959,0.425779291565072,0.420085728411806,0.414375580993284,0.408649074736349,0.402906435713663,0.397147890634781,0.391373666837202,0.385583992277397,0.379779095521801,0.373959205737801,0.368124552684678,0.362275366704546,0.356411878713251,0.350534320191259,0.344642923174517,0.338737920245292,0.332819544522987,0.326888029654943,0.32094360980721,0.314986519655305,0.309016994374948,0.303035269632774,0.297041581577035,0.291036166828271,0.285019262469976,0.27899110603923,0.272951935517325,0.266901989320376,0.260841506289897,0.254770725683382,0.248689887164855,0.242599230795407,0.236498997023725,0.230389426676591,0.224270760949381,0.218143241396543,0.212007109922055,0.205862608769881,0.199709980514407,0.19354946805086,0.187381314585725,0.181205763627138,0.175023058975276,0.168833444712734,0.162637165194884,0.156434465040231,0.150225589120757,0.144010782552252,0.137790290684638,0.131564359092282,0.125333233564305,0.11909716009487,0.112856384873482,0.10661115427526,0.100361714851215,0.0941083133185144,0.0878511965507432,0.0815906115681575,0.0753268055279326,0.0690600257144061,0.0627905195293136,0.0565185344820247,0.0502443181797697,0.043968118317865,0.0376901826699345,0.0314107590781282,0.0251300954433374,0.018848439715408,0.0125660398833528,0.00628314396555913,0,-0.00628314396555888,-0.0125660398833526,-0.0188484397154082,-0.0251300954433376,-0.0314107590781284,-0.0376901826699347,-0.0439681183178647,-0.0502443181797694,-0.0565185344820244,-0.0627905195293133,-0.0690600257144058,-0.0753268055279328,-0.0815906115681577,-0.0878511965507433,-0.0941083133185145,-0.100361714851215,-0.10661115427526,-0.112856384873481,-0.11909716009487,-0.125333233564304,-0.131564359092283,-0.137790290684637,-0.144010782552252,-0.150225589120756,-0.156434465040231,-0.162637165194884,-0.168833444712734,-0.175023058975276,-0.181205763627137,-0.187381314585725,-0.19354946805086,-0.199709980514407,-0.205862608769882,-0.212007109922055,-0.218143241396542,-0.224270760949381,-0.23038942667659,-0.236498997023725,-0.242599230795407,-0.248689887164855,-0.254770725683382,-0.260841506289897,-0.266901989320375,-0.272951935517325,-0.278991106039229,-0.285019262469976,-0.291036166828272,-0.297041581577035,-0.303035269632773,-0.309016994374948,-0.314986519655305,-0.320943609807209,-0.326888029654943,-0.332819544522986,-0.338737920245291,-0.344642923174517,-0.350534320191259,-0.35641187871325,-0.362275366704546,-0.368124552684677,-0.3739592057378,-0.379779095521801,-0.385583992277397,-0.391373666837202,-0.397147890634781,-0.402906435713662,-0.408649074736349,-0.414375580993284,-0.420085728411807,-0.425779291565072,-0.431456045680959,-0.437115766650933,-0.442758231038901,-0.448383216090032,-0.453990499739546,-0.459579860621488,-0.465151078077458,-0.470703932165333,-0.476238203667939,-0.481753674101715,-0.487250125725332,-0.492727341548292,-0.498185105339491,-0.503623201635761,-0.509041415750371,-0.514439533781507,-0.519817342620709,-0.525174629961296,-0.530511184306734,-0.535826794978996,-0.541121252126876,-0.546394346734269,-0.55164587062843,-0.556875616488188,-0.56208337785213,-0.567268949126756,-0.572432125594591,-0.577572703422267,-0.582690479668576,-0.587785252292473,-0.59285682016106,-0.597904983057518,-0.602929541689025,-0.607930297694605,-0.612907053652977,-0.617859613090334,-0.622787780488112,-0.6276913612907,-0.632570161913124,-0.63742398974869,-0.642252653176584,-0.647055961569444,-0.651833725300878,-0.656585755752956,-0.661311865323652,-0.666011867434251,-0.67068557653672,-0.675332808121024,-0.679953378722419,-0.684547105928688,-0.689113808387348,-0.693653305812805,-0.698165418993473,-0.702649969798849,-0.707106781186547,-0.711535677209285,-0.715936483021831,-0.720309024887907,-0.724653130187046,-0.728968627421411,-0.73325534622256,-0.737513117358174,-0.741741772738739,-0.745941145424182,-0.750111069630459,-0.754251380736104,-0.758361915288722,-0.762442511011448,-0.76649300680935,-0.770513242775789,-0.774503060198734,-0.778462301567023,-0.782390810576588,-0.786288432136619,-0.79015501237569,-0.793990398647835,-0.797794439538571,-0.801566984870876,-0.805307885711122,-0.809016994374947,-0.812694164433094,-0.816339250717184,-0.819952109325452,-0.823532597628427,-0.827080574274562,-0.830595899195813,-0.834078433613171,-0.837528040042142,-0.840944582298169,-0.844327925502015,-0.847677936085083,-0.850994481794692,-0.854277431699295,-0.857526656193652,-0.860742027003944,-0.863923417192835,-0.86707070116449,-0.870183754669526,-0.87326245480992,-0.876306680043863,-0.879316310190556,-0.882291226434953,-0.885231311332455,-0.888136448813544,-0.891006524188368,-0.893841424151264,-0.896641036785236,-0.899405251566371,-0.902133959368203,-0.904827052466019,-0.907484424541117,-0.910105970684996,-0.912691587403503,-0.915241172620918,-0.917754625683981,-0.92023184736587,-0.922672739870115,-0.925077206834458,-0.927445153334661,-0.929776485888251,-0.932071112458211,-0.934328942456612,-0.936549886748192,-0.938733857653874,-0.940880768954225,-0.942990535892864,-0.945063075179805,-0.947098304994744,-0.949096144990294,-0.951056516295154,-0.952979341517219,-0.954864544746643,-0.956712051558831,-0.958521789017376,-0.960293685676943,-0.962027671586086,-0.96372367829001,-0.965381638833274,-0.967001487762435,-0.968583161128631,-0.970126596490106,-0.971631732914674,-0.973098510982127,-0.974526872786577,-0.975916761938747,-0.977268123568193,-0.978580904325472,-0.979855052384247,-0.981090517443334,-0.982287250728688,-0.98344520499533,-0.984564334529205,-0.985644595148998,-0.986685944207868,-0.987688340595138,-0.988651744737914,-0.989576118602651,-0.990461425696651,-0.991307631069506,-0.992114701314478,-0.992882604569814,-0.993611310520008,-0.994300790396999,-0.9949510169813,-0.99556196460308,-0.996133609143173,-0.99666592803403,-0.997158900260614,-0.997612506361225,-0.998026728428272,-0.998401550108975,-0.998736956606017,-0.999032934678125,-0.999289472640589,-0.999506560365732,-0.9996841892833,-0.999822352380809,-0.999921044203816,-0.999980260856137,-1,-0.999980260856137,-0.999921044203816,-0.999822352380809,-0.9996841892833,-0.999506560365731,-0.999289472640589,-0.999032934678125,-0.998736956606018,-0.998401550108975,-0.998026728428272,-0.997612506361225,-0.997158900260614,-0.99666592803403,-0.996133609143172,-0.99556196460308,-0.9949510169813,-0.994300790396999,-0.993611310520008,-0.992882604569814,-0.992114701314478,-0.991307631069507,-0.990461425696651,-0.989576118602651,-0.988651744737914,-0.987688340595138,-0.986685944207868,-0.985644595148998,-0.984564334529205,-0.98344520499533,-0.982287250728689,-0.981090517443334,-0.979855052384247,-0.978580904325472,-0.977268123568193,-0.975916761938747,-0.974526872786577,-0.973098510982127,-0.971631732914674,-0.970126596490106,-0.968583161128631,-0.967001487762435,-0.965381638833274,-0.963723678290009,-0.962027671586086,-0.960293685676943,-0.958521789017376,-0.95671205155883,-0.954864544746643,-0.952979341517219,-0.951056516295154,-0.949096144990295,-0.947098304994744,-0.945063075179805,-0.942990535892864,-0.940880768954226,-0.938733857653874,-0.936549886748193,-0.934328942456612,-0.932071112458211,-0.929776485888251,-0.927445153334662,-0.925077206834458,-0.922672739870115,-0.92023184736587,-0.917754625683981,-0.915241172620918,-0.912691587403503,-0.910105970684996,-0.907484424541117,-0.90482705246602,-0.902133959368203,-0.899405251566371,-0.896641036785236,-0.893841424151264,-0.891006524188368,-0.888136448813544,-0.885231311332455,-0.882291226434954,-0.879316310190556,-0.876306680043864,-0.87326245480992,-0.870183754669526,-0.86707070116449,-0.863923417192835,-0.860742027003943,-0.857526656193653,-0.854277431699295,-0.850994481794692,-0.847677936085083,-0.844327925502016,-0.840944582298169,-0.837528040042142,-0.834078433613171,-0.830595899195813,-0.827080574274562,-0.823532597628428,-0.819952109325452,-0.816339250717184,-0.812694164433094,-0.809016994374948,-0.805307885711122,-0.801566984870877,-0.797794439538571,-0.793990398647835,-0.790155012375691,-0.786288432136619,-0.782390810576588,-0.778462301567023,-0.774503060198734,-0.770513242775789,-0.766493006809351,-0.762442511011448,-0.758361915288722,-0.754251380736104,-0.75011106963046,-0.745941145424182,-0.741741772738739,-0.737513117358174,-0.73325534622256,-0.728968627421412,-0.724653130187046,-0.720309024887907,-0.715936483021831,-0.711535677209286,-0.707106781186548,-0.702649969798849,-0.698165418993473,-0.693653305812805,-0.689113808387349,-0.684547105928688,-0.679953378722419,-0.675332808121025,-0.670685576536721,-0.666011867434252,-0.661311865323652,-0.656585755752957,-0.651833725300879,-0.647055961569444,-0.642252653176584,-0.63742398974869,-0.632570161913125,-0.6276913612907,-0.622787780488113,-0.617859613090335,-0.612907053652976,-0.607930297694606,-0.602929541689025,-0.597904983057519,-0.59285682016106,-0.587785252292473,-0.582690479668576,-0.577572703422268,-0.572432125594591,-0.567268949126757,-0.56208337785213,-0.556875616488187,-0.551645870628431,-0.54639434673427,-0.541121252126876,-0.535826794978997,-0.530511184306734,-0.525174629961296,-0.519817342620709,-0.514439533781507,-0.509041415750372,-0.503623201635761,-0.498185105339491,-0.492727341548292,-0.487250125725333,-0.481753674101715,-0.47623820366794,-0.470703932165333,-0.465151078077459,-0.459579860621488,-0.453990499739547,-0.448383216090033,-0.442758231038902,-0.437115766650933,-0.431456045680959,-0.425779291565073,-0.420085728411806,-0.414375580993284,-0.40864907473635,-0.402906435713663,-0.397147890634781,-0.391373666837202,-0.385583992277397,-0.379779095521801,-0.373959205737801,-0.368124552684678,-0.362275366704546,-0.356411878713251,-0.350534320191259,-0.344642923174518,-0.338737920245291,-0.332819544522987,-0.326888029654942,-0.32094360980721,-0.314986519655305,-0.309016994374948,-0.303035269632775,-0.297041581577035,-0.291036166828272,-0.285019262469976,-0.27899110603923,-0.272951935517325,-0.266901989320376,-0.260841506289897,-0.254770725683382,-0.248689887164855,-0.242599230795408,-0.236498997023725,-0.230389426676591,-0.224270760949382,-0.218143241396542,-0.212007109922055,-0.205862608769881,-0.199709980514407,-0.193549468050861,-0.187381314585725,-0.181205763627138,-0.175023058975276,-0.168833444712734,-0.162637165194883,-0.156434465040231,-0.150225589120757,-0.144010782552252,-0.137790290684639,-0.131564359092283,-0.125333233564305,-0.11909716009487,-0.112856384873482,-0.10661115427526,-0.100361714851215,-0.094108313318514,-0.0878511965507433,-0.081590611568158,-0.0753268055279327,-0.0690600257144062,-0.0627905195293133,-0.0565185344820248,-0.0502443181797693,-0.0439681183178651,-0.0376901826699342,-0.0314107590781284,-0.0251300954433379,-0.0188484397154081,-0.012566039883353,-0.00628314396555881};
static struct gpio_dt_spec sync_pin_spec = GPIO_DT_SPEC_GET(DT_ALIAS(sync0), gpios);
static struct gpio_dt_spec sync1_pin_spec = GPIO_DT_SPEC_GET(DT_ALIAS(sync1), gpios);
- Configure the hardware peripherals
In src/main.cpp
, in the setup_hardware()
function, paste the code below. It initializes the DC/AC buck mode. The O2 converter has two legs which can be operated independently or interleaved. In this example, the legs will be interleaved, which means both will share the total current flowing through the power converter. Both legs are set to work on buck mode. This is done with the function hwConfig.initInterleavedBuckMode()
.
void setup_hardware()
{
hwConfig.setBoardVersion(v_1_1_2);
hwConfig.configureAdcDefaultAllMeasurements();
hwConfig.initInterleavedBuckMode();
hwConfig.setLeg2PhaseShift(0); //sets the second leg at the same phase shift as the first
console_init();
}
- Configure the software scheduling
In src/main.cpp
, in the function setup_software()
, paste the code below. It starts the acquisition. And it starts the control task, the communication task and the application task. Due to its time criticity, the control task has a precise period and with a higher priority over the others.
void setup_software()
{
dataAcquisition.start();
scheduling.startControlTask(loop_control_task,control_task_period);
scheduling.startApplicationTask(loop_application_task,APPLICATION_THREAD_PRIORITY);
scheduling.startCommunicationTask(loop_communication_task,COMMUNICATION_THREAD_PRIORITY)
}
- Loop communication task
In src/main.cpp
, in the list enum serial_interface_menu_mode
, do not modify anything.
enum serial_interface_menu_mode //LIST OF POSSIBLE MODES FOR THE OWNTECH CONVERTER
{
IDLEMODE =0,
SERIALMODE,
POWERMODE,
};
In src/main.cpp
, in the function loop_communication_task()
, paste the code below. This code listens to the serial port. Depending on the character it has received, it switches between IDLEMODE, SERIALMODE and POWERMODE. In POWERMODE, the PWM is enabled thus activating the power conversion.
void loop_communication_task()
{
while(1) {
received_serial_char = console_getchar();
switch (received_serial_char) {
case 'h':
//----------SERIAL INTERFACE MENU-----------------------
printk(" ________________________________________\n");
printk("| ------- MENU --------- |\n");
printk("| press i : idle mode |\n");
printk("| press s : serial mode |\n");
printk("| press p : power mode |\n");
printk("|________________________________________|\n\n");
//------------------------------------------------------
break;
case 'i':
printk("idle mode\n");
mode = IDLEMODE;
break;
case 's':
printk("serial mode\n");
mode = SERIALMODE;
break;
case 'p':
printk("power mode\n");
mode = POWERMODE;
pwm_enable = true;
break;
default:
break;
}
}
}
- Loop application task
In src/main.cpp
, in the function loop_application_task()
, add the following code. The IDLEMODE
stops the power flow, the transmission of data and turns the LED1 off. The SERIALMODE
only turns on the LED1. The POWERMODE
turns the LED1 ON and prints the values of duty_cycle
on the serial monitor, and turns on the inverter mode with a fixed frequency.
void loop_application_task()
{
while(1){
if(mode==IDLEMODE) {
hwConfig.setLedOff();
}else if(mode==SERIALMODE || mode==POWERMODE) {
hwConfig.setLedOn();
printk("%f:", duty_cycle);
printk("%f:", Vhigh_value);
printk("%f:", V1_low_value);
printk("%f:", V2_low_value);
printk("%f:", ihigh_value);
printk("%f:", i1_low_value);
printk("%f\n", i2_low_value);
}
k_msleep(100);
}
}
- Loop control task
The control task turns the power conversion on or off and sends the duty cycle to the power converter.
In src/main.cpp
, in the function loop_control_task()
, add the following code. It states that if either the IDLEMODE
or SERIALMODE
are on, the power conversion stops. If POWERMODE
is on, then the power conversion resumes and the duty cycle is sent to the drivers and to the power hardware. If INVERTERMODE
is on, the power converter will output a triangular wave form with a fixed frequency.
void loop_control_task()
{
// Get measurements
meas_data = dataAcquisition.getVHigh();
if(meas_data!=-10000) Vhigh_value = meas_data;
meas_data = dataAcquisition.getV1Low();
if(meas_data!=-10000) V1_low_value = meas_data;
meas_data = dataAcquisition.getV2Low();
if(meas_data!=-10000) V2_low_value= meas_data;
meas_data = dataAcquisition.getIHigh();
if(meas_data!=-10000) ihigh_value = meas_data;
meas_data = dataAcquisition.getI1Low();
if(meas_data!=-10000) i1_low_value = meas_data;
meas_data = dataAcquisition.getI2Low();
if(meas_data!=-10000) i2_low_value = meas_data;
//Reset counter for synchronization
old_state = pin_state;
pin_state = gpio_pin_get_dt(&sync1_pin_spec);
if (pin_state && !old_state){ //detect rising edge
cpt_step = 0; //reset counter
hwConfig.setLedToggle();
}
if(mode==IDLEMODE || mode==SERIALMODE)
{
pwm_enable = false;
hwConfig.setInterleavedOff();
}
else if(mode==POWERMODE)
{
if(!pwm_enable) {
pwm_enable = true;
hwConfig.setInterleavedOn();
}
//update duty cycle
if (cpt_step<period && cpt_step>=0){
duty_cycle=0.3*sin_angle[(cpt_step+phase_shift)%period]+0.5;
cpt_step ++;
}
if (cpt_step==period/2) gpio_pin_set_dt(&sync_pin_spec,1);
if (cpt_step>=period){
cpt_step=0;
gpio_pin_set_dt(&sync_pin_spec,0);
}
//Sends the PWM to the switches
hwConfig.setInterleavedDutyCycle(duty_cycle);
}
}
- Connect hardware
Now we will connect OwnTech’s O2 to the power supply and to the PC.
⚠️ Make sure that the jumper JP4 is open.- Connect the USB power supply cable. The LED2 of the O2 should be ON.
- Connect the pins Vhigh and GND of the O2 to the DC power supply (set its current limitation at 1 A).
- Connect the micro-JTAG connector of the O2 to the PC thanks to the STLinkV3.
- Switch ON the DC power supply. Choose a voltage between 0 and 48 V.
- Build and Upload (+ ).
- In the bottom toolbar, click on the Serial Monitor icon . Select it and press the
h
key. Remember to click on theopen
button in the SerialPlot application to liberate the serial interface.
- Press the
i
key to switch to IDLEMODE: disables the power conversion, stops printingduty_cycle
and turns the LED1 OFF. - Press the
s
key to switch to SERIALMODE: disables the power conversion, prints theduty_cycle
and turns the LED1 ON. - Press the
p
key to switch to POWERMODE: enables the power conversion, printsduty_cycle
and turns the LED1 ON.
You can measure the sinusoidal output voltage thanks to an oscilloscope. Its mean value is Vdc/2.
- How would you obtain a mean value of 0?
- How can you close the loop and drive the single phase AC?
That’s it!
Contributors
- 2022.06.24: Luiz Villa, Romain Delpoux, Loïc Quéval