本文最后更新于 2024-10-18,文章内容距离上一次更新已经过去了很久啦,可能已经过时了,请谨慎参考喵。

title: 四足机器人开发之三(原理篇)
top_img: false
tags:
  - 物联网
  - 四足机器人
  - ESP8266
categories:
  - 嵌入式开发
abbrlink: e920e948
date: 2023-07-11 20:55:44
cover: '/upload/cdn4files/202307112059894.png'
copyright:
comments:

续前言

四足机器人开发之一(食材篇) (链接已失效,可在本站内搜索标题)

四足机器人开发之二(开发环境篇)(链接已失效,可在本站内搜索标题)

原理

参考文献:60元成本打造esp8266四足机器人

PWM

因为使用了pca9685舵机驱动板,所以这里使用的是Adafruit_PWMServoDriver库驱动sg90舵机,原理是通过PWM来控制舵机角度,动作主要是靠PWM来写的

舵机0-180度对应的PWM数值写在表格里了,这里取的是近似值,自己进行微调也可以

角度PWM数值
0100
45190
90280
135370
180510

舵机号

图中的舵机号与代码中的舵机号是对应的,比如代码中的 pwm.setPWM(4, 0, 370) ,就是将4号脚置于135度

舵机角度

蓝色角度对应的是4、5、6、7脚,黄色对应的是0、1、2、3脚

这里的角度范围表示舵机能运动的范围以及最大角度

比如当4、6号脚为0度时向上,而5、7脚0度时向下,反之当4、6号脚为180度时向下,而5、7脚180度时向上

操作界面

操作页面有用小程序的也有用软件的,这里使用的是网页

用esp8266作为网络服务器,按钮使用html写的

原理就这么简单,期待最后的组装篇~

代码

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
 
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
 
const char* ssid = "WIFI名称";     
const char* password = "WIFI密码"; 
 
WiFiServer server(80);  //http的服务端口
 
void setup() {
  pinMode(16, OUTPUT);  //用于连接Wi-Fi的LED指示灯
  digitalWrite(16, LOW);
  Serial.begin(115200);
  pwm.begin();
  pwm.setPWMFreq(60); //伺服电机以60hz刷新率工作
  delay(1000);
 
  //连接到无线网络
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("-");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  //启动服务器
  server.begin();
  Serial.println("Server started");
 
  //打印IP地址
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
  digitalWrite(16, HIGH);
}
 
void loop() {
  //检查客户端是否已连接
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  //等到客户端发送一些数据
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }
 
  //阅读请求的第一行
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  //匹配请求
  if (request.indexOf("/S") != -1) {
    sleep();
  }
  if (request.indexOf("/W") != -1) {
    normal();
  }
  if (request.indexOf("/F") != -1) {
    normal();
    forward();
    forward();
    forward();
    forward();
  }
  if (request.indexOf("/B") != -1) {
    normal();
    backward();
    backward();
    backward();
    backward();
  }
  if (request.indexOf("/L") != -1) {
    normal();
    left();
    left();
  }
  if (request.indexOf("/R") != -1) {
    normal();
    right();
    right();
  }
  if (request.indexOf("/H") != -1) {
    normal();
    hello();
  }
 if (request.indexOf("/C") != -1) {
    normal();
    come();
  }
  //返回响应
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
  client.println("<style>");
  client.println(".button{");
/*----------网页设计----------*/
  client.println("background-color: #4CAF50; /* GREEN */");
  client.println("width: 48%;");
  client.println("border: none;");
  client.println("color: white;");
  client.println("padding: 50px 40px;");
  client.println("text-align: center;");
  client.println("text-decoration: none;");
  client.println("display: inline-block;");
  client.println("font-size: 50px;");
  client.println("margin: 4px 2px;");
  client.println("cursor: pointer;");
  client.println("border-radius: 12px;");
  client.println("}");
 
  client.println("</style>");
  client.println("</head>");
  client.println("<body bgcolor=black>");
 
  client.println("<button class=\"button\" onclick=\"window.location.href='/F';\">前进</button>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/B';\">后退</button>");
  client.println("<br></br>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/L';\">左转</button>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/R';\">右转</button>");
  client.println("<br></br>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/S';\">睡觉</button>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/W';\">正常</button>");
  client.println("<br></br>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/H';\">你好</button>");
  client.println("<button class=\"button\" onclick=\"window.location.href='/C';\">来啊</button>");
  client.println("</body>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
}
 
/*----------动作函数----------*/
 
// |   7  |             |   6  |
//  ----- -----   ----- -----
//       |  3   | |  2   |
//        -----   -----
//       |   1  | |  0   |
//  ----- -----   ----- -----
// |  5   |             |  4   |
 
void sleep() 
{
  pwm.setPWM(4, 0, 280);
  delay(100);
  pwm.setPWM(5, 0, 280);
  delay(100);
  pwm.setPWM(6, 0, 280);
  delay(100);
  pwm.setPWM(7, 0, 280);
  delay(100);
  pwm.setPWM(0, 0, 510);
  delay(100);
  pwm.setPWM(1, 0, 100);
  delay(100);
  pwm.setPWM(2, 0, 100);
  delay(100);
  pwm.setPWM(3, 0, 510);
}
 
