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
Next revision Both sides next revision
en:sw:01-mervis:advanced-modes-of-digital-outputs-hidden [2018/10/17 09:03]
mkudlacek
en:sw:01-mervis:advanced-modes-of-digital-outputs-hidden [2021/04/29 13:15]
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 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.
Line 21: Line 32:
 The duty cycle, as mentioned above, is the actual value of the regulation. Typically, you will encounter PWM duty cycle represented as a percentage of the ''​ON''​ time in regard of the period. In the Mervis however, **the duty cycle is represented by the number of resolution steps**. Let's see how to calculate this value. The duty cycle, as mentioned above, is the actual value of the regulation. Typically, you will encounter PWM duty cycle represented as a percentage of the ''​ON''​ time in regard of the period. In the Mervis however, **the duty cycle is represented by the number of resolution steps**. Let's see how to calculate this value.
  
-                         resolution - 1 +                           resolution - 1 
-duty_cycle [mervis] = ------------------- * duty_cycle [%] +  duty_cycle [mervis] = ------------------- * duty_cycle [%] 
-                              100+                               ​100
  
 <WRAP center round info 80%> <WRAP center round info 80%>
-The only correct values of duty_cycle [mervis] ​is 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%) 
  
-duty_cycle [%] = 34 +  resolution = 101 steps (0 - 100% in the step of 1%) 
-<​HTML>​ +   
-                         ​resolution - 1                           101 - 1 +  ​duty_cycle [%] = 34 
-duty_cycle [mervis] = ------------------- * duty_cycle [%] = ------------------- * 34 = 34 +   
-                              100                                   100  +                           ​resolution - 1                           101 - 1 
-</​HTML>​+  duty_cycle [mervis] = ------------------- * duty_cycle [%] = ------------------- * 34 = 34 
 +                               ​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 
-duty_cycle [mervis] = ------------------- * duty_cycle [%] = -------------- * 40 = 2+  duty_cycle [mervis] = ------------------- * duty_cycle [%] = -------------- * 40 = 2
                               100                                 ​100 ​                               100                                 ​100 ​
  
Line 56: Line 68:
 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**:​
  
-<​HTML>​ +  ​PWM_Cycle = resolution - 2 
-PWM_Cycle = resolution - 2 +   
-</​HTML>​ +                            4.8 * 10^7 
- +  PWM_Prescale = ------------------------------- - 1 
-                          4.8 * 10^7 +                  frequency * ( resolution - 1)     
-PWM_Prescale = ------------------------------- - 1 +
-                frequency * ( resolution - 1)     +
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
Line 70: Line 80:
 Alternatively,​ to get the **resolution** and **frequency** from **PWM_Cycle** and **PWM_Prescale** values, use these equations: ​ Alternatively,​ to get the **resolution** and **frequency** from **PWM_Cycle** and **PWM_Prescale** values, use these equations: ​
  
-<​HTML>​ +  ​resolution = PWM_Cycle + 2 
-resolution = PWM_Cycle + 2 +   
-</​HTML>​ +                           ​4.8 * 10^7 
- +  frequency = ----------------------------------------  
-<​HTML>​ +               ​( PWM_Cycle + 1) * (PWM_Prescale + 1)     
-                         ​4.8 * 10^7 +
-frequency = ----------------------------------------  +
-             ​( PWM_Cycle + 1) * (PWM_Prescale + 1)      +
-</​HTML>​+
  
 === 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 = 4kHz 
- +   
-<​HTML>​ +  PWM_Cycle = resolution - 2 = 11 - 2 = 9 
-PWM_Cycle = resolution - 2 = 11 - 2 = 9 +   
-</​HTML>​ +                            4.8 * 10^7                      4.8 * 10^7 
- +  PWM_Prescale = ------------------------------- - 1 = --------------------- - 1 = 1199 
-<​HTML>​ +                  frequency * ( resolution - 1)          4000 * ( 11 - 1 ) 
-                          4.8 * 10^7                      4.8 * 10^7 +
-PWM_Prescale = ------------------------------- - 1 = --------------------- - 1 = 1199 +
-                frequency * ( resolution - 1)          4000 * ( 11 - 1 )  +
-</​HTML>​+
  
 === 2. example === === 2. example ===
  
-Resolution = 41 (step 2.5%) +  ​Resolution = 41 (step 2.5%) 
- +   
-Frequency = 0.1Hz +  Frequency = 0.1Hz 
- +   
-<​HTML>​ +  PWM_Cycle = resolution - 2 = 41 - 2 = 39 
-PWM_Cycle = resolution - 2 = 41 - 2 = 39 +   
-</​HTML>​ +                            4.8 * 10^7                      4.8 * 10^7 
- +  PWM_Prescale = ------------------------------- - 1 = ------------------- - 1 = 119999999 
-<​HTML>​ +                  frequency * ( resolution - 1)          0.1 * (41 - 1 ) 
-                          4.8 * 10^7                      4.8 * 10^7 +
-PWM_Prescale = ------------------------------- - 1 = ------------------- - 1 = 12.631 * 10^6 +
-                frequency * ( resolution - 1)          0.1 * (39 - 1 )  +
-</​HTML>​+
  
 <WRAP center round tip 60%> <WRAP center round tip 60%>
Line 118: Line 116:
 === 3. example === === 3. example ===
  
-Resolution = 10001 (step 0.01%) +  ​Resolution = 10001 (step 0.01%) 
- +   
-Frequency = 0.1Hz +  Frequency = 0.1Hz 
- +   
-<​HTML>​ +  PWM_Cycle = resolution - 2 = 10001 - 2 = 9999 
-PWM_Cycle = resolution - 2 = 10001 - 2 = 9999 +   
-</​HTML>​ +                            4.8 * 10^7                        4.8 * 10^7 
- +  PWM_Prescale = ------------------------------- - 1 = ------------------------- - 1 = 47999 
-<​HTML>​ +                  frequency * ( resolution - 1)            0.1 * (10001 - 1 ) 
-                          4.8 * 10^7                        4.8 * 10^7 +
-PWM_Prescale = ------------------------------- - 1 = ------------------------- - 1 = 47999 +
-                frequency * ( resolution - 1)            0.1 * (10001 - 1 )  +
-</​HTML>​+
  
 <WRAP center round tip 60%> <WRAP center round tip 60%>
Line 147: Line 141:
 {{ :​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 **V** 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.