Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

移動ロボット駆動用Arduinoのプログラム

Last updated at Posted at 2020-07-06

移動ロボット駆動用Arduinoのプログラム

microbit_20200505drive-turn.ino

#include <SoftwareSerial.h>
#include <SabertoothSimplified.h>
//
// Connections to make:
//   Arduino TX->1  ->  Sabertooth S1
//   Arduino GND    ->  Sabertooth 0V
//   Arduino VIN    ->  Sabertooth 5V
//
SoftwareSerial mySerial(13, 12); // RX, TX
SabertoothSimplified ST;
char cmd = 'n';
int pw = 0;
int cnt = 0;
String str;

void setup() {
//  Serial.begin(9600); // ハードウェアシリアルを準備
  SabertoothTXPinSerial.begin(9600);
  // This is the baud rate you chose with the DIP switches.
  ST.drive(0); // The Sabertooth won't act on mixed mode until
  ST.turn(0);  // it has received power levels for BOTH throttle and turning, since it
  // mixes the two together to get diff-drive power levels for both motors.
  // So, we set both to zero initially.

  //  Serial.println("Ready");
  mySerial.begin(115200); // ソフトウェアシリアルの初期化
//  mySerial.println("#V:");
}

void loop() {
  if (mySerial.available()) {
    cmd = mySerial.read();
    //    Serial.write(cmd);
    if (cmd == 'F' ) {
      //      Serial.write("Forward");
      ST.drive(pw);  // Go forward at full power.
      delay(20);         // Wait 20 mili-seconds.
      ST.turn(0);    // Stop.

    } else if ( cmd == 'B' ) {
      //      Serial.write("Back");
      ST.drive(-pw); // Reverse at full power.
      delay(20);         // Wait 20 mili-seconds.
      ST.turn(0);    // Stop.

    } else if ( cmd == 'S' ) {
      //      Serial.write("Stop!");
      cnt = 0;
      pw = 0;
      
      ST.turn(0);    // Stop.
      ST.drive(0);         // Wait 20 mili-seconds.

    } else if ( cmd == 'G' ) {
      //      Serial.write("Stop!");
      cnt = cnt + 1;
      if (cnt == 5 ) {
        cnt = 4;
      }
//      str = String("#"+String(cnt)+":");
//      mySerial.println(str);
      pw = 32 * cnt - 1;

      ST.drive(0);  
      ST.turn(0);    // Stop.

    } else if ( cmd == 'R' ) {
      //      Serial.write("Right");
      ST.drive(0);
      ST.turn(pw);

    } else if ( cmd == 'L' ) {
      //     Serial.write("Left");
      ST.drive(0);
      ST.turn(-pw);

    } else {
      //
    }
  }
  //  if (Serial.available()) {
  //    mySerial.write("#Hello!:");
  //  }
}

ドライバのプログラムに必要なヘッダファイル等の情報

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?