HIL Simulation of a Tram Regenerative Braking System

Regenerative braking systems are an efficient way to increase the energy efficiency of electric rail vehicles. During the development phase, testing of a regenerative braking system in an electric vehicle is costly and potentially dangerous. For this reason, Hardware-In-the-Loop (HIL) simulation is a useful technique to conduct the system’s testing in real time where the physical parts of the system are replaced by simulation models. This paper presents a HIL simulation of a tram regenerative braking system performed on a scaled model. First, offline simulations are performed using a measured speed profile in order to validate the tram, supercapacitor, and power grid model, as well as the energy control algorithm. The results are then verified in the real-time HIL simulation in which the tram and power grid are emulated using a three-phase converter and LiFePO4 batteries. The energy flow control algorithm controls a three-phase converter which enables the control of energy flow within the regenerative braking system. The results validate the simulated regenerative braking system, making it applicable for implementation in a tram vehicle.


Introduction
Regenerative braking systems for rail vehicles enable energy savings and a stabilizing effect on the supply network. Depending on the design of the regenerative braking system, there is also the possibility of semi-autonomous driving of the rail vehicle, i.e., driving without energy from the power line. The development of a regenerative braking system is a complex process that requires testing and verification on a rail vehicle after the simulation part of the development. The need for a safer and simpler way to test regenerative braking systems under real conditions has led to the use of Hardware-In-The-Loop (HIL) simulations. Using appropriate models, HIL simulations enable real-time system testing without the need for physical implementation on a real vehicle.
The basic components of the regenerative braking system are the energy storage system, a bidirectional converter connecting the energy storage system to the DC link of the main drive of the rail vehicle, and a control system with a built-in energy storage charging and discharging algorithm. In [1,2], an overview of technological solutions for regenerative braking energy storage and their implementation in practice are given. The energy storage usually consists of supercapacitors or lithium-ion batteries in the vehicle (on-board storage) or outside the vehicle (wayside storage). The authors of [3] discuss the topology and operational concept of on-board energy storage in light railway vehicles, while the authors of [4] present an on-board supercapacitor energy storage device implemented in metro trains. In [5], an optimal localization of wayside energy storage is presented and, in [6], the effect of wayside energy storage on line voltage stabilization is presented. The topologies of bidirectional DC-DC converters for charging and discharging the used energy storage are described in the following works. The authors of [7] give an overview of energy storage systems and associated power converters topologies, the authors of [8] give a review of isolated bidirectional DC-DC converters, the authors of [9] present a comprehensive review of converter topologies and control while the authors of [10] review DC-DC converter topologies and present simulation results of a selected topology. The energy flow in the converter is controlled by an algorithm implemented in the converter's control system. By choosing the right algorithm, a higher system efficiency and a longer lifetime of the energy storage system can be achieved.
In the early stages of regenerative braking system design and energy management algorithm development, mainly electrical equivalent models are used to model the system components. The rule is to use the simplest models that give satisfactorily accurate results.
In [11][12][13], the control algorithms are described in the case of rail vehicle braking energy from the point of view of voltage stabilization of the supply network using wayside energy storage. In [11], smooth changes between charging and discharging states are allowed depending on the current actions of the vehicle near the supercapacitor energy storage. The vehicles, the grid and the energy storage system were modeled using an electrical equivalent model, while the substation was modeled using Thevenin's equivalent model. The results were verified in offline simulations and in real-time operating conditions using a HIL setup, with average energy savings of 13.5%. The authors of [12] use a wayside energy storage system consisting of lithium-ion batteries and a control algorithm that changes the limits of charging and discharging the batteries depending on the load, i.e., traffic congestion. The verification of the algorithm is based on actual measurements of the voltage and current waveforms of the power grid, i.e., the regenerative braking system model is not used in the work. The algorithm is then implemented in Okegawa SS, resulting in 9% average energy savings over several months. In [13], an algorithm was presented that dynamically controls the level of charge and discharge limits depending on the prediction of the level of open circuit voltage, which is performed using a neural network that learned from previous measurements. The offline simulation results in a 9.9% reduction in energy usage and recovers braking energy with greater efficiency. In [14][15][16], more complex control algorithms are described for braking with in-vehicle energy storage systems. In [14], a fuzzy braking energy management system is presented to increase the efficiency of the system represented by the equivalent circuit. The conducted offline simulations show an increase in cumulative output energy of the supercapacitor of 23.7%. The partitioning into subsystems is described in [15], where dynamic programming is used to constantly compute the optimal control values even when modeled by the equivalent circuit. The energy saving from the offline simulation depends on the chosen weight coefficients with a reported maximum of 18.132% energy saving. In [16], a simple algorithm aimed at keeping the supercapacitor module charged in order to limit occurrences of reaching the SOC limits is presented. The algorithm was tested in offline simulations within the MATLAB software, as well as an experimental verification using a track voltage emulator, NI CompactRIO controller and a supercapacitor.
The aforementioned research proposes different energy saving algorithms and different types of regenerative braking system models. Validation of the algorithms is generally carried out in offline simulations, which is simpler and cost effective, and/or implementation in railways, which can be costly and rarely available for multiple tests but gives very accurate results. This paper presents models and simulation experiments for the design and testing of the regenerative braking system that is planned to be installed in an existing tram vehicle. Using the developed models and conducting offline simulations and HIL experiments, the supercapacitor energy storage system is dimensioned and an appropriate control algorithm for energy flow is designed, which further increases the efficiency of the tram vehicle.
The simulation models for offline simulation were developed in the MATLAB/Simulink programming environment and they indicate the necessary capacitance of energy storage device and basic functionality of the energy flow control algorithm. Parameters of these models were determined according to real-time measurement data [17]. To experimentally test the operation of the algorithm in approximately real time, a scaled HIL system was created which fits as a middle step between offline simulations and real-world implementation. The HIL system enables testing of the algorithm as well as the supervision of the current flow in the energy storage system and at the power line connection point. The experiment with HIL is carried out using measured values of the current and voltage of the power grid and the speed and acceleration of the tram. The described HIL system consists of models of the power grid, a tram vehicle, and an energy flow control algorithm for the regenerative braking energy implemented in the dSpace microcontroller. The models in the dSpace microcontroller are connected to the real supercapacitor storage device and the DC-DC converter via a physical interface that consists of batteries and half-bridge bidirectional DC-DC converters. The second section of this paper describes the regenerative braking system, i.e., the basic components of the system, their interrelationships and the basic requirements determined by the desired goal of the energy flow control algorithm. The third section presents the offline simulation model in MATLAB/Simulink and the analysis of the simulation results. In the fourth section, a scaled model of regenerative tram braking system is presented using HIL simulation in real time. In the fifth section, the results of the operation of the developed algorithm are presented and analyzed. The methodology of the paper is illustrated in Figure 1.
Electronics 2021, 10, x FOR PEER REVIEW 3 of 28 these models were determined according to real-time measurement data [17]. To experimentally test the operation of the algorithm in approximately real time, a scaled HIL system was created which fits as a middle step between offline simulations and real-world implementation. The HIL system enables testing of the algorithm as well as the supervision of the current flow in the energy storage system and at the power line connection point. The experiment with HIL is carried out using measured values of the current and voltage of the power grid and the speed and acceleration of the tram. The described HIL system consists of models of the power grid, a tram vehicle, and an energy flow control algorithm for the regenerative braking energy implemented in the dSpace microcontroller. The models in the dSpace microcontroller are connected to the real supercapacitor storage device and the DC-DC converter via a physical interface that consists of batteries and half-bridge bidirectional DC-DC converters. The second section of this paper describes the regenerative braking system, i.e., the basic components of the system, their interrelationships and the basic requirements determined by the desired goal of the energy flow control algorithm. The third section presents the offline simulation model in MATLAB/Simulink and the analysis of the simulation results. In the fourth section, a scaled model of regenerative tram braking system is presented using HIL simulation in real time. In the fifth section, the results of the operation of the developed algorithm are presented and analyzed. The methodology of the paper is illustrated in Figure 1.

