制作一个通过wifi联网,可以远程控制的小车(二)

毁了,烧毁了

是的,本来想通过L298P上的引脚驱动舵机,把ESP32的GPIO5接到L298P的ANALOG0, 然后就烧掉了,烧不进去程序了,毁了!!!

淘宝下单了两片新的ESP32,肉痛----------

总结一下:就是尽量单独给芯片供电,别用其他芯片给单片机供电。

新的开始

终于到了,加油干吧!!!

先搞一下舵机控制

这里需要通过PWM来控制舵机的角度,用的舵机是MG996R
这里参考这个页面的说明

https://components101.com/motors/mg996r-servo-motor-datasheet

参考这个图片
周期20ms, 有效占空比是1ms-2ms,1ms时0°,2ms时120°
PWM的位数设置为10,精度2^10=1024,那么有效占空比就是51-102,51时0°,102时120°

话说这里研究了好久

看看连接

然后是效果

然后是代码
初始化PWM输出参数

const int freq = 50;
const int ledChannel = 0;
const int resolution = 10;//精度1024

setup里面设置

ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(ledPin, ledChannel);

loop里面控制舵机来回摇

  for(int i=0; i<120; i+=5){
    ledcWrite(ledChannel, map(i, 0, 180, 51, 102));
    get_pwm_info();
    delay(500);
  }

  for(int i=120; i>0; i-=5){
    ledcWrite(ledChannel, map(i, 0, 180, 51, 102));
    get_pwm_info();
    delay(500);
  }

完整的,之前连MQTT的先注释了,下次再加上MQTT的控制

#include <WiFi.h>
#include <Ethernet.h>
#include <PubSubClient.h>

const char* ssid     = "***";
const char* password = "***";
int currentDirection = 90;

const int freq = 50;
const int ledChannel = 0;
const int resolution = 10;//精度1024

const int ledPin = 5;

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  int signal;
  for (int i=0;i<length;i++) {
    if((char)payload[i] == 'b'){

      digitalWrite(4, HIGH);
    }

    if((char)payload[i] == 'a'){
      digitalWrite(4, LOW);
    }

    if((char)payload[i] == 'r'){

    }

    if((char)payload[i] == 'l'){

    }
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup()
{
    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network

    Serial.println(ssid);
    Serial.println(password);
    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");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    client.setServer("*.*.*.*", 1883);
    client.setCallback(callback);

    pinMode(4, OUTPUT);

    ledcSetup(ledChannel, freq, resolution);
    ledcAttachPin(ledPin, ledChannel);
}

void loop()
{
//  if (!client.connected()) {
//    reconnect();
//  }
//
//  client.loop();

  //周期20ms, 有效占空比是1ms-2ms,1ms时0°,2ms时120°
  //精度2^10=1024,那么有效占空比就是51-102,51时0°,102时120°
  //90°时就是(51+102)/2 = 76
//  ledcWrite(ledChannel, 76);

  for(int i=0; i<120; i+=5){
    ledcWrite(ledChannel, map(i, 0, 120, 51, 102));
    get_pwm_info();
    delay(500);
  }

  for(int i=120; i>0; i-=5){
    ledcWrite(ledChannel, map(i, 0, 120, 51, 102));
    get_pwm_info();
    delay(500);
  }
}

发表评论

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