搜索代码

第85课:伺服电机入门 | Arduino逐步课程

第85课:伺服电机入门 | Arduino逐步课程

本项目指南将带您了解连续旋转舵机的世界,也称为360度舵机。与移动到特定角度(如0到180度)的标准舵机不同,这些电机可以沿任一方向连续旋转。这使得它们非常适合构建小型机器人、坦克履带,或任何需要简单、经济实惠的驱动电机的项目。本指南将向您展示如何使用Arduino和几个按钮来控制它,让您亲手控制其方向和速度。

以下是360度舵机的一些实际应用:

  • 构建小型两轮机器人小车。
  • 创建可无限旋转的云台摄像机支架。
  • 驱动小型传送带或绞盘。
  • 制作简单的雷达或传感器扫描平台。

硬件与组件

对于本项目,您需要以下组件:

  • Arduino开发板(例如Uno)
  • 连续旋转舵机(例如FS90R或类似型号)
  • 三个按钮
  • 跳线
  • 面包板(可选,便于接线)

接线指南

本项目的接线非常简单。舵机有三根线:通常为棕色(地线)、红色(电源)和橙色或黄色(信号线)。按钮采用简单配置进行接线。

以下是连接方式的详细说明:

  • 舵机:
    • 棕色线(地线)连接到Arduino的GND引脚。
    • 红色线(电源)连接到Arduino的5V引脚。
    • 橙色/黄色线(信号线)连接到Arduino的9号引脚。
  • 按钮:
    • 每个按钮的一侧连接到Arduino的GND引脚。
    • “逆时针”按钮的另一侧连接到2号引脚。
    • “停止”按钮的另一侧连接到3号引脚。
    • “顺时针”按钮的另一侧连接到4号引脚。

在视频中,制作者强调舵机的信号线必须连接到Arduino上支持PWM的引脚,这些引脚通常标有波浪号(~)符号。9号引脚就是其中之一。按钮使用Arduino的内部上拉电阻,这简化了接线,无需外部电阻(视频中07:02处)。

代码说明

代码旨在读取三个按钮的状态,并向舵机发送相应的命令。让我们分解一下用户可配置的部分。

配置引脚和命令

在代码顶部,您会找到引脚和舵机控制值的定义。这是您可以自定义项目行为的地方。


const int servoPin = 9;    // 舵机信号的PWM引脚
const int stopPin = 3;     // 停止按钮的引脚
const int cwPin = 4;       // 顺时针按钮的引脚
const int ccwPin = 2;      // 逆时针按钮的引脚

这些常量定义了连接到按钮和舵机的Arduino引脚。如果您为按钮使用不同的引脚,则需要在此处更改数字。servoPin必须是支持PWM的引脚。


const int csServoCommand[3] = {106, 52, 0};  // {逆时针, 停止, 顺时针}
const String csServoCommandText[3] = {"逆时针", "已停止", "顺时针"};

这是需要理解和调整的最重要部分。csServoCommand数组保存发送到舵机的值。对于连续舵机,这些值不表示角度,而是表示速度和方向。

  • 106是逆时针全速旋转的值。
  • 52是完全停止的值。
  • 0是顺时针全速旋转的值。

这些值并非通用。由于制造差异,“停止”值(52)可能需要调整。如果按下停止按钮时舵机缓慢漂移,您需要微调此值。视频演示了这一过程,展示了将值更改为54或51如何影响电机行为(视频中13:45处)。

您也可以调整旋转值来改变电机速度。例如,将逆时针值设置为70将使其比106时旋转得更慢(视频中12:23处)。

csServoCommandText数组保存打印到串行监视器以显示当前命令状态的文本字符串。

自定义函数:servoCommand()

代码使用自定义函数来处理命令逻辑。该函数接受一个数字(0、1或2)作为参数,更新状态文本,并向舵机发送相应的命令。


void servoCommand(int n) {
  statusText = csServoCommandText[n];
  myservo.write(csServoCommand[n]);
  Serial.println(csServoCommandText[n]);
  Serial.println(csServoCommand[n]);
}

当按下按钮时,此函数从主循环中调用。它集中了更新显示和控制舵机的逻辑,使代码更简洁且更易于修改。