Tram Vehicle Regenerative Braking System
In general, a regenerative braking system of a tram vehicle consists of a power supply network, a tram, a supercapacitor energy storage system (SC), a bidirectional DC-DC converter, and an energy flow control algorithm, Figure 2. In this paper, models are developed and simulation experiments are performed to dimension a supercapacitor energy storage system for retrofitting to an existing tram vehicle and to design an appropriate control algorithm of the tram vehicle braking energy.
In order to develop a suitable energy flow control algorithm within the regenerative braking system, a system model is first used in the MATLAB/Simulink environment to test the basic principles of the algorithm through an offline simulation. In this case, the model of the whole system should describe the voltage-current relationships on the power grid as a function of the tram vehicle speed and acceleration with sufficient accuracy to determine the minimum energy storage capacity and the energy flow control algorithm.

Tram Vehicle Regenerative Braking System
In general, a regenerative braking system of a tram vehicle consists of a power supply network, a tram, a supercapacitor energy storage system (SC), a bidirectional DC-DC converter, and an energy flow control algorithm, Figure 2. In this paper, models are developed and simulation experiments are performed to dimension a supercapacitor energy storage system for retrofitting to an existing tram vehicle and to design an appropriate control algorithm of the tram vehicle braking energy. The bidirectional DC-DC converter is modeled as a proportional element because a real converter is much faster than the rest of the system; so, its dynamics in the MATLAB/Simulink model could be neglected. The mathematical models of the regenerative braking system components are determined according to the fundamental physical In order to develop a suitable energy flow control algorithm within the regenerative braking system, a system model is first used in the MATLAB/Simulink environment to test the basic principles of the algorithm through an offline simulation. In this case, the model of the whole system should describe the voltage-current relationships on the power grid as a function of the tram vehicle speed and acceleration with sufficient accuracy to determine the minimum energy storage capacity and the energy flow control algorithm.
The bidirectional DC-DC converter is modeled as a proportional element because a real converter is much faster than the rest of the system; so, its dynamics in the MAT-LAB/Simulink model could be neglected. The mathematical models of the regenerative braking system components are determined according to the fundamental physical laws of vehicle motion and electrical phenomena in the power grid to provide the power required to accelerate and decelerate the vehicle.

