**1. Introduction**

With the fast-growing greenhouse gas emissions and other environmental issues, distributed generations (DGs) are rapidly connected to the electricity network [1,2]. Connecting different distributed energy resources (DER) with a group of loads is defined as a microgrid [3]. It acts as a single controllable entity with respect to the grid where it can be connected or disconnected from the grid to operate in both grid-connected and island modes respectively [3]. Different renewable sources and microgrid objectives such as stability, reliability resource penetration, AC and DC analysis, sustainability analysis, controlling voltage source converter (VSC) and DG integration are recently discussed extensively [4–8]. Integrating DGs with the grid can solve several typical problems of conventional AC network such as energy security and cost saving [2]. Microgrid capability to inject power to the grid while maintaining the system stability after getting disturbed is considered as one of the microgrid challenges [9,10]. Different control techniques such as active–reactive power control (PQ control), active power–voltage control (PV control) and voltage–frequency control (VF control) are utilized to control the DG units and achieve the required goals [11,12]. PQ scheme is used to control the exchanged real and reactive powers between the DG and grid [11]. Vf control is employed to keep the inverter voltage at constant value and return the frequency to its nominal value after getting real power disturbance [12]. For integrating inverter-based DG with the power system, several PQ control schemes such as hysteresis, dead-beat (DB) controllers, proportional–integral (PI) controllers and proportional–resonant (PR) have been proposed [13–19]. Hysteresis control is simple and has fast responses, but the output current contains high ripples leading to poor current quality and finding some di fficulties to design the output filter [13]. DB predictive control is widely used because it o ffers high performance for current-controlled DGs. Nevertheless, it is quite complicated and sensitive to system parameters [14]. If the target is to compensate multiple harmonics behind eliminate the steady-state error and regulate the sinusoidal signals, PR control scheme in the stationary ( α, β) reference frame is popular [15]. However, to maintain good performance, the resonant frequency and the varying grid frequency should be identical [16]. PI controller has many advantages such as instantaneous control, better wave shaping and fixed inverter switching frequency resulting in known harmonics [17–19]. PQ controller usually adopts double loop controls [20]. Based on power target, the outer power loop produces the reference current while the current inner loop plays the role of fine-tuning [20]. The current control is implemented in the rotating (synchronous) *dq* reference frame because the synchronous frame controller can eliminate steady state error and has fast transient response.

In the literature, several PQ control techniques have been presented to control the injected powers of the DGs in the grid-connected microgrid [21–26]. In our previous work [21], a power controller was implemented to calculate the *dq* reference currents using the *dq* output voltages and reference real and reactive powers. Utilizing Newton–Raphson-based parameter estimation and feed forward control approaches, a robust servomechanism voltage controller and a discrete-time sliding mode current controller were used to control the DG power flow in the grid-connected mode [22]. For a single-phase grid-connected fuel cell system, a second order generalized integrator to control the active and reactive powers was presented [23]. Based on six control degrees, an individual-phase decoupled PQ controller was anticipated for a three-phase VSC [24]. In grid-connected microgrid, utilizing maximum power point tracking (MPPT), a PQ control method was proposed to control the power of the solar photovoltaic and battery storage [25]. PQ control was used in the load-following mode and PV control was utilized in maximum power point tracking mode to control a solar photovoltaic in distribution systems [26]. Unfortunately, all the previous presented work su ffers from the bad performance especially under dynamic loads and generation variations since they are relying on deeply empirical engineering rules for designing the multivariable parameters of the PQ controllers [26]. Designing an optimal PQ controller is essentially to overcome the aforementioned problems and to solve the constrained optimization problem [26]. Recently, optimal control using swarm algorithms and popular evolutionary has been e ffectively utilized in power systems and power converters [27–32]. Moreover, designing an optimal PQ controller has been reported in a few works [26,30]. In [26], an optimal active and reactive power control was developed for a three-phase grid-connected inverter in a microgrid by using an adaptive population-based extremal optimization algorithm (APEO). In the grid-connected microgrid, a particle swarm optimization (PSO)-based PQ control technique under variable loads conditions was proposed [30]. This important work has confirmed the importance of PSO in the automatic tuning of PQ control parameters for optimized operation during abrupt load changes. However, this work did not optimize the controller parameters of the two current controllers in the designed control system. Therefore, the problem may not be considered as an incomplete optimization process for designing PQ controllers [26]. Moreover, they did not consider the optimization of the filter components which they have a grea<sup>t</sup> e ffect on the microgrid stability [3]. Additionally, the microgrid will be under more stress and the controller design needs to be more accurate when we have a generation disturbance not a load disturbance as presented in [26].

In this paper, an e fficient PI power controller is proposed to regulate the predefined injected real and reactive powers to the grid. The control problem is optimally designed based on minimizing the error between the calculated and the injected powers to ge<sup>t</sup> the optimal controller parameters. Particle swarm optimization (PSO) is employed to design the controller parameters and LC filter components. Di fferent microgrid structures are implemented and examined in MATLAB. Firstly, the optimal proposed controller is designed to control the injected real and reactive powers of one inverter-based DG. Secondly, the optimal proposed controller is designed for two different rated inverter-based DG units to share their injected powers to the grid. Sever disturbances such as step up/down changes of real and reactive injected powers, three-phase fault and losing DG unit are applied to investigate the proposed controller effectiveness and to ensure the system stability after getting disturbed. Additionally, to validate the usefulness of the proposed controller, the considered microgrid is implemented on real time digital simulator (RTDS). To confirm the effectiveness of the proposed optimal control scheme, it is compared with the exiting work in [21] through extensive simulation and experiments under various disturbances. The results confirm the superiority of the proposed control strategy in providing a fast, accurate and decoupled power control with a lower AC current distortion.

The major contributions of this work are described as follows:

