极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12286|回复: 6

原创带调速四轮小车

[复制链接]
发表于 2012-5-8 14:17:54 | 显示全部楼层 |阅读模式
设计想法是:通过双击ALT键来完成速度的改变,一共分为四档且是一个循环的功能.初始设置是停止,编写processing完成上位机软件控制部分,最终成型除了前后左右带调速,还装备舵机摄像头实现远程遥控.

  1. //arduino编程部分
  2. #include <Servo.h>
  3. Servo myservo1;
  4. Servo myservo2;
  5. int E1 = 5; //定义M1 使能端
  6. int E2 = 6; //定义M2 使能端
  7. int M1 = 4; //定义M1 控制端
  8. int M2 = 7; //定义M1 控制端
  9. int x=0;
  10. int y=0;
  11. int count=5;
  12. void stop() //停止
  13. {
  14.   digitalWrite(E1,LOW);
  15.   digitalWrite(E2,LOW);
  16. }
  17. void advance(char a,char b) //前进
  18. {
  19.   analogWrite (E1,a); //PWM 调速
  20.   digitalWrite(M1,HIGH);
  21.   analogWrite (E2,b);
  22.   digitalWrite(M2,HIGH);
  23. }
  24. void back_off (char a,char b) //后退
  25. {
  26.   analogWrite (E1,a);
  27.   digitalWrite(M1,LOW);
  28.   analogWrite (E2,b);
  29.   digitalWrite(M2,LOW);
  30. }
  31. void turn_L (char a,char b) //左转
  32. {
  33.   analogWrite (E1,a);
  34.   digitalWrite(M1,LOW);
  35.   analogWrite (E2,b);
  36.   digitalWrite(M2,HIGH);
  37. }
  38. void turn_R (char a,char b) //右转
  39. {
  40.   analogWrite (E1,a);
  41.   digitalWrite(M1,HIGH);
  42.   analogWrite (E2,b);
  43.   digitalWrite(M2,LOW);
  44. }
  45. void setup()
  46. {
  47.   myservo1.attach(1);
  48.   myservo2.attach(2);
  49.   int i;
  50.   for(i=4;i<=7;i++)
  51.     pinMode(i, OUTPUT);
  52.   Serial.begin(19200); //设置串口波特率
  53. }
  54. void loop()
  55. {
  56.   char val = Serial.read();
  57.   if(val!=-1)
  58.   {
  59.     if(val==1 || val==2 || val==3 || val==4 || val==5 || val==0)
  60.     {
  61.       count=val;
  62.     }
  63.   }
  64.   if(val == 0x26 && x<=180)
  65.   {
  66.     x++;
  67.   }
  68.   else if(val == 0x28 && x>=0)
  69.   {
  70.     x--;
  71.   }
  72.   else if(val == 0x25 && y<=180)
  73.   {
  74.     y++;
  75.   }
  76.   else if(val == 0x27 && y>=0)
  77.   {
  78.     y--;
  79.   }
  80.   else if(x>180 || x<0)
  81.   {
  82.     while(true);
  83.   }
  84.   else if(y<0 || y>180)
  85.   {
  86.     while(true);
  87.   }
  88.   myservo1.write(x);
  89.   myservo2.write(y);
  90.   if(count==1 && val=='w')
  91.   {
  92.     advance (100,100); //PWM 调速
  93.     delay(10);
  94.   }
  95.   else if(count==2 && val=='w')
  96.   {
  97.     advance (150,150); //PWM 调速
  98.   }
  99.   else if(count==3 && val=='w')
  100.   {
  101.     advance (200,200); //PWM 调速
  102.   }
  103.   else if(count==4 && val=='w')
  104.   {
  105.     advance (255,255); //PWM 调速
  106.   }
  107.   else if(count==1 && val=='s')
  108.   {
  109.     back_off (100,100); //PWM 调速
  110.   }
  111.   else if(count==2 && val=='s')
  112.   {
  113.     back_off (150,150); //PWM 调速
  114.   }
  115.   else if(count==3 && val=='s')
  116.   {
  117.     back_off (200,200); //PWM 调速
  118.   }
  119.   else if(count==4 && val=='s')
  120.   {
  121.     back_off (255,255); //PWM 调速
  122.   }
  123.   else if(count==1 && val=='a')
  124.   {
  125.     turn_L (80,100);
  126.   }
  127.   else if(count==2 && val=='a')
  128.   {
  129.     turn_L (150,150);
  130.   }
  131.   else if(count==3 && val=='a')
  132.   {
  133.     turn_L (200,200);
  134.   }
  135.   else if(count==4 && val=='a')
  136.   {
  137.     turn_L (255,255);
  138.   }
  139.   else if(count==1 && val=='d')
  140.   {
  141.     turn_R (100,100);
  142.   }
  143.   else if(count==2 && val=='d')
  144.   {
  145.     turn_R (150,150);
  146.   }
  147.   else if(count==3 && val=='d')
  148.   {
  149.     turn_R (200,200);
  150.   }
  151.   else if(count==4 && val=='d')
  152.   {
  153.     turn_R (255,255);
  154.   }
  155.   else stop();
  156.   delay(10);
  157. }
复制代码

回复

使用道具 举报

发表于 2012-5-8 14:29:48 | 显示全部楼层
学习学习!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-5-8 14:50:28 | 显示全部楼层
本帖最后由 sss_sxs 于 2012-5-8 14:52 编辑

