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個でも可能ですが
元のプログラムを生かし、左右独立のコントロールさせています。
プログラムは、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);
}