Linux初学

Spring 依赖注入的几种方式 和 Bean的自动装配

  返回  

利用Nodemcu+Arduino nano+TB6612+点灯科技APP制作简易麦克纳姆轮Wi-Fi遥控小车

2021/7/21 1:28:36 浏览:

摘要

麦克纳姆轮小车由于车轮本身的特殊结构,可以实现全向行驶,可玩性非常强。麦克纳姆轮原理在这里不做展开,麦克纳姆小车主要是通过控制四个轮胎的转与不转以及转动的方向来实现多方向的运动,其中一种X型车轮分布车轮转动方向与车体运动方向关系如下:在这里插入图片描述

所需材料

1. Nodemcu模块
2. Arduino nano 开发板
3. TB6612电机驱动模块×2
4. 麦克纳姆轮小车底盘
5. 点灯科技APP
6. 电烙铁、导线、稳压电源等

制作步骤

1. 将各模块按如下图所示进行连接

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.打开点灯科技APP,添加独立设备,在添加的设备界面添加如图所示的按键组件,数据键名与代码内一致

在这里插入图片描述
3.分别在Nodemcu和Arduino nano中写入以下代码

Nodemcu

#define BLINKER_WIFI          //选择WIFI连接模式
#include <Blinker.h>
#include <Wire.h>

char auth[] = "***************";       //BlinkerAPP内获取的设备密钥
char ssid[] = "Your Wi-Fi Name";       //Wi-Fi名称
char pswd[] = "Your Wi-Fi Password";   //Wi-Fi密码
BlinkerButton Button1("1");   //新建按键对象
BlinkerButton Button2("2");
BlinkerButton Button3("3");
BlinkerButton Button4("4");
BlinkerButton Button5("5");
BlinkerButton Button6("6");
BlinkerButton Button7("7");
BlinkerButton Button8("8");
BlinkerButton Button9("9");
BlinkerButton Button0("0");
BlinkerButton Button11("11");

void button1_callback(const String & state)      //按键1所执行函数
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED)     //按键按下
    {
      Wire.beginTransmission(1);                 //发送数据到设备号为1的从机
      Wire.write("f");                           // 发送字符串
      Wire.endTransmission();                    // 停止发送
      Button1.text("向前行驶");
      Button1.print();
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)//按键释放
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();                          //手机震动
}

void button2_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("b");           
      Wire.endTransmission();   
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();   
    }
    Blinker.vibrate();
}

void button3_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("l");           
      Wire.endTransmission();   
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();   
    }
    Blinker.vibrate();
}

void button4_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1);
      Wire.write("r");           
      Wire.endTransmission();  
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");          
      Wire.endTransmission();   
    }
    Blinker.vibrate();
}

void button5_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("t1");           
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();   
    }
    Blinker.vibrate();
}

void button6_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("t2");           
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();
}

void button7_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("lf");          
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();   
    }
    Blinker.vibrate();
}

void button8_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("rf");           
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();
}

void button9_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("lb");           
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();
}

void button0_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_PRESSED) 
    {
      Wire.beginTransmission(1); 
      Wire.write("rb");           
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_RELEASED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();
}

void button11_callback(const String & state) 
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_TAP) 
    {
      Wire.beginTransmission(1); 
      Wire.write("s");          
      Wire.endTransmission();    
    } 
    else if(state == BLINKER_CMD_BUTTON_PRESSED)
    {
      Wire.beginTransmission(1); 
      Wire.write("s");           
      Wire.endTransmission();    
    }
    Blinker.vibrate();
}

void setup()
{
  Wire.begin(D1,D2);      //连接从机
  Serial.begin(115200);   //初始化串口
  BLINKER_DEBUG.stream(Serial);
  Blinker.begin(auth, ssid, pswd);
  Button1.attach(button1_callback);
  Button2.attach(button2_callback);
  Button3.attach(button3_callback);
  Button4.attach(button4_callback);
  Button5.attach(button5_callback);
  Button6.attach(button6_callback);
  Button7.attach(button7_callback);
  Button8.attach(button8_callback);
  Button9.attach(button9_callback);
  Button0.attach(button0_callback);
  Button11.attach(button11_callback);
}

void loop()
{
  Blinker.run();
  delay(100);
}

Arduino

#include <Wire.h>

int PWM1=5;int PWM2=6;int PWM3=9;int PWM4=10;
int M1A1=2;int M1A2=3;
int M2A1=4;int M2A2=7;
int M3A1=8;int M3A2=11;
int M4A1=12;int M4A2=13;

