Arduino Fioでラジコン その2

Arduino Fioでラジコンシリーズ >その1 >その2 >その3

前回、PCとArduino Fioが通信するところまで出来たので、今回は工作キットの車に乗せて、走らせてみました。

■準備したもの
楽しい工作シリーズ
No.168 ダブルギヤボックス 左右独立4速タイプ
No.101 トラックタイヤ 36mm
No.98 ユニバーサルプレートセット
No.152 単4電池ボックス 1本用 スイッチ付

説明書に従って作ります。久々の工作、楽しかったです。



■モーターを動かす回路
Fioの出力は、モーターを回せるほどの電流を供給できません。そこでトランジスタを使います。下図の2SC1815というパーツです。電池からモーターを通過した電流は、トランジスタに入ります。ここでD12,D13がOnの場合は、電流が流れてモーターが回り、Offなら流れません。こちらにしっかりとした説明があります。トランジスタ回路の基本設計法



上の回路図を組上げるとこんな感じになります。この状態で、前回テストで使ったProcessingのarduino_output を操作すると、左右のタイヤが動くと思います。


■openFrameworks
次に操作アプリを作ります。先ほどはProcessingを使いましたが、今後の拡張のしやすさを考えて、慣れているopenFrameworksを使います。
ofフォルダのapps/example/firmataExampleにArduinoを扱うサンプルがあります。これを必要な部分だけ残し、以下のように改造しました。上キーでD12,13をOn(前進)、右キーでD12だけOn(右旋回)、左キーでD13だけOn(左旋回)です。

testApp.h
#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed  (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);

void setupArduino();
void updateArduino();
ofArduino ard;
bool bSetupArduino;
};
#endif


testApp.cpp
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
ard.connect("/dev/cu.usbserial-A700eEMl", 57600); //適宜、変更してください
bSetupArduino = false;       // flag so we setup arduino when its ready, you don't need to touch this :)
}
//--------------------------------------------------------------
void testApp::update(){
if ( ard.isArduinoReady()){

// 1st: setup the arduino if haven't already:
if (bSetupArduino == false){
setupArduino();
bSetupArduino = true; // only do this once
}
// 2nd do the update of the arduino
updateArduino();
}
}
//--------------------------------------------------------------
void testApp::setupArduino(){
// this is where you setup all the pins and pin modes, etc
for (int i = 0; i < 13; i++){
ard.sendDigitalPinMode(i, ARD_OUTPUT);
}
ard.sendDigitalPinMode(12, ARD_OUTPUT);
ard.sendDigitalPinMode(13, ARD_OUTPUT);  // on diecimelia: 11 pwm?*/
}
//--------------------------------------------------------------
void testApp::updateArduino(){
// update the arduino, get any data or messages:
ard.update();
}
//--------------------------------------------------------------
void testApp::draw(){
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
switch ((int)key) {
case 357:
ard.sendDigital(12, ARD_HIGH);
ard.sendDigital(13, ARD_HIGH);
break;
case 356:
ard.sendDigital(12, ARD_LOW);
ard.sendDigital(13, ARD_HIGH);
break;
case 358:
ard.sendDigital(12, ARD_HIGH);
ard.sendDigital(13, ARD_LOW);
break;
default:
ard.sendDigital(12, ARD_LOW);
ard.sendDigital(13, ARD_LOW);
break;
}
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
ard.sendDigital(12, ARD_LOW);
ard.sendDigital(13, ARD_LOW);
cout << "Release" << endl;
}


■試運転

Arduino Fio Demo1 from HR2 on Vimeo.



■まとめ
この回路ではモーターを回すか止めるかしかできないので、バックは出来ません。また、後輪を独立して動かすだけでは、左右に曲がれませんでした。
後進するためには、モーターに流す電流の向きを逆にする必要があります。今回のようにトランジスタを組み合わせて、回路を作る事も出来ますが、基板が大きくなるのと面倒なので、普通は専用のモータードライバICを使います。また、旋回は、左右のモーターを逆回転させて行います。


という事で、次回は戦車のようなキャラピラに変え、後進と旋回できるようにしたいと思います。

Arduino Fioでラジコンシリーズ >その1 >その2 >その3