📜 ⬆️ ⬇️

Measurement of ore weight by stator current. Practice. Part 1. Signal processing algorithm in MK

In the last article, we looked at the theoretical basis for measuring the weight of ore in a silo vessel for measuring force developed by an engine during lifting. Schematically, they prepared the current and voltage signals of the motor stator for processing them in the MC. In this article we will look at the software implementation of calculating the mass of the lifted load on the MC In order to start writing a program for the MC, it is necessary to figure out how to properly process these signals in the MC. So let's get started.

The current and voltage signals are a sinusoidal signal, which basically contains the main frequency of the mains supply (50 Hz for our realities). We will not speak about harmonics and other components in the mains voltage, they exist and affect the network quality and dynamic properties of electric motors. In any industry, they are struggling with them as much as possible, so that their influence, though not insignificant, is still small. We will not dwell on this.


And again formulas and some theory


Consider the graph of the voltage signal supplied to the analog input of the MK. All calculations, conclusions will be fully valid for the current signal.
')


The virtual zero of the sine wave is at the level of 1.5 Volts, the artificial earth on the Shelter in the scheme. To calculate the effective value of voltage, you need to use the formula:

As you can see, the whole sinusoid is positive and direct use of this formula will not lead to real data. That is, the readings read from the ADC need to be scaled to the realities of the sinusoidal signal, that is, from the positive and negative half-wave. For this, from the read ADC code, you must subtract the ADC code corresponding to our artificial earth, midpoint. That is, our formula will take this form:

The calculation of the ADC code can be implemented in several ways.



In this implementation, we will go in the second way, although we will automate a little this procedure for determining the value of the CED for each channel used. But we will not run ahead of the locomotive.

The more measurements taken during the mains voltage period (that is, the sampling rate), the higher the accuracy of our calculations. Processing the obtained results after scanning the channel before starting a new measurement will impose its limitations on this frequency. Here it is necessary to approach with common sense and hardware capabilities of the MK, which will be assigned this task. Accuracy can be improved if measurements are taken not in one period, but in several. The main condition is that the number of measurements fit a multiple of the periods of the signal under study.

For the current signal, everything is as accurate as the voltage.

This formula is used to calculate active power:

Let's rewrite, taking into account our realities:

So at the same time point, we need to take readings from two ADC channels (voltage and current), then do the necessary calculations, and go to the next measurement, etc. From here about such algorithm is born.

Algorithm to measure values ​​on the MK.


  1. According to the signal from the detector of the beginning of measurements, which is wound up on the leg of the MC with a hardware interrupt handler for changing the signal level, we start a timer to count equal time intervals when measuring current and voltage signals. Set the measurement counter to 0.
  2. Interrupt the timer, we read the ADC code from two analog channels. We make the necessary calculations by preprocessing signals. Increase the measurement counter by 1. If the counter value has not reached the desired value of N measurements, then go to the beginning of step 2.
  3. If the number of measurements is N, stop the timer. Set the flag of the end of measurements.
  4. In the main cycle, according to the flag of the end of measurements, we make final calculations on the measured data.
  5. After all calculations, we start the interruption from the detector of the start of measurements and all iterations are repeated again.


MK program is not limited to this. The results of measurements and calculations will have to interact with the person, in the form of a display on the display, or sent via the interface on a PC, or arrange communication with another display device or device. In this implementation, the MC will interact with the PC via the RS-232 interface using the ModBus RTU protocol. The implementation of this protocol on the MC is not complicated and is not demanding on the hardware resources of the MC.

Choosing a hardware platform and calculating the signal processing sample rate


The requirements for the MK hardware platform for this task are not exorbitant. 8-bit microcontrollers will cope with this task. To visualize the program on the MK and the results obtained, we will use the Proteus simulator. To understand the principles of solving this problem is enough. This simulator supports many different MK, but still not all. In this implementation, I will show an example on the MK from the company Microchip family PIC18F. The model of this MK is in the proteus.

MK PIC18F2580. Consider its main characteristics. Go to the manufacturer's website and download the datasheet for this family. Here is a table of "opportunities" of this MK.


To calculate the sampling rate, i.e. time interruption from the hardware timer, it is necessary to calculate the conversion time for the ADC of two channels, the time for the execution of intermediate results from measurements and leave a little time to execute the rest of the program code. And if this time is scanty, then it makes sense to increase it.

For intermediate calculations, it is necessary to square the measured values ​​and sum up, and then divide by the number of measurements and take the square root. Even dividing an integer is quite an expensive operation for an MC, in terms of speed, so it is better to replace it with a bit shift, that is, the number of measurements should be taken as a multiple of degree 2.

All other conversions will occur after the timer stops, that is, outside the interruption in the main program loop.
The result of the conversion with the ADC in this MK is a 10-bit value, after squaring, we get a 20-bit value. Let us set a 4-byte register of storing the sum of squares of ADC measurements, that is, 32 bits, therefore, the maximum number of measurements, until the overflow of our 32-bit register, is 2 ^ 12. Decent value. To calculate the active power, where there is a sign value, one bit becomes unavailable to us. So the number of measurements decreases to 2 ^ 11. Add common sense and limit the number of measurements N = 2 ^ 9 = 512.

