PDA

View Full Version : source code for robot



snk_naveen
06-22-2009, 07:50 PM
hi all,
i am trying to build a obstacle avoiding robot following the instructions given in society of robots , i have finished the circuit board and played with some flash led programs . the problem is the guy has used servos for his robot but its too costly for me , hence i built a motor driver and chasis for my gear motors . now iam stuck with the programing part i dont know much about writing codes so if anyone has built a robot of similar configuration please share your code or post some links for the same !!! i searched net for a while but no use....

here's that link

http://www.societyofrobots.com/step_by_step_robot_step3B.shtml

everything exept the servos are the same !! i anyone knows of a better design please give your suggestions

karthikprasad
06-29-2009, 07:43 AM
can u specify the language u will be using

snk_naveen
06-29-2009, 07:06 PM
C

karthikprasad
06-29-2009, 07:11 PM
I know that I am kinda interfering but photoresistors kinda suck for
obstacle sensing,i will let u know about other sensors if u wanna know

And why not make this worthwhile for u too

U tell me what is the strategy u have in mind

I convert it into a program for u
Cheerio

snk_naveen
07-01-2009, 08:13 PM
ya your right photoresistors aren't that good . so i tried this

http://ir-led-photodiode.blogspot.com/

actually i was successful first but then for reason unknown to me it never worked again no matter how many times i try . can u suggest a better simpler sensor circuit....

i am not asking for spoon feeding , but i need a initial push into it !!!
hope u agree ??

karthikprasad
07-02-2009, 07:15 AM
First download express pcb (google it)

then download this pcb and try it out

it works 100 out of 100 times for me.It uses an Lm324 and a simple ir pair

http://rapidshare.com/files/250890420/amar_encoderbeta.pcb.html

google on for a tuto on how to make pcbs

snk_naveen
07-02-2009, 07:12 PM
OK i got that how about some source code for the same ....
i am using ATmega8

karthikprasad
07-02-2009, 08:22 PM
Like I said gimme ur strategy .I"ll code it for u

snk_naveen
07-03-2009, 06:04 PM
ok ,i dont know wat u mean by strategy but i'll tell wat i have in mind.

i want to build a obstacle avoider ,iam using a H bridge for motor control, obviously a differential drive .

And about the sensor circuit u posted ,i cant follow it clearly.i have some doubts like where is the 10k resistor connected ,what is connected to the three pins (vcc vout gnd) is it a TSOP?? ,and what is (m+ m-)
so if you happen to have a schematic of it would be useful .....
hope iam not troubling you !!!!

karthikprasad
07-04-2009, 08:06 AM
dude sorry for giving u the wrong circuit.that was for the motor encoder
but it should work fine since it is the same thing, anyway i am uploading the schematic of the ir sensor ie the adc version not the comparator version
http://img191.imageshack.us/img191/3804/irsensor.th.png (http://img191.imageshack.us/i/irsensor.png/)

If the image aint clear just download it and then zoom it

when i said strategy what i meant was ,suppose u had to bring a notebook from a room what would u do
=>Go to that room
=> Look for that notebook
=>Check if its the right one
=> and bring it back....
I know it was a lame example but u get the point
Cheerio
KP

ashish_agarwal123456
07-04-2009, 09:04 PM
guys, first of all sorry for not bieng so active these days..... guess all seniors were right... once in job, hobbies die away......

now coming to point

@naveen

actually i was successful first but then for reason unknown to me it never worked again no matter how many times i try
did you saw the caution tag under the circuit design?
i made that circuit to test ir-led pair for long distance.... the circuit is actually torturing the ir-led by giving it 5v without protection.... use a resistance in series.... it will decrease the range but will save the led.

snk_naveen
07-05-2009, 08:10 PM
@karthikprasad
i just wanted to modify the provided souce code in society of robots to run on H-Bridge.
so i would appreciate it if you or anyone else could to do just that .
i am not lazy , i tried it myself but i dont know the complexities involved .

here is that code.......

//AVR includes
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support

//AVRlib includes
#include "global.h" // include global settings
//#include "buffer.h" // include buffer function library
//#include "uart.h" // include uart function library
//#include "rprintf.h" // include printf function library
//#include "timer.h" // include timer function library (timing, PWM, etc)
#include "a2d.h" // include A/D converter function library

//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )


