programming your ibot

10
TAG ROBOTICS www.tagonnet.com Programming your iBot 1. Program to blink an LED Since this is our first program, we will try to keep things simple. The following program will blink an LED on the board at a particular rate. A generic algorithm for such a program will be something like this: Step 1: Turn ON the LED Step 2: Wait for sometime Step 3: Turn OFF the LED Step 4: Wait for sometime Step 5: Goto Step1 To turn ON an LED on the iBOT board, we need to send logic 0 to that particular pin (since LEDs are connected in the active low configuration). In order to introduce a delay, we need to eat up some processor time. The simplest way to achieve this is to perform a NOP (No Operation) multiple times. And continuous looping can be achieved by either using the statement “goto” or more appropriately by using the while() loop. Now let’s see how we can put all of these things together in a C program. // Program to blink an LED // #include<REG52MOD.h> //we include the necessary header file here void delay(unsigned int delay) //This a simple delay function using the //nested ‘for loop’ { unsigned int i,j; for(i=0;i<=1000;i++) for(j=0;j<=delay;j++); } void main(void) //main program begins here { while (1) //since there is no where to return //we put it in an infinite loop { RXD=0; //LED 1 is on pin RXD at PORT 3_1, we //turn it ON delay (20); //wait for a short time RXD=1; //turn the LED 1 OFF delay(20); //wait for a short time } } We begin with including the header file for the microcontroller. Next we write the delay() function using the nested for loop. Don’t worry about the exact time of delay over here, you can experiment with different values and see what difference it makes. For example, replacing delay(20) with delay(60) will reduce the frequency while delay(10) would increase the frequency. You could also try and use all the LEDs and generate different patterns. Note: The LEDs are connected in an active-low configuration. Therefore, to turn ON an LED, we

Upload: sai-srikar

Post on 09-Apr-2015

119 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

Programming your iBot

1. Program to blink an LED Since this is our first program, we will try to keep things simple. The following program will

blink an

LED on the board at a particular rate.

A generic algorithm for such a program will be something like this:

Step 1: Turn ON the LED

Step 2: Wait for sometime

Step 3: Turn OFF the LED

Step 4: Wait for sometime

Step 5: Goto Step1

To turn ON an LED on the iBOT board, we need to send logic 0 to that particular pin (since

LEDs are

connected in the active low configuration).

In order to introduce a delay, we need to eat up some processor time. The simplest way to achieve

this

is to perform a NOP (No Operation) multiple times. And continuous looping can be achieved by

either using the statement “goto” or more appropriately by using the while() loop.

Now let’s see how we can put all of these things together in a C program. // Program to blink an LED // #include<REG52MOD.h> //we include the necessary header file here void delay(unsigned int delay) //This a simple delay function using the //nested ‘for loop’ { unsigned int i,j; for(i=0;i<=1000;i++) for(j=0;j<=delay;j++); } void main(void) //main program begins here { while (1) //since there is no where to return //we put it in an infinite loop { RXD=0; //LED 1 is on pin RXD at PORT 3_1, we //turn it ON delay (20); //wait for a short time RXD=1; //turn the LED 1 OFF delay(20); //wait for a short time } }

We begin with including the header file for the microcontroller. Next we write the delay()

function

using the nested for loop. Don’t worry about the exact time of delay over here, you can

experiment

with different values and see what difference it makes. For example, replacing delay(20) with

delay(60) will reduce the frequency while delay(10) would increase the frequency. You could

also try

and use all the LEDs and generate different patterns.

Note: The LEDs are connected in an active-low configuration. Therefore, to turn ON an LED, we

Page 2: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

need to send logic 0 and not logic 1.

2. Program using Switches and LEDs Now that we know how to turn on/off a particular pin of the microcontroller, as seen in the

previous

example, let’s see how we can take inputs from a particular pin and execute actions based on its

state.

In this example, we will be using switches as our inputs and LEDs as the outputs. The simplest

way to

know the state (is it high or low?) of a particular pin is to poll it continuously.

The algorithm would go something like this:

Step 1: Check if Switch 1 is pressed; if pressed, turn ON LED 1, else, turn it OFF.

Step 2: Check if Switch 2 is pressed; if pressed, turn ON LED 2, else, turn it OFF.

.

.

.

Step n: goto step 1.

Lets see how a C program based on this algorithm would look like: // Program using Switches and LEDs // #include<REG52MOD.h> //we include the necessary header file here void main(void) //main program begins here { P3=0x3C; //initialize PORT 3 to 00111100 = 0x3C while (1) //since there is no where to return, //we put it in an infinite loop { if (P3_2==0) //check if Switch 1 is pressed { P3_0=0; //if pressed, turn ON LED 1 } else P3_0=1; //else turn it OFF if (P3_3==0) //check if Switch 2 is pressed { P3_1=0; //if pressed, turn ON LED 2 } else P3_1=1; //else turn it OFF if (P3_4==0) //check if Switch 3 is pressed { P3_6=0; //if pressed, turn ON LED 3 } else P3_6=1; //else turn it OFF if (P3_5==0) //check if Switch 4 is pressed { P3_7=0; //if pressed, turn ON LED 4 } else P3_7=1; //else turn it OFF } }

You will notice that we have written P3=0x3C at the very beginning of our program. This is done

to

set PORT 3’s pin 2, 3, 4 and 5 as inputs since we have connected our switches to them while pin

0, 1,

6 and 7 are the outputs where LEDs are connected. (1 � Input, 0 � Output)

Page 3: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

Note: The switches are connected in an active-low configuration. Therefore, whenever a switch is

pressed, we will receive logic 0 on that particular pin and not logic 1.

3. Program using a Sensor and a DC Motor Let’s try and make our next program more animated. In this example, we will control the

direction of

the motor based on the state of a sensor.

Here’s the algorithm:

Step 1: Initialize the ports

Step 2: Check the state of the sensor; if active, turn motor in one direction, else, turn it in other

direction.

Step 3: goto step 2

One IR proximity sensor is connected to PORT 1’s pin 0 and one motor is connected to M1 (pin 0

and

1 of PORT 2) // Program using Sensor and DC Motor // #include<REG52MOD.h> //we include the necessary header file here void main(void) //main program begins here { P1=0xff; //initialize PORT 1 as input P2=0x00; //initialize PORT 2 as output while (1) //since there is no where to return, //we put it in an infinite loop { if (P1_0==0) //check if sensor at PORT1.0 is active { //0 active, 1 inactive P2=0x01; //if active, turn motor in one //direction (0000 0001) } else P2=0x02; //else turn it in other direction (0000 0010) } } Similarly, you can write a code to turn both the motors and use both sensors together.

4. Program using the LCD module LCD is one of the most multipurpose module a robot can have. It can be used to display

information,

check various states of the robot, see the parameters as you feed them and also as a handy

debugging

tool. And the ‘cool’ factor cannot be understated aswell. iBOT uses a 2x16 backlight display that

is

compatible with the universally accepted Hitachi format. LCDs are not as easy to use as the LEDs

but

they are not very difficult either.

To make things simpler for you, we have written a small library for all the LCD functions. All

you

need to do is to add the LCD.h and delay.h header files in the C:\Keil\C51\INC folder before you

use

it.

Page 4: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

Let’s look at the LCD functions that we have at our disposal:

• LCD_INIT()

Initializes the LCD in 2 line 4 bit mode with blinking cursor.

• LCD_WRITE(‘T’)

Used to write any character T to the LCD.

• LCD_STRING(“Xplore Robotics”)

Used to write a string on the LCD (max 16 characters in length)

• LCD_CMD(X)

To give commands to the LCD. X can be as follows

PUTLINE1 – Places cursor on line1

PUTLINE2 - Places cursor on line2

To place the cursor on a particular character box it is addressed as follows:

Command = LCD_CMD(0b1XXX XXXX);

where xxx xxxx is the address of character in binary.

Note: LCD row 1 address starts at 0x00 and row 2 starts at 0x40 but with the MSB set, the

addresses

are 0x80 and 0xC0 respectively.

Eg: To put cursor on 3 character line 1

LCD_CMD(0x83);

To put cursor on 5 character line 2

LCD_CMD(0xC5);

• LCD_CLEAR()

Clears LCD screen.

Here’s a sample program for using the LCD module with the help of the above functions: // Program using the LCD module// #include<REG52MOD.h> //Include the necessary header files #include<delay.h> #include<LCD.h> void main (void) { LCD_INIT(); //Initialize the LCD module LCD_STRING("TRI"); //Send a string to display LCD_CMD(PUTLINE2); //Move the cursor to second line LCD_STRING("HELLO WORLD !!"); //Send a string to display while(1){} //Loop Infinitely }

