Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
en:sw:01-mervis:advanced-modes-of-digital-outputs-hidden [2020/05/11 13:54]
avsetula [Setting the PWM in Mervis]
en:sw:01-mervis:advanced-modes-of-digital-outputs-hidden [2021/08/05 12:12]
avsetula
Line 1: Line 1:
 ====== Advanced mode of digital outputs: PWM ====== ====== Advanced mode of digital outputs: PWM ======
 +<WRAP group 100%>
 +<WRAP half column 81%>
 The PWM stands for Pulse-Width Modulation. It is for generating a square signal of given frequency and certain duty cycle on a digital output. Typically, it is used for regulating a DC powered component in a way the analog regulation cannot achieve. The PWM stands for Pulse-Width Modulation. It is for generating a square signal of given frequency and certain duty cycle on a digital output. Typically, it is used for regulating a DC powered component in a way the analog regulation cannot achieve.
 +</​WRAP>​
 +<WRAP half column 15%>
 +;;#
 +<​html><​span class="​dev-tag dev-patron">​Patron</​span></​html>​ \\
 +<​html><​span class="​dev-tag dev-neuron">​Neuron</​span></​html>​ \\
 +<​html><​span class="​dev-tag dev-axon">​Axon</​span></​html>​
 +;;#
 +</​WRAP>​
 +</​WRAP>​
  
-Let's take a 24V DC motor for example. To achieve 100% RPMs, you have to supply it with 24V. Logically, to achieve 10% RPMs, you have to supply it 24V/10 = 2.4V. But if you apply this voltage, the motor will not even start, because of the friction and its load. And if it starts, its torque will be very low. This is one of the downsides of analogue regulation.+Let's take a 24 V⎓ motor for example. To achieve 100 % RPMs, you have to supply it with 24 V⎓. Logically, to achieve 10 % RPMs, you have to supply it 24/10 = 2.4 V⎓. But if you apply this voltage, the motor will not even start, because of the friction and its load. And if it starts, its torque will be very low. This is one of the downsides of analogue regulation
 + 
 +The PWM works **only on digital outputs**, that means it can only output 0 V⎓ or 24 V⎓. To achieve the regulation, the PWM generates a square signal of given frequency. That means it will periodically set the output ''​ON''​ and ''​OFF''​. The parameter of the regulation is the **duty cycle** - a proportion of the ''​ON''​ time and ''​OFF''​ time within one period. When the duty cycle is represented in percents and the value is 50 %, the output signal will be 50 % of the period ''​ON''​ and the rest will be ''​OFF''​. If the duty cycle is 66 %, it will be 66 % of the cycle ''​ON''​ and 34 % ''​OFF''​.
  
-The PWM works **only on digital outputs**, that means it can only output 0V or 24V. To achieve the regulation, the PWM generates a square signal of given frequency. That means it will periodically set the output ''​ON''​ and ''​OFF''​. The parameter of the regulation is the **duty cycle** - a proportion of the ''​ON''​ time and ''​OFF''​ time within one period. When the duty cycle is represented in percents and the value is 50%, the output signal will be 50% of the period ''​ON''​ and the rest will be ''​OFF''​. If the duty cycle is 66%, it will be 66% of the cycle ''​ON''​ and 34% ''​OFF''​. 
  
 ===== Selecting a frequency ===== ===== Selecting a frequency =====
-One of the parameters you need to set is the frequency of the PWM. You have to set this value according to your regulated system. The rule of thumb is - the slower the system you regulate is, the lower the frequency can be and vice versa. Take for example a large water heater. It can take up to several hours to heat a large tank of water to desired temperature,​ therefor we don't need high frequency regulation. Even tenths or hundredths of Hz will be sufficient. For regulating a LED light, we need to set the frequency to at least 100Hz, because lower frequencies would be visible by naked eye.+One of the parameters you need to set is the frequency of the PWM. You have to set this value according to your regulated system. The rule of thumb is - the slower the system you regulate is, the lower the frequency can be and vice versa. Take for example a large water heater. It can take up to several hours to heat a large tank of water to desired temperature,​ therefor we don't need high frequency regulation. Even tenths or hundredths of Hz will be sufficient. For regulating a LED light, we need to set the frequency to at least 100 Hz, because lower frequencies would be visible by naked eye.
  
 Another thing considering the frequency is the switching element you are turning ''​ON''​ and ''​OFF''​. If it is a mechanical relay, you have to use very low frequency or you could destroy it. If it is SSR relay, MOSFET or IGBT transistor, you can set couple of kHz or even MHz (always referr to the datasheet of the component). Another thing considering the frequency is the switching element you are turning ''​ON''​ and ''​OFF''​. If it is a mechanical relay, you have to use very low frequency or you could destroy it. If it is SSR relay, MOSFET or IGBT transistor, you can set couple of kHz or even MHz (always referr to the datasheet of the component).
  
 The frequency of the PWM in UniPi depends on the used resolution. See below. The frequency of the PWM in UniPi depends on the used resolution. See below.
