Measuring Current in a Power Converter Using Fuzzy Automatic Gain Control

: The accuracy of current measurements can be increased by appropriate ampliﬁcation of the signal to within the measurement range. Accurate current measurement is important for energy monitoring and in power converter control systems. Resistance and inductive current transducers are used to measure the major current in AC/DC power converters. The output value of the current transducer depends on the load motor, and changes across the whole measurement range. Modern current measurement circuits are equipped with operational ampliﬁers with constant or programmable gain. These circuits are not able to measure small input currents with high resolution. This article proposes a precise loop gain system that can be implemented with various algorithms. Computer analysis of various automatic gain control (AGC) systems proved the effectiveness of the Mamdani controller, which was implemented in an MCU (microprocessor). The proposed fuzzy controller continuously determines the value of the conversion factor. The system also enables high resolution measurements of the current emitted from small electric loads ( ≥ 1 A) when the electric motor is stationary.


Introduction
Current measurements play a very important role in energy management systems, as well as in vector control of electric motors. The harmonics in the current of an input power converter for a railway vehicle were presented in [1]. The harmonic spectra of traditional and modern trains were presented in [2]. Data acquired from a current pantograph of a 3 kV DC railway locomotive and a 1.5 kV metro vehicle were presented in [3]. In [4], changes in the current peak values of a DC substation were presented. An important example of the use of electronic systems to record the consumption of electrical energy is to calculate the cost of electricity consumed by business entities, such as the supplier (electricity distribution company) and the customer (e.g., an electric vehicle). These calculations are usually made using electricity meters, which are not without limitations. To determine the important operating parameters of a power converter, the current must be measured. The dynamically changing value of the current in a power circuit can affect the metrological properties of the measurement system, depending on the load of the drive system. The main challenge is to properly match the peak current to the input range of the analog-to-digital converter (ADC). When measuring the signal, a dynamic error should be expected. This dynamic error depends on the selection of measuring current transducers, and also on the signal processing procedures. The gain of the measured signal can be measured using a programmable gain amplifier (PGA) or a precision amplifier with a fixed gain factor. This method does not allow use of the upper limit of the dynamic range of the ADC, which operates at the end of the measuring circuit, and therefore does not enable high resolution measurements.
Several methods that allow the value of the measured signal to be adjusted to the measurement range are discussed in the literature. An automatic gain control system has a feedback loop to control the gain factor. In modern applications, AGC circuits are equipped with microcontrollers that are compatible with many signals. An example of an AGC based on a variable gain amplifier (VGA) is given in [5]. In [6], the authors used a programmable amplifier loop control based on input signal amplitude. In [7], an AGC system is described based on a VGA connected in a cascade with a PGA. Programmable gain amplifier circuits are often used in front-end circuits, as demonstrated in [8] for quartz tuning fork (QTF) signal conditioning. The structure of a PGA system controlled by a serial interface with the control algorithm is presented in [9]. A method of designing PGA-based amplification systems using Matlab and Spice is presented in [10]. An accurate method of signal processing using programmable amplifiers can be found in [11]. Fuzzy controllers can also be used to implement an AGC system, as described in [12]. Such systems enable the creation of a hyperplane that acts as an algorithm controlling the operation of the AGC system, without the need for mathematical transformations. These capabilities have led to the implementation of fuzzy controllers into AGC systems. Intelligent current measurement systems increase the reliability of the systems in which they process signals. Smart energy meter are supported by fuzzy logic, as discussed in [13]. An example of the use of a fuzzy logic in an energy management system for food manufacturing processes is presented in [14]. Another use of fuzzy logic to minimize energy consumption in residential buildings was described in [15].
In [16] the author of the current work proposed a measurement system using a fuzzy controller. The circuit contained an analog multiplier, which caused a measurement error. An additional problem with this system was the limited output range of the high-voltage DAC. The present article is a continuation of that work. A precise AGC system is proposed that can be implemented with various algorithms. The inspiration for the creation of the intelligent measurement system came from applying a cybernetic perspective to the use of artificial intelligence (AI) for solving issues related to the problem of measuring current in power inverters with dynamically changing loads. The proposed intelligent system is not based on mathematical relationships that describe the gain factor of the current measurement circuit in a power inverter, but on linguistically formulated rules that allow for the rapid creation of an accurate measurement system. This is an advantage over traditional measurement solutions.
Expert knowledge of the current signal trend, thus, allows for precise modeling of the AGC system. The proposed AGC system has the additional advantage of enabling the introduction of an additional input. Usually, speed feedback is not used in automatic gain control systems. Fuzzy logic allows additional signals to be introduced into the system by creating a premise linked by an appropriate logical operator, as the control variable uses the derivative of the signal (dt(V oIV (t))/dt), which is the product of the value of the shunt resistance R sh and the measured current. This derivative provides information on the rate of change in the measured current. In the case of a high value for V/s, the AGC fuzzy system loses accuracy and does not amplify the measured signal. A steep rise in the peak value of the measuring current increases rapidly towards the upper range limit of the measuring circuit. Measuring this signal at time t 1 using the AGC fuzzy system causes error. The system generates (A) gain only at t 1 +t agc and strengthens V oIV 's signal only at V oIV (t 1 +t agc ) with a higher peak than V oIV (t 1 ). An AGC system operating in this way may result in saturation of the signal, which is associated with the loss of measurement information.
The solution using fuzzy logic is able to generate accurate values continuously for the voltage gain A d = var., and therefore allows for high resolution measurements. The AGC system is able to accurately measure current ≥1 A resulting from a load other than the electric motor. This is important for energy management, motor control, and other systems that use current measurement.
To summarize, in current measuring systems in which the measured quantity changes across the whole measurement range, an appropriate system should be used to adjust the gain. Appropriate gain adjustment increases the resolution of the measurement. AGC systems based on programmable amplifiers or intelligent algorithms are used primarily in converter systems for controlling the operation of electric motors (e.g., in electric vehicles). Here, a system is presented that is created primarily to measure small values for current in the power supply circuit of a power inverter. This current may appear when a power inverter controlled electric motor is not running but other circuits are on. The Fuzzy AGC system enables the measurement of values representing 2% of the rated current. A novel feature of the proposed AGC is the possibility of implementing the system in any structure of microprocessor system. Moreover, a multiplication operation was used in the microprocessor, so an analog multiplier was not needed and the precision was increased. The main problems with using an analog multiplier are the settling time and the phase shift between input and output [17]. Multiplier errors consist primarily of input and output offsets, scale factor errors, and nonlinearity. An expression for the output of a real analog multiplier is given by where k is the scale factor multiplier; ∆k is the scale factor error; V in1 , V in2 is the input signal; V 1os , V 2os is the input offset voltage; V o is the multiplier output offset voltage; f(in, out) is nonlinearity).

