PWM控制智能小车速度

 

用Arduino板子做小车,下面的简单程序基本能实现小车的基本行走

 
#define STOP      0
#define FORWARD   1
#define BACKWARD  2
#define TURNLEFT 3
#define TURNRIGHT 4

int leftmotor1=4;
int leftmotor2=5;
int rightmotor1=6;
int rightmotor2=7;
void setup() {
  pinMode(leftmotor1,OUTPUT);
  pinMode(leftmotor2,OUTPUT);
  pinMode(rightmotor1,OUTPUT);
  pinMode(rightmotor2,OUTPUT);
}
void loop() {
  int a;
  for(a=0;a<5;a++)
  {
    run(a);
    delay(2000);
    }
}
void run(int b)
{
  switch(b)
  {
     case FORWORD
     digitalWrite(leftmotor1,HIGH);
     digitalWrite(leftmotor2,LOW);
     digitalWrite(rightmotor1,HIGH);
     digitalWrite(rightmotor2,LOW);
     break;
     case BACKWARD:
     digitalWrite(leftmotor1,LOW);
     digitalWrite(leftmotor2,HIGH);
     digitalWrite(rightmotor1,LOW);
     digitalWrite(rightmotor2,HIGH);
     break;
     
     case TURNLEFT
     digitalWrite(leftmotor1,LOW);
     digitalWrite(leftmotor2,HIGH);
     digitalWrite(rightmotor1,HIGH);
     digitalWrite(rightmotor2,LOW);
     break;
     case TURNRIGHT
     digitalWrite(leftmotor1,HIGH);
     digitalWrite(leftmotor2,LOW);
     digitalWrite(rightmotor1,LOW);
     digitalWrite(rightmotor2,HIGH);
     break;

     default:
     digitalWrite(leftmotor1,LOW);
     digitalWrite(leftmotor2,LOW);
     digitalWrite(rightmotor1,LOW);
     digitalWrite(rightmotor2,LOW);
    }
 }

 

 

但是呢,经过测试,可能会出现这样的问题,比如:左右轮子的速度不一样,因此小车怎样都走不了直线。

那可能是因为电源对左右马达的供电不同对轮子的速度存在影响,因此想让轮子速度一样就可用PWM控制轮子的速度使其保持同速,将PWM值赋值给轮子,轮子的速度就可以一样,这样的话小车就不会走斜了。修改后的代码如下:

 
#define STOP 0 
#define FORWARD 1 
#define BACKWARD 2 
#define TURNLEFT 3 
#define TURNRIGHT 4 
int leftmotor1=4; 
int leftmotor2=5; 
int rightmotor1=6; 
int rightmotor2=7;
int val=200; 
void setup() 
{
    pinMode(leftmotor1,OUTPUT); 
    pinMode(leftmotor2,OUTPUT); 
    pinMode(rightmotor1,OUTPUT); 
    pinMode(rightmotor2,OUTPUT); 
} 
void loop() 
{ 
    // put your main code here, to run repeatedly: 
    int a; 
    for(a=0;a<5;a++) 
    { 
        run(a); 
        delay(2000); 
    } 
} 
void run(int b)
{ 
    switch(b) 
    { 
    case FORWORD:
        analogWrite(leftmotor1,val); 
        analogWrite(leftmotor2,0); 
        analogWrite(rightmotor1,val); 
        analogWrite(rightmotor2,0); 
        break; 
    case BACKWARD: 
        analogWrite(leftmotor1,0); 
        analogWrite(leftmotor2,val); 
        analogWrite(rightmotor1,0); 
        analogWrite(rightmotor2,val); 
        break; 
    case TURNLEFT:
        analogWrite(leftmotor1,0); 
        analogWrite(leftmotor2,val); 
        analogWrite(rightmotor1,val); 
        analogWrite(rightmotor2,0); 
        break; 
    case TURNRIGHT:
        analogWrite(leftmotor1,val); 
        analogWrite(leftmotor2,0); 
        analogWrite(rightmotor1,0); 
        analogWrite(rightmotor2,val); 
        break; 
    default: 
        analogWrite(leftmotor1,0); 
        analogWrite(leftmotor2,0); 
        analogWrite(rightmotor1,0); 
        analogWrite(rightmotor2,0); 
    } 
}

只需将一例程序中数字量改为模拟量即可:digitalWrite--analogWrite,再将HIGH改为PWM值,LOW改为0。

改过的代码还有一个好处就是将PWM值用变量保存,如果要改变速度,只需改一个数字而已。

你可能感兴趣的:(Arduino)