//************CONFIGURE PORTS************
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
{
DDRC = 0x00; //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF; //configure all D ports for output
DDRB = 0xC7; //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
}
//***************************************

//************DELAY FUNCTIONS************
//wait for X amount of cycles (23 cycles is about .992 milliseconds)
//to calculate: 23/.992*(time in milliseconds) = number of cycles
void delay_cycles(unsigned long int cycles)
{
while(cycles > 0)
cycles--;
}
//***************************************

//*********SIMPLIFIED FUNCTIONS**********
//functions to make coding easier for a beginner
//but could cause port mixup confusion for intermediate users
void LED_on(void)
{
PORT_OFF(PORTD, 4);//turn LED on
}
void LED_off(void)
{
PORT_ON(PORTD, 4);//turn LED off
}
void servo_left(signed long int speed)
{
PORT_ON(PORTD, 0);
delay_cycles(speed);
PORT_OFF(PORTD, 0);//keep off
delay_cycles(200);
}
void servo_right(signed long int speed)
{
PORT_ON(PORTD, 1);
delay_cycles(speed);
PORT_OFF(PORTD, 1);//keep off
delay_cycles(200);
}

karthikprasad
07-06-2009, 07:18 AM
/* OK WELL ITS QUITE EASY
first tell me whether u want to use pwm or not

snk_naveen
07-06-2009, 06:04 PM
for now i just want see my bot do its thing,so no need for pwm now. but in future i have to use it !!!

karthikprasad
07-06-2009, 11:41 PM
//PREPARE TO BE SCARED NO JUST KIDDIN BUT
//THIS CODE DOES WORK FOR ME ON PROTEUS
//DIDNT HAVE THE TIME TO TEST IT OTHERWISE
//IF U DONT HAVE CODEVISION AVR I WILL SEND THE HEX FILE
//I WASNT SURE ABOUT THE SHIFT OPERATORS
//WHEN I WAS WRITING DIS BECUZ I WAS FEELING SLEEPY
//SO I USED '.' OPERATOR FOR PORT ACCESS
//EDIT U can use instead Of PORTA.3=1 =>PORTA|=(1<<3);
// PORTA.3=0 => PORTA&=~(1<<3);


// PS I TOOK HELP OF THE CODEVISION PROGRAM GENERATOR
// U MIGHT FIND IT A BIT CONFUSING BUT I AM USING A LOOKUPTABLE BECUZ UR CLOCK SPEED MAY BE LOW ASSUMING
//THAT U HAVENT SET THE FUSEBITS

// "chicken-The cowardly obstacle avoider"
// His speciality=> He isnt intimidated only "if" someone tells him,he gets scared intuitively poor guy
// His expertise=> Though he is primarily a coward,He also can track lines very well if
//u know what I mean

// STARRING darpokSingh as "Chicken"
//PRODUCED AND EDITED BY Team LESINVINCIBLES




#include <mega8.h>

#include <delay.h>

#define ADC_VREF_TYPE 0x60

#define USERWAIT 8000 //TIME IN MILLISECONDS TO MAKE SURE USER GETS EVERYTHING RIGHT
#define TIMEOUT 2000

/**********LED DEFINITIONS USE TWO LEDS FOR VISUAL IDENTIFICATION************* */
//PIN NOS

//CONNECTIONS => pin LED1 - to led1+ led- to 220 ohms to ground
//pin LED2 - to led2+ led- to 220 ohms to ground


#define LED1 4 //LEFT SENSOR KA JUDWA BHAI
#define LED2 5 //RIGHT SENSOR KA JUDWA BHAI

#define LED1ON() PORTB.LED1=1;PORTB.LED2=0 //PORTB=(1<<LED1)||(1<<!(LED2)) OR SIMPLY PORTB=(1<<LED1)
#define LED2ON() PORTB.LED2=1;PORTB.LED1=0
#define BOTHLEDSON() PORTB.LED1=1;PORTB.LED2=1
#define BOTHLEDSOFF() PORTB.LED1=0;PORTB.LED2=0

/************************************************** ************************************************** ***************************/
//ADC PINS
#define leftSensor 0
#define rightSensor 1
unsigned int leftSensorOffset;
unsigned int rightSensorOffset;
unsigned int leftSensorThreshold;
unsigned int rightSensorThreshold;
unsigned int SensorThreshold;

