Hey, i bought the DFRobotShop Rover about a week ago, and im new to the Arduino coding language, but familiar with PBASIC. Is there anywhere i can get a tutorial on learning to write code specifically for this Rover? Arduino has a lot of tutorials but none seem to be what i need to know, like motor control. If anyone can help me out or point me in teh right direction i'd greatly appreciate it.
Motor control is simply a matter of setting pins high and low to get the motor to do what you want. Exactly what combination of high and low depend on what the motor driver circuit is.
Look at the sites selling motor shields for example code or look here for theory:-
http://www.thebox.myzen.co.uk/Workshop/Motors_1.html
and
http://www.thebox.myzen.co.uk/Workshop/Motors_2.html
Thanks for the quick reply. I understand motors and such, but whats confusing me is translating what i want the motors to do into code. I cant find any tutorials on how to code things as simple as making the Rover move forward a foot and stop.
Oi, that's already the complicated stuff. The easy stuff is run both motors forward at full speed for 1 second.
If you're in need of tutorial and information, look for the relevant parts and add Arduino. For example on the DFRobotShop page you're told:
... incorporates a standard Arduino Duemilanove (surface mount ATMega328), L293B motor driver (connected to pins 5 to 8),
Now try: Arduino L293B motor driver and then follow any link that looks promising
Korman
Wow, THANK YOU! I dont know i missed that, ive serched that site a few times, sorry... heheh, really nooby to Arduino... Thanks a lot guys, youve both been a big help!
I cant find any tutorials on how to code things as simple as making the Rover move forward a foot and stop.
Something to keep in mind about that rover: It is configured for open-loop control.
In other words, there's no feedback system to tell the Arduino how many rotations a given motor has moved; without this information you can't accurately tell it to move forward a given number of units, you can only at best tell it how long to leave a motor on. PWM control, the surface under the treads, battery voltage, etc - will all skew this. During one run you may get it to move so far perfectly, and a future run will cause it to move less (or more!).
Also, without feedback, you'll never get it to run in a perfectly straight line; feedback allows to speed up or slow down one side or the other in order to match conditions so that the robot can move in a straight line.
You aren't there yet with your knowledge to implement a method to introduce feedback control on the treads/motors. But keep it in mind as you progress, because at some point you will likely want to implement something to correct this deficiency...
Well, to revise my first statement regarding making it move a foot and stop, all i want it to do is move at all, i dont care what amount of time or distance all i want is an understand of making the motors run, code wise. Based on what ive been learning through tutorials, (Mind you, ive only had time to do a few) this is the code ive come up with (And promise you wont laugh at me if its horribly wrong.):
int motorPin5 = 2;
int motorPin8 = 3;void setup() {
pinMode(motorPin5, OUTPUT);
pinMode(motorPin8, OUTPUT);}
void loop() {
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin8, LOW);digitalWrite(motorPin5, LOW);
digitalWrite(motorPin8, HIGH);}
Not all that wrong, just one glaring point.
void loop() {
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin8, LOW);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin8, HIGH);
}
Turn pin5 high, a tiny fraction of time later, turn it low again.. then again, a tiny fraction of time (read this is a fraction of a millisecond) turn it high again, etc. etc.
You could use a delay after switching the pins around so you can see the changes.
As it is right now, it will turn it on and off at rediculous speeds.. which can produce any number of effects, but certainly none you expect.
PS: from what I've read in this topic, the motorPin# variables have values that don't work.. as the driver is connected to pins 5 and 8, not 2 and 3.
If you've attached LED's to pin2 and 3 to test the motor driving part without actually using the motors, my bad, but you didn't share this part
Ive looked over what you said, and this is my revised code:
int motorPin5 = 2;
int motorPin8 = 3;
void setup() {
pinMode(motorPin5, OUTPUT);
pinMode(motorPin8, OUTPUT);
}
void loop() {
digitalWrite(motorPin5, HIGH);
digitalWrite(motorPin8, LOW);
delay(1000);
digitalWrite(motorPin5, LOW);
digitalWrite(motorPin8, HIGH);
}
But i still get nothing. One thing i think may be the problem is the Rovers motors arnt connected to pins on the micro-controller, there in those blue +/- screw terminals, labeled M1 and M2 on the board and each have 2 micro led leads on them. Whats the code for assigning them? Its the one parts thats utterly kicking my butt.
But i still get nothing. One thing i think may be the problem is the Rovers motors arnt connected to pins on the micro-controller, there in those blue +/- screw terminals, labeled M1 and M2 on the board and each have 2 micro led leads on them. Whats the code for assigning them? Its the one parts thats utterly kicking my butt.
[edit]Stupid question: There should be wires from each motor running to the blue terminals - are these hooked up (I hope so)?
I re-read your sentence; the "there"/"they're" was throwing me off - so I think you were saying the motors are hooked up, but you don't know which pins control them - correct?[/edit]
You didn't say where you got the robot from, but have you read the manual (for the example code) and/or schematic for the robot from here?:
http://www.robotshop.com/dfrobotshop-rover-tracked-robot-basic-kit.html
(see the tab labeled "Useful Links")
The code seems to indicate that you need to apply a PWM signal using analogWrite() to the enable pin of the motor being controlled (sets the speed), then bring the motor's digital pin LOW with digitalWrite() to drive it forward, or HIGH to drive it backward (assuming the wires are connected properly - personally, I would have HIGH be forward, and LOW backward); the code in the manual reads:
/*
Title: DFRobotShop Rover Sample Sketch #1
Authors: DFRobot, RobotShop
Date: 12/03/2010
Licence: GPL v3
Description: Sketch for the DFRobotShop Rover.
URL: www.robotshop.com
This program allows serial commands to be sent to the DFRobotShop Rover by either a wired (USB) or
wireless (Bluetooth, XBee) connection. The commands are "w", "a", "s" and "d" for driving forward,
turning left, turning right and reversing. The rover will execute the command until it is told to stop by
pressing any other character.
We suggest using the DFRobot Bluetooth module or XBee module with Windows Hyperterminal for fluid
wireless control. Ensure you select the correct COM port and 9600 baud rate. Note that it is likely the
two motors are not identical and you will need to adjust the speed so the robot goes perfectly straight.
*/
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
void setup(void)
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}
void loop(void)
{
while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
switch(val) // Perform an action depending on the command
{
case 'w'://Move Forward
forward (leftspeed,rightspeed);
break;
case 's'://Move Backwards
reverse (leftspeed,rightspeed);
break;
case 'a'://Turn Left
left (leftspeed,rightspeed);
break;
case 'd'://Turn Right
right (leftspeed,rightspeed);
break;
default:
stop();
break;
}
}
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward(char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void reverse (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void left (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void right (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
Note at the top, the pins for PWM are pins 5 and 6, while the pins for the directional control are 7 and 8 (respective for each motor). This code of course is meant for serial control via bluetooth or xbee, but you could test it using hyperterminal pointed to the USB serial port, with a cable connecting the robot, and prop the robot on a stand so that the tracks aren't touching anything (this should be your testing method anyhow, at least initially).
Hope this helps...
Exactly what I was looking for, the pins they were assigned to, thank you. I got this program to run and work, so i know the motors work. It helped a lot so know I'm just playing around with this program but the motor assignment is STILL not seeming to make sense to me. Ill keep playing around with this program and post anything i get right, in hopes to help others.