Materials and Methods
The main reason for using gain correction circuits in measurement systems is to increase the resolution. This section describes the AGC structures used in different implementations. Metrological problems are caused by the change in the measured values across the entire measuring range. In this case, low-peak signals are not measured accurately, due to the lack of appropriate signal amplification. For example, a 12-bit ADC powered by 5 V reference voltage can measure an input voltage equal to 0.0048 V. The appropriate gain of this signal allows the signal to be recorded using more digital levels. To accomplish this task, computer analysis was performed on various AGC systems. On the basis of the results, a system of signal gain control using a fuzzy controller was selected. To achieve high measurement resolution, the measured signal should be amplified to the upper limit of the dynamic range of the ADC converter. Due to the measurement error of the AGC system and the rapidly changing values, the input range was reduced to 80% of the reference voltage value.
Programmable amplifiers are the most common AGC structures used in measurement systems. These amplifiers are controlled digitally, based on an algorithm implemented in a microprocessor system. The algorithm determines the gain value and selects the closest value from the available gain in the PGA amplifier, in such a way that the measurement signal reaches the maximum value in the ADC range. The operation of a programmable amplifier in a loop is presented in Figure 1 and the tasks are listed below.
Appl. Sci. 2021, 11, x FOR PEER REVIEW 4 of 17 using the PGA circuit or amplifier with a fixed gain factor does not allow the measurement signal to reach the upper limit of the dynamic range of the ADC converter.  In an AGC PGA, electronic circuits need time to perform the following tasks:

•
The PGA amplifier measures the input voltage over time (t p ) in V inPGA (t p ); • The ADC converter supplies conversion data (t ADC ) in (V inPGA (t p +t ADC )); • Determine the gain value (A PGA ) in MCU (t alg ); • Send the order from MCU to the PGA amplifier (t conv ).
According to the signal processing times given in the AGC system, the input signal V inPGA (t p ) is amplified after the time t k = t p + t alg + t conv has elapsed. For this reason, a 20% reduction in the measuring range protects against information loss due to too much amplification of the measured signal. The algorithm implemented in MCU (Figure 1) determines the gain value and selects the closest value from the available gain in the PGA amplifier. The main disadvantage of this system is its inability to generate any gain value. Digital regulation of the conversion factor of the measuring current in the power inverter using the PGA circuit or amplifier with a fixed gain factor does not allow the measurement signal to reach the upper limit of the dynamic range of the ADC converter.
The waveforms in Figure 2a were obtained in a computer simulation of voltage response V outPGA of the PGA amplifier for the input signal V inPGA .   Figure 2b shows the voltage gain generated by the PGA amplifier controlled by a microprocessor system. The large discrepancy between the available gain in the PGA amplifier and the calculated gain does not allow the desired increase in the resolution of the measurement of current in the power inverter.  Figure 2b shows the voltage gain generated by the PGA amplifier controlled by a microprocessor system. The large discrepancy between the available gain in the PGA amplifier and the calculated gain does not allow the desired increase in the resolution of the measurement of current in the power inverter.

