2009年6月14日日曜日

モータドライブICは便利!?


この回路、おぼえてますか?
LB1650というICを使って2つのDCモータを制御できる回路です。
LB1650のデータシートは、ここなどから入手できます。
さて、この回路とArduinoをどうつなぐか?
モータ用の電源は、Arduinoとは別に用意して接続します。
IN1 IN2 IN3 IN4にどのような状態にするとモータがどうなるか?

左モータ IN1 IN2
 正転  1  0
 逆転  0  1
ブレーキ 1  1
ブレーキ 0  0

右モータは、同様にIN3 IN4で制御できます。
正転・逆転は、モータとの接続によって変化します。
出来上がった回路に、Arduinoをつないで、スケッチを動かして、思ったのと逆に動いてしまったら、スケッチの1/0を逆にすれば大丈夫です。
pwmも使いたいので
左モータ用 5 6
右モータ用 10 11
と接続することにしました。

スケッチは、FETのライントレーサを手直しして

 

#define LmotorPin1 5 // Right Motor in1 connected to analog pin 5
#define LmotorPin2 6 // Right Motor in2 connected to analog pin 6
#define RmotorPin3 10 // Left Motot in3 connected to analog pin 10
#define RmotorPin4 11 // Left Motot in3 connected to analog pin 11

#define Rpower 100
#define Lpower 100

#define photPin 0
#define ledPin 13

int pt = 0;

void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT);
pinMode(LmotorPin1, OUTPUT); // sets the analog pin as output
pinMode(LmotorPin2, OUTPUT);
pinMode(RmotorPin3, OUTPUT);
pinMode(RmotorPin4, OUTPUT);
Serial.begin(9600);
}

void loop() // run over and over again
{
pt = readPhot(photPin);
if(pt > 80)
right(50);
else
left(50);
}

int readPhot(int p){
int val = 0;
digitalWrite(ledPin, HIGH);
delay(5);
val = analogRead(p);
Serial.print(val);
Serial.print("\n");
digitalWrite(ledPin, LOW);
return val;
}

void right(int t){
analogWrite(LmotorPin1, Lpower); // sets the LeftMotor on
analogWrite(LmotorPin2, 0);
analogWrite(RmotorPin3, 0);
analogWrite(RmotorPin4, 0);
delay(t);
}

void left(int t){
analogWrite(LmotorPin1, 0); // sets the RightMotor on
analogWrite(LmotorPin2, 0);
analogWrite(RmotorPin3, Rpower);
analogWrite(RmotorPin4, 0);
delay(t);
}


関数にしておいたので、修正も簡単でしたね。

0 件のコメント:

コメントを投稿