#include "user.h"
#include "../API/controllers.h" // Discrete-time controllers
MPPTracker string1_mppt, string2_mppt; // Maximum Power Point Trackers
PIDController Ipv_reg; // Controller for the PV current
tUserSafe UserBackground();
void SlowSubTask();
float SubTaskTimer = 0.0;
bool SubTaskFlag = false;
USER_SAFE UserInit(void)
{
// Configure the main timebases on CLOCK_0:
Clock_SetPeriod(CLOCK_0, (int)(SWITCHING_FREQUENCY));
// Configure the interrupts:
ConfigureMainInterrupt(UserInterrupt, CLOCK_0, 0.5);
RegisterBackgroundCallback(UserBackground);
// Configure the PI controllers:
ConfigPIDController(&Ipv1_reg, 12.0, 0.3, 0.0, 60, -60, SAMPLING_PERIOD, 10);
// Configure and initialize the MPP-trackers:
#define increment 0.01
ConfigMPPTracker(&mymppt, increment, 7.0, 16.0, 1.5, 0.01);
return SAFE;
}
tUserSafe UserInterrupt(void)
{
// Measure all the necessary quantities:
Upv = Adc_GetValue(8); // Voltage on the PV string
Ipv = -Adc_GetValue(0); // PV current
// Execute the current controllers on the MPPT string:
Epv = Upv - RunPIController(&Ipv_reg, Ipv_ref - Ipv);
CbPwm_SetDutyCycle(PWM_CHANNEL_3, Epv/Udc);
// Compute the power drawn from the PV panel:
#define k_iir_lpf 0.05
Ppv = k_iir_lpf* (Upv*Ipv) + (1.0-k_iir_lpf)* Ppv;
SubTaskTimer += SWITCHING_PERIOD;
if(SubTaskTimer >= MPPTPERIOD){
SubTaskTimer = SubTaskTimer - MPPTPERIOD;
SubTaskFlag = true;
}
return SAFE;
}
tUserSafe UserBackground()
{
if(SubTaskFlag)
{
SlowSubTask();
SubTaskFlag = false;
}
return SAFE;
}
void SlowSubTask()
{
// When appropriate, execute the MPPT algorithm:
if (enable_MPPT==1){
// Run the Maximum Power Point Tracking (MPPT) algorithm:
Ipv_ref = RunMPPTracking(&string_mppt, Ipv, Ppv);
}
else{
// Leave the setpoints unaltered
}
}
Lab 8, Working With structs
Programmer: Maiar Khattab
Editor(s) used: Code Blocks 13.12
Compiler(s) used: Code Blocks 13.12
File: prog.cpp
Complied: Dec 17 2024 at 14:13:38
noon is 12:0:0
midnight is 0:0:0
supper is 6:0:0
bedtime is 11:30:0
L?o g� is 1504393768:32764:-1397654528