用arduino做一台机甲大师-Arduino中文社区 - Powered by Discuz! Archiver

wzzzq 发表于 2020-2-16 18:50

用arduino做一台机甲大师

本帖最后由 wzzzq 于 2020-8-18 17:40 编辑


先看看成品图
一 材料购买
必要材料
arduino主板,最好是mega2560 x1
两路直流电机pwm驱动版 x2
麦轮底盘 x1
2s电池x3
遥控+pwm输出接收机(最好6通道以上) x1
主板电池 x1
水弹发射器x1
可选材料
图传+摄像头+2s电池x1
激光头x1
压电传感器 x2
发射器云台x1
蜂鸣器x1
血条灯x1
工具(可有可无,只要能达到固定目的即可)
烙铁
扎带
胶枪
这里还提供了一些选购方案,总价在500多到1600不等

顶配方案 1622元
二 功能介绍
使用arduino mega2560做主控,用航模接收机与遥控做通讯,用arduino读取接收机的pwm信号,发送信号给电机驱动板,电机驱动板再驱动电机。
并且有水弹发射器,可以发射水弹。
本项目采用麦克纳姆轮,可以实现全向移动


三 组装

先安装好底盘


电机座与联轴器安装图
首先安装底盘
用扎带安装主板,驱动板



然后焊好电机和驱动板,并安装电机(电机,驱动版焊太久会坏)
然后将电机驱动板并联起来,接个电池。arduino主板和它们并联
安装麦轮(重点)
麦轮有顺序!!!
千万别装错了!!!

底盘安装好以后安装发射器,发射器这里就有很多拓展,比如云台,图传,激光瞄准器etc.
这里云台一般商家不会给安装教程,所以买云台需要一定的动手能力,我就不详细讲了,以后有时间可能再写个这样的教程

接下来安装压电传感器,用于感知其他机甲的打击,也就是裁判系统。很简单,把压电的DO接到35/37口,GND接到GND,V接到5V。
这里注意如果只有一个或没有压电,要把对应的判断部分只留下用的端口,因为压电是0有打击,与arduino默认的相反。
四 接线

按照接线图接线
五 调试
每台机子安装的电机可能方向不一样,所以我们 要调试

改变接线或程序,直到电机如图所示正常转动为止。
比如M1反了就把ma1端口和ma2端口换一下。
六 开战
如果你有条件做两台机甲大师,或你已经有一台大疆的s1,或你有朋友也做了一台机甲大师,你们就可以对战了
瞄准对方的压电传感器,击中了就可以使对方扣血
Tips 血量可以在程序的第一行改 int hp=你想要的值 默认20点血
如果一开机蜂鸣器就不停的叫,然后就没血了,说明是压电传感器的问题,解决方法见上文
-----------------------------------------------------------------------------------------------------
程序:(2020.02.23更新版,改为电调鸣叫代替蜂鸣器,更易懂的串口输出,增加斜向移动)