void setup()
{
  Serial.begin(115200);
  Wire.begin(1);                // 加入 i2c 总线,设置从机地址为1
  Wire.onReceive(receiveEvent); //注册接收到主机字符的事件
  pinMode(PWM1,OUTPUT);
  pinMode(PWM2,OUTPUT);
  pinMode(PWM3,OUTPUT);
  pinMode(PWM4,OUTPUT);
  pinMode(M1A1,OUTPUT);
  pinMode(M1A2,OUTPUT);
  pinMode(M2A1,OUTPUT);
  pinMode(M2A2,OUTPUT);
  pinMode(M3A1,OUTPUT);
  pinMode(M3A2,OUTPUT);
  pinMode(M4A1,OUTPUT);
  pinMode(M4A2,OUTPUT);
}

void loop ()
{
  
  delay(200);

}

void receiveEvent(int a)    // 当从机接收到主机字符,执行该事件
{
  String c;
  while( Wire.available()>0) 
  {
    c += char(Wire.read()); // 作为字符接收字节
  }
   Serial.print(c);         // 把字符打印到串口监视器中
    if(c=="s")       {s();}
    if(c=="f")       {f();}
    if(c=="b")       {b();}
    if(c=="l")       {l();}
    if(c=="r")       {r();}
    if(c=="t1")      {turn1();}
    if(c=="t2")      {turn2();}
    if(c=="lf")      {leftforth();}
    if(c=="rf")      {rightforth();}
    if(c=="lb")      {leftback();}
    if(c=="rb")      {rightback();}
   
 
}
 
void PWM()     //修改PWM数值可改变小车速度 0~255
{
  analogWrite(PWM1,200);
  analogWrite(PWM2,200);
  analogWrite(PWM3,200);
  analogWrite(PWM4,200);
}

void s()       //小车停止 
{
  digitalWrite(M1A1,LOW);  //通过修改高低电频来修改电机转与不转和转向
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,LOW);
}

void f()        //向前行驶
{
  digitalWrite(M1A1,HIGH);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,HIGH);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,HIGH);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,HIGH);
  digitalWrite(M4A2,LOW);
  PWM();
}

void b()        //向后行驶
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,HIGH);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,HIGH);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,HIGH);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,HIGH);
  PWM();
}

void l()        //向左平移
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,HIGH);
  digitalWrite(M2A1,HIGH);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,HIGH);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,HIGH);
  PWM();
}

void r()        //向右平移
{
  digitalWrite(M1A1,HIGH);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,HIGH);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,HIGH);
  digitalWrite(M4A1,HIGH);
  digitalWrite(M4A2,LOW);
  PWM();
}
void turn1()  //顺时针旋转
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,HIGH);
  digitalWrite(M2A1,HIGH);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,HIGH);
  digitalWrite(M4A1,HIGH);
  digitalWrite(M4A2,LOW);
  PWM();
}

void turn2()  //逆时针旋转
{
  digitalWrite(M1A1,HIGH);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,HIGH);
  digitalWrite(M3A1,HIGH);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,HIGH);
  PWM();
}

void leftforth()  //向左前方行驶
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,HIGH);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,HIGH);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,LOW);
  PWM();
}

void rightforth() //向右前方行驶
{
  digitalWrite(M1A1,HIGH);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,HIGH);
  digitalWrite(M4A2,LOW);
  PWM();
}
void leftback()   //向左后方行驶
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,HIGH);
  digitalWrite(M2A1,LOW);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,LOW);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,HIGH);
  PWM();
}
void rightback()  //向右后方行驶
{
  digitalWrite(M1A1,LOW);
  digitalWrite(M1A2,LOW);
  digitalWrite(M2A1,HIGH);
  digitalWrite(M2A2,LOW);
  digitalWrite(M3A1,HIGH);
  digitalWrite(M3A2,LOW);
  digitalWrite(M4A1,LOW);
  digitalWrite(M4A2,LOW);
  PWM();
}

总结

本作品非常的简单,利用ArduinoIDE进行编写的代码也非常通俗易懂。本人在这里只是提供了一种思路,结构、代码等方面还有很多可以优化的地方,很多功能也有待开发。本作品利用了无线Wi-Fi去控制,优点是无需自己开发APP端,互联网连接,没有遥控距离的限制;但缺点也很明显,由于接入的是互联网而非局域网,所以在操控上有明显的延迟。
(若各个车轮转向错误可以尝试更改两电机线线序或者更改代码中的高低电频以修改电机转向)

演示视频:

【Nodemcu+arduino nano+点灯科技APP制作简易Wi-Fi遥控麦克纳姆轮小车-哔哩哔哩】https://b23.tv/kdcCtV

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号