Now we define the number of full periods that will fit into our number of measurements. Take for the beginning 5 full periods, i.e. 100 ms in timeline. Therefore, the hardware timer overflow time is T = 100ms / 512 = 195.3125 μs. Hence the sampling frequency is f = 1 / T = 1 / 195.3125 * 10-6 = 5120 Hz. It is quite imputed value, both for accuracy and the required speed of our MC.

The execution time of one instruction for this MC is max. 0.1 µs. So for the time of the "ticking" of the hardware timer until the next interruption, we will be able to process 1953,125 instructions, which undoubtedly should be enough for us with a "head." But the resulting number is not integer. And the clocking of hardware timers that are 8-bit or 16-bit comes from a clock generator equal to the frequency after the PLL divided by 4. That is, we cannot adjust the timer so that it counts exactly 100 ms for 512 equal measurement intervals . Exit as always a few.



For MC other manufacturers and architectures, this problem may not occur.

In this example, we will not look for an "exotic" quartz resonator. So let's load the processor MK a bit with mathematical operations.

Subtotal

The number of measurements for 5 full periods of the mains voltage will be 500. Hence, the time interval of the hardware timer will be 200 μs. The sampling rate in this case will be 5000 Hz.

Setting the ADC and timer MK


Let us turn to the datasheet for the PIC18F2580 family, in particular, everything related to the ADC. To measure using the ADC, you need to be guided by some aspects:



According to the datasheet, time for a 10-bit conversion is necessary 11 Tad. Where, Tad should be> = 2 μs. This ensures the greatest accuracy when converting.

The time required for charging the capacitor in the ADC module, after connecting the channel to the ADC module, can be calculated using the above method in the datasheet. There is an example, with an indication of the necessary data involved in the calculations. The values ​​included in the calculation of time depend on the design of circuitry and the topology of the printed circuit board. So in this article, they will not be taken into account, and take the example of datasheet as a basis.

The time required for charging the capacitor of the ADC module is taken from the example, which is equal to 12.86 μs. Time for conversion, we calculate based on the inequality Tad> = 2 μs. The conversion clock is configured in the ADCON2 register. And may have the following meanings:



In our case, when clocking MK from 40 MHz after PLL, Tosc = 0.1 μs, therefore, Tad = 3.2 μs, that is, 32 Tosc.

So, for one ADC conversion, it will take time Tac = 12.86 + 11 * 3.2 = 48.06 μs. For two channels 96.12 μs. The hardware timer overflow in our country is 200 µs. Consequently, all calculations remain: 200 - 96.12 = 103.88 μs. Should be enough.
Set up a timer to process values ​​from the ADC.

We will use the TMR0 module. It can work both in 8-bit mode and in 16-bit mode. He has a prescaler. It is clocked both from an external source and from an internal one.

To ensure an exact time interval of 200 µs, it is necessary during a timer interrupt, to load into it a preset value other than 0.

Results display


For communication with the outside world, we will use the usual USART peripheral module, with the implementation of the ModBus RTU protocol. Software implementation for MK family PIC16F described in this article . The register table will be filled as the program is written.

The proteus simulator allows you to use a component that works with the COM port of the system, both virtual and physical. As the end user of data using the ModBus RTU protocol, we will use the OPC server, in principle, of any company. The OPC server provides polling, displaying the survey results with its tools, which in this case is a complete set. Data on the OPC standard, it can transmit to many third-party applications. SCADA packages, for example.

The USART module is configured based on the clock frequency and the necessary exchange rate.

Lyrical digression
Since updating the results of calculations, both over the network and on any indicator used, does not make sense more than once a second, to improve the accuracy of measurements, there is a sense in the main cycle to average the intermediate values ​​by calculating the average value over 8 measurements. That is, after 100 ms measurements, we calculate the root-mean-square values ​​of current, voltage and active power value, in integer arithmetic we average over 8 measurements, and only after that we make final calculations of all necessary values, and transmit these values ​​over the network or map to indicator.


Automating the process of calculating the midpoint


At the beginning of the article, it was announced that the calculation of the midpoint ADC code for each channel would be automated:
“We automate a little this procedure for determining the value of APS0 for each channel used”

Let's look at the algorithm for such an automatic calculation.

We measure the signals by calculating the rms value. At the initial moment of time, when we turned on the device, we have zeros for non-volatile memory of both channels in the non-volatile memory. Turning off the sources of our signals, current and voltage transformers, preferably at the sources. This means that the DC voltage of the midpoint will come to us at the ADC input, with some interference from a long cable line. We calculate the root-mean-square value of the DC voltage according to the established signal measurement algorithm. According to the command, for example, via the network using the ModBus RTU protocol, we enter these calculated values ​​into the LABS for the corresponding channel, and also update the data in non-volatile memory. These values ​​with a well-stabilized power supply of analog circuits and the use of an accurate voltage source will hardly change in time.

In the future, during the operation of the device, it will be possible to carry out these works, for the correction of a CLC. Here we will be able to filter out the constant component of the cable line, if any. In addition, the average point of the current input, after the chip, in another way, with sufficient accuracy, is calculated and is not possible.

Conclusion


We considered an algorithm for measuring signals from current and voltage transformers. Conducted an analysis of the necessary settings of hardware modules MK. Now let's start writing the actual program for the MC.

PS
Since the article turned out to be quite extensive, the description of the resulting program itself, with examples of source codes, will be considered in the following publication .

Source: https://habr.com/ru/post/275459/


All Articles