Tram Model
The tram is modeled as a dynamic system that determines the acceleration or braking power, i.e., the amount of electricity currently consumed or generated by the vehicle, from a given speed profile. To describe the dynamics of the vehicle motion, it is common to introduce a simplification and assume that there is no lateral or vertical displacement of the vehicle relative to the rails. In this case, the total force acting on the vehicle is represented by the following equation: where f i is the inertial force, f v is the viscous frictional force, f rr is the rolling frictional force, f g is the gravitational force, and f b is the mechanical braking force. In general, it is difficult to determine all the coefficients of each force because the necessary measurements cannot be made. Therefore, in practice, the Davis formula is often used to calculate the force of vehicle motion resistance f r , which replaces the force of viscous friction f v and the rolling friction force f rr , and depends on the current vehicle speed v. The basic form of this formula is: where the coefficients A, B and C are calculated from the vehicle's physical characteristics and driving conditions (e.g., the vehicle is driving through a tunnel, etc.). The inertial force f i is calculated from the following equation: where m is the vehicle mass, and the expression dv dt represents the vehicle's instantaneous acceleration.
The resulting force f t is used to calculate the vehicle's instantaneous power: The input variable to the tram model is the tram speed, and the output is the tram power. The forces f g and f b are constants within the model, and they do not depend upon the current value of the tram speed. The tram power P vehicle is further used to calculate the tram drive current i d : where v l is by the instantaneous value of the power line voltage.

Power Grid Model
The power grid model used is an RL circuit with an ideal DC voltage source, Figure 3. The model input is the instantaneous power line current i l and the output is the instantaneous power line voltage v l .
Electronics 2021, 10, x FOR PEER REVIEW 5 of 28 where vl is by the instantaneous value of the power line voltage.

Power Grid Model
The power grid model used is an RL circuit with an ideal DC voltage source, Figure  3. The model input is the instantaneous power line current il and the output is the instantaneous power line voltage vl. From the model in Figure 3, the power line voltage vl is equal to: where Rm is the resistance of the power grid, Lm is the inductance of the power grid, and Vm is the nominal voltage of the grid. The model does not consider the influence of other vehicles on the network, nor the distance of the observed vehicle from the power substation, which would affect the values of resistance and inductance of the power grid.

Energy Storage System Model
The supercapacitor module model used in this paper consists of a series connection of the capacitance Csc and the resistance Rsc, Figure 4. The capacitance Csc represents the total capacitance and the resistance Rsc represents the total equivalent series resistance of all cells in the module. The magnitudes of these quantities depend on the number and type of interconnection of the cells within the module. In this work, the electrical model of the supercapacitor module is needed to obtain information about the accumulated energy within the module and the magnitude of the voltage. From the model in Figure 3, the power line voltage v l is equal to: where R m is the resistance of the power grid, L m is the inductance of the power grid, and V m is the nominal voltage of the grid. The model does not consider the influence of other vehicles on the network, nor the distance of the observed vehicle from the power substation, which would affect the values of resistance and inductance of the power grid.

Energy Storage System Model
The supercapacitor module model used in this paper consists of a series connection of the capacitance C sc and the resistance R sc , Figure 4. The capacitance C sc represents the total capacitance and the resistance R sc represents the total equivalent series resistance of all cells in the module. The magnitudes of these quantities depend on the number and type of interconnection of the cells within the module. In this work, the electrical model of the supercapacitor module is needed to obtain information about the accumulated energy within the module and the magnitude of the voltage.  The model's input variable is the supercapacitor current isc and the output is the supercapacitor voltage vsc. From Figure 4, the equation describing the supercapacitor voltage is given by:

Energy Flow Control Algorithm
When controlling the flow of regenerative braking, one of two opposing approaches is often used. The first approach is focused on maximizing the energy efficiency and aims to store and use all the energy generated by regenerative braking. The second approach is based on saving the number of charge and discharge cycles of the energy storage.
The goal of the energy flow control algorithm designed in this paper is to store the maximum amount of regenerative braking energy of the vehicle on a given route, with the minimum number of charging and discharging cycles used. The charging and discharging currents of the supercapacitor module depend on the state of charge of the energy storage system, i.e., the supercapacitor voltage, and current of the vehicle drive. In the charging state, the algorithm allows all charging currents until the maximum supercapacitor voltage is reached (e.g., 500 V). In the discharge state, the algorithm allows

Energy Flow Control Algorithm
When controlling the flow of regenerative braking, one of two opposing approaches is often used. The first approach is focused on maximizing the energy efficiency and aims to store and use all the energy generated by regenerative braking. The second approach is based on saving the number of charge and discharge cycles of the energy storage.
The goal of the energy flow control algorithm designed in this paper is to store the maximum amount of regenerative braking energy of the vehicle on a given route, with the minimum number of charging and discharging cycles used. The charging and discharging currents of the supercapacitor module depend on the state of charge of the energy storage system, i.e., the supercapacitor voltage, and current of the vehicle drive. In the charging state, the algorithm allows all charging currents until the maximum supercapacitor voltage is reached (e.g., 500 V). In the discharge state, the algorithm allows all discharge currents, i.e., vehicle acceleration, until the minimum supercapacitor voltage is reached (e.g., 200 V). The transition between the charge and discharge states occurs according to Figure 5 when the vehicle drive current i d crosses the boundary curve I dp or I dn . For example, if the supercapacitor is full, even a small vehicle acceleration current will switch it from the charging state to the discharging state. If the supercapacitor is less full, higher vehicle acceleration currents are required to change the energy flow state, i.e., the supercapacitor module remains in the charging state and the energy required to accelerate the vehicle is drawn from the power grid. The reverse is true when the supercapacitor is in the discharging state. It is assumed that the limiting values of the currents at which the transition between the charging and discharging states of the supercapacitor module occurs are determined by the directional equations: where: The coefficients kp and kn are determined by optimization according to the minimization of the criterion determined by the Number of cycles and Saved energy ratio in Figure 12.

