2018年6月23日土曜日

GPSモジュールを使ってロボットを動かそう! #1


皆さんこんにちは
Arduino工房管理人です。

今回は動画のロボットの作り方を紹介していきたいと思います。
これですね。

※使っているもの
 Arduino UNO
 GPS受信機キット 1PPS出力付き 「みちびき」3機受信対応
 モータードライバ(TA7291P)
 (玩具屋に売っている戦車のおもちゃの下半分)
 上記の回路に必要な抵抗、ダイオード、ジャンパワイヤ
 等々

そこそこのお値段はします。


よくこのGPSモジュールで読み取った値をシリアルモニタに表示させている方が多いのですが、私のコンセプトはそれを使ってロボットを動かすこと。

なかなか難しいかもしれませんができるだけわかりやすく解説していくつもりです。

〇プログラムの仕組み
 どのようなプログラムなのか直接書いてもわかりにくいものです。
 そこで今回のプログラムがどのようなものなのかを説明します。
  1.止まりたい座標を決める
  2.前進する
  3.止まりたい座標かを調べる
  4.違うなら前進その座標なら回転した後止まる
 このような感じです。
 では どのようなプログラムなのか?

#include <TinyGPS++.h>
#include <SoftwareSerial.h>

float a;
const int motor1A = 9;
const int motor1B = 10;
const int PWM_mot_1 = 11;
const int motor2A = 4;
const int motor2B = 5;
const int PWM_mot_2 = 6;
TinyGPSPlus gps;
SoftwareSerial mySerial(2, 3); // RX, TX
//TinyGPSCustom magneticVariation(gps, "GPRMC", 10);

void setup() {
 // Open serial communications and wait for port to open:
 Serial.begin(57600);
 while (!Serial) {
 ; // wait for serial port to connect. Needed for native USB port only
 }

Serial.println("Goodnight moon!");

 // set the data rate for the SoftwareSerial port
 mySerial.begin(9600);
 mySerial.println("Hello, world?");
}

void loop() { // run over and over
 while (mySerial.available() > 0){
  char c = mySerial.read();
  //Serial.print(c);
  gps.encode(c);
  if (gps.location.isUpdated()){
    Serial.print("LAT="); Serial.println(gps.location.lat(), 6);
    Serial.print("LONG="); Serial.println(gps.location.lng(), 6);
    Serial.print("ALT="); Serial.println(gps.altitude.meters());
    a = gps.location.lng();
    Serial.println(a,5);
  }

  if ( a < 139.9243 ) {
    analogWrite(PWM_mot_1,200); 
    analogWrite(PWM_mot_2,200);
    digitalWrite(motor1A,LOW);
    digitalWrite(motor1B,HIGH);
    digitalWrite(motor2A,LOW);
    digitalWrite(motor2B,HIGH);
    delay(5);
  } else {
    analogWrite(PWM_mot_1,225); 
    analogWrite(PWM_mot_2,225);
    digitalWrite(motor1A,HIGH);
    digitalWrite(motor1B,LOW);
    digitalWrite(motor2A,LOW);
    digitalWrite(motor2B,HIGH);
    delay(5000);
    analogWrite(PWM_mot_1,0); 
    analogWrite(PWM_mot_2,0);
    digitalWrite(motor1A,HIGH);
    digitalWrite(motor1B,LOW);
    digitalWrite(motor2A,LOW);
    digitalWrite(motor2B,HIGH);
    delay(30000);
  }
 }
}

あれ?予想以上にあっさり?

次回はプログラムの説明をしていきたいと思います。
(もしまだArduinoの初心者なら少し自分で考えてから#2を見ていただくとありがたいです)


それでは
#2をお楽しみに

Arduino工房:My Channel

0 件のコメント:

コメントを投稿