实际项目与演示

在视频中,制作者演示了项目的实际运行。按下“顺时针”按钮会向舵机发送值0,使其沿一个方向旋转。按下“逆时针”按钮会发送值106,使其沿另一个方向旋转。“停止”按钮发送52,应使电机停止。

串行监视器用于显示当前状态以及发送到舵机的确切值。这对于调试以及为您的特定电机找到完美的“停止”值非常宝贵。如视频中所述,由于舵机的内部电路,停止命令可能并不完美,您可能会看到缓慢的漂移或振动。这是较便宜的连续旋转舵机的常见特性,可以通过在代码中调整停止值来最小化(视频中01:15处)。

章节

  • [00:05] 360度舵机电机介绍
  • [02:02] 组件和硬件概述
  • [02:40] 详细接线图和连接
  • [05:07] 代码解释:常量、数组和设置
  • [09:56] 代码解释:servoCommand函数
  • [10:54] 现场演示和串行监视器输出
  • [12:23] 调整舵机速度和停止值

图像

SG90 Mini Servo Motor
SG90 Mini Servo Motor
SG90_servo_motor-1
SG90_servo_motor-1
115-Arduino code to control continuous 360° servo from serial monitor command
语言: C++
/* 
 *  
 *  Demonstration of Controlling Continuous Servo (360 servo)
 *  This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor. 
📚⬇️ Download and resource page https://robojax.com/RJT762
 *  
 * Modified by Ahmad Shamshiri for Robojax.com
 * on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
 * Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
 * Get this code from Robojax.com
 * 
 Original code by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.
 Modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}



void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("received: ");
                Serial.print (incomingByte);
                if(incomingByte == 108){
                 Serial.println(" sent 0 Rotating CW "); 
                 myservo.write(0); 
                }else if(incomingByte == 114){
                  Serial.println(" sent 180 Rotating CCW "); 
                  myservo.write(180); 
                }else if(incomingByte == 60){
                  Serial.println(" sent Stopped "); 
                  myservo.write(60); 
                }else{
                  Serial.println(" moving Randomly"); 
                  myservo.write(incomingByte); 
                }
                  
                 
        }

  
}
893-Arduino code to control continuous 360° servo from serial monitor command (Code 2)
语言: C++
/* 
 *  
 *  Demonstration of Controlling Continuous Servo (360 servo)
360 Servo -2
 *  This code allows you to control a 360-degree servo by a command from the Serial Monitor.
* this 350 servo motor is a just for fun. it can't be used for real application where you need exact positioning. get stepper motor or expensive servo motor. 
📚⬇️ Download and resource page https://robojax.com/RJT762
 *  
 * Modified by Ahmad Shamshiri for Robojax.com
 * on Sunday, July 1, 2018, at 11:09 AM in Ajax, Ontario, Canada
 * Watch a video instruction of this project: https://youtu.be/b_xvu6wWafA
 * Get this code from Robojax.com
 * 
 Original code by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.
 Modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int servoPin = 9;// this pin must be of those with PWM ~
int pos = 0;    // variable to store the servo position
int incomingByte = 0;   // for incoming serial data
int CWBS, CCWBS, SBS;

void setup() {
  Serial.begin(9600);
 pinMode(2,INPUT_PULLUP);// set pin for push button STOP
  pinMode(3,INPUT_PULLUP);// set pin for push button CCW  
  pinMode(4,INPUT_PULLUP);// set pin for push button CW
   
  myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
}



void loop() {
    CCWBS = digitalRead(2);// read status of button CCW
    SBS = digitalRead(3);// read status of button STOP
    CWBS = digitalRead(4);// read status of button CW
                 if(CCWBS == LOW){
                 Serial.println(" sent 0 Rotaing CW "); 
                 myservo.write(0); 
                }else if(CWBS == LOW){
                  Serial.println(" sent 180 Rotaing CCW "); 
                  myservo.write(180); 
                }else if(SBS == LOW){
                  Serial.println(" sent Stopped "); 
                  myservo.write(60); 
                }else{
                  Serial.println(" moving Random"); 
                  myservo.write(48); 
                }
                  


  
}// loop 

|||您可能需要的东西

文件📁

其他文件