Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:emb10_1 [2024/04/10 18:02] – [Start] ktokarzen:iot-open:practical:hardware:sut:stm32:emb10_1 [2024/04/10 19:09] (current) – [EMB10: Controlling standard servo] ktokarz
Line 1: Line 1:
-====== EMB10: Controlling standard servo ====== +====== STM_10: Controlling standard servo ====== 
-You will learn how to control a standard miniature servo with the STM32 System on Chip. Standard miniature analogue servo is controlled with a PWM signal of frequency 50Hz with a duty cycle period between 1 ms (rotate to 0) and 2 ms (rotate to 180 degrees), where 1.5 ms corresponds to 90 degrees. +You will learn how to control a standard miniature servo with the STM32 System on Chip. Standard miniature analogue servo is controlled with a PWM signal of frequency 50Hz with a duty cycle period between 1 ms (rotate to 0) and 2 ms (rotate to 180 degrees), where 1.5 ms corresponds to 90 degrees. Some servos have other duty cycle minimum and maximum values, always refer to the documentation.
 <note warning>Do not keep the servo rotating when leaving the lab. Implement your code as non-repeating. A servo that remains rotating for a long time may easily wear out its gears, overheat and even burn!</note> <note warning>Do not keep the servo rotating when leaving the lab. Implement your code as non-repeating. A servo that remains rotating for a long time may easily wear out its gears, overheat and even burn!</note>
 A servo has a red arrow presenting the gauge's current position. <note important>Note that servos tend to have relatively high implementation inaccuracy. Moreover, as the arrow is not in the centre of view of the camera but instead to the corner, the reading may be subject to error because of the parallaxes and lens distortion.</note>  A servo has a red arrow presenting the gauge's current position. <note important>Note that servos tend to have relatively high implementation inaccuracy. Moreover, as the arrow is not in the centre of view of the camera but instead to the corner, the reading may be subject to error because of the parallaxes and lens distortion.</note> 
Line 8: Line 8:
 A good understanding of the PWM signal and duty cycle is necessary. In this scenario, we use built-in timers to control the PWM hardware channels of the STM32WB55 chip. In this case, we do not use any external library; instead, we use the hardware timer library built in the Arduino Core STM32 framework for STM32WB55 (stm32duino)(("stm32duino", https://github.com/stm32duino)) so that no additional external libraries will be included in the project. A good understanding of the PWM signal and duty cycle is necessary. In this scenario, we use built-in timers to control the PWM hardware channels of the STM32WB55 chip. In this case, we do not use any external library; instead, we use the hardware timer library built in the Arduino Core STM32 framework for STM32WB55 (stm32duino)(("stm32duino", https://github.com/stm32duino)) so that no additional external libraries will be included in the project.
 \\ \\
-Some servos tend to go below 1 ms and above 2 ms to achieve a full 180-degree rotation range. If there is a need for a high accuracy of rotation, it is possible to fine-tune the minimum and maximum duty cycle values.+Some servos tend to go below 1 ms and above 2 ms to achieve a full 180-degree rotation range. For example, the servo in our laboratory (MG90/SG90) accepts values starting from 500 ms, and ending at 2500 ms. If there is a need for a high accuracy of rotation, it is possible to fine-tune the minimum and maximum duty cycle values.
  
 ===== Suggested Readings and Knowledge Resources ===== ===== Suggested Readings and Knowledge Resources =====
Line 45: Line 45:
 HardwareTimer *MyTimServo;       //Hardware Timer class for Servo PWM HardwareTimer *MyTimServo;       //Hardware Timer class for Servo PWM
 </code> </code>
- 
-MG 90 servos that we use in our lab are specific. As mentioned above, to achieve a full 180-degree rotation range, their minimum and maximum duty cycle timings go far beyond standards. We will create function for calculating the duty cycle for the servo with the angle as the input parameter. 
- 
  
 === Step 2 === === Step 2 ===
Line 53: Line 50:
  
 Create the timer instance type based on the servo pin: Create the timer instance type based on the servo pin:
 +
 +<code c>
 +TIM_TypeDef *SRVInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(SERVO_PWM_PIN), PinMap_PWM);
 +</code>
 <note> <note>
 The same timer is used for the fan in the scenario STM_1A. The same timer is used for the fan in the scenario STM_1A.
 </note> </note>
-<code c> 
-TIM_TypeDef *SRVInstance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(SERVO_PWM_PIN), PinMap_PWM); 
-</code> 
  
 Define the channel for servo: Define the channel for servo:
Line 72: Line 70:
 Configure and start PWM Configure and start PWM
 <code c> <code c>
-MyTimServo->setPWM(channelSRV,SERVO_PWM_PIN,50,5); // 50 Hertz, 5% dutycycle.+MyTimServo->setPWM(channelSRV, SERVO_PWM_PIN, 50, 5); // 50 Hertz, 5% dutycycle.
 </code> </code>
  
 === Step 3 === === Step 3 ===
-Initialise parameters (duty cyclechannelGPIO):+MG 90 servos that we use in our lab are specific. As mentioned aboveto achieve a full 180-degree rotation rangetheir minimum and maximum duty cycle timings go far beyond standards. We will create a function for calculating the duty cycle for the servo with the angle as the input parameter. 
 <code c> <code c>
-srv.attach(SRV_PINPWMSRV_Ch,srv_min_ussrv_max_us);+void fSrvSet(int pAngle) 
 +
 +  if (pAngle<0) pAngle=0;        //check boudaries of angle 
 +  if (pAngle>180) pAngle=180; 
 +  int i_srv=500+(200*pAngle)/18; //minimal duty cycle is 0,5ms, maximal 2,5ms 
 +  MyTimServo->setCaptureCompare(channelSRVi_srvMICROSEC_COMPARE_FORMAT);  //modify duty cycle 
 +};
 </code> </code>
-50Hz frequency is standard, so we do not need to configure it. 
- 
 === Step 4 === === Step 4 ===
-Rotating a servo is as easy as writing the desired angle to the controller class, e.g.:+Rotating a servo is as easy as calling our function with the desired angle as the parameter, e.g.:
 <code c> <code c>
-srv.write(SRV_PIN,180);+fSrvSet(90);
 delay(2000); delay(2000);
 </code> </code>
en/iot-open/practical/hardware/sut/stm32/emb10_1.1712772149.txt.gz · Last modified: 2024/04/10 18:02 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0