Home

用STM32F103C8T6控制伺服电机蓝色药丸

在此项目中,我将向您展示如何将伺服电机与STM32F103C8T6蓝色药丸板接口,并使用电位计和MCU的内置PWM功能控制它。通过了解如何使用STM32控制伺服电机,您可以在几个基于STM32的高级项目中使用这些知识。

Introduction

伺服电机是工业自动化的工作马。例如,它们通常用于工业机器人技术,自动化设备,CNC机器等。

进入嵌入式系统,小型塑料齿轮型伺服电机在业余爱好者和初学者中非常受欢迎。它们用于小型机器人,机器人臂,太阳能电池板定位和其他相关项目。

TowerPro SG90是在小型嵌入式应用中使用的最流行的伺服电动机类型之一。甚至我在几个基于Arduino的应用程序中都使用了相同的功能。

在这个项目中,我将演示与STM32F103C8T6蓝色药丸板的伺服电机接口。在实施此项目时,我无法访问常用的TowerPro SG90伺服电机。

但是我有一个更大的塔普罗MG 996R金属齿轮伺服电动机,我在另一个机器人应用中使用了它。我将解释此伺服电机的规格,您可以关注该项目,因为即使使用较小的SG 90伺服电动机。

关于MG 996R伺服电机的简短说明

TowerPro的MG 996R数字伺服电动机是带有金属齿轮的高扭矩伺服电机。与1.8公斤SG90相比,它的平均失速扭矩为10kg。

MG 996R伺服电机

The angle of rotation of MG996R is approximately 120 degrees i.e. 60 degrees in either direction. This is less compared to the 180 degrees of rotation of the SG90 Servo.

以下是MG996R伺服电机的规格列表。

  • 重量 - 55克
  • Stall Torque – 9.4 kgf-cm at 4.8V and 11 kgf-cm at 6V
  • 电压 - 4.8V - 7.2V
  • 运行电流 - 500mA - 900mA,6V
  • Stall Current – 2.5A at 6V

像SG90一样,MG996R也有三个销钉,橙色PWM引脚,红色VCC引脚和棕色的接地销。PWM信号的周期为20ms,位置为0、60和-60的位置可以设置为脉冲1.5ms,2ms和1ms。

Interfacing Servo Motor with STM32F103C8T6

Even though the MG 996R Servo Motor is bigger and powerful, the connections with respect to the MCU are similar to that of a SG90 Servo Motor. The Orange Wire must be connected to any PWM pin of the MCU.

For controlling the position, a potentiometer can be used and as per the readings from the ADC (whose input is the POT), the angle of rotation can be adjusted.

电路原理图

下图显示了将伺服电机与STM32F103C8T6蓝色药丸板接口的电路图。

Controlling a Servo Motor with STM32F103C8T6 Circuit Diagram

需要组件

  • STM32F103C8T6 Blue Pill Board
  • 伺服电机
  • 10kΩ电位器
  • Connecting Wires
  • USB到UARTAdapter (if programming via UART)

连接解释了

对于此项目,我们必须使用STM32F103C8T6 MCU的ADC和PWM引脚。下图显示了STM32F103C8T6 MCU的引脚布局。

STM32F103C8T6蓝色药根销布局

如您所见,有10个ADC引脚(PA0 - PA7和PB0 - PB1)和15个PWM引脚(PA0 - PA3 - PA3,PA6 - PA6 - PA10,PB0 - PB0 - PB1,PB1,PB6 - PB9)。

因此,PA3用作连接锅和PA0的ADC引脚用作连接伺服电机PWM引脚的PWM引脚。

用于伺服电机控制的STM32编程

使用STM32F103C8T6蓝色药丸控制伺服电机的代码非常简单,实际上,如果您有与Arduino接口伺服电机的经验,那么编写代码非常相似。但是关于ADC,您需要记住一件事。

如果您参考STM32F103C8T6 MCU的数据表,则您会注意到ADC具有12位分辨率。这意味着STM32F103C8T6的ADC的输出的值将在0到2^12之间,即0 - 4096。

Coming to the specification of the MG 996R Servo Motor, the maximum rotation angle is 120 degrees. So, in the program, in order to map the 0 – 4096 to the angle 0 – 120, we have to divide the output value of the ADC by 4096/120 i.e. 34.

If you have SG90 Servo Motor, whose maximum angle is 180 degrees, you have to divide the output of the ADC by 22.

代码

以下是将伺服电机与STM32F103C8T6 MCU连接的简单代码实现。

#include

int servoPin = PA0;
int potPin = PA3;
int potvalue = 0;
int servoAngle = 0;

Servo myServo;

void setup()

{
myservo.attach(伺服);
}

void loop()
{
potvalue = aLATERED(POTPIN);
servoAngle = (potValue / 34);
myServo.write(servoAngle);
delay(100);
}

在职的

该项目的工作非常简单。首先,电位计的值由STM32蓝色药丸MCU的ADC读取。然后将ADC的输出缩放到伺服电动机的旋转角度。

Using this value, the servo motor is driven to that particular angle. Alternatively, you can implement a full sweep functionality between the angles 0 degrees and 120 degrees (in case of MG 996R Servo).

结论

这里实施了用于控制使用STM32F103C8T6蓝色药丸板的简单项目。该项目中使用了一种名为MG 996R的高级伺服电机进行示范。

Leave a Reply

您的电子邮件地址不会被公开。

电子豪华粉丝
<\/i>","library":""}}" data-widget_type="nav-menu.default">
Baidu