int hp=20;         
const int ma1=9;
const int ma2=2;
const int mb1=3;
const int mb2=4;
const int mc1=5;
const int mc2=6;
const int md1=7;
const int md2=8;
const int aile=23;
const int rudd=27;
const int elev=25;
const int gone=31;
int b1=35;
int b2=37;
int sa;
int sb;
int sc;
void forward(int v)
{
int out=map(v,sa,2007,0,255);
analogWrite(ma2,map(v,sa,2007,0,255));
digitalWrite(ma1,0);
   analogWrite(mb1,map(v,sa,2007,0,255));
digitalWrite(mb2,0);
   analogWrite(mc2,map(v,sa,2007,0,255));
digitalWrite(mc1,0);
   analogWrite(md1,map(v,sa,2007,0,255));
digitalWrite(md2,0);
printf("GO FORWARD PWMOUT:%d\n",out);
}
void goright(int v)
{
int out=map(v,sb,2007,0,255);
analogWrite(ma2,out);
digitalWrite(ma1,0);
   analogWrite(mb2,out);
digitalWrite(mb1,0);
   analogWrite(mc2,out);
digitalWrite(mc1,0);
   analogWrite(md2,out);
digitalWrite(md1,0);
printf("GO RIGHT PWMOUT:%d\n",out);
}
void goleft(int v)
{
int out=map(v,sb,1010,0,255);
analogWrite(mb1,out);
digitalWrite(mb2,0);
   analogWrite(ma1,out);
digitalWrite(ma2,0);
   analogWrite(md1,out);
digitalWrite(md2,0);
   analogWrite(mc1,out);
digitalWrite(mc2,0);
printf("GO LEFT PWMOUT:%d\n",out);
}
void backward(int v)
{
int out=map(v,sa,1010,0,255);
   analogWrite(ma1,out);
digitalWrite(ma2,0);
   analogWrite(mb2,out);
digitalWrite(mb1,0);
   analogWrite(mc1,out);
digitalWrite(mc2,0);
   analogWrite(md2,out);
digitalWrite(md1,0);
printf("GO BACKWARD PWMOUT:%d\n",out);
}
void stop_all()
{
   digitalWrite(ma1,0);
   digitalWrite(mb1,0);
   digitalWrite(mc1,0);
   digitalWrite(md1,0);
   digitalWrite(ma2,0);
   digitalWrite(mb2,0);
   digitalWrite(mc2,0);
   digitalWrite(md2,0);
}
void turnleft(int v)
{
int out=map(v,sc,1010,0,255);
analogWrite(ma2,out);
digitalWrite(ma1,0);
   analogWrite(mb2,out);
digitalWrite(mb1,0);
   analogWrite(mc1,out);
digitalWrite(mc2,0);
   analogWrite(md1,out);
digitalWrite(md2,0);
printf("TURN LEFT PWMOUT:%d\n",out);
}
void turnright(int v)
{
int out=map(v,sc,2007,0,255);
analogWrite(ma1,out);
digitalWrite(ma2,0);
   analogWrite(mb1,out);
digitalWrite(mb2,0);
   analogWrite(mc2,out);
digitalWrite(mc1,0);
   analogWrite(md2,out);
digitalWrite(md1,0);
printf("TURN RIGHT PWMOUT:%d\n",out);
}
void forright(int a,int b)
{
int out;
if((a-sa)>(b-sb))
out=map(b,sb,2007,0,255);
else
out=map(a,sa,2007,0,255);
   digitalWrite(md2,0);
digitalWrite(md1,0);
analogWrite(ma2,out);
digitalWrite(ma1,0);
   digitalWrite(mb1,0);
digitalWrite(mb2,0);
   digitalWrite(mc1,0);
analogWrite(mc2,out);
printf("GO FORWARD&RIGHT PWMOUT:%d\n",out);
}
void backright(int a,int b)
{
int out;
if((a-sa)>(b-sb))
out=map(b,sb,2007,0,255);
else
out=map(a,sa,1010,0,255);
   digitalWrite(ma2,0);
digitalWrite(ma1,0);
   analogWrite(mb2,out);
digitalWrite(mb1,0);
   analogWrite(md2,out);
digitalWrite(md1,0);
   digitalWrite(mc1,0);
digitalWrite(mc2,0);
printf("GO BACKWARD&RIGHT PWMOUT:%d\n",out);
}
void forleft(int a,int b)
{
int out;
if((a-sa)>(b-sb))
out=map(b,sb,1010,0,255);
else
out=map(a,sa,2007,0,255);
digitalWrite(ma2,0);
digitalWrite(ma1,0);
analogWrite(mb1,out);
digitalWrite(mb2,0);
   digitalWrite(mc1,0);
digitalWrite(mc2,0);
   digitalWrite(md2,0);
analogWrite(md1,out);
printf("GO FORWARD&LEFT PWMOUT:%d\n",out);
}
void backleft(int a,int b)
{
int out;
if((a-sa)>(b-sb))
out=map(b,sb,1010,0,255);
else
out=map(a,sa,1010,0,255);
   digitalWrite(md2,0);
digitalWrite(md1,0);
analogWrite(ma1,out);
digitalWrite(ma2,0);
   digitalWrite(mb1,0);
digitalWrite(mb2,0);
   digitalWrite(mc2,0);
analogWrite(mc1,out);
printf("GO BACKWARD&LEFT PWMOUT:%d\n",out);
}
void buzz(int T)
{
analogWrite(ma1,40);
digitalWrite(ma2,0);
analogWrite(mb1,40);
digitalWrite(mb2,0);
analogWrite(mc1,40);
digitalWrite(mc2,0);
analogWrite(md1,40);
digitalWrite(md2,0);
delay(T);
digitalWrite(ma1,0);
digitalWrite(ma2,0);
digitalWrite(mb1,0);
digitalWrite(mb2,0);
digitalWrite(mc1,0);
digitalWrite(mc2,0);
digitalWrite(md1,0);
digitalWrite(md2,0);
}
int serial_putc( char c, struct __file * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
void setup()
{
   pinMode(ma1,OUTPUT);
   pinMode(ma2,OUTPUT);
   pinMode(mb1,OUTPUT);
   pinMode(mb2,OUTPUT);
   pinMode(mc1,OUTPUT);
   pinMode(mc2,OUTPUT);
   pinMode(md1,OUTPUT);
   pinMode(md2,OUTPUT);
   pinMode(aile,INPUT);
   pinMode(elev,INPUT);
   pinMode(rudd,INPUT);
   pinMode(33,OUTPUT);
   unsigned char i;
   sc=pulseIn(rudd,HIGH);
   sa=pulseIn(elev,HIGH);
   sb=pulseIn(aile,HIGH);
   buzz(300);
   delay(100);
   buzz(300);
   delay(100);
   buzz(600);
   Serial.begin(9600);
   printf_begin();
   printf("ELEV MID:%d\n",sa);
   printf("AILE MID:%d\n",sb);
   printf("RUDD MID:%d\n",sc);
}
void loop()
{
int c=pulseIn(rudd,HIGH);
int a=pulseIn(elev,HIGH);
int b=pulseIn(aile,HIGH);
int d=pulseIn(gone,HIGH);
printf("ELEV OUTPUT:%d ",a);
printf("AILE OUTPUT:%d ",b);
printf("RUDD OUTPUT:%d\n",c);
if(d>1500)
digitalWrite(33,HIGH);
else
digitalWrite(33,LOW);
/* if((digitalRead(b1)==0)||(digitalRead(b2)==0))
{
    hp--;
    unsigned char i;
   buzz(100);
    if(hp==0)
    {
      buzz(100);
   delay(100);
   buzz(500);
   delay(3000);
   hp=20;
    }
}*/
if((a<1540)&&(a>1480)&&((b>1540)||(b<1480)))
{
    if(b>1540)
   goright(b);
    if(b<1480)
    goleft(b);
}
if((b<1540)&&(b>1480)&&((a>1540)||(a<1480)))
{
   if(a>1540)
   forward(a);
   if(a<1480)
    backward(a);
}
if(((a<1450)||(a>1580))&&((b<1450)||(b>1580)))
{
if((a>1580)&&(b>1580))
{
    forright(a,b);
}
if((a>1580)&&(b<1450))
{
    forleft(a,b);
}
if((a<1450)&&(b>1580))
{
    backright(a,b);
}
if((a<1450)&&(b<1450))
{
    backleft(a,b);
}
}
if(c>1540)
turnright(c);
if(c<1460)
turnleft(c);
if((b>1460)&&(b<1540)&&(a>1460)&&(a<1540)&&(c>1460)&&(c<1540))
stop_all();
}


如果你在制作过程中遇到任何问题,欢迎联系我的邮箱2936385857@qq.com!
下一个帖子会讲讲PID控制,最近在做
附:我买麦轮底盘的链接: 复植这行话¢M2qN1fOpONT¢转移至τáǒЬáǒ【麦克纳姆轮车底盘,全向遥控小车智能车80mm包胶麦轮模块化底盘】;或https://m.tb.cn/h.VWZeGNa?sm=7d8d91 掂击鏈→接,再选择瀏覽●噐○咑№亓
这家东西不错,老板人也挺好的,只是出于个人使用后的感受,绝对不是托!
发射器一开始也找了很久 推荐这一家的东西
fu至这行话€Yqhy1fOJofY€转移至τáǒЬáǒ【水弹枪波箱 金属云台 麦克纳姆轮 机甲大师 水弹发射器 麦轮小车】;或https://m.tb.cn/h.V2kSPsf?sm=d963f1 掂击鏈→接,再选择瀏→覽→嘂..咑ぺkai欢迎加微交谈 2936385857视频
https://www.iqiyi.com/v_19rxetevzg.html






wzzzq 发表于 2020-4-16 14:02

经过了一段时间,我换了种算法,这样控制的更平滑
原理是先通过接收机数值计算出四个电机分别的转速,再控制pwm
#include <MsTimer2.h>
int a,b,c;
int hp=20;         
const int ma1=9;
const int ma2=2;
const int mb1=3;
const int mb2=4;
const int mc1=5;
const int mc2=6;
const int md1=7;
const int md2=8;
const int aile=23;
const int rudd=27;
const int elev=25;
const int gone=31;
int b1=35;
int b2=37;
int sa;
int sb;
int sc;
int ref1,ref2,ref3,ref4;
int mafor,mago,maturn,mbfor,mbgo,mbturn,mcgo,mcfor,mcturn,mdfor,mdgo,mdturn;
void out()
{
printf("ELEV OUTPUT:%d ",a);
printf("AILE OUTPUT:%d ",b);
printf("RUDD OUTPUT:%d\n",c);
printf("电机A的转速:%d\n",ref1);
printf("电机B的转速:%d\n",ref2);
printf("电机C的转速:%d\n",ref3);
printf("电机D的转速:%d\n",ref4);
}
void moving()
{
if(ref1>0)
{
    analogWrite(ma2,ref1);
    digitalWrite(ma1,0);
}
else
{
    analogWrite(ma1,abs(ref1));
    digitalWrite(ma2,0);
}
if(ref2>0)
{
    analogWrite(mb1,ref2);
    digitalWrite(mb2,0);
}
else
{
    analogWrite(mb2,abs(ref2));
    digitalWrite(mb1,0);
}
if(ref3>0)
{
    analogWrite(mc2,ref3);
    digitalWrite(mc1,0);
}
else
{
    analogWrite(mc1,abs(ref3));
    digitalWrite(mc2,0);
}
if(ref4>0)
{
    analogWrite(md1,ref4);
    digitalWrite(md2,0);
}
else
{
    analogWrite(md2,abs(ref4));
    digitalWrite(md1,0);
}
}
void stop_all()
{
   digitalWrite(ma1,0);
   digitalWrite(mb1,0);
   digitalWrite(mc1,0);
   digitalWrite(md1,0);
   digitalWrite(ma2,0);
   digitalWrite(mb2,0);
   digitalWrite(mc2,0);
   digitalWrite(md2,0);
   ref1=0;
   ref2=0;
   ref3=0;
   ref4=0;
   mago=0;
   mbgo=0;
   mcgo=0;
   mdgo=0;
   mafor=0;
   mbfor=0;
   mcfor=0;
   mdfor=0;
}

void buzz(int T)
{
analogWrite(ma1,40);
digitalWrite(ma2,0);
analogWrite(mb1,40);
digitalWrite(mb2,0);
analogWrite(mc1,40);
digitalWrite(mc2,0);
analogWrite(md1,40);
digitalWrite(md2,0);
delay(T);
digitalWrite(ma1,0);
digitalWrite(ma2,0);
digitalWrite(mb1,0);
digitalWrite(mb2,0);
digitalWrite(mc1,0);
digitalWrite(mc2,0);
digitalWrite(md1,0);
digitalWrite(md2,0);
}
int serial_putc( char c, struct __file * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
void setup()
{
   pinMode(ma1,OUTPUT);
   pinMode(ma2,OUTPUT);
   pinMode(mb1,OUTPUT);
   pinMode(mb2,OUTPUT);
   pinMode(mc1,OUTPUT);
   pinMode(mc2,OUTPUT);
   pinMode(md1,OUTPUT);
   pinMode(md2,OUTPUT);
   pinMode(aile,INPUT);
   pinMode(elev,INPUT);
   pinMode(rudd,INPUT);
   pinMode(33,OUTPUT);
   pinMode(b1,INPUT);
   pinMode(b2,INPUT);
   unsigned char i;
   sc=pulseIn(rudd,HIGH);
   sa=pulseIn(elev,HIGH);
   sb=pulseIn(aile,HIGH);
   buzz(300);
   delay(100);
   buzz(300);
   delay(100);
   buzz(600);
   Serial.begin(9600);
   printf_begin();
   printf("ELEV MID:%d\n",sa);
   printf("AILE MID:%d\n",sb);
   printf("RUDD MID:%d\n",sc);
   //MsTimer2::set(100, out);
   //MsTimer2::set(50, remote);
   //MsTimer2::start(); //开始计时
}
void loop()
{
c=pulseIn(rudd,HIGH);
a=pulseIn(elev,HIGH);
b=pulseIn(aile,HIGH);
int d=pulseIn(gone,HIGH);
if(d>1500)
digitalWrite(33,HIGH);
else
digitalWrite(33,LOW);
if((digitalRead(b1)==1)||(digitalRead(b2)==1))
{
    hp--;
   buzz(300);
    if(hp==0)
    {
      buzz(100);
   delay(100);
   buzz(500);
   delay(3000);
   hp=20;
    }
}
if(!((a>1480)&&(a<1530)))
{
    mafor=map(a,1010,2007,-255,255);
    mbfor=map(a,1010,2007,-255,255);
    mcfor=map(a,1010,2007,-255,255);
    mdfor=map(a,1010,2007,-255,255);
}
else
{
    mafor=0;
    mbfor=0;
    mcfor=0;
    mdfor=0;
}
if(!((b>1480)&&(b<1530)))
{
    mago=map(b,1010,2007,-255,255);
    mbgo=map(b,1010,2007,255,-255);
    mcgo=map(b,1010,2007,-255,255);
    mdgo=map(b,1010,2007,255,-255);
}
else
{
    mago=0;
    mbgo=0;
    mcgo=0;
    mdgo=0;
}
if(!((c>1480)&&(c<1530)))
{
    maturn=map(c,1010,2007,255,-255);
    mbturn=map(c,1010,2007,-255,255);
    mcturn=map(c,1010,2007,-255,255);
    mdturn=map(c,1010,2007,255,-255);
}
else
{
    maturn=0;
    mbturn=0;
    mcturn=0;
    mdturn=0;
}
ref1=max(min(mafor+mago+maturn,255),-255);
ref2=max(min(mbfor+mbgo+mbturn,255),-255);
ref3=max(min(mcfor+mcgo+mcturn,255),-255);
ref4=max(min(mdfor+mdgo+mdturn,255),-255);
moving();
out();
maturn=0;
mbturn=0;
mcturn=0;
mdturn=0;
mago=0;
mbgo=0;
mcgo=0;
mdgo=0;
mafor=0;
mbfor=0;
mcfor=0;
mdfor=0;
if((b>1480)&&(b<1530)&&(a>1480)&&(a<1530)&&(c>1480)&&(c<1530))
stop_all();
}

余涛 发表于 2020-9-29 13:32

感谢分享马克
页: [1]
查看完整版本: 用arduino做一台机甲大师