-===== Resolution of the PWM ===== 
  
-Second parameter of the PWM is the **resolution**. This value indicates in **how many steps** we want to divide the regulation. If the resolution is 3, we can set the duty cycle to 3 values: 0%, 50% and 100%. If the resolution is 11, we can set it from 0% to 100% in the steps of 10%.+ 
 +===== Resolution of the PWM ===== 
 +Second parameter of the PWM is the **resolution**. This value indicates in **how many steps** we want to divide the regulation. If the resolution is 3, we can set the duty cycle to 3 values: 0 %, 50 % and 100 %. If the resolution is 11, we can set it from 0 % to 100 % in the steps of 10 %.
  
 In UniPi controllers,​ you can set the resolution up to 65536 steps. In UniPi controllers,​ you can set the resolution up to 65536 steps.
 +
  
 ===== The duty cycle ===== ===== The duty cycle =====
Line 28: Line 42:
 The only correct values of duty_cycle [mervis] are between 0 and resolution - 1. These values you will set from the program. ​ The only correct values of duty_cycle [mervis] are between 0 and resolution - 1. These values you will set from the program. ​
 </​WRAP>​ </​WRAP>​
 +
  
 === 1. example === === 1. example ===
  
-  resolution = 101 steps (0 - 100% in the step of 1%)+  resolution = 101 steps (0-100 % in the step of 1 %)
   ​   ​
   duty_cycle [%] = 34   duty_cycle [%] = 34
Line 39: Line 54:
                                ​100 ​                                  ​100 ​                                ​100 ​                                  ​100 ​
  
-For resolution of 101 steps and desired duty_cycle of 34% we need to set the duty cycle in Mervis to **34**. That was easy, let's try something more complicated.+For resolution of 101 steps and desired duty_cycle of 34 % we need to set the duty cycle in Mervis to **34**. That was easy, let's try something more complicated. 
  
 === 2. example === === 2. example ===
  
-  resolution = 6 steps (0 - 100% in the step of 20%)+  resolution = 6 steps (0-100 % in the step of 20 %)
   ​   ​
-  duty_cycle [%] = 40%+  duty_cycle [%] = 40
   ​   ​
                           resolution - 1                          6 - 1                           resolution - 1                          6 - 1
Line 51: Line 67:
                               100                                 ​100 ​                               100                                 ​100 ​
  
-For resolution of 6 steps and desired duty_cycle of 40% we need to set the duty cycle in Mervis to **2**.+For resolution of 6 steps and desired duty_cycle of 40 % we need to set the duty cycle in Mervis to **2**.
  
-===== Calculations of Mervis configuration parameters ===== 
  
 +===== Calculations of Mervis configuration parameters =====
 Unfortunatelly,​ you cannot set the **frequency** and the **resolution** values in the Mervis directly. The variables for configuring PWM in Mervis are called **PWM_Cycle** and **PWM_Prescale** and you need to calculate their values according to desired **resolution** and **frequency**:​ Unfortunatelly,​ you cannot set the **frequency** and the **resolution** values in the Mervis directly. The variables for configuring PWM in Mervis are called **PWM_Cycle** and **PWM_Prescale** and you need to calculate their values according to desired **resolution** and **frequency**:​
  
Line 64: Line 80:
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
-The **PWM_Cycle** and **PWM_Prescale** variables are 16bit words, therefor you can set only integer values 0 - 65535. If the result of your calculations is real number, you need to either round it, or change the parameters **frequency** or **resolution**.+The **PWM_Cycle** and **PWM_Prescale** variables are 16 bit words, therefor you can set only integer values 0 - 65535. If the result of your calculations is real number, you need to either round it, or change the parameters **frequency** or **resolution**.
 </​WRAP>​ </​WRAP>​
  
Line 74: Line 90:
   frequency = ---------------------------------------- ​   frequency = ---------------------------------------- ​
                ( PWM_Cycle + 1) * (PWM_Prescale + 1)                     ( PWM_Cycle + 1) * (PWM_Prescale + 1)     
 +
  
 === 1. example === === 1. example ===
  
-  Resolution = 11 (duty cycle 0 - 100% with 10% steps)+  Resolution = 11 (duty cycle 0-100 % with 10 % steps)
   ​   ​
-  Frequency = 4kHz+  Frequency = 4 kHz
   ​   ​
   PWM_Cycle = resolution - 2 = 11 - 2 = 9   PWM_Cycle = resolution - 2 = 11 - 2 = 9
Line 86: Line 103:
   PWM_Prescale = ------------------------------- - 1 = --------------------- - 1 = 1199   PWM_Prescale = ------------------------------- - 1 = --------------------- - 1 = 1199
                   frequency * ( resolution - 1)          4000 * ( 11 - 1 )                    frequency * ( resolution - 1)          4000 * ( 11 - 1 ) 
 +
  
 === 2. example === === 2. example ===
  