5. Program to communicate with the PC via

UART In this section we will learn how to make your iBOT communicate with the PC through the

UART

channel. In order to do this, first refer to the datasheet of 89V51RD2 microcontroller and have a

look

at the UART section and the SCON (serial port control) register configurations.

In the following program, the microcontroller will send a “start” string to the PC at the beginning

of

the execution and simultaneously accept data coming from the PC (anything that you type on the

keyboard) and display in on to the onboard LCD module. //Program using to communicate with the PC via UART// #include<REG52MOD.h> #include<delay.h>

Page 5: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

#include<LCD.h> unsigned char RXED,TXED; void UART_INIT(void) //UART Initialization { SBUF=0x00; //Empty Serial Buffer SM0=0; //Set UART SM1=1; //in mode 1 SM2=0; REN=1; //Receive Enable TMOD=0x2F; //Set baud TH1=253; //rate as 9600bps TR1=1; ES=1; //Enable serial interrupts EA=1; //Global interrupt enable } void TX(unsigned char dat) //Transmit Data { SBUF=dat; delay(10); } void RXTX_int(void) interrupt 4 //Serial interrupt ISR { if(RI==1) { RI=0; //Reset receive data bit RXED=SBUF; LCD_WRITE(RXED); //Display received character on LCD } if(TI==1) { TI=0; //Reset transmit data bit } } void main(void) //Main program begins here { LCD_INIT(); //Initialize LCD UART_INIT(); //Initialize UART delay(1); //wait for a while

TX('S'); //Transmit S TX('t'); //Transmit t TX('a'); //Transmit a TX('r'); //Transmit r TX('t'); //Transmit t while(1){} }

After loading the program on to your iBOT controller, open the Hyper Terminal on your PC.

You can find it in Start > Programs > Accessories > Communications > Hyper Terminal Step 1: Enter a name for the connection

Step 2: Select the COM port and configure its properties

6. Line Following Robot Time to actually build a robot! Let’s put everything that we learned till now in our next program.

Line

following is one of the simplest task a robot can perform. There are many algorithms and sensor

Page 6: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

configurations designed to do this.

Here’s one of the simplest and fail safe line follower’s algorithm:

In this configuration we use two line sensors.

YES

YES

YES

NO

YES

NO

NO

NO

START

Initialize

Ports

Is left line

sensor

active?

Is right line

sensor

active?

Turn right

Turn Left

Is left line

sensor

active?

Go straight

Is right line

sensor

active?

Basically, the robot will always try to get the sensors on the line alternately, thus moving in a

‘zigzag’

path and eventually follow the line. The following code is written to follow a ‘black’ line on a

‘white’ surface but it can be easily modified to follow a ‘white’ line on a black surface. // LINE FOLLOWING ROBOT // //Right line sensor connected to PORT1.0 //Left line sensor connected to PORT1.1 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD.h> //we include the necessary header file here #define forward 0x05; // 0000 01 01 #define right 0x01; // 0000 00 01 #define left 0x04; // 0000 01 00 #define linesensor_right P1_0 #define linesensor_left P1_1 void main(void) //main program begins here { P1=0xff; //initialize PORT 1 as input (sensors) P2=0x00; //initialize PORT 2 as output (motors) while (1) //since there is no where to return,

Page 7: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

//we put it in an infinite loop { if (linesensor_right==0) //check if right sensor has detected // a line (black surface) { while(linesensor_left==1) //if detected, then turn right till { //the left sensor comes on the line P2=right; } } if (linesensor_left==0) // check if left sensor has detected // a line { while(linesensor_right==1) //if detected, then turn left till