//CHANGE THIS TO SUIT UR SENSOR => GREATER,LOWER IN TERMS OF VOLTAGES THAT IS
#define GreaterMeansObstacle 1
//#define GreaterMeansObstacle 0

int sensorType=GreaterMeansObstacle;

/************************************************** ************************************************** ***************************/

#define LeftMotorPositive 0 //PIN NOS OF PORTB USED FOR MOTORS
#define LeftMotorNegative 1
#define RightMotorPositive 2
#define RightMotorNegative 3

/************************************************** ************************************************** ***************************/
// DEFINITIONS FOR MOTOR CONTROL
#define DELAYTIME 250 //WAIT TIME VARIES FROM MOTOR TO MOTOR CHECK IT OUT USING TRIAL AND ERROR

#define LeftMotorForward() PORTB.LeftMotorPositive=1;PORTB.LeftMotorNegative= 0 // PORTB=(1<<LeftMotorPositive) //||(1<<!(LeftMotorNegative))
#define LeftMotorReverse() PORTB.LeftMotorPositive=0;PORTB.LeftMotorNegative= 1 //PORTB=(1<<!(LeftMotorPositive)) //||(1<<(LeftMotorNegative))
#define RightMotorForward() PORTB.RightMotorPositive=1;PORTB.RightMotorNegativ e=0 //(1<<RightMotorPositive) //||(1<<!(RightMotorNegative))
#define RightMotorReverse() PORTB.RightMotorPositive=0;PORTB.RightMotorNegativ e=1 //PORTB=(1<<(RightMotorNegative)) //||(1<<!(RightMotorPositive))
#define StopBot() PORTB.LeftMotorPositive=0;PORTB.LeftMotorNegative= 0;PORTB.RightMotorPositive=0;PORTB.RightMotorNegat ive=0

/************************************************** ************************************************** ***************************/
// FUNCTIONS FOR MOTOR CONTROL

void MOVELEFT()
{

RightMotorForward();
LeftMotorReverse();
}

void MOVERIGHT()
{

LeftMotorForward();
RightMotorReverse();
}

void MOVESTRAIGHT()
{

LeftMotorForward();
RightMotorForward();
}

void MOVEBACK()
{

LeftMotorReverse();
RightMotorReverse();
}


/************************************************** ************************************************** ***************************/

// CONSTANTS FOR SIMPLIFYING CONDITIONS FOR OBSTACLE DETECTION


#define LeftObstacle 1


#define RightObstacle 2


#define NoObstacle 3


#define StraightObstacle 4

#define length 256 //8 BITS ADC PRECISION
//I KNOW ITS OVERKILL BUT TRUST ME ITS WORTH CODING SUCH A MEMORY SUCKER WHEN IT ACTUALLY
// MAKES ITS OWN DECISIONS RATHER THAN DEPEND ON PATHETIC IF DIS DO THAT ELSE IF DO THAT ELSE CRAP

int obstacleInfo[length];
/************************************************** ************************************************** ***************************/

void fillObstacleInfo(int LowerThreshold,int leftHigherThreshold,int rightHigherThreshold)
{ int temp1,dummy;
int i;
int diff=leftHigherThreshold-rightHigherThreshold;
if(diff>0)
{
temp1=length-diff;


//diff remains the same
}
if(diff<=0)
{
temp1=-diff;
diff=length+diff;

//SWAP VALUES
dummy=temp1;
temp1=diff;
diff=dummy;


}
// SEE THE REASON FOR DOING THIS IS UR SENSOR MAY BE PHOTOPHOBIC OR A PHOTOVORE






if(sensorType==GreaterMeansObstacle)
{

for(i=0;i<length;i++)
{
obstacleInfo[i]=NoObstacle;

}
for( i=LowerThreshold;i<diff;i++)
{
obstacleInfo[i]=LeftObstacle;
}

for(i=diff;i<=temp1;i++)
{

obstacleInfo[i]= StraightObstacle;

}
for(i=temp1;i<length;i++)
{


obstacleInfo[i]=RightObstacle;
}

}

if(sensorType!=GreaterMeansObstacle)
{

for(i=0;i<length;i++)
{
obstacleInfo[i]=StraightObstacle;

}
for( i=LowerThreshold;i<diff;i++)
{
obstacleInfo[i]=RightObstacle;
}

for(i=diff;i<=temp1;i++)
{

obstacleInfo[i]= NoObstacle;

}
for(i=temp1;i<length;i++)
{


obstacleInfo[i]=LeftObstacle;
}

}



}