Structure of the Automatic Gain Control
The main role of the AGC corrector system is to select a suitable value for the gain of the output signal from the current transducer, and thereby increase measurement accuracy. For this purpose, electronic circuits (i.e., a DAC converter) were used. This system operates on the basis of the value of two input signal peaks and its derivative. On the basis of these signals, the AGC system generates appropriate gains and sends this value through feed-back loops to the amplifying stage. The input signal should be close to 80% of the level of its dynamic range. Figure 3 shows two different electric circuits used to correct the gain of the output signal (V oIV ) from the current transducer. Each of the systems contains a separate ADC M1 or ADC M2 converter, which is used to measure the current signal. Measurement outputs are marked as (ch 1_agc1/ ch 1_agc2 ). of these signals, the AGC system generates appropriate gains and sends this value through feed-back loops to the amplifying stage. The input signal should be close to 80% of the level of its dynamic range. Figure 3 shows two different electric circuits used to correct the gain of the output signal (VoIV) from the current transducer. Each of the systems contains a separate ADCM1 or ADCM2 converter, which is used to measure the current signal. Measurement outputs are marked as (ch1_agc1/ch1_agc2). The AGC corrector system is equipped with isolation amplifiers, shown in Figure 3 as Iso1. Their main role is to provide galvanic isolation between the circuits of the current converter (IV) and the digital circuits of the current meter and the AGC corrector. The current converter converts the measured Ish current into a proportional low-voltage VoIV signal. The first realization procedure for determining the desired value of the voltage gain (Ad) relies on measurement of the peak input voltage VoIV by an auxiliary circuit (auxiliary channel aux_c with a ADCIA converter ( Figure 3a) or ADCd). The obtained information is transmitted to the microprocessor unit (MCU1) memory. Once the values for a few voltage samples VoIV(nT) have been measured (where n is the sample number and T is the sampling interval), their derivative is determined (where * is the crisp value (input or output signal from the fuzzy controller), derivative operator of the I th order). These values are provided at the inputs of the fuzzy system implemented in the microprocessor (MCU1). According to expert knowledge given via rules, the fuzzy controller generates the required gain. This value is converted into an analogue signal (Ad) by the digital-toanalogue converter (DAC1). The required conversion factor of the current measurement The AGC corrector system is equipped with isolation amplifiers, shown in Figure 3 as Iso1. Their main role is to provide galvanic isolation between the circuits of the current converter (IV) and the digital circuits of the current meter and the AGC corrector. The current converter converts the measured I sh current into a proportional low-voltage V oIV signal. The first realization procedure for determining the desired value of the voltage gain (A d ) relies on measurement of the peak input voltage V oIV by an auxiliary circuit (auxiliary channel aux_c with a ADC IA converter ( Figure 3a) or ADC d ). The obtained information is transmitted to the microprocessor unit (MCU1) memory. Once the values for a few voltage samples V oIV (nT) have been measured (where n is the sample number and T is the sampling interval), their derivative is determined (where * is the crisp value (input or output signal from the fuzzy controller), derivative operator of the I th order). These values are provided at the inputs of the fuzzy system implemented in the microprocessor (MCU1). According to expert knowledge given via rules, the fuzzy controller generates the required gain. This value is converted into an analogue signal (A d ) by the digital-to-analogue converter (DAC 1 ). The required conversion factor of the current measurement circuit (ch_agc1) in the analog form and V oIV are applied to the input terminal of the analog multiplier MULT (Figure 3a). The multiplier performs an output function V oIV *A d . Consequently, the ranges between the current converter (IV) output and the current input channel ch_agc1 are matched.
The main problems with the proposed fuzzy collector AGC system (shown in Figure 3a) are the high voltage of the DAC 1 converter, which has limited ability to generate a voltage signal on its output (e.g., the output range for the AD5540 converter is equal 60 V), and the small range of input voltages for the analog multiplier (e.g., for the MPY 634 multiplier the input range is 10 V). The DAC 1 is connected to the input of the multiplier. For this reason, the same fuzzy controller was used in the AGC system as had been implemented in the CPU of the microprocessor (MCU2, see Figure 3b). The value of the desired gain (limited to the floating-point number range: float or double defined in the IEEE 754 standard) is sent to the memory of the microprocessor system. The ADC d converter (Figure 3b) from the structure of the microprocessor was used to measure the V oIV signal. The digitally generated gain value (A d , see signal #2 in Figure 3b) and the peak value (V oIV , see signal #1 in Figure 3b) are multiplied digitally in the memory of the microprocessor. The product is given to the input of the DAC 2 converter (signal #3, see Figure 3b). The DAC 2 transmits the properly amplified input voltage (signal #3, see Figure 3b) to the input of the ADC M2 converter, which allows use of its dynamic range. To determine the signal measured (V oIV ) in the microprocessor system (MCU3, see Figure 3b), information about the designated gain, A d (signal #4, see Figure 3b) is needed.
The fuzzy system shown in Figure 3 consists of three controllers operated in parallel. The shapes of the input/output planes of these controllers differ from each other only in the range of the considered interval (X1-axis V oIV ) for each controller, as follows: The output value of this three-stage structure is selected by the structural program acting as a switch-case. The selection of one output value out of three fuzzy controllers (s, l, h) depends on the peak value of the signal V oIV , as measured directly by the ADC IA converter ( Figure 3). The input ranges of the controllers result from the possibility of spacing the modal of the membership function of fuzzy sets containing the voltage V oIV and its derivative.
There are reports in the literature of AGC circuits based on intelligent algorithms, such as in [12]. Most of the AGC structures are used to amplify signals such as radio waves. To improve the metrological properties of current measurement systems in power inverters, AGC correctors based on programmable amplifiers can be used. The family of integrated electricity meters provides an example of such solutions.

Structure of the Fuzzy Controller
An important feature of fuzzy systems is their robustness under conditions when the system parameters are uncertain. The disturbance attenuation problem of fuzzy large-scale systems was shown in [18]. The robustness of the designed fuzzy controller with complexity reduced was shown in [19]. The grid division of the input space and the overlapping sector should be properly designed [20], and therefore ensure a smooth transition of the measurement point between the sectors of the input space. In fuzzy control, the stability of the system operation can be checked by means of a state phase analysis known as the geometric method [20]. This method reveals how robust the controller can be with an appropriate set of database parameters, especially the premises of the rules. The sliding control ensures the existence of sliding motion, which results in constancy with regard to parameter changes [20]. The sliding law of a fuzzy controller is a combination of sliding control and fuzzy logic. The robustness of fuzzy regulators is a consequence of the proven resistance of the sliding regulator.
Another example of a controlled AGC system is the cascade connection of a PGA amplifier with a VGA. In this structure, the PGA amplifier matches the measured signal to the input VGA. The signal from the shunt output terminals at 1 A current is 125 µV and is amplified by the input amplifier to 0.1 V. then, it is amplified by the PGA with a constant factor of 8 v/v to the level of 0.8 V, while the VGA amplifier with a set gain of 13 dB amplifies the signal to the value of 3.57 V. The measured signal is matched to 80% of the dynamic range of the ADC. The main problem with this AGC solution is that the two integrated circuits are controlled by an algorithm consisting mainly of conditional instructions, which extend the gain control time. The SR (slew rate) coefficient for this structure of AGC can be determined from the formula min{SR OA , SR PGA , SR VGA }. A significant disadvantage of the AGC solutions described above is the changing gain every 2 dB or as a function of 2 n , which makes it impossible to generate a gain signal in a continuously way. The bandwidth is dependent on the gain set by the amplifiers. An additional error in the system comes from the settling time from the amplifier. The use of two programmable circuits allows for better adjustment of the measurement signal to the dynamic range of the ADC. A detailed description of the proposed fuzzy controller is given in [16,[20][21][22][23][24][25][26]. The input/output signals are as follows: (1) For the module (implemented Mamdani-type inference): where V oIV (nT s )* is the instantaneous value of the voltage sample from power inverter measurement current circuit at a discrete time nT s ; n is the sample number, T s is the sample interval; is the derivative operator of the I th order; * is the crisp value; T is the vector transposition; A d * is the output signal of the fuzzy controller, representing the desired gain value; N  [24]. The proposed fuzzy model uses two inputs (V oIV ) and its derivative (d(V oIV )/dt) and contains six fuzzy sets (the database contains 64 rules (2 6 )). The membership functions selected for the construction of the fuzzy controller are triangles, due to their easy implementation in a microprocessor system. The placement of rules in controller space is a more important parameter influencing the accuracy of the AGC system than their number.
Algorithm 1 presents the principle of operation of the AGC for two exemplary activated rules, R 1 and R 2 (see Equation (1)). The V oIV data and the derivative d(V oIV )/dt are entered into the AGC system (see Algorithm 1, points 1 and 2). The V oIV values are checked by conditional statements (see Algorithm 1,points 4 or 14,15). This data is then entered into the input of the controller as crisp values (V oIV (nT) * ; d(V oIV (nT) * /dt) (see Algorithm 1, point 5), where it is subjected to fuzzyfication (see Algorithm 1, point 6). The data is assigned to fuzzy sets with a determination of the degree of membership ([µ A1 (V oIV ), µ A2 (V oIV ), µ B1 (V oIV '), µ B2 (V oIV ')]). Operator MIN was used to determine the weight w j of the rule (see Algorithm 1, point 8). The decision is a fuzzy set C j ' (see Algorithm 1, point 9) whose membership function is determined by equation µ cj' (A d ) = w j *µ cj (A d ). A conclusion from the two rules is given by the equation µ cj ' (A d ) = max(w 1 *µ c1 (A d ), w2*µ c2 (A d )) (see Algorithm 1, point 10).
The conditional if statement implements a sub-scope of the AGC system. Algorithm 1 shows the sub-range for the voltages V oIV > 0.2 V (see Figure 4)   Input of fuzzy controller: V oIV (nT) * ; d(V oIV (nT) * )/dt; 6.
Fuzzification of input data 7.
A d *=(∑µ C (A d(i) )* A d(i) )/(∑µ C (A d(i) )) 13. }  Resistance measuring shunts Rsh (metal alloy with low inductance low and thermal electromotive force EMF < 1 μV/°C) allow for accurate measurement of DC and AC signals, which vary across the whole measuring range. The input range of the resistance shunt is from 1 A to a maximum of 50 A. The current below 1 A is impossible to measure from the 125 μΩ shunt using classical methods and measuring elements. The minimum Resistance measuring shunts R sh (metal alloy with low inductance low and thermal electromotive force EMF < 1 µV/ • C) allow for accurate measurement of DC and AC signals, which vary across the whole measuring range. The input range of the resistance shunt is from 1 A to a maximum of 50 A. The current below 1 A is impossible to measure from the 125 µΩ shunt using classical methods and measuring elements. The minimum current value appears when the electric motor connected to the DC/AC power inverter is turned off, while the main power circuit is loaded by the 1 A current device. Two amplifiers operating in non-inverting configuration are connected to the output terminals of the measuring resistor R sh , creating a system with a differential input and output with a voltage gain constant of 800 V/V. These amplifiers (zero drift) have offset voltage and temperature drift minimization, due to the small measurement signal output from shunt at the minimum current of 1 A (see Figure 4-I L ). The measurement circuit must have an isolated galvanic power supply (e.g., using a push-pull converter consisting of a transformer driver connected to a transformer with a reference voltage). The gain in the measurement circuit should be such that it matches the value of the measured signal to the range of the ADC converter (12-bit SAR type) (see Figure 4). In the case of large changes in the value of the measured current, instead of an operational amplifier a PGA system with programmable gain PGA or a cascade of PGA circuits with a VGA should be used (see Figure 4).
The programmable amplifier has the possibility of voltage gain from 1 v/v to 2048 V/V. In the case of a measuring shunt with a resistance of 125 µΩ, a current of 1 A is amplified by the input amplifier to a voltage level 0.1 V. Next, this signal is amplified by a programmable amplifier so that it reaches a value equal to 80% FS (4 V at V ref = 5 V, and full scale (FS) = V ref ) of the ADC converter. For this purpose, the PGA sets the gain at 32 V/V. In this case, the voltage at its output is equal to 3.2 V, which means that the 80% of the FS range is not used (unused 0.8 V). There are 655 unused transitions (3276 − 2621 = 655). This implies a loss in the converted signal accuracy. The maximum rate of voltage change is described by the slew rate. The measurement structure is estimated on the basis of the relationship min{SR OA , SR PGA } (where SR OA is the slew rate of the input amplifier and SR PGA is the slew rate of the PGA amplifier). These parameters should not differ from each other, otherwise the system will be determined by the inferior parameter. Using a AGC fuzzy system allows for continuity of amplification and adjustment to the ADC dynamic range with an error resulting from the properties of the elements used for its implementation. The proposed solution is simple to implement and enables the measurement of small values of the current.

Simulations of AGC System
To verify the error in the fuzzy corrector's conversion factor (Figure 3b), a family of first-order polynomial signals with various slopes (converted into geometric degrees) was applied to the input terminals (in+, in−: terminal, Figure 3). Matlab/Simulink and Fuzzy Logic ToolBox were used to carry out this analysis. The results are presented in Figure 5.
The voltage waveforms f 2 , f 4 , f 6 shown in Figure 5 Figure 5 (marked in red) show the qualitative advantage of the operators PROD (the type of the implication of tuned fuzzy sets) over conventional mathematical operators used in fuzzy logic. The circles c 1 , c 2 , c 3 in Figure 6 mark the area where a rapid increase appears in the waveforms f 2 , f 4 , f 6 caused by the switchover (switch-case statement) between fuzzy controllers operated in parallel (s, l, h, Figure 4). The tuned membership function and the PROD operator enables the waveform to be smoothed. Fuzzy AGC tuning relies on appropriate membership function spacing while observing the output signal.
The main advantage of a tuned fuzzy controller is to generate a more accurate gain factor in the AGC system. This advantage can be seen between the signals f 1 -f 2 , f 3 -f 4 , f 5 -f 6 shown in Figure 5. The output signals f 1 , f 3 , f 5 in the tuned fuzzy controller increase much faster than in a conventional controller. Thanks to this, they reach 80% of dynamic range of the ADC faster. On the basis of the results shown in Figure 5 and compared to the possibilities for digitally controlled amplifiers, fuzzy controllers have an obvious potential for use in AGC systems. It should be noted that fuzzy controllers and other technologies related to artificial intelligence are increasingly being used in the development of microprocessor systems. The main advantage of fuzzy controllers is the ease with which the input/output plane can be corrected, by changing the parameters of the rules defined by Equation (1) without the need for mathematical modeling. This type of change parameter in the fuzzy controller allows for rapid responses and high accuracy without impacting the computational complexity of the microprocessor system.  The main advantage of a tuned fuzzy controller is to generate a more accurate gain factor in the AGC system. This advantage can be seen between the signals f1-f2, f3-f4, f5-f6 shown in Figure 5. The output signals f1, f3, f5 in the tuned fuzzy controller increase much faster than in a conventional controller. Thanks to this, they reach 80% of dynamic range  The main advantage of a tuned fuzzy controller is to generate a more accurate gain factor in the AGC system. This advantage can be seen between the signals f1-f2, f3-f4, f5-f6 shown in Figure 5. The output signals f1, f3, f5 in the tuned fuzzy controller increase much faster than in a conventional controller. Thanks to this, they reach 80% of dynamic range

Results and Analysis
The fuzzy AGC system used a 32-bit microprocessor system equipped with a 12-bit successive-approximation ADC. The AGC test system corresponds to the structure shown in Figure 4. Figure 4 shows the AGC system implemented in the microprocessor. Other examples of the implementation of fuzzy drivers in microprocessor systems have been presented in [27,28].
To verify the operation of the proposed AGC system, the analog experimental signal (V oIV ) shown in Figure 6 (channel 2) was given at the input.
The output signal (V ch2 ) of the AGC circuit receives 3.98 V, which is close to the desired value of 4 V ( Figure 6, channel 1). The voltage drop shown in Figure 6 (Cs) is a result of switching between controllers working on different ranges (see Figure 4, marked as: l, s, h). Analysis of the signals shown in Figure 6 reveals that the AGC systems implemented on the microprocessor allow for a measuring range of 4 V. To verify the dynamic operation of the AGC system, a square signal was provided at the input ( Figure 6, channel 2). The output (V ch2 ) of the DAC (Figure 6, channel 1) is shifted in phase to the given signal (V oIV ), due to the processing time required by the AGC algorithm. This should be considered in the second measuring circuit (voltage channel) of the electricity meter.
A comparison of the results for AGC systems based on different structures is given in Table 1. The results were obtained by a computer simulation in Matlab/Simulink software for three types of AGC structures, based on a PGA amplifier, a PGA connected to a VGA, and the fuzzy controller. Table 1. Errors in the measurement system using a 12-bit ADC converter supplied with a reference voltage of 5 V. (PGA, gain can be programmed by 2 n with a max value 4096 and VGA, gain range 2-32 dB with 1 dB step, R sh = 125 µΩ, and OA = 800 V/V). The NOT_P symbol in Table 1 indicates that measurement is not possible. This problem occurs with a cascaded connection of two amplifiers (PGA and VGA) and is related to the permitted input range of the VGA. The data in Table 1 prove the effectiveness of AGC systems based on a fuzzy controller. The proposed structure does not have an analog multiplier, which is associated with greater dynamics in relation to other AGCs. An important advantage of the AGC fuzzy system is its ability to measure each signal thanks to a switchable range.

Input
The workbench of AGC circuit with fuzzy logic were presented in Appendix A in Figure A1.

Accuracy of AGC
Determining the measurement error and uncertainty is very important. The estimated errors in this case show the possible measurement spread. Thanks to this analysis, it is possible to reduce the error of the correction system, by finding the element that most affects it. This analysis is used to estimate the safe range within which the signal can be amplified, and therefore not exceed the measurement range.
The main reason for not considering AGC systems containing a PGA amplifier is the lack of continuity in the generation of gain factor, as presented in Figure 2b, A PGA . For this reason, the measurement uncertainty related to the AGC structure was determined based on a microprocessor containing the fewest possible analog electronic elements, which increases its accuracy. In simplified form, the structure from Figure 3b can be presented as shown Figure 7.
amplified, and therefore not exceed the measurement range.
The main reason for not considering AGC systems containing a PGA amplifier is the lack of continuity in the generation of gain factor, as presented in Figure 2b, APGA. For this reason, the measurement uncertainty related to the AGC structure was determined based on a microprocessor containing the fewest possible analog electronic elements, which increases its accuracy. In simplified form, the structure from Figure 3b can be presented as shown Figure 7.  Table 2 shows the most important individual errors for the AGC systems shown in Figure 7. The data given in least significant bits (LBS) in Table 2 are converted into volts using the formula Error in (V) = Error in LBS*(Vref/2 number of bits ). The measured value depends on the measurement function: f = AGC (V1 = Voiv, V2 = VoutADC, V3 = ADSP, V4 = VDAC2) (see Figure 7). To calculate the limiting error in the considered AGC structure, the error of propagation should be used with a confidence level p = 0.95 given by the relationship: where f is the measurement function, Δxj is the individual limiting errors of the electronic component included in the AGC structure, is the sensitivity factor of the function to changes in the input quantity . The limiting errors (variable in Equation (2)) are given by the relationship [30]: where ei is the errors appearing in electronic devices.  Table 2 shows the most important individual errors for the AGC systems shown in Figure 7. The data given in least significant bits (LBS) in Table 2 are converted into volts using the formula Error in (V) = Error in LBS*(V ref /2 number of bits ). The measured value depends on the measurement function: f = AGC (V1 = V oiv , V2 = V outADC , V3 = A DSP , V4 = V DAC2 ) (see Figure 7). To calculate the limiting error in the considered AGC structure, the error of propagation should be used with a confidence level p = 0.95 given by the relationship: where f is the measurement function, ∆xj is the individual limiting errors of the electronic component included in the AGC structure, ∂ f ∂X j is the sensitivity factor of the function to changes in the input quantity X j .
The limiting errors (variable in Equation (2)) are given by the relationship [30]: where e i is the errors appearing in electronic devices. According to Equation (3), it is possible to determine the processing error (root sum square (RSS)) of the ADC converter from the AGC structure (see Figure 7), which can be written as [30]: where ∆ V os is offset voltage, gain is gain error, and I NL is integral nonlinearity. The limiting error of the fuzzy logic controller was determined by A system − A model [31,32] (where m is the number of data) and equals 0.05. For the operational amplifier, DAC errors are determined based on Equations (2) and (3) and catalogue data. The limit error of fuzzy AGC was determined and presented in Figure 8.
where ∆ is offset voltage, is gain error, and is integral nonlinearity. The limiting error of the fuzzy logic controller was determined by e 1 = ( 1 m ) × ∑ |A system − A model | m i=1 [31,32] (where m is the number of data) and equals 0.05. For th operational amplifier, DAC errors are determined based on Equations (2) and (3) and cat alogue data. The limit error of fuzzy AGC was determined and presented in Figure 8. The error ΔAGC_MULT (Figure 8) for an AGC structure with analog multiplier is constan at 0.3 V across the whole measurement range. When the gain control structure is imple mented in the microprocessor (Figure 4), the error ΔAGC1 increases as the input value in creases Voiv. The error ΔAGC1 depends on the input value and the error of the operationa amplifier (see Figure 8 ΔAGC2 which uses a more precise amplifier).
Type B uncertainty is used to estimate the uncertainty associated with the AGC meas urement results in indirect measurements. This uncertainty is defined by the relationship where ∆ is the complex uncertainty of the AGC system, f is the measurement func tion, and is the input quantity. The components of uncertainty B with uniform error distribution are defined by th relationship: where ∆ is limiting error.   (Figure 4), ∆ AGC2 is the limiting error of the AGC structure implemented on a microprocessor with a more accurate amplifier (Figure 4), U is the uncertainty, and V oiv is the value of the input signal.
The error ∆ AGC_MULT (Figure 8) for an AGC structure with analog multiplier is constant at 0.3 V across the whole measurement range. When the gain control structure is implemented in the microprocessor (Figure 4), the error ∆ AGC1 increases as the input value increases V oiv . The error ∆ AGC1 depends on the input value and the error of the operational amplifier (see Figure 8 ∆ AGC2 which uses a more precise amplifier).
Type B uncertainty is used to estimate the uncertainty associated with the AGC measurement results in indirect measurements. This uncertainty is defined by the relationship: where u B ∆ AGC is the complex uncertainty of the AGC system, f is the measurement function, and X j is the input quantity.
The components of uncertainty B with uniform error distribution are defined by the relationship: where ∆ x j is limiting error.
For example, the uncertainty B of ADC is computed by u B ADC = ∆ ADC √ 3 (where ∆ ADC is defined by Equation (4)).
In the last step, the expanded uncertainty determining the uncertainty range of the measurement result should be estimated based on the relationship: In the calculations, k p = 2 was assumed, and assigned a confidence level of p = 0.95. The determined uncertainty is presented in Figure 8 (waveform U). The maximum value for complex uncertainty is equal to 0.95. For this reason, the desired range of the AGC was reduced from 5 to 4 V.
In the processing of dynamic signals, the actual sampling frequency should be determined. This parameter can be determined based on the dependencies, f s = 1/(t ADC + tf uzzy + t DAC ), where t ADC , t DAC are the processing times in the ADC and the DAC, and t fuzzy is the execution time required for the algorithm to determine the gain. For this reason, it is necessary to optimize the operation of the fuzzy controller and reduce the t fuzzy . The DAC converter should have low a settling time of around 5 µs.

Discussion
When using a resistive shunt to measure the current, the output signal must be properly conditioned. The AGC with fuzzy logic provides excellent results with high measurement resolution. This system enables use of all the transitions of the ADC. The SR factor only depends on the input and isolation amplifiers (<2 V/µs). The minimum number of analog components introduces a small settling time (<2 µs) which is important because the system operates in a loop. In the energy meter, when an AGC is used in the current measurement circuit, the loop is first performed, and then in the parallel circuit (voltage channel) voltage is measured (thus, no phase shift is introduced).

Current Shunt
The selection of the shunt and its resistance value depends on the type of the measured signal, operating temperature, current value, and the rated power. In the analyzed case, where the test current did not exceed the value of 50 A, the shunt was selected with a value of 125 µΩ, power rating 36 W with a tolerance of 5%. The thermal noise was computed using the equation KTR sh ( f 1 − f 2 ) (where K is Boltzmann's constant 1.38 × 10 −23 joules/degree Kelvin, T is the absolute temperature ( • C + 273), and ( f 1 − f 2 ) is the bandwidth in hertz) and does not exceed <1 µV.

First Stage of Amplification
Special zero drift amplifiers were used to amplify the output signal from the shunt. This block is a differential-input to differential-output stage. Because the current flow of 1 A on the shunt is 125 µΩ, it generates a voltage of 125 mV. Therefore, the amplifier should have a much lower offset voltage V os . The settling time of the amplifier should be small, t s < 2 µs, which influences the correct operation of the AGC loop gain.
The common mode rejection ratio (CMRR) of this structure depends on the gain factor (10 V/V) and the isolation amplifier improves signal attenuation. Common voltages appear in circuits with power inverters loaded with electric motors. Therefore, the measuring circuit should be considered accordingly. This means that a precise reference voltage should be used.

Automatic Gain Control Using PGA Amplifier
The main problem with PGA systems is the constant gain with step changes, which do not allow the input signal to be amplified to the desired signal level. As a result, the input range of the ADC is not used, and the accuracy is reduced. This limits its ability to measure the minimum current >1 A with high resolution. PGA amplifiers should be chosen with a rail-to-rail input range. The error in these systems is determined by the following relationship: System error gain = reference tolerance + gain error.

Fuzzy Automatic Gain Control
Fuzzy controllers have many advantages in terms of their potential use in AGC systems. Their most important advantages are easy construction and implementation in microprocessor systems. To modify the operation only requires setting the parameters of the rule or changing the parameters of the controller. Their main disadvantage is the speed of operation, which can be minimized by optimizing the number and location of rule entries and by proper processor selection. The accuracy of the controller was computed as having an error level of 0.05, but the error increased in the real system to 0.12. Measurements using the fuzzy AGC in electric motor loaded power inverters allowed for precise monitoring of values ≥1 A from load sources other than the electric drive.

Conclusions
In this research, a new fuzzy automatic gain control method was proposed that increases the resolution of measurement current in power inverter loads in electric motors and other electric devices. The AGC method uses the full dynamic range of the ADC working at the end of the current circuit. For safety reasons, the dynamic range was lowered to 80% of the reference. The system determines the gain using a fuzzy controller. The intelligent system relies on expert knowledge, given via rules. The use of a PGA circuit allows for the gain to be switched based on conditional instructions using a mathematical model. In a real-time system, this task is not easy to solve quickly. The AGC system was tested experimentally in a microprocessor. Laboratory and simulation tests were conducted. The result are presents in Table 2. The maximum signal value converted by the ADC was 3276 (4 V). When a PGA was used to measure 1 A in the structure shown in Figure 4, there were 655 unused transitions out of 3276. This implies a loss in the converted signal accuracy. The proposed AGC based on a fuzzy controller had only two unused transitions (the result obtained in the simulation). In the real system, due to errors, the result increases to 20. These results proved the usefulness of the proposed system. Moreover, the fuzzy error was minimized, which improved the accuracy of the AGC structure. This concept can be considered to be an extension of current measurement systems. Designing such a system requires acquiring knowledge that describes its operation. Most often, the experience of process operators who can achieve the control aim is used. Due to the computational effort, the system works correctly for two inputs. The selection of experts is the biggest problem in AGC design.
In the future, the aim is to use a DSP processor in the AGC system.

Conflicts of Interest:
The author declares no conflict of interest. Table A1 presents the most important parameters of the systems shown in Figure A1. Table A1. List of elements from the circuit AGC in Figure A1.

Resources Parameters
DAC 2 12-bit MCU 3 ADC 10-bit SAR MCU 2 ADC 12-bit SAR Table A1. List of elements from the circuit AGC in Figure A1.