System Model
The simulation model of the regenerative tram braking system in the MATLAB/Simulink software tool is shown in Figure 6. Using this model, the effect of the energy control algorithm, which minimizes the number of charging and discharging cycles while maximizing the energy savings of regenerative braking, on the characteristic test track of the vehicle was studied. For example, if the supercapacitor is full, even a small vehicle acceleration current will switch it from the charging state to the discharging state. If the supercapacitor is less full, higher vehicle acceleration currents are required to change the energy flow state, i.e., the supercapacitor module remains in the charging state and the energy required to accelerate the vehicle is drawn from the power grid. The reverse is true when the supercapacitor is in the discharging state. It is assumed that the limiting values of the currents at which the transition between the charging and discharging states of the supercapacitor module occurs are determined by the directional equations: where: • I dp -charging to discharging threshold [A]; The coefficients k p and k n are determined by optimization according to the minimization of the criterion determined by the Number of cycles and Saved energy ratio in Figure 12.

System Model
The simulation model of the regenerative tram braking system in the MATLAB/Simulink software tool is shown in Figure 6. Using this model, the effect of the energy control algorithm, which minimizes the number of charging and discharging cycles while maximizing the energy savings of regenerative braking, on the characteristic test track of the vehicle was studied. Since the converter model is idealized (no delay, unity gain), the calculated reference current of the supercapacitor iSC_REF is equal to the current of the supercapacitor iSC. The output Scap cycle number counts the changes of state of the supercapacitor, i.e., cycles. For example, the transition from the charging state to the discharging state and then back to the charging state corresponds to one cycle.
The energy flow control algorithm determines the charging and discharging current of the energy storage system depending on the vehicle current and the charging state of the energy storage system according to the control algorithm described previously.

Model Parameters and Inputs
The test route on which all the investigations were carried out is the line number 11 from the Zagreb Electric Tram, which runs for about 60 min through the streets with mixed traffic and a pedestrian zone. The values of the vehicle current and power grid voltage were recorded. The speed and acceleration profiles of the vehicle were also recorded, Figure 7. The energy flow control algorithm determines the charging and discharging current of the energy storage system depending on the vehicle current and the charging state of the energy storage system according to the control algorithm described previously.

Model Parameters and Inputs
The test route on which all the investigations were carried out is the line number 11 from the Zagreb Electric Tram, which runs for about 60 min through the streets with mixed traffic and a pedestrian zone. The values of the vehicle current and power grid voltage were recorded. The speed and acceleration profiles of the vehicle were also recorded, Figure 7.
The parameters of the simulation model in Figure 6 were determined by the Matlab Optimization Toolbox, according to the IAE criterion based on the error between the measured signal at the real system and the model output. The parameters of each subsystem were determined by separate optimization procedures. So, the parameters of the power grid model and the tram vehicle model are obtained in separate optimizations according to the measured signals from the real vehicle.
The parameters of the power grid model are shown in Table 1. The measured power line current i l was used as input signal to the model, and the error signal is determined as a difference between the measured voltage and the voltage generated by the model. Optimization according to the IAE criterion determined the parameters shown in Table 1. The parameters of the simulation model in Figure 6 were determined by the Matlab Optimization Toolbox, according to the IAE criterion based on the error between the measured signal at the real system and the model output. The parameters of each subsystem were determined by separate optimization procedures. So, the parameters of the power grid model and the tram vehicle model are obtained in separate optimizations according to the measured signals from the real vehicle.
The parameters of the power grid model are shown in Table 1. The measured power line current il was used as input signal to the model, and the error signal is determined as a difference between the measured voltage and the voltage generated by the model. Optimization according to the IAE criterion determined the parameters shown in Table 1. The final difference between the measured power line voltage from the Zagreb Electric Tram power grid and the simulated power line voltage after finishing the optimization procedure is shown in Figure 8. The similarity between the waveforms indicates that the modeled tram vehicle and the power grid accurately model the studied tram and power grid.  The final difference between the measured power line voltage from the Zagreb Electric Tram power grid and the simulated power line voltage after finishing the optimization procedure is shown in Figure 8. The similarity between the waveforms indicates that the modeled tram vehicle and the power grid accurately model the studied tram and power grid. The modeled tram vehicle is the tram TMK2200 from the Končar company from Zagreb, Croatia. The tram model parameters are shown in Table 2. The inputs for the optimization are the tram speed, acceleration, and power line current and voltage, while the error signal is calculated as a difference between the measured current and the current generated by the model.  The modeled tram vehicle is the tram TMK2200 from the Končar company from Zagreb, Croatia. The tram model parameters are shown in Table 2. The inputs for the optimization are the tram speed, acceleration, and power line current and voltage, while the error signal is calculated as a difference between the measured current and the current generated by the model. The measured power line current waveform compared to the power line current waveform obtained through simulation using the tram parameters from Table 2 is exhibited in Figure 9.  Table 2.
The parameters of the energy storage system are taken from the technical documentation of the manufacturer of the selected supercapacitor module (Maxwell BMOD0083 P048 B01). The required energy storage system capacitance is determined depending on the regenerative braking energy of the maximally loaded vehicle when stopping from the maximum permissible speed to a complete standstill according to Equation (9): For the tram TMK2200 with a nominal vehicle weight of 56,725 kg, at a permissible speed of 50 km/h, the maximum regenerative braking energy on a straight track is about 1.5 kWh. At a maximum capacitor voltage of 500 V, the required capacitor capacity is approximately 43.7 F.

