0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GR-LycheeでMotorShield

Last updated at Posted at 2018-10-21

GR-Lycheeでローバーを駆動させます。

MotorShieldを使用します。
https://www.adafruit.com/product/1438
http://akizukidenshi.com/download/lib/adafruit/Adafruit_Motor_Shield_V2_Library-master.zip
Adafruit_MotorShield.hをインクルードしてください。

GR-LycheeのVINから5Vが出力されていないので、5VからVINへ直結する必要があります。
今回は、フロントにサーボを搭載したローバーを使うため、本来出力は1個でも可能ですが
元のプログラムを生かし、左右独立のコントロールさせています。

IMG_8307.JPG

プログラムは、Githubからの物を加工しています。
https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

# include <Wire.h>
# include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

// You can also make another motor on port M2
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(150);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myOtherMotor->setSpeed(150);
  myOtherMotor->run(FORWARD);
  // turn on motor
  myOtherMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
  
  Serial.print("Foward\n\r");

  myMotor->run(FORWARD);
    myOtherMotor->run(FORWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i); 
       myOtherMotor->setSpeed(i);   
    delay(20);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
        myOtherMotor->setSpeed(i);  
    delay(20);
  }
    Serial.print("LeftFoward\n\r");
    myMotor->run(FORWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);  
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
    delay(10);
  }
      Serial.print("RightFoward\n\r");
    myOtherMotor->run(FORWARD);
  for (i=0; i<255; i++) {
    myOtherMotor->setSpeed(i);  
    delay(10);
  }
  for (i=255; i!=0; i--) {
    myOtherMotor->setSpeed(i);  
    delay(10);
  }
  Serial.print("Back\n\r");
  myMotor->run(BACKWARD);
    myOtherMotor->run(BACKWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);  
        myOtherMotor->setSpeed(i);  
    delay(20);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
        myOtherMotor->setSpeed(i);  
    delay(20);
  }
   Serial.print("LeftBack\n\r");
  myMotor->run(BACKWARD);
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);  
    delay(20);
  }
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
    delay(20);
  }

  Serial.print("RightBack\n\r");
    myOtherMotor->run(BACKWARD);
  for (i=0; i<255; i++) {
        myOtherMotor->setSpeed(i);  
    delay(20);
  }
  for (i=255; i!=0; i--) {
        myOtherMotor->setSpeed(i);  
    delay(20);
  }
 
  
  Serial.print("RELEASE\n\r");
  myMotor->run(RELEASE);
    myOtherMotor->run(RELEASE);
  delay(1000);
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?