Академический Документы
Профессиональный Документы
Культура Документы
Arduino
Two motors with gears and power wires.Motor with gears has 100-200 RPM
(revolutions per minute). It is good to connect ceramic capacitor 1uF to motor power
contacts - to reduce electric noise.
Two wheels
Wires to connect to battery set. Or USB cable - to connect to power-bank with USB
power connector.
Cardboard
Cut the circle from cardboard - chassis of the robot, arrange components on it.
Step 3: Setup motors
Setup the motors (with gears) and wheels on it - cut out holes for wheels and secure motors
on cardboard with ropes, wires or plastic ties.
Step 4: Setup turbine
Cut the round hole in the chassis for turbine and glue the turbine on the chassis.
Step 5: Turbine and motors
Mark the place where the plastic box will be fixed - this will be a dust-bin. Pin holes around
where the magnets will be located on the top side of the chassis
On the top side of the chassis Glue magnets using hols pinned on the previous step as marks.
Put a piece of paper on the place where the dust-bin will be set.
Put the plastic box on top of this paper - this will protect the box to be glued to chassis!
Put magnets around the plastic box. Magnets will stick on the places above magnets glued on
the top side - holding the paper below the box.
Fix magnets to the box with glue. When the glue dries - the box will be hold on the chassis by
magnets and it can be removed.
Cut away the paper.
Wooden stick are fixed with glue so Arduino board can be put on these sticks.
Or use another way to fix the Arduino board with motor-shield.
Connect all power wires from Arduino, turbine, motor-shield and battery-set.
Be careful
Bumpers are made from the cardboard stripes. They bended and glued in such shape.
Bumpers are fixed on the chassis in the way so when they are hit - they bend a bit hitting the
chassis.
Chassis is cut a bit if it is needed some space till bumpers.
Clean and scratch tin and wires for better contacts. Pieces of tin are connected to wires and
fixed by glue. Be sure the glue is not put between tin and wires.
Step 15: Connect bumpers
Fix contacts from tin to bumpers with glue - connect them to Arduino inputs (e.g. pins 5 and
8).
Another wires (with contacts on its end) are fixed in-front of such contacts on chassis.
Connect these two wires to Arduino GND.
When the bumper is pushed (e.g. it hits the wall) - contacts are connected, a pin gets
connected to GND.
Motor-shield can be connected to the Arduino board with wires. Just connect following
contacts between Arduino and motor-shield: 3, 4, 5, 6, 7, 8, GND, +5V
in the way:
pin GND Arduino is connected to pin GND on motor-shield,
The motor-driver on HG7881 can be connected to Arduino by wires. It is much cheaper than
motor-shield but maximum current is 800mA - which should be enough for most small DIY
robots.
PROGRAM
/*
Program for controlling a robot with two motors.
The robot turns when motors changes their speed and direction.
Front bumpers on left and right sides detect obstacles.
Motor shield is used to run motors.
*/
//right side
const int pinRightMotorDirection = 4;//"Dir A" on motor-shield
const int pinRightMotorSpeed = 3;//"PWM A" on motor-shield
const int pinRightBumper = 5;
//left side
const int pinLeftMotorDirection = 7;//"Dir B" on motor-shield
const int pinLeftMotorSpeed = 6;//"PWM B" on motor-shield
const int pinLeftBumper = 8;
//timeouts - they are different to avoid robot stuck in a corner
const int turnRightTimeout = 150;
const int turnLeftTimeout = 200;
//set in counter how long a motor is running back: N/10 (in milliseconds)
int countDownWhileMovingToRight;
int countDownWhileMovingToLeft;
//Initialization
void setup() {
initPins();
runRightMotorForward();
runLeftMotorForward();
startMotors();
}
//Main loop
void loop() {
verifyAndSetRightSide();
verifyAndSetLeftSide();
processRightSide();
processLeftSide();
delay(10);//repeat every 10 milliseconds
}
//--------------------------------------------------void initPins(){
pinMode(pinRightMotorDirection, OUTPUT);
pinMode(pinRightMotorSpeed, OUTPUT);
pinMode(pinRightBumper, INPUT_PULLUP);
pinMode(pinLeftMotorDirection, OUTPUT);
pinMode(pinLeftMotorSpeed, OUTPUT);
pinMode(pinLeftBumper, INPUT_PULLUP);
}
void startMotors(){
runRightMotorForward();
runLeftMotorForward();
}
void processRightSide(){
if(countDownWhileMovingToRight <= 0)//checks if counter was NOT ran when bumper
had been pressed
return;
//otherwise - counter is counting down (as a delay) while the right motor is moving
backward
countDownWhileMovingToRight--;//decrease the counter if it WAS ran when bumper
had been pressed
if(countDownWhileMovingToRight <= 0)//if the running counter got down to zero
runRightMotorForward();//run the right motor forward
}
void processLeftSide(){
if(countDownWhileMovingToLeft <= 0)
return;
countDownWhileMovingToLeft--;
if(countDownWhileMovingToLeft <= 0)
runLeftMotorForward();
}
void verifyAndSetRightSide(){
if(checkBumperIsNotPressed(pinRightBumper) //checks if right bumper has NOT been
pressed
)
return;
if(checkCounterIsNotSet(countDownWhileMovingToRight))//if the counter is not yet
counting down
runRightMotorBackward();//run the right motor backward
countDownWhileMovingToRight = turnRightTimeout;//set the counter to maximum
value to start it counting down
}
void verifyAndSetLeftSide(){
if(checkBumperIsNotPressed(pinLeftBumper) //checks if left bumper has NOT been
pressed
)
return;
if(checkCounterIsNotSet(countDownWhileMovingToLeft))//if the counter is not yet
counting down
runLeftMotorBackward();//run the right motor backward
countDownWhileMovingToLeft = turnLeftTimeout;//set the counter to maximum value
to start it counting down
}
bool checkCounterIsNotSet(int counter){
return counter <= 0;
}
bool checkBumperIsNotPressed(int pinBumper){
return digitalRead(pinBumper) == HIGH;
}
void runRightMotorForward(){
runMotorForward(pinRightMotorDirection, pinRightMotorSpeed);
}
void runLeftMotorForward(){
runMotorForward(pinLeftMotorDirection, pinLeftMotorSpeed);
}
void runRightMotorBackward(){
runMotorBackward(pinRightMotorDirection, pinRightMotorSpeed);
}
void runLeftMotorBackward(){
runMotorBackward(pinLeftMotorDirection, pinLeftMotorSpeed);
}
void runMotorForward(int pinMotorDirection, int pinMotorSpeed){
digitalWrite(pinMotorDirection, true); //set direction forward
analogWrite(pinMotorSpeed, 255); //set max speed forward
}
void runMotorBackward(int pinMotorDirection, int pinMotorSpeed){
digitalWrite(pinMotorDirection, false); //set direction backward
analogWrite(pinMotorSpeed, 255); //set max speed backward
}