sss_sxs 发表于 2012-5-8 14:17:54

原创带调速四轮小车

设计想法是:通过双击ALT键来完成速度的改变,一共分为四档且是一个循环的功能.初始设置是停止,编写processing完成上位机软件控制部分,最终成型除了前后左右带调速,还装备舵机摄像头实现远程遥控.

//arduino编程部分
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int E1 = 5; //定义M1 使能端
int E2 = 6; //定义M2 使能端
int M1 = 4; //定义M1 控制端
int M2 = 7; //定义M1 控制端
int x=0;
int y=0;
int count=5;
void stop() //停止
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void advance(char a,char b) //前进
{
analogWrite (E1,a); //PWM 调速
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void back_off (char a,char b) //后退
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void turn_L (char a,char b) //左转
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void turn_R (char a,char b) //右转
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void setup()
{
myservo1.attach(1);
myservo2.attach(2);
int i;
for(i=4;i<=7;i++)
    pinMode(i, OUTPUT);
Serial.begin(19200); //设置串口波特率
}
void loop()
{
char val = Serial.read();
if(val!=-1)
{
    if(val==1 || val==2 || val==3 || val==4 || val==5 || val==0)
    {
      count=val;
    }
}
if(val == 0x26 && x<=180)
{
    x++;
}
else if(val == 0x28 && x>=0)
{
    x--;
}
else if(val == 0x25 && y<=180)
{
    y++;
}
else if(val == 0x27 && y>=0)
{
    y--;
}
else if(x>180 || x<0)
{
    while(true);
}
else if(y<0 || y>180)
{
    while(true);
}
myservo1.write(x);
myservo2.write(y);
if(count==1 && val=='w')
{
    advance (100,100); //PWM 调速
    delay(10);
}
else if(count==2 && val=='w')
{
    advance (150,150); //PWM 调速
}
else if(count==3 && val=='w')
{
    advance (200,200); //PWM 调速
}
else if(count==4 && val=='w')
{
    advance (255,255); //PWM 调速
}
else if(count==1 && val=='s')
{
    back_off (100,100); //PWM 调速
}
else if(count==2 && val=='s')
{
    back_off (150,150); //PWM 调速
}
else if(count==3 && val=='s')
{
    back_off (200,200); //PWM 调速
}
else if(count==4 && val=='s')
{
    back_off (255,255); //PWM 调速
}
else if(count==1 && val=='a')
{
    turn_L (80,100);
}
else if(count==2 && val=='a')
{
    turn_L (150,150);
}
else if(count==3 && val=='a')
{
    turn_L (200,200);
}
else if(count==4 && val=='a')
{
    turn_L (255,255);
}
else if(count==1 && val=='d')
{
    turn_R (100,100);
}
else if(count==2 && val=='d')
{
    turn_R (150,150);
}
else if(count==3 && val=='d')
{
    turn_R (200,200);
}
else if(count==4 && val=='d')
{
    turn_R (255,255);
}
else stop();
delay(10);
}

arduino-tinker 发表于 2012-5-8 14:29:48

学习学习!

sss_sxs 发表于 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++;
}
}

Randy 发表于 2012-5-8 15:19:46

楼主是否可以把小车图片秀一下。最好再上一个视频,供大家学习学习!

sss_sxs 发表于 2012-5-8 16:36:54

主要提供代码给大家看看设计思路,照片拍得水平太差,凑合着看了

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

Malc 发表于 2012-5-8 22:01:59

恩、、还有很多可以改进的
电机没加闭环

萧芸凤 发表于 2012-9-15 23:46:39

我也做了,貌似pwm和舵机不能同时使用。另外采用差速转向,两个轮子不同速度,走直线比较空难,刚开始没问题,避障几次后就不行了,可能和开环控制有关系
页: [1]
查看完整版本: 原创带调速四轮小车