//PROCESSING编程
import processing.serial.*;
Serial myport;
int count=0;
void setup()
{
size(480, 240);
smooth();
rect(100,20,40,40);
rect(20,100,40,40);
rect(100,100,40,40);
rect(180,100,40,40);
fill(0);
textSize(30);
text('w',110,48);
text('s',112,128);  
text('a',30,128);
text('d',190,128);
   fill(255);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   smooth();
   strokeWeight(1);
   stroke(0, 0, 0);
   triangle(360,18,330,70,390,70);
   triangle(328,72,276,102,328,132);  
   triangle(330,134,360,186,390,134);   
   triangle(392,132,444,102,392,72);   
   rect(330,72,60,60);
   textSize(11);
   fill(0);
   text("UP",355,58);
   text("DOWN",345,155);
   text("LEFT",300,107);
   text("RIGHT",398,107);  
   textSize(13);
   text("云台控制",334,107);     
   myport=new Serial(this,"COM3",19200);
   count=0;
}

void draw()
{
if(keyPressed && key==CODED && keyCode==UP)
{
   textSize(11);   
   fill(255,0,0);
   text("UP",355,58);
   myport.write(0x26);
}
else if(keyPressed && key==CODED && keyCode==DOWN)
{
     textSize(11);
     fill(255,0,0);
     text("DOWN",345,155);
     myport.write(0x28);
}
else if(keyPressed && key==CODED && keyCode==LEFT)
{
     textSize(11);
     fill(255,0,0);
     text("LEFT",300,107);
     myport.write(0x25);
}
else if(keyPressed && key==CODED && keyCode==RIGHT)
{
     textSize(11);
     fill(255,0,0);
     text("RIGHT",398,107);  
     myport.write(0x27);
}

if(keyPressed && key == 'w')
{
myport.clear();
fill(255,0,0);
text('w',110,48);
myport.write('w');
}
else if(keyPressed && key == 's')
{
  myport.clear();
fill(255,0,0);
text('s',112,128);
myport.write('s');
}
else if(keyPressed && key == 'a')
{
  myport.clear();
fill(255,0,0);
text('a',30,128);
myport.write('a');
}
else if(keyPressed && key == 'd')
{
  myport.clear();
fill(255,0,0);
text('d',190,128);
myport.write('d');
}



if(keyPressed && key == CODED)
{
if(keyCode==ALT && count == 1)
   {
     myport.clear();
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   myport.write(1);
      }
   }
if(keyPressed && key == CODED)
{   
if(keyCode==ALT && count==2)
{
      myport.clear();
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   myport.write(2);
    }
   }
if(keyPressed && key == CODED)
{   
if(keyCode==ALT && count==3)
   {
         myport.clear();
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   myport.write(3);
    }
   }
if(keyPressed && key == CODED)
{   
if(keyCode==ALT && count==4)
   {
         myport.clear();
   fill(255);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(30,144,255);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   myport.write(4);
     }
   }
if(keyPressed && key == CODED)
{   
if(keyCode==ALT && (count==5 || count==0))
   {
         myport.clear();
   fill(255);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(20,190,60,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(62,190,102,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(104,190,144,190);
   stroke(190);
   strokeWeight(20);
   strokeCap(SQUARE);
   line(146,190,186,190);
   myport.write(5);
   }
   }
if(count==5)
{
count=0;
}
}


void keyReleased()
{
fill(0);
textSize(30);
text('w',110,48);
text('s',112,128);  
text('a',30,128);
text('d',190,128);
if(keyCode==UP)
{
   textSize(11);
   fill(0);
   text("UP",355,58);  
}
else if(keyCode==DOWN)
{
   textSize(11);
   fill(0);
   text("DOWN",345,155);
}
else if(keyCode==LEFT)
{
   textSize(11);
   fill(0);
   text("LEFT",300,107);
}
else if(keyCode==RIGHT)
{
   textSize(11);
   fill(0);
   text("RIGHT",398,107);
}
}
void keyPressed()
{
if(keyCode==UP)
{
   textSize(11);
   fill(255,0,0);
   text("UP",355,58);
}
else if(keyCode==DOWN)
{
   textSize(11);
   fill(255,0,0);
   text("DOWN",345,155);
}
else if(keyCode==LEFT)
{
   textSize(11);
   fill(255,0,0);
   text("LEFT",300,107);
}
else if(keyCode==RIGHT)
{
   textSize(11);
   fill(255,0,0);
   text("RIGHT",398,107);
}
if(keyCode==ALT)
{
count++;
}
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-5-8 15:19:46 | 显示全部楼层
楼主是否可以把小车图片秀一下。最好再上一个视频,供大家学习学习!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-5-8 16:36:54 | 显示全部楼层
主要提供代码给大家看看设计思路,照片拍得水平太差,凑合着看了

作品成型一年多了,云台部分还是亲手用铁皮做的,算上人力,成本很高,主要享受DIY乐趣

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2012-5-8 22:01:59 | 显示全部楼层
恩、、还有很多可以改进的
电机没加闭环
回复 支持 反对

使用道具 举报

发表于 2012-9-15 23:46:39 | 显示全部楼层
我也做了,貌似pwm和舵机不能同时使用。另外采用差速转向,两个轮子不同速度,走直线比较空难,刚开始没问题,避障几次后就不行了,可能和开环控制有关系
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 14:14 , Processed in 0.048874 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表