Offline MATLAB/Simulink Simulation Results
Multiple simulations of the model from Figure 6   The parameters of the energy storage system are taken from the technical documentation of the manufacturer of the selected supercapacitor module (Maxwell BMOD0083 P048 B01). The required energy storage system capacitance is determined depending on the regenerative braking energy of the maximally loaded vehicle when stopping from the maximum permissible speed to a complete standstill according to Equation (9): For the tram TMK2200 with a nominal vehicle weight of 56,725 kg, at a permissible speed of 50 km/h, the maximum regenerative braking energy on a straight track is about 1.5 kWh. At a maximum capacitor voltage of 500 V, the required capacitor capacity is approximately 43.7 F.

Offline MATLAB/Simulink Simulation Results
Multiple simulations of the model from Figure 6 produced the surface plots of the dependence of the number of cycles and stored energy as a function of the slope coefficients of the charging state transition threshold curves k p and k n , shown in Figures 10 and 11.
The dependence of the ratio of the number of cycles and the amount of saved energy, which is used as an optimization criterion, on the slope coefficients that represent the threshold lines between the charging and discharging state are shown in Figure 12.
It is evident from Figure 12 that the surface representing the number of cycles and the saved energy ratio has minimal values for k p > 5 and k n ≥ 2. Since the goal is to achieve the highest possible stored energy, according to Figure 12, the values k p = 13 and k n = 2 were chosen.
The behavior of the system with obtained switching state threshold curve coefficients k p and k n is shown in Figure 13.  The dependence of the ratio of the number of cycles and the amount of saved energy, which is used as an optimization criterion, on the slope coefficients that represent the threshold lines between the charging and discharging state are shown in Figure 12. It is evident from Figure 12 that the surface representing the number of cycles and the saved energy ratio has minimal values for kp > 5 and kn ≥ 2. Since the goal is to achieve the highest possible stored energy, according to Figure 12, the values kp = 13 and kn = 2 were chosen.
The behavior of the system with obtained switching state threshold curve coefficients kp and kn is shown in Figure 13.
The total stored energy on the test route is 6.68 kWh for 8 counted cycles using the

HIL Simulation Experiment
To verify the results of the offline simulation, a HIL simulation of the tram regenerative braking system was conducted. The reason for using the HIL simulation was to check how the system, i.e., the control algorithm, behaves in real time and with the use of an actual supercapacitor module as energy storage. A part of the HIL laboratory setup are mathematical models implemented in dSpace MicroLab Box. These models are connected via a suitable interface to an actual supercapacitor module and a bidirectional DC/DC converter. As an input to this complex model, the measured speed of the tram on the real route for a duration of 60 min was used.

HIL Laboratory Setup
To perform an experiment, a HIL laboratory setup is used that combines: (i) mathematical models of trams and power grid in a real-time computer (dSpace), (ii) hardware emulation models of trams and power grid (DC-DC converters and battery packs), and (iii) the actual supercapacitor energy storage device and the bidirectional DC-DC converter, Figure 14. The total stored energy on the test route is 6.68 kWh for 8 counted cycles using the Scap cycle number output in Figure 6. The stored energy is calculated using the change in the supercapacitor voltage over time.

HIL Simulation Experiment
To verify the results of the offline simulation, a HIL simulation of the tram regenerative braking system was conducted. The reason for using the HIL simulation was to check how the system, i.e., the control algorithm, behaves in real time and with the use of an actual supercapacitor module as energy storage. A part of the HIL laboratory setup are mathematical models implemented in dSpace MicroLab Box. These models are connected via a suitable interface to an actual supercapacitor module and a bidirectional DC/DC converter. As an input to this complex model, the measured speed of the tram on the real route for a duration of 60 min was used.