-  Resolution = 41 (step 2.5%)+  Resolution = 41 (step 2.5 %)
   ​   ​
-  Frequency = 0.1Hz+  Frequency = 0.1 Hz
   ​   ​
   PWM_Cycle = resolution - 2 = 41 - 2 = 39   PWM_Cycle = resolution - 2 = 41 - 2 = 39
Line 102: Line 120:
 As you can see, with given **resolution** and **frequency**,​ the **PWM_Prescale** is larger than its maximum value 65535. We either have to increase the **frequency**,​ **resolution** or **both**. ​ As you can see, with given **resolution** and **frequency**,​ the **PWM_Prescale** is larger than its maximum value 65535. We either have to increase the **frequency**,​ **resolution** or **both**. ​
 </​WRAP>​ </​WRAP>​
 +
  
 === 3. example === === 3. example ===
  
-  Resolution = 10001 (step 0.01%)+  Resolution = 10001 (step 0.01 %)
   ​   ​
-  Frequency = 0.1Hz+  Frequency = 0.1 Hz
   ​   ​
   PWM_Cycle = resolution - 2 = 10001 - 2 = 9999   PWM_Cycle = resolution - 2 = 10001 - 2 = 9999
Line 118: Line 137:
 With increased resolution, we are able to achieve lower frequencies and vice versa. For high frequency PWM, we need to sacrifice the resolution. With increased resolution, we are able to achieve lower frequencies and vice versa. For high frequency PWM, we need to sacrifice the resolution.
 </​WRAP>​ </​WRAP>​
 +
  
 ===== Setting the PWM in Mervis ===== ===== Setting the PWM in Mervis =====
-The **PWM_Cycle** and **PWM_Prescale** are configuration variables of the UniPi module and cannot be changed from the program. Select the UniPi module in the **Left panel**. In the **Properties panel**, you can find the **PWM_Cycle** and **PWM_Prescale** variables. Set those according to your calculations. We will set it to **PWM_Cycle** = 99 and **PWM_Prescale** = 119. This will set the frequency to 4kHz with resolution of 101.+The **PWM_Cycle** and **PWM_Prescale** are configuration variables of the UniPi module and cannot be changed from the program. Select the UniPi module in the **Left panel**. In the **Properties panel**, you can find the **PWM_Cycle** and **PWM_Prescale** variables. Set those according to your calculations. We will set it to **PWM_Cycle** = 99 and **PWM_Prescale** = 119. This will set the frequency to 4 kHz with resolution of 101.
  
 {{ :​en:​sw:​01-mervis:​pwm-01-selecting-module.png?​direct |}} {{ :​en:​sw:​01-mervis:​pwm-01-selecting-module.png?​direct |}}
Line 130: Line 150:
 {{ :​en:​sw:​01-mervis:​pwm-06-searching-for-ports.png?​direct |}} {{ :​en:​sw:​01-mervis:​pwm-06-searching-for-ports.png?​direct |}}
  
-Turn on the **Debugging** and in the column **PLC Value**, you will see the current PWM value of DO ports 1.01 - 1.04. If you didn't change it previously, you should see zeros. Change the value of **PWM_1.01** to 50 and confirm by clicking on {{:​files:​dialog-confirm_button_2.png?​nolink|}} or submiting with **ENTER**.+Turn on the **Debugging** and in the column **PLC Value**, you will see the current PWM value of DO ports 1.01-1.04. If you didn't change it previously, you should see zeros. Change the value of **PWM_1.01** to 50 and confirm by clicking on {{:​files:​dialog-confirm_button_2.png?​nolink|}} or submiting with **ENTER**.
  
 We can hook up the oscilloscope to the UniPi'​s DO 1.01 and check the waveform. We can hook up the oscilloscope to the UniPi'​s DO 1.01 and check the waveform.
Line 136: Line 156:
 {{ :​en:​sw:​01-mervis:​pic_425_2.gif?​direct |}} {{ :​en:​sw:​01-mervis:​pic_425_2.gif?​direct |}}
  
-And we can see the frequency of the wave is 4kHz with 50% duty cycle.+And we can see the frequency of the wave is 4 kHz with 50 % duty cycle. 
  
 ===== What is the magical 4.8 * 10^7 constant? ===== ===== What is the magical 4.8 * 10^7 constant? =====
 In calculations of the **PWM_Prescale**,​ we use constant 4.8 * 10^7. The constant represents 48MHz clock source for the internal timers/​counters which together with PWM_Prescale and PWM_Count set the desired frequency In calculations of the **PWM_Prescale**,​ we use constant 4.8 * 10^7. The constant represents 48MHz clock source for the internal timers/​counters which together with PWM_Prescale and PWM_Count set the desired frequency