11 { //the right sensor comes on the line P2=left; } } else { P2=forward; //else go forward } } }

7. Obstacle Avoiding robot Obstacle avoiding behavior is a prerequisite in any mobile robotic application. In this project we

will

learn to add such a behavior in our iBOT.

Here’s a simple algorithm:

Step 1: check if right ir proximity sensor is active; if active, turn left for a while

Step 2: check if left ir proximity sensor is active; if active, turn right for a while

Step 3: else move forward

Step 4: goto step 1 // OBSTACLE AVOIDING ROBOT // //Right proximity sensor connected to PORT1.2 //Left proximity sensor connected to PORT1.3 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD.h> //we include the necessary header file here #include<delay.h> #define forward 0x05; // 0000 01 01 #define turnleft 0x06; // 0000 01 10 left motor = backwards, // right motor = forward #define turnright 0x09; // 0000 10 01 left motor = forward, // right motor = backwards #define obst_right P1_2 #define obst_left P1_3 void main(void) //main program begins here { P1=0xff; //initialize PORT 1 as input (sensors) P2=0x00; //initialize PORT 2 as output (motors)

Page 8: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

while (1) //since there is no where to return, //we put it in an infinite loop { if (obst_right==0) //check if right sensor has detected an obstacle { P2=turnleft; //if detected, turn left for some time delay(30); } if (obst_left==0) //check if left sensor has detected an obstacle { P2=turnright; //if detected, turn right for some time delay(30); } else { P2=forward; //else go forward } } }

By changing the delay variable we can make the robot turn for some specific degrees after it

detects

an obstacle. But the turns will not be precise since they will change as the battery drains.

Note: In this particular case, the turning would be ‘in place’ i.e., while turning, both the motors

will

run in opposite direction unlike the line follower, where the turns were turning by ‘stopping’

either of

the motor.

8. Sumo Robot In the previous two examples we learned how to

use the line sensing modules as well as the IR

proximity modules. Now let’s see how we can

have a behavior that uses both of these sensors

together. A sumo robot fits this bill perfectly.

Inspired from the tradition ‘human’ sumo

competitions held in Japan, robotics enthusiasts

soon started having robotics sumo. Sumo

robotics competitions are now held around the

world under various weight and size ‘classes’

just like the real ones.

In a typical sumo robotics competition, two

robots compete against each other inside a sumo

ring. The ring is a made up of a black surface

with a white line around its circumference as

shown. The idea is to push the opponent out of

the ring in the stipulated time frame.

A typical sumo robot flowchart: NO

Start

Initialize ports

Is the opponent

Page 9: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

on the left and

me not on the

edge?

Is the right

line sensor

active?

Is the opponent

on the right and

me on the edge?

Is the opponent

in front and me

not on the edge?

Is the right

line sensor

active?

Turn left

Turn right

Go forward

Turn backwards

then turn left

Turn backwards

then turn right

Go forward

NO

NO

NO

NO

YES

YES

YES

YES

16 // SUMO ROBOT // //Right line sensor connected to PORT1.0 //Left line sensor connected to PORT1.1 //Right proximity sensor connected to PORT1.2 //Left proximity sensor connected to PORT1.3 //Right motor connected at M2 //Left motor connected at M1 #include<REG52MOD.h> //we include the necessary header file here #include<delay.h> #define forward 0x05 // 0000 01 01 #define reverse 0x0A // 0000 10 10 #define turnleft 0x06 // 0000 01 10 left motor = backwards, // right motor = forward #define turnright 0x09 // 0000 10 01 left motor = forward, // right motor = backwards #define linesensor_right P1_0 #define linesensor_left P1_1 #define obst_right P1_2 #define obst_left P1_3 void main(void) //main program begins here { P1=0xff; //initialize PORT 1 as input (sensors) P2=0x00; //initialize PORT 2 as output (motors) while (1) //since there is no where to return,

Page 10: Programming Your iBot

TAG ROBOTICS

www.tagonnet.com

//we put it in an infinite loop { while ( obst_right==0 //check if right sensor has && obst_left==1 //detected the opponent && linesensor_right==1 //and make sure that the robot is && linesensor_left==1) //not on the edge { P2=turnright; //if detected, turn right } //(i.e., towards the opponent) while ( obst_left==0 //check if left sensor has && obst_right==1 //detected the opponent && linesensor_right==1 //and make sure that the robot is && linesensor_left==1) //not on the edge { P2=turnleft; //if detected, turn left } //(i.e., towards the opponent) while ( obst_left==0 //check if both sensors && obst_right==0 //have detected the opponent && linesensor_right==1 && linesensor_left==1) { P2=forward; //if detected, go forward } //chase him and push him

17 if (linesensor_right==0 ) //check if right line sensor has // detected the ring { P2=reverse; //if detected, go reverse for a while delay(40); P2=turnleft; //then turn left for a while delay(30); P2=forward; //and go forward } if (linesensor_left==0 ) //check if left line sensor has //detected the ring { P2=reverse; //if detected, go reverse for a while delay(40); P2=turnright; //then turn right for a while delay(30); P2=forward; //and go forward } else { P2=forward; //else go forward } } }