HIL Laboratory Setup
To perform an experiment, a HIL laboratory setup is used that combines: (i) mathematical models of trams and power grid in a real-time computer (dSpace), (ii) hardware emulation models of trams and power grid (DC-DC converters and battery packs), and (iii) the actual supercapacitor energy storage device and the bidirectional DC-DC converter, Figure 14.
The mathematical models of the tram, power grid and controller, previously developed in MATLAB/Simulink were converted to C code using Matlab's real-time toolbox and compiled for real-time execution on the target system dSpace MicroLab Box. In addition, the PI controllers and PWM generators to drive the Danfoss converter are also implemented in the dSpace controller. The user control interface is implemented by the Control Desk software of the dSpace system. All models of the system and controllers are implemented as subsystem Model shown in Figure 15.
The tram model calculates the tram current i d_ref based on the tram speed. The energy flow control system calculates the reference value of the supercapacitor current i SC_ref . The power grid model calculates the power line voltage reference v l_ref , from the calculated power line current by solving the power grid model (Equation (6)), Figure 16. The measured drive and power line currents were calculated from the measured inductor currents and duty-cycle of the respective half-bridge ( Figure 16). They are calculated because it was not possible to measure the input current of the half-bridge. The mathematical models of the tram, power grid and controller, previously developed in MATLAB/Simulink were converted to C code using Matlab's real-time toolbox and compiled for real-time execution on the target system dSpace MicroLab Box. In addition, the PI controllers and PWM generators to drive the Danfoss converter are also implemented in the dSpace controller. The user control interface is implemented by the Control Desk software of the dSpace system. All models of the system and controllers are implemented as subsystem Model shown in Figure 15.   To convert the calculated quantities (i d_ref , i SC_ref , v l_ref ) from the dSpace computer model into physical quantities and connect them to the real supercapacitor, the emulation part of the laboratory setup is used. The emulation part is performed using a three-phase converter connected to the corresponding energy storage devices via inductors. For this purpose, the three-phase output stage of the Danfoss FC 301 converter with two battery packs of LiFePO4 batteries (48 V and 12 Ah) and a supercapacitor stack are used. The converter branches with the inductors serve as bidirectional DC-DC converters. The input stage of the converter is not used, while the DC link represents the power line of the tram vehicle and tram DC-link at the same time, i.e., the current summation point. The connection between the dSpace output signals and the converter switches is made via optical fibers connected through an IPC board.
One branch of the battery-powered converter emulates the tram current. In dSpace, the calculated tram current i d_ref represents the reference for the emulated current in this branch of the converter. To ensure that the current in this branch is equal to the reference, a corresponding controller has been developed in dSpace to control switches T3 and T4, shown in the block Control in Figure 14. To convert the calculated quantities (id_ref, iSC_ref, vl_ref) from the dSpace computer model into physical quantities and connect them to the real supercapacitor, the emulation part of the laboratory setup is used. The emulation part is performed using a three-phase converter connected to the corresponding energy storage devices via inductors. For this purpose, the three-phase output stage of the Danfoss FC 301 converter with two battery packs of LiFePO4 batteries (48 V and 12 Ah) and a supercapacitor stack are used. The converter branches with the inductors serve as bidirectional DC-DC converters. The input stage of the converter is not used, while the DC link represents the power line of the tram vehicle and tram DC-link at the same time, i.e., the current summation point. The connection between the dSpace output signals and the converter switches is made via optical fibers connected through an IPC board.
One branch of the battery-powered converter emulates the tram current. In dSpace, the calculated tram current id_ref represents the reference for the emulated current in this branch of the converter. To ensure that the current in this branch is equal to the reference, a corresponding controller has been developed in dSpace to control switches T3 and T4, shown in the block Control in Figure 14. The second branch of the converter emulates the power line voltage. In dSpace, the calculated power line voltage v l_ref represents the reference voltage at the DC link of the converter. To ensure that the voltage at the DC link corresponds to the power line voltage, a suitable controller was developed to control switches T1 and T2.
The third branch of the converter is connected to the actual Maxwell supercapacitor module BMOD0083 P048 B01. The control model in dSpace calculates the target charging and discharging current of the supercapacitor based on the calculated vehicle current, the power line current and the measured supercapacitor voltage and, according to the tested algorithm, controls the switches T5 and T6. The switches T5 and T6 represent the bidirectional DC-DC converter.
A diagram of the controller from the Control block in Figure 15 to control the switches T1-T6 of a three-phase converter is shown in Figure 17. The control loop sampling time is 83 µs and is determined by the switching frequency of the inverter. The energy flow control algorithm operates with a sampling time of 1 s. The structure of the controller for setting the DC link voltage, the converter branch current emulating the tram vehicle and the supercapacitor current according to the reference values from the model in Figure 17 are shown in Figure 18. The controller is PI type and allows manual reference tracking or automatic operation.
The State Machine block in Figure 19 determines the boot sequence. Since the input rectifier part of the Danfoss FC301 converter is not used, a state machine is created to boot the converter, bringing the converter to the operating point so that its electronic circuits are ready for operation. The startup sequence raises the voltage to 200 V using an external source and contactor. After starting the converter's control electronics and control systems, the voltage drop is monitored and, at 100 V, the converter's branch is switched on, which together with the battery emulates the power grid. Then, the control system balances the voltages at the outputs of the other converter branches with the voltages at the battery and supercapacitor. After the contactor is switched on, the current control is started with the current setpoint 0. Only then the converter is ready to switch on the tram and grid model. The structure of the controller for setting the DC link voltage, the converter branch current emulating the tram vehicle and the supercapacitor current according to the reference values from the model in Figure 17 are shown in Figure 18. The controller is PI type and allows manual reference tracking or automatic operation. The State Machine block in Figure 19 determines the boot sequence. Since the input rectifier part of the Danfoss FC301 converter is not used, a state machine is created to boot the converter, bringing the converter to the operating point so that its electronic circuits are ready for operation. The startup sequence raises the voltage to 200 V using an external source and contactor. After starting the converter's control electronics and control systems, the voltage drop is monitored and, at 100 V, the converter's branch is switched on, which together with the battery emulates the power grid. Then, the control system balances the voltages at the outputs of the other converter branches with the voltages at the battery and supercapacitor. After the contactor is switched on, the current control is started with the current setpoint 0. Only then the converter is ready to switch on the tram and grid model.
For the laboratory setup, the voltages, and currents of the components of the real system are scaled to amounts corresponding to the operating voltage of the DC link of the converter. Maximum and minimum power grid voltages of 690 V and 630 V are scaled to 120 V and 90 V, respectively. Maximum tram and grid line currents of up to 900 A are scaled to currents in the 10 A converter branch. The scaled currents and voltages will result in different EMI relative to EMI due to the actual currents and voltages. Due to the scaled currents and voltages, the capacitance of the available supercapacitor module is oversized. For this purpose, the operating voltage of the supercapacitor was scaled from 200 V to 500 V to the 30 V to 31.5 V range. An affine transformation was used to scale the voltage, while the currents were scaled by a linear transformation to obtain the current equilibrium at the contact line. Prior to the emulator test, a simulation was performed with the MATLAB/Simulink model using pre-scaled quantities. The overall laboratory setup is shown in Figure 20. For the laboratory setup, the voltages, and currents of the components of the real system are scaled to amounts corresponding to the operating voltage of the DC link of the converter. Maximum and minimum power grid voltages of 690 V and 630 V are scaled to 120 V and 90 V, respectively. Maximum tram and grid line currents of up to 900 A are scaled to currents in the 10 A converter branch. The scaled currents and voltages will result in different EMI relative to EMI due to the actual currents and voltages. Due to the scaled currents and voltages, the capacitance of the available supercapacitor module is oversized. For this purpose, the operating voltage of the supercapacitor was scaled from 200 V to 500 V to the 30 V to 31.5 V range. An affine transformation was used to scale the voltage, while the currents were scaled by a linear transformation to obtain the current equilibrium at the contact line. Prior to the emulator test, a simulation was performed with the MATLAB/Simulink model using pre-scaled quantities. The overall laboratory setup is shown in Figure 20. A SCADA system was developed using the Control Desk software environment in conjunction with the dSpace computer to control the entire system and to start and monitor the startup sequence. The SCADA system allows both the start of the start sequence and the manual setting of parameters during the start as well as the integration of the system into the automatic mode. All voltages and currents in the system are measured via the Aalborg interface, currents via measuring probes and fed to the dSpace computer via analog inputs. All results are recorded on the computer and transferred to a PC in MATLAB format via the Control Desk system so that they are ready for further display and processing.
The remaining two blocks are used to receive the measured current and voltage signals (analog inputs) and as digital outputs to control the converter switches.