int returnObstacleInfo(unsigned int sensorDiff)
{

return (sensorDiff>=0?obstacleInfo[sensorDiff]:obstacleInfo[(length+sensorDiff)]);

}




















// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
















// Declare your global variables here

// Declare your global variables here

void main(void)
{
int j;


/************************************************** ***********************************/

//PREREQUISITES

// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 250.000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x82;

/************************************************** ************************************************** ***************************/

//WAIT FOR EIGHT SECONDS TO ALLOW USER TO MAKE SURE HE GETS EVERYTHING RIGHT
BOTHLEDSON();
delay_ms(USERWAIT);
BOTHLEDSOFF();

delay_ms(TIMEOUT);


BOTHLEDSON();

//INITIALLY READ NOISY ADC VALUES AND STORE THEM
// U CAN ALSO USE IT TO DEFINE THE MIN DISTANCE FOR TRACKING

leftSensorOffset=read_adc(leftSensor) ;
rightSensorOffset=read_adc(rightSensor) ;
delay_ms(USERWAIT);

BOTHLEDSOFF();

delay_ms(TIMEOUT);

//NOW PUT AN OBSTACLE IN FRONT OF THE LEFT SENSOR at or less than equal to REQUIRED MAX DISTANCE
leftSensorThreshold=read_adc(leftSensor) ;
LED1ON();
delay_ms(USERWAIT);
BOTHLEDSOFF();


//NOW PUT AN OBSTACLE IN FRONT OF THE RIGHT SENSOR at or less than equal to REQUIRED MAX DISTANCE
rightSensorThreshold=read_adc(rightSensor) ;
LED2ON();
delay_ms(USERWAIT);
BOTHLEDSOFF();

SensorThreshold=leftSensorOffset-rightSensorOffset;

fillObstacleInfo(SensorThreshold,leftSensorThresho ld,rightSensorThreshold);

//FLASH LEDS RAPIDLY TO SIGNIFY THAT BOT IS READY TO DO OBSTACLE DETECTION

for(j=0;j<200;j++)
{
BOTHLEDSON();
delay_ms(500);
BOTHLEDSOFF();
delay_us(500);
}

delay_ms(TIMEOUT);

while (1) // A VERY PRIMITIVE STATE MACHINE IS USED
{
switch(returnObstacleInfo(read_adc(leftSensor)-read_adc(rightSensor)))

{
case LeftObstacle:
MOVERIGHT();
LED1ON();
delay_ms(DELAYTIME);
break;
case RightObstacle:
MOVELEFT();
LED2ON();
delay_ms(DELAYTIME);
break;
case StraightObstacle:
MOVEBACK();
BOTHLEDSON();
delay_ms(DELAYTIME);
break;
case NoObstacle:
MOVESTRAIGHT();
BOTHLEDSOFF();
delay_ms(DELAYTIME);
break;

}

delay_us(500); //DELAY BETWEEN CYCLES TO AVOID SPAGHETTI CODING

}
}

snk_naveen
07-17-2009, 05:10 PM
thank you for your help /but i work in avr studio .i have code vision avr but i dont know how to work in it . the code is not compiling in avr studio or code vision avr

karthikprasad
07-28-2009, 07:10 PM
Dude really really sorry i thought that the post had only one page so didnt reply

Like i said,
tell me ur hardware configuration

and i will attach the hex file which u can burn directly]

and gimme ur intro

there is a reason for my asking that

avinash_g
04-28-2010, 09:11 AM
Hello,

You may also try this Obstacle Avoiding Robot (http://extremeelectronics.co.in/robotics/obstacle-avoiding-robot-using-avr-atmega32-part-i/) its build from parts that are easily available in India.

http://extremeelectronics.co.in/robotics/obstacle-avoiding-robot-using-avr-atmega32-part-i/

akshay66
05-01-2010, 12:40 PM
Great Tutorial......really Nice.

abhi006
03-02-2013, 09:44 PM
sir can u provide me the full ckt diagram of obstacle avoidence robot using 8051 and also provide me the layout design using express pcb.