void normal() 
{
  pwm.setPWM(0, 0, 370);
  delay(100);
  pwm.setPWM(1, 0, 190);
  delay(100);
  pwm.setPWM(2, 0, 190);
  delay(100);
  pwm.setPWM(3, 0, 370);
  delay(100);
  pwm.setPWM(4, 0, 510);
  delay(100);
  pwm.setPWM(7, 0, 100);
  delay(100);
  pwm.setPWM(6, 0, 510);
  delay(100);
  pwm.setPWM(5, 0, 100);
}
 
void left()
{
  pwm.setPWM(4, 0, 370);
  delay(100);
  pwm.setPWM(0, 0, 510);
  delay(100);
  pwm.setPWM(4, 0, 510);
  delay(100);
  pwm.setPWM(6, 0, 370);
  delay(100);
  pwm.setPWM(2, 0, 280);
  delay(100);
  pwm.setPWM(6, 0, 510);
  delay(100);
  pwm.setPWM(7, 0, 190);
  delay(100);
  pwm.setPWM(3, 0, 510);
  delay(100);
  pwm.setPWM(7, 0, 100);
  delay(100);
  pwm.setPWM(5, 0, 190);
  delay(100);
  pwm.setPWM(1, 0, 280);
  delay(100);
  pwm.setPWM(5, 0, 100);
  delay(100);
  pwm.setPWM(0, 0, 370);
  pwm.setPWM(2, 0, 190);
  pwm.setPWM(3, 0, 370);
  pwm.setPWM(1, 0, 190);
}
 
void right()
{
  pwm.setPWM(5, 0, 190);
  delay(100);
  pwm.setPWM(1, 0, 100);
  delay(100);
  pwm.setPWM(5, 0, 100);
  delay(100);
  pwm.setPWM(7, 0, 190);
  delay(100);
  pwm.setPWM(3, 0, 280);
  delay(100);
  pwm.setPWM(7, 0, 100);
  delay(100);
  pwm.setPWM(6, 0, 370);
  delay(100);
  pwm.setPWM(2, 0, 100);
  delay(100);
  pwm.setPWM(6, 0, 510);
  delay(100);
  pwm.setPWM(4, 0, 370);
  delay(100);
  pwm.setPWM(0, 0, 190);
  delay(100);
  pwm.setPWM(4, 0, 510);
  delay(100);
  pwm.setPWM(1, 0, 190);
  pwm.setPWM(3, 0, 370);
  pwm.setPWM(2, 0, 190);
  pwm.setPWM(0, 0, 370);
}
 
void forward()
{ 
  pwm.setPWM(5, 0, 190);
  delay(50);
  pwm.setPWM(1, 0, 100);
  delay(50);
  pwm.setPWM(5, 0, 100);
  delay(50);
  pwm.setPWM(4, 0, 370);
  delay(50);
  pwm.setPWM(0, 0, 510);
  delay(50);
  pwm.setPWM(4, 0, 510);
  delay(50);
  pwm.setPWM(6, 0, 370);
  delay(50);
  pwm.setPWM(2, 0, 280);
  delay(50);
  pwm.setPWM(6, 0, 510);
  delay(50);
  pwm.setPWM(7, 0, 190);
  delay(50);
  pwm.setPWM(3, 0, 280);
  delay(50);
  pwm.setPWM(7, 0, 100);
  delay(50);
  pwm.setPWM(0, 0, 370);
  pwm.setPWM(1, 0, 190);
  pwm.setPWM(2, 0, 190);
  pwm.setPWM(3, 0, 370);
}
 
void backward()
{ 
  pwm.setPWM(7, 0, 190);
  delay(50);
  pwm.setPWM(3, 0, 510);
  delay(50);
  pwm.setPWM(7, 0, 100);
  delay(50);
  pwm.setPWM(6, 0, 370);
  delay(50);
  pwm.setPWM(2, 0, 100);
  delay(50);
  pwm.setPWM(6, 0, 510);
  delay(50);
  pwm.setPWM(5, 0, 190);
  delay(50);
  pwm.setPWM(1, 0, 280);
  delay(50);
  pwm.setPWM(5, 0, 100);
  delay(50);
  pwm.setPWM(4, 0, 370);
  delay(50);
  pwm.setPWM(0, 0, 280);
  delay(50);
  pwm.setPWM(4, 0, 510);
  delay(50);
  pwm.setPWM(3, 0, 370);
  pwm.setPWM(2, 0, 190);
  pwm.setPWM(1, 0, 190);
  pwm.setPWM(0, 0, 370);
}
 
void hello()
{
  pwm.setPWM(0, 0, 510);
  delay(100);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 190);
  delay(500);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 190);
  delay(500);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 510);
}
 
void come()
{
  pwm.setPWM(0, 0, 280);
  delay(100);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 190);
  delay(500);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 190);
  delay(500);
  pwm.setPWM(4, 0, 100);
  delay(500);
  pwm.setPWM(4, 0, 510);
}