HIL Experiment Results
The experiment was performed during a time period determined by the duration of the input signal of the measured speed of the tram vehicle, Figure 7. The measurement of current and voltage of the system controlled by the proposed control algorithm during the regenerative braking energy was performed in the laboratory system. The recorded and denormalized measurement results are shown in Figure 21. A SCADA system was developed using the Control Desk software environment in conjunction with the dSpace computer to control the entire system and to start and monitor the startup sequence. The SCADA system allows both the start of the start sequence and the manual setting of parameters during the start as well as the integration of the system into the automatic mode. All voltages and currents in the system are measured via the Aalborg interface, currents via measuring probes and fed to the dSpace computer via analog inputs. All results are recorded on the computer and transferred to a PC in MATLAB format via the Control Desk system so that they are ready for further display and processing.
The remaining two blocks are used to receive the measured current and voltage signals (analog inputs) and as digital outputs to control the converter switches.

HIL Experiment Results
The experiment was performed during a time period determined by the duration of the input signal of the measured speed of the tram vehicle, Figure 7. The measurement of current and voltage of the system controlled by the proposed control algorithm during the regenerative braking energy was performed in the laboratory system. The recorded and denormalized measurement results are shown in Figure 21. A comparison of the denormalized results from Figure 21 with the simulation results presented in Figure 13 shows a good agreement of the results with minor deviations at small currents due to measurement noise.
The denormalized results of the supercapacitor current and voltage measurements on a magnified time scale are demonstrated in Figure 22. The measurements show the agreement of the currents with the simulation results in Figure 13. The voltage deviations of the supercapacitor were caused by a small measurement range and the measurement noise, which significantly affects the current control. An additional deviation from the simulation is given by the power consumption of the converter itself, which takes energy from the DC link. A comparison of the denormalized results from Figure 21 with the simulation results presented in Figure 13 shows a good agreement of the results with minor deviations at small currents due to measurement noise.
The denormalized results of the supercapacitor current and voltage measurements on a magnified time scale are demonstrated in Figure 22. The measurements show the agreement of the currents with the simulation results in Figure 13. The voltage deviations of the supercapacitor were caused by a small measurement range and the measurement noise, which significantly affects the current control. An additional deviation from the simulation is given by the power consumption of the converter itself, which takes energy from the DC link.
The supercapacitor current obtained through offline simulation, HIL experiment and the error signal that is obtained as the difference between the offline and HIL experiment is presented in Figure 23.
The waveforms indicate a congruity between the offline simulation and HIL experiment. The error signal values indicate that noticeable differences occur at different times, mainly due to two reasons: (i) time delays between similar waveform shapes caused by the PI controllers in the HIL simulation (denoted with 1 on the figure), (ii) measurement and modeling uncertainties caused by the scaled supercapacitor voltage range which results in charge/discharge state changes in the HIL simulation that do not occur during the offline simulation (denoted with 2 on the figure). Despite the error signal values, it is shown that the energy flow control algorithm functions properly during the HIL simulation.
These experimental results demonstrate the feasibility of a regenerative brake energy storage system for tram vehicles using the proposed energy management algorithm. This provided the basis for building a prototype system with real performance in a tram vehicle. The supercapacitor current obtained through offline simulation, HIL experiment and the error signal that is obtained as the difference between the offline and HIL experiment is presented in Figure 23. The waveforms indicate a congruity between the offline simulation and HIL experiment. The error signal values indicate that noticeable differences occur at different times, mainly due to two reasons: (i) time delays between similar waveform shapes caused by the PI controllers in the HIL simulation (denoted with 1 on the figure), (ii) measurement and modeling uncertainties caused by the scaled supercapacitor voltage range which results in charge/discharge state changes in the HIL simulation that do not occur during the offline simulation (denoted with 2 on the figure). Despite the error signal values, it is shown that the energy flow control algorithm functions properly during the HIL simulation.
These experimental results demonstrate the feasibility of a regenerative brake energy storage system for tram vehicles using the proposed energy management algorithm. This provided the basis for building a prototype system with real performance in a tram 1 2 Figure 23. Comparison of the supercapacitor current obtained from offline simulation, HIL experiment and the error signal obtained from the difference between the offline simulation and HIL experiment.
Measurements on the tram vehicle and simulation of the system according to real driving operation showed that the tram can achieve a maximum of 1.5 kWh of regenerative braking energy at reduced driving speeds. The nominal voltage of the power grid is 600 V; therefore, a voltage of the supercapacitor module of 500 V is proposed. The reason for this is the need to design a bidirectional DC-DC converter that works as a step-down converter during charging, but as a step-up converter during discharging. Taking the operating voltage of a supercapacitor from 200 V to 500 V, it follows that a braking energy of 1.5 kWh would require a supercapacitor capacitance of 51.4 F. In order to realize a prototype with real powers and energies, it is necessary in the next research phase to use the converter of the main drive of the tram as an interface to the supercapacitor. An additional advantage can be achieved by using the position information of the tram control lever, which determines the acceleration.

