go_away

Author Topic: Building autonomous robot, Arduino Duemillanove board , programming help needed!  (Read 5147 times)

0 Members and 1 Guest are viewing this topic.

Offline RobobuilderTopic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Hi ,

as I mentioned in another post I am building a robot based on the arduino board.

Now it is built and the components are:
Arduino Duemilanove Board , 2 solarbotics motors that are hooked up to a Pololu Low Voltage Dual Serial Motor Controller. The robot has one servo which a Sharp GP2D12 IR Range Sensor is mounted on. I intended this to rotate and "scan" the area that the robot is travelling in.It is basicly the same robot as this one ;
http://www.robotshop.ca/robotshop-rover-arduino-basic-kit-1.html
But mine has only one servo and the IR is mounted on it. Is this possible ?

Now , my problem is with the programming. I am a complete novice and so far I have devised this code (mainly by taking the example code and removing servo 1, leaving one servo)  ;

int servopulse2 = 1550; //test servo pan position (0 to 180)
int servopin2 = 9; //analog pin 3=17
int motor_reset = 8; //digital pin 8 assigned to motor reset
int irpin = 3;//Changed from 0
int distance = 0; // what is this?
int ledpin = 13;
void setup()
{
pinMode(motor_reset, OUTPUT);
pinMode(servopin2, OUTPUT);
Serial.begin(9600);
digitalWrite(motor_reset, LOW);
delay(100);
digitalWrite(motor_reset, HIGH);
delay(100);
// reset motor controller
}
void loop()
{
servoposition();
delay(20);
digitalWrite(servopin2, LOW);
motorforward();
irdetection();
}
//subroutine servoposition
void servoposition()
{
for(int i=1; i<=20; i++) // Ensures the servos reach their final position
{

digitalWrite(servopin2, HIGH); // Turn the L motor on
delayMicroseconds(50); // Length of the pulse sets the motor position//Changed from servopulse 2
digitalWrite(servopin2, LOW); // Turn the L motor off
delay(20);}
}
void irdetection()
{
distance=analogRead(irpin); // Interface with the Sharp IR Sensor
if (distance<=575&&distance>=425) // Detecting objects within roughly 10cm
{
motorstop(); // stop the rover
ledwarning(); // light up the LED for 1 second
delay(1000);
servopulse2=2000;// move the pan/tilt
servoposition(); // refresh the servos
delay(500);
motorreverse(); // reverse the motors for 1 second
delay(1000);
motorstop(); // stop the motors
delay(1000); // wait 1 second
rotateccw(); // rotate for 2 seconds
delay(2000);
motorstop(); // stop the motors
delay(1000);
servopulse2=1500;// move the servos
servoposition();
delay(1000);
}
}
void ledwarning()
{
digitalWrite(ledpin, HIGH); // sets the LED on
delay(1000);
digitalWrite(ledpin, LOW); // sets the LED off
}
//subroutine motor forward
void motorforward()
{
//left motor
unsigned char buff1[6];
buff1[0]=0x80; //start byte - do not change
buff1[1]=0x00; //Device type byte
buff1[2]=0x01; //Motor number and direction byte; motor one =00,01buff1[3]=0x45; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff1, BYTE);}
//right motor
unsigned char buff2[6];
buff2[0]=0x80; //start byte - do not change
buff2[1]=0x00; //Device type byte
buff2[2]=0x03; //Motor number and direction byte; motor two=02,03
buff2[3]=0x45; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff2, BYTE);}
}
//subroutine reverse at half speed
void motorreverse()
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80; //start byte - do not change
buff3[1]=0x00; //Device type byte
buff3[2]=0x00; //Motor number and direction byte; motor one =00,01
buff3[3]=0x35; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff3, BYTE);}
//right motor
unsigned char buff4[6];
buff4[0]=0x80; //start byte - do not change
buff4[1]=0x00; //Device type byte
buff4[2]=0x02; //Motor number and direction byte; motor two=02,03
buff4[3]=0x35; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff4, BYTE);}
}
//Motor all stop
void motorstop()
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80; //start byte - do not change
buff3[1]=0x00; //Device type byte
buff3[2]=0x00; //Motor number and direction byte; motor one =00,01
buff3[3]=0x00; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff3, BYTE);}
//right motor
unsigned char buff4[6];
buff4[0]=0x80; //start byte - do not changebuff4[1]=0x00; //Device type byte
buff4[2]=0x02; //Motor number and direction byte; motor two=02,03
buff4[3]=0x00; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff4, BYTE);}
}
void rotateccw()
{
//left motor
unsigned char buff1[6];
buff1[0]=0x80; //start byte - do not change
buff1[1]=0x00; //Device type byte
buff1[2]=0x01; //Motor number and direction byte; motor one =00,01
buff1[3]=0x40; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff1, BYTE);}
//right motor
unsigned char buff2[6];
buff2[0]=0x80; //start byte - do not change
buff2[1]=0x00; //Device type byte
buff2[2]=0x02; //Motor number and direction byte; motor two=02,03
buff2[3]=0x40; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff2, BYTE);}
}


Now my question is why it won't work. The robot is meant to be autonomous and when the IR sensor encounters an obstacle it is meant to turn around and find a clear path.

Any help is appreciated , I have sat for hours on end withpout being able to make it work.

Cheers / Daniel

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Quote
Now my question is why it won't work. The robot is meant to be autonomous and when the IR sensor encounters an obstacle it is meant to turn around and find a clear path.
So ummmm . . . you forgot to tell us what it's doing instead of what you want it to do.

Did you verify everything with a multimeter, like battery voltage, PWM frequency, etc?

Did you break your code down into parts, testing each individually?

 


Get Your Ad Here

data_list