It's time to play Arduino and Motor Shield
I received many weeks ago enough part to build a small arduino robot, and I have nos the time. Let's build it...For this project I have no real objective. I just want to asseble an autonomous robot, avoiding wall or other object.
The parts
- Arduino uno for control-command
- Seeed motor shield V2 for power and simplifaction of command
- An HC-SR04 ultra-sonic sensor for objects detection
- A 9g servo motor for ultra sonic sensor rotation
- battery clip connector 9v to 2,1 power plug (see here to build or buy )
- a chassi 2wd motor, with bolts and nuts, wheel, two small DC motor, a four battery pack AAA some wires and an end wheel
The assembly
Nothing very important to say here. Try to assemble, see what goes wrong, re assemble , what goes wrong and so on.The only thing to say is that I make parts work separately first :
- the HC-SR04 ultra-sonic sensor
- then the servo
- then the motor shield
- Motor shield use pin 8 to 13 to work
- AND servo (library) disable PWM for 9 and 10 pin
So it does not work. The solution is to change the pin mapping in motor shield libraries from 9,10 to 5,6 and connect them physicaly (not shown on this picture).
I saw this on a blog here. It works for me too ,but if you try, you try at your own risks.
Servo
Library used : arduino Servo#include "Servo.h"
const int SERVO_CMD = 14;
void setup() {
myServo.attach(SERVO_CMD);
}
and
myServo.write(angle);
MotorShield (Seeed v2)
Library used : seeed MotorDriver modified in MotorDriver56 to use pwm 5,6 in place of 9 and 10include "MotorDriver56.h"
void setup() {
motordriver.init();
motordriver.setSpeed(200,MOTORB);
motordriver.setSpeed(200,MOTORA);
}
andmotordriver.goForward();
motordriver.goBackward();
motordriver.goLeft();
motordriver.goRight();
motordriver.stop();
typical use
motordriver.go${Action}();
delay(time);
motordriver.stop();
HC-SR04
use const int trigPin = 4;
const int echoPin = 7;
and
int calculateDistance(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
int distance = duration*0.034/2;
return distance;
}
The basic control / command
if (nothing front) {
goFront()
} else if ( something left ) {
goBackward()
goRight()
} else {
goBackward()
goLeft()
}
But ...
My small rover does not go straightaway. The reasons ... motor are differents, the end wheel is not so good. Those two points cause deviations.Next improvment
- the tail whell cause trajectory deviation. Need to change to a round ball, or use chains
- add encoder measurement to have a better distance/movement estimation
- there's no sensor at back (securing goBackward() )
- Use arduino interrupt
No comments:
Post a Comment