Conclusions
Mathematical models and offline simulations are mainly used in the designing of an energy flow control algorithm for tram vehicle regenerative braking systems. The developed control algorithm is then mostly tested on an actual system. Due to the amount of voltage and current quantities in this system, it is not common to check the designed algorithm using HIL simulation models in real-time conditions. In this paper, a design process of an energy flow control algorithm, in which a simulation using a HIL laboratory setup is introduced between offline simulation and verification on an actual system, is introduced. In this way, the real-time behavior of the algorithm was further tested considering the actual supercapacitor energy storage device and the influence of the bidirectional DC-DC converter. Using the defined mathematical models, a simulation model of the regenerative braking system of a tram vehicle was developed in the MATLAB/Simulink programming environment. The model parameters were determined with satisfying accuracy by optimization procedures and current-voltage measurements during acceleration and braking of the tram. Using offline simulations, based on the measured speed profile of the tram on the characteristic route, the influence of the proposed algorithm on the energy saving and the number of the supercapacitor energy storage cycles was investigated. In the optimization procedure, the values of the coefficients of the transition lines from the state of charging to discharging the storage and vice versa were determined in such a way that the objective function is minimized. Offline simulations of the proposed algorithm showed that the tested supercapacitor storage with a maximum voltage of 500 V and a capacity of 43.7 F, on the test route, allows energy savings of 6.68 kWh with 8 charge and discharge cycles.
After testing the functionalities of the algorithm in the MATLAB/Simulink simulation environment, a HIL type laboratory setup was developed as a combination of a model in the computer for approximately real-time operation and a physical model for emulating the calculated quantities. A bidirectional DC-DC converter and real supercapacitor energy storage device were included in this setup as real components. This setup additionally tested the behavior of the chosen supercapacitor storage device and the proposed algorithm under more realistic conditions, with constraints set by the actual elements of the system. The results of the HIL experiment show that the behavior of the developed algorithm in approximately real time and the chosen supercapacitor storage device meet the set requirements. Further research will consist of testing the algorithm on the tram vehicle which will enable fine tuning of the algorithm behavior.