Upgrading your i-MACH into A BULL DOZER

December 26th, 2009

Work done by-Krishna Kumar Rao, Std.X
How cool would it be if u could upgrade your i-MACH by adding basic mechano parts and some brains into complex robots! Here i would be teaching (or rather guiding) you to build one yourself.
Bull Dozer
Now, let’s take a step forward and build an autonomous Bull Dozer! Let me tell you this could take some time……….so BE PREPARED!
Well, what all features would you want to be included in your Bull Dozer? If they are these, then you are at the right place.
• Could sense the object.
• Pick it up on its own.

• Throw it into the dustbin!(or any other place u want to).
Building one would require a few extra parts which are not included in the
i-MACH kit. The list of such parts is given below-
• 2 TSOP Obstacle Sensors
• 1 DC Geared Motor(500mA,6v)
• Few mechano parts or any kind of aluminum strips

Keep in mind that we are building a bot for white colored arena bordered by a black line.
We would be using 4 obstacle sensors and 1 line sensor for building our bot.

Here are the sensor placements I have chosen.

14.JPG


The 2 sensors at the front (sensors 4 & 5) can also be replaced by one sensor but in such a case detecting the dustbin would be slightly difficult.

25.JPG


With this, you could very easily lift things and throw it into the dustbins.
I have added a long but robust framework to my bot built with mechano parts. Kids might find it difficult 2 build it, but after all, it’s a necessity.
Note that the length of “x” if quite large as the bucket has to come back to drop the object too. Also, I have used gears to increase the torque of the motor. Even after adding gears, if the torque isn’t enough, you could add counter weights.

31.JPG

This is what my final bull looked like:

41.JPG

ALGORITHM:
So, any idea what the algorithm would look like?
If this is what you guessed you are surely a genius!
• If sensor1 is on, go straight till sensor4 detects object.
• If sensor1 is off, come back and turn.
• If sensor 2 is detecting, come back and turn right.
• If sensor3 is detecting, come back and turn left.
• If sensor4 is detecting, pick the object.

• If sensor5 is detecting turn right slightly.

Note- All Obstacle Sensors bought from TRI are active low sensors, so “detecting” would mean the sensor is OFF.

PROGRAMCODE:

BEGIN
{
FOREVER
{
IF(SENSOR1 IS OFF) // If boundary is detectd then

{ go back and turn.
RIGHTMOTOR(BACKWARD,100);
LEFTMOTOR(BACKWARD,100);
WAIT(1 SEC);
RIGHTMOTOR(FORWARD,80); //lower RPM used to avoid
Skidding.
LEFTMOTOR(BACKWARD,80);
WAIT(1 SEC);
}

ELSE // Keep moving forward
{
RIGHTMOTOR(FORWARD,100);
LEFTMOTOR(FORWARD,100);
}
IF(SENSOR4 IS OFF) // If object is in front.
{
WAIT (700); //continue to go forward for 700
milliseconds so that the shovel

reaches the object.
CENTREMOTOR (FORWARD,70); // pick up the object
WAIT(1 SEC);
CENTREMOTOR (0,0);
FOREVER
{
IF(SENSOR1 IS OFF) // Check if you have detected the
boundary.
{

RIGHTMOTOR(BACKWARD,100); //Come back and turn to
LEFTMOTOR(BACKWARD,100); stay in the arena.
WAIT(1 SEC);

RIGHTMOTOR(FORWARD,80); //lower RPM used to avoid
Skidding.
LEFTMOTOR(BACKWARD,80);
WAIT(1 SEC);
}
ELSE // keep going straight.

{
RIGHTMOTOR(FORWARD,100);
LEFTMOTOR(FORWARD,100);
}

IF(SENSOR5 IS OFF) //dustbin is in front.
{
WAIT(450); //continue going forward so that shovel is near the bin.
RIGHTMOTOR(0,0);
LEFTMOTOR(0,0);

CENTREMOTOR(BACKWARD,50); // Lower down the
WAIT(1300); shovel to place object
CENTREMOTOR(0,0); in front of dustbin.
RIGHTMOTOR(BACKWARD,100); // go back.
LEFTMOTOR(BACKWARD,100);
WAIT(500);
RIGHTMOTOR(0,0);
LEFTMOTOR(0,0); CENTREMOTOR(FORWARD,50); // raise shovel.
WAIT(1 SEC);

CENTREMOTOR(0,0);
RIGHTMOTOR(FORWARD,100); // go forward to push
the object into the
bin.
LEFTMOTOR(FORWARD,100);
WAIT(1 SEC);
RIGHTMOTOR(BACKWARD,100); // come back.
LEFTMOTOR(BACKWARD,100);
WAIT(1 SEC);

FOREVER //After the task is done
{ stay in standstill position.
RIGHTMOTOR(0,0);
LEFTMOTOR(0,0);
}
}

IF(SENSOR2 IS OFF) // If the right obstacle sensor detects
the dustbin
{

RIGHTMOTOR(BACKWARD,100); // Come back.
LEFTMOTOR(BACKWARD,100);
WAIT(1000);
WHILE(SENSOR5 IS ON) // turn right till
sensor5 detects bin.
{
LEFTMOTOR(FORWARD,80); // to avoid skidding.
RIGHTMOTOR(BACKWARD,80);
} // once sensor5 detects

the object stop turning.

}
IF(SENSOR3 IS OFF) // If the left obstacle
sensor detects the
dustbin.
{
RIGHTMOTOR(BACKWARD,100); // Come back.
LEFTMOTOR(BACKWARD,100);
WAIT(1000);

WHILE(SENSOR5 IS ON) //turn left till front obstacle
sensor5 detects the bin.
{
LEFTMOTOR(BACKWARD,80); //lower RPM used to
RIGHTMOTOR(FORWARD,80); avoid skidding.
} // once sensor5 detects the bin
stop turning
}
}

} // forever loop closed
IF(SENSOR2 IS OFF) // If the right obstacle sensor detects the
object.
{
RIGHTMOTOR(BACKWARD,100); // Come back and turn right till front obstacle sensor5
detects the object.
LEFTMOTOR(BACKWARD,100);
WAIT(1000);
WHILE(SENSOR5 IS ON)

{
LEFTMOTOR(FORWARD,100);
RIGHTMOTOR(BACKWARD,100);
} // once front obstacle sensor5
detects object stop turning
}

IF(SENSOR3 IS OFF) // If the left obstacle sensor detects the object.
{
RIGHTMOTOR(BACKWARD,100); // Come back and turn till

LEFTMOTOR(BACKWARD,100); front obstacle SENSOR 5
WAIT(1000); detects the object.
WHILE(SENSOR5 IS ON)
{
LEFTMOTOR(BACKWARD,100);
RIGHTMOTOR(FORWARD,100);
} // once front obstacle sensor5 detects
object stop turning.
}

IF(SENSOR5 IS OFF) // If front obstacle sensor detects the object then turn
slightly to make SENSOR4 detect the object.
{
RIGHTMOTOR(BACKWARD,100);
LEFTMOTOR(FORWARD,100);
WAIT(34);
}
}
} END

To catch a glimpse of what your i-MACH DOZER is up to,check out this video.

http://www.youtube.com/watch?v=Rg1swZtBR_g

So go on and build one for yourself, and do remember 2 give it a cool, funky, and a raging look!
You may read my program once and try to understand the logic. If your program isn’t working you may refer the above one for debugging. THERE IS NOTHING AS GOOD AS TO SEE YOUR FIRST VERY OWN COMPLEX PROGRAM WORK. So please DO NOT blindly copy the above program.
Also, you could build a gripper with the same algorithm and program, only the gripping mechanism is to be added. Do remember to adjust the “wait” or “delay” command of the gripper motor in such a way that the gripper could hold the object easily.
So go on and try it out! Everything need not be copied, you could very much use your imagination and build cooler stuffs. We too are open for your suggestions, which could be posted in the forums.

MATLAB Introduction

December 26th, 2009

Hey!!!  Wanna give your ibot a sense to see and a feel to hear? Then the only feasible and flexible solution is MATLAB….

MATLAB!   
   It’s a power tool to work on with matrices and vectors (i.e. image and voice processing). It was originally designed to simplify the implementation of numerical linear algebra routines but with time it grew into something much bigger and is now used to implement numerical algorithms for a wide range of applications.

Programming in Matlab is very similar to standard C programming, but there are few extensions that will likely cause you some problems at first. The best way to learn what Matlab can do is to work through some examples at the computer.

So what do you say? 

Wanna feel the essence of MATLAB… scroll down through the slides…

This article is to make you comfortable with matlab, so that you can appreciate the concepts of image processing with ease in next article…

Step 1:
Open the Matlab and you can find some thing like this

001

It’s a default desktop layout and to the left you can find Current Directory, Work space, Command History.

Click on Current directory lists all the files in work folder and workspace lists all the variables used in the program when executed.

Hmm…  
     Here is a sample session with Matlab. Text in bold is what you type in command window and ordinary text is what the computer “types.”, so imitate this example at the computer.

% Matlab      

Remark: Something written after ‘%’ in a line will not be executed.

>> a = [ 1 2; 2 1 ] 

a =

     1     2

     2     1

>> a = [ 1 2; 2 1 ]; 

Remark: What’s the difference between above 2 instructions?

    Yeah, its like result will be displayed for the instruction without           semicolon and vice versa.

>> a*a

ans =

     5     4

     4     5

 

In this example you started Matlab by (you guessed it) typing Matlab. Then you defined matrix a and computed its square (“a times a”).

% if else syntax
if (you feel enough for the day)
disp (’type ‘quit’ in the command window’)

       else
disp (’go to next page’)
end

 

Wohh!! Its good, you’re not bored still…              

Soo let’s have hands on feel of Matrices in Matlab

  • Matrices:

 Let’s say you wanna enter the matrix

             1 2

            3 4

and store it in a variable a, do this:

       >> a = [ 1 2; 3 4 ];

 Do this now: work out each of them with matlab. Learn by doing!

To display the matrix, just type its name:

       >> a

Once you know how to enter and display matrices, it is easy to compute with them. First we will square the matrix a:

       >> a * a

 Wasn’t that easy? Now we’ll try something a little harder. First we define a matrix b:

       >> b = [ 1 2; 0 1 ]

Then we compute the product ab:

       >> a*b

Finally, we compute the product in the other order:

       >> b*a

 Notice that the two products are different: matrix multiplication is noncommutative.

Of course, we can also add matrices:

       >> a + b

Now if you wanna store the result of addition in a variable s then…..

       >> s = a + b

Wanna have a look into vectors as such…

  • Vectors:

Now consider a linear equation

               ax + by = p

               cx + dy = q

We can write this more compactly as

               AX = B

where the coefficient matrix A is

               a b

               c d

the vector of unknowns is

               x

               y

and the vector on the right-hand side is

               p

               q

If A is invertible, X = (1/A) B, or, using Matlab notation, X = A\B. (Check this is out A/B!= A\B both are inverse to each other)..

Lets try this out by solving ax = b with a as before and b = [ 1; 0 ]. Note that b is a column vector.

       >> b = [1; 0]              

       >> a\b

 Finally, I feel comfortable to start with piece of programming….

                                                                                    How about you?

 Step 2:
 
We need to create a new m file to continue with programming…   go to   file > new > M-File

002

 

Step 3:

It is always better to save the file before we start off with programming…

003

 

Step 4:
 
Go to   File > save as > (any name other than the name of inbuilt function).m

004

 

Step 5:
Yes, it’s the place to command your pc….

005

Let’s say you wanna display all the numbers from 1 to 10 in command window

Type this program in the editor

% for loop syntax

  for i = 1:10

    p=i
  end

Now if u feel displaying only odd numbers between 1 and 10 would be sufficient….
What would you do?

 for i = 1:10
    p= rem (i, 2);     % find the remainder when i is divided by 2

if (p= =1)
q=i
end                                                                
  end

But simplest way would be

for i = 1:2:10 

    p=i
  end

By now you must have been comfortable using loops…

  • Hey!! There is something interesting you can find with matlab programming… guess what???

 

It’s with declaration of variables… how many ever variables of any kind you use in coding there is no need to declare them…

Step 6:
 
Let’s see how to run a program…
It’s quite simple as such… just a single click on the run icon which you can find in the toolbar.

006

  • Numerical values to the left indicate the program line number… and this comes to be very handy to debug any error in the program because Matlab displays the error in the Command window, referring to line number and column number.

 

After running your program, just have a look at command window if you got any errors…

007

Ohhh….  Code seems to have some error.

 

So it’s clear from the error file that error is in 3rd line and so the only possibility to have error is in syntax of if condition (i.e. double equal to)…

Now changed code will be

for i = 1:10

    p= rem (i, 2);
if (p==1)
q=i
end
  end

Now when this code is executed, you can find no error report in command window…

016

 

Tips to use Matlab:

  • Never use file name as standard function name in Matlab

  • Inbuilt functions should not be used as variables in the program.

 

Getting into debugging mode:

  • We need to use break points in the program to get into debugging mode.

  • We can create a break point just by one click at line number and remove the same by one more click at the same point.

  • We can view source code of inbuilt functions in debugging mode by clicking on        step into icon in the toolbar.

  • Step and Step in icons pops out only in debugging mode.

 

Matlab help is a wonderful tool to solve any problem and effective use of this documentation makes you an effective programmer.

Pull-Ups and Pull-downs

December 26th, 2009

This is supposed to be such a simple concept but is often neglected or taken for granted. We have come across many engineering undergrads who have a hard time applying these concepts to the real world. So let’s see what do you exactly mean by pull-up/ pull-down resistors and where/how do you use them.

Here’s a classic example:

This is supposed to be such a simple concept but is often neglected or taken for granted. We have come across many engineering undergrads who have a hard time applying these concepts to the real world. So let’s see what do you exactly mean by pull-up/ pull-down resistors and where/how do you use them.

Here’s a classic example:
Lets take the case of a simple NOT gate. It simply negates the input.

 

Input
Output
0
1
1
0

Now imagine what would appear at the output if its input is left unconnected. Is it going to be Logic 0 or Logic 1 or something else? Some of you might argue that since we haven’t connected anything to the input, the voltage at that point would be 0 and hence the output of the NOT gate shall be high. Think again. The input isn’t at zero volts but its “floating”. (In electronics, an unconnected input pin is termed as floating) In the digital world, particularly TTL, any floating input is considered as Logic 1. But it’s never a good idea to leave the input pins unconnected. Why? Well, that’s because noise generated by adjacent electrical activity can induce a significant amount of voltage across the input which could result in errant behavior of the system.

Hence, its always advisable to have a default value at the inputs. This can be achieved by connecting pull-up or pull-down resistors at the inputs.

 

The same logic applies while interfacing devices to the microcontroller. Most of the microcontrollers available in the market have inbuilt pull-up resistors which can be enabled or disabled through software. These prove to be very handy while connecting external devices, say for example a switch. But in case when internal pull-ups are unavailable, the user needs to connect external pull-ups. You can also connect pull-downs but make sure that there are no internal pull-ups activated on that particular port.

But remember, in the above case, the swtich is connected in an active-low configuration. Which means whenever you press the swtich, logic 0 appears at the input and thus you have to write the code accordingly.

eg:

if (switch==0)
{
        //switch pressed
}

else   //switch open

There are no specific values for such pull-ups or pull-downs; they could be anywhere between 1K and 47K. For TTL based device,s it needs to be at the lower end of the scale while CMOS based devices can do away with high value resistors.

1381-based Solar Engines

December 26th, 2009

SocialTwist Tell-a-Friend

This solar engine is configured around the popular volage trigger IC-1381.This by far the most efficient solar engine. The reason being while monitoring the storage capacitor, it draws currents of order of only microamperes (better than zener SE & FLED SE which require current in milliamperes).Also, it is least tolerant to motor type, transistors & solar panels used.

About 1381: It is a CMOS voltage detector IC used to monitor power supply voltage supplied to microcomputers and is normally used to reset CPUs when the power supply drops too low for reliable operation or for preventing runaway operation when the supply voltage fluctuates. So, it detects and switches when the monitored voltage crosses the rated upper and lower threshold voltages. Say for example the output (VOUT) remains in the logic HIGH state as long as monitored voltage VIN is greater than the specified threshold voltage Vth. When VIN falls below Vth, the output is driven to a logic LOW. VOUT remains LOW, until VIN rises above Vth by an amount Vhyst, whereupon it resets to logic HIGH. This hysteresis (Vhyst) keeps input noise around the switching threshold from resulting in multiple output cycles as the transition occurs. Because of their extremely low current requirement i.e. in microamperes it is the most efficient solar engines. It’s just a three pin element with wide selection of detection thresholds from 2.0-4.9 volts. The one which we are using is 1381-E with a detection threshold of

Refer the data sheet to know more:  http://www.solarbotics.com/assets/datasheets/mn1380.pdf

1381 based SE:

solar-cel_and.JPG

Working: The voltage across the capacitor rises slowly as it is charging from the output of a solar cell. This voltage also appears at the input pin (no.2) of 1381-E. The output (no.1) of 1381-E stays low until the capacitor voltage does not reach Vtrip= Vth (2.2V) + Vhyst (of 50mV-300mV). So the base of NPN transistor is at 0V, which keeps the motor in off state. Now, the capacitor voltage reaches the Vtrip & the output of 1381 switches to 0.8 Vdd (the solar supply voltage). This turns on the NPN and it starts to draw current from the capacitor which flows through the motor and causes it to rotate. The capacitor voltage starts to drop as the motor is running. When NPN turns on, Vce starts to drop and reach a potential of approx 0V at saturation. This makes the base of PNP – 0V via Resistor and PNP turns on. Ideally, as the capacitor voltage begins to drop and it reaches the Vth of 1381, its output would be 0v to stop NPN from conducting and thus stop the motor. But since when NPN turned on it also turned on the PNP which started giving the required base current to keep the NPN on and motor running . So, at this point the 1381 is out of the circuit. Now PNP gives the required base current to keep NPN on. This results in even more NPN current due to amplification by both transistors, but it also starts to drop the voltage on the capacitor since it has to now supply most of the current flowing through the motor. The motor draws current until the voltage in the circuit is able to provide minimum Vbe to keep the transistors on or able to overcome the motor resistance. Now, the voltage in the capacitor again rises until it reaches the trigger voltage of the trigger element and the above cycle repeats. This 1381 based SE is designed to increase the hysteresis width from a mere Vhyst to Vtrip-0.6V (0.6 V -being the minimum transistor turn on voltage).

Circuit modifications: Use 1381 of higher trigger voltage to further increase the hysteresis width would give longer running time but at less frequent rates. What happens if we vary the value of resistor? If you have a look at the circuit, you can figure out that the current through motor is because of Ic of NPN + Ib of PNP. So, increasing the value resistor will decrease Ib which will decrease motor current. So, the capacitor will take more time to discharge thus giving longer runtime but with decreased starting power. Similarly, decreasing R would increase Ib which will increase motor current. So, the capacitor will discharge faster and giving shorter running time with high starting power.

There is one more variant of 1381 based SE

Modified 1381 based SE:

solar-cel_and111.JPG

Working:

This is an effective voltage trigger solar engine with configurable turn-onvoltage and discharge time.The turn-on voltage is determined by the type of 1381 selected & discharge time is determined by the timer capacitor C2. Here, the hysteresis width is not increased to a large value but it is tricked to take some time to come down from Vth + Vhyst to Vth, thus giving longer runtime of motor. While the solar cell charges the storage capacitor C1, the 1381 monitors the capacitor C2 which is also being charged through diode D1. At this point, the output (pin no.1) of 1381 stays low until the capacitor voltage does not reach Vtrip= Vth (2.2V) + Vhyst (of 50mV-300mV). Now, the capacitor voltage reaches the Vtrip & the output of 1381 switches to high state thus turning on the NPN device. The current then flows out of the storage capacitor, through the motor, through the transistor, and back to the other side of the capacitor. The 1381 output stays in high state as long as its voltage is above Vth. Once the storage capacitor voltage goes below Vth, the circuit goes in off condition. Suppose there was no C2 & diode in circuit then 1381 would have been watching the storage capacitor C1, it would reset as soon as the power dipped below Vth (as there is no PNP to keep the motor running and increasing the hysteresis width). But, here 1381 watches the timer capacitor C2 which is blocked from the power part of the circuit by the diode. So, it discharges out through 1381 to transistor. The discharging current is at operating current of 1381 which is of micro amperes, thus giving longer runtime of motor.

Circuit Modifications: The first thing you can vary is the 1381 trigger point, which will vary the turn on time of the motor i.e. varying the trigger point would vary the burst time of the motor giving longer bursts at lesser frequent rates for increasing 1381 trigger point v/s shorter bursts at higher frequent rates for decreasing 1381 trigger point. The next thing we can vary is the timer capacitor C2. Increasing the timer capacitor will increase the recharge time with longer bursts of motor runs and decreasing C2 will decrease the recharge time with shorter bursts of motor runs. Don’t increase the value of C2 so high that it keeps 1381 on even after the storage capacitor is fully discharged. The next thing you can vary is storage capacitor C1. Suppose we double the value of C1. This will make the motor run two times longer with double time to charge. To take full advantage of increased C1, increase the value of C2 to increase the runtime of motor.

Try building this effective solar engine for your solar rollers to give you better results than all previous solar engines.

The 1381 trigger ic & high storage capacitors:

http://triindia.co.in/shop/index.php?cPath=55

Understanding AVR Fusebits (ATMega 16)

December 26th, 2009

The AVR fuse bits settings are a crucial part of getting your AVR microcontroller up and running.
The fuse bits help to set the clock frequency being used, the startup time to be allotted and some more features like enabling programming through SPI, JTAG etc.

Let us look at the different bits one by one.

OCDEN (On chip debug enable)
This fuse bit when programmed keeps the clock source enabled even when the microcontroller is in sleep mode.
We generally keep it unprogrammed.

JTAGEN (JTAG Enable)
This fuse bit when programmed enables the use of JTAG and boundary scan. When you get a new ATMega 16 from the market the JTAG fuse is by default programmed i.e. JTAG is enabled. This causes the JTAG pins on PortC (TDI, TDO, TMS, TCK) to be pulled high and used in the JTAG mode.

So to use PortC for normal operation disable the JTAGEN fuse bit.

SPIEN (SPI Enable)

This bit when programmed enables programming of the AVR through SPI.

CKOPT
With CKOPT unprogrammed the AVR can be used for clock frequencies up to 8MHz. For operations above that keep CKOPT programmed.

EESAVE
When programmed the contents of eeprom are preserved even during chip erase.

BOOTSZ1…0 (Boot Size)

It selects the boot section size (For setting the size refer datasheet)

BOOTRST (Boot Reset)
When programmed, the device will start from the boot loader address at reset.

BODLEVEL (Brown out detection level)
Brown out level is the supply voltage level below which the normal working of microcontroller cannot be guaranteed. A brown out reset occurs whenever the supply voltage falls below a specific voltage level.
When unprogrammed the brown out voltage level is set at 2.7V and when it is programmed it is set at 4V.

BODEN (Brown out detection enabled)
Enables the brown out detection reset when programmed

SUT1…0 (Start up time)

The SUT1…0 along with the CKSEL0 bits aid you to select a particular startup time. The startup time is the time period between switching on of the microcontroller and the start of instruction execution.
A higher startup time is a safer option usually. This gives enough time for the crystal oscillations to stabilize.

CKSEL3…0 (Clock Select)

Using the CKSEL3…1 you can set the frequency that you will be using.

The following are some decent configurations according to the clock frequency to be used.

1MHz (Internal Oscillator)
Higher Byte 11011001
Lower Byte 11100001

8MHz (External crystal oscillator)
Higher Byte 11011001
Lower Byte 11111111

16MHz (External crystal oscillator)
Higher Byte 11001001
Lower Byte 11111111

Note: Here programmed means setting to 0 and unprogrammed means setting to 1

Junction Detector using iPitara

December 26th, 2009

Consider a grid, we want the robot to move from one corner of the grid to another. The most important part of being able to do so would be to detect a junction. Let us see if we can do this…

Here is what the grid looks like:

Basic Grid

The first thing that strikes me is that maybe I can develop on the concept of a line sensor. So I use three of the IR sensors.

iPitara on black line

Thought process:

The most basic way to detect a junction would be that if all the three sensors detect the black line then, a junction has been reached and for other areas it acts like a basic line follower. If the right sensor detect the line then turn right and if the left sensor detects a line then turn left.

iPitara on Junction

iPitara turning Right

iPitara turning left

Algorithm:


1: start
2: if IR1, IR3 on WHITE AND IR2 on BLACK
3: then GO FWD
4: if IR1 on BLACK AND IR2, IR3 on WHITE
5: then TURN LEFT
6: if IR3 on BLACK AND IR1, IR2 on WHITE

7: then TURN RIGHT
8: if IR1, IR2, IR3 on BLACK
9: then (junction detected)
10: go BACK to STEP 2
11: end

So now that we have detected the junction we can program the robot to go left and right alternately on each junction to reach the other corner of the grid. Try it out!

You can use basic black electric tape to make the grid on the floor.

Consider a grid, we want the robot to move from one corner of the grid to another. The most important part of being able to do so would be to detect a junction. Let us see if we can do this…

Here is what the grid looks like:

Basic Grid

The first thing that strikes me is that maybe I can develop on the concept of a line sensor. So I use three of the IR sensors.

iPitara on black line

Thought process:

The most basic way to detect a junction would be that if all the three sensors detect the black line then, a junction has been reached and for other areas it acts like a basic line follower. If the right sensor detect the line then turn right and if the left sensor detects a line then turn left.

iPitara on Junction

iPitara turning Right

iPitara turning left

Algorithm:


1: start
2: if IR1, IR3 on WHITE AND IR2 on BLACK
3: then GO FWD
4: if IR1 on BLACK AND IR2, IR3 on WHITE
5: then TURN LEFT
6: if IR3 on BLACK AND IR1, IR2 on WHITE

7: then TURN RIGHT
8: if IR1, IR2, IR3 on BLACK
9: then (junction detected)
10: go BACK to STEP 2
11: end

So now that we have detected the junction we can program the robot to go left and right alternately on each junction to reach the other corner of the grid. Try it out!

You can use basic black electric tape to make the grid on the floor.

About TRIC

December 26th, 2009

TRI has come up with source code development environment “TRIC” which has everything you need for rapid program development . TRIC helps expedite the development process of your embedded applications by providing the following features:

1.Windows-based IDE that combines a robust editor, project manager, and compiler.
2.TRI C integrates all tools including the SDCC compiler(which includes macro assembler, linker/locator, and HEX file generator).
3.Full-featured source code editor,
4.Project manager for creating and maintaining your projects,
5.Integrated compiling facility.

6.iBoard compatible TRIC LIBRARY to easy programming
7.Comprehensive User Guide

As already mentioned the compiler used by TRI C is SDCC. SDCC is an ANSI – C compiler that targets the Intel 8051, Maxim 80DS390, Zilog Z80 and the Motorola 68HC08 based MCUs. SDCC is Free Open Source Software, distributed under GNU General Public License (GPL).

*you can get SDCC from :http://sdcc.sourceforge.net/

Some of the features include:
1.ASXXXX and ASLINK, a Freeware, retargettable assembler and linker.
2.Extensive MCU specific language extensions, allowing effective use of the underlying hardware.
3.MCU specific optimizations, including a global register allocator.
4.Adaptable MCU specific backend that should be well suited for other 8 bit MCUs

5.Independent rule based peep hole optimizer.
6.A full range of data types: char (8 bits, 1 byte), short (16 bits, 2 bytes), int (16 bits, 2 bytes), long (32 bit, 4 bytes) and float (4
byte IEEE).
7.The ability to add inline assembler code anywhere in a function.
8.The ability to report on the complexity of a function to help decide what should be re-written in assembler.
9.A good selection of automated regression tests.

Installing TRIC
Just download the installer form the link given below and start off…
NOTE:

• You can always get product updates, application notes, latest Version and sample programs from :

Download TRI C : http://triindia.co.in/download/tric_Package.zip

For Technical Support Drop A mail On :support-tric@triindia.co.in

8051 Fundaes

December 26th, 2009

What are micro controllers?
A highly integrated microprocessor designed specifically for use in embedded systems
A microcontroller is a computer-on-a-chip optimised to control devices.

These are some of the definitions of a microcontroller or �C as we would refer to it from now on. We can conclude safely from the definitions above that a �C can be used to control a robot and manipulate its actions in various ways.

Bit by Bit
�Cs are classified according to the number of bits it can process at a time (eg: 8-bit, 16-bit, 32-bit). For all entry level applications an 8-bit �C usually suffices. For robotics too there is a host of 8-bit �Cs enough for all our applications.

RISC or CISC
RISC – Reduced instruction set computing
CISC � Complex instruction se computing

THE DIFFERENCE:
Suppose we need to multiply 2 and 6.
In a CISC core we generally would have an instruction like �Multiply 2,6�
But in a RISC core we wouldn�t have a multiply instruction. Instead we would have to add 2 to itself 6 times. This increases software complexity but reduces hardware complexity and gives higher throughput.

The BIG FIGHTWhich �C??

Choices galore: AVR, PICs, 8051s

?AVR series
:) RISC core, on chip ADC, PWM generator
:( Price slightly on the higher side, no free samples, Overkill for many applications

?PICs
:) Lots of variety, free samples delivered to your residence, feature rich

:( A bit complex to begin with

?8051 & variants
:) Easy to use, vast support, lots of variants for all types of applications, easy availability
:( Relatively slow.

Our choice � The 8051 variant 89V51RD2 from Philips Semiconductors. The reasons being
?8051 core
?Closely related to academic syllabus
?Easy availability
?In built bootloader

The P89V51RD2

8051 architecture

Features:
?80C51 Central Processing Unit
?5 V Operating voltage from 0 MHz to 40 MHz
?64 kB of on-chip Flash user code memory with ISP (In-System Programming) and IAP (In-Application Programming)
?Supports 12-clock (default) or 6-clock mode selection via software or ISP
?SPI (Serial Peripheral Interface) and enhanced UART
?PCA (Programmable Counter Array) with PWM and Capture/Compare functions
?Three 16-bit timers/counters
?Programmable watchdog timer

?Eight interrupt sources with four priority levels

80C51 CPU:
This is the heart of the �C. The place where all the computations take place. It contains the general purpose registers, the ALU etc.

Code Flash (64KB):
Flash is a non volatile form of storage. So it can be used to store the user program.

Data RAM (1KB):
RAM is a volatile memory. It can be accessed faster than flash. Hence it is used for storing program variables.

Port (0-3):
These are used for general I/O operations.

Eg: reading from switches, turning LEDs on.

Oscillator:
Generates clock frequency with the help of the crystal.

UART:
The CPU processes data in parallel. But parallel communication has its limitations with respect to the length of wire used to communicate. So for longer distances we use serial communication. The UART (Universal Asynchronous Receiver Transmitter) converts the parallel stream of data to serial and encapsulates it into a specific protocol.

SPI:

SPI (Serial Peripheral Interface) is another serial communication port. It can be used to communicate between �C and PC as well as between 2 or more �Cs in master/slave configuration.

Timer/Counter:

These are set of registers which can be incremented on either each clock cycle (Timer) or on the occurrence of an external event (Counter).
Eg: A stop watch is a Timer while an object counter is a counter

Interrupts:
An asynchronous input which changes the normal flow of execution of a program is called an interrupt.
Eg: You are reading a book, and your mom calls out to you to do some work. So you place a bookmark, do your mom�s work, come back and resume from where you left off.
Here mom is the interrupt, her work is the function that is supposed to be done when interrupt occurs and you reading the book is normal flow of execution of program.

Good 8051 references:
www.8052.com
C51 Primer

Books:
8051 Microcontroller and Embedded Systems by Muhammad Ali Mazidi, Janice Mazidi
The 8051 Microcontroller by Kenneth Ayala

Bristle Bot – Simply Fun!

December 26th, 2009

Guy’s, this is too good to resist..

While developing the course content for one of our workshops for school kids, my colleagues came across a cool bot to make.. I can’t claim that it can be made from scrap parts, but it’s simplicity amazed me.. I thought I have to show it to anyone who cares…

So here it is..

All you need to make this bot is:

1.       A tooth brush. The new kinds of ZIG-ZAG bristles will be better.

2.       A pager/mobile vibrator motor. U’ll get this in mobile repair shops. It’ll cost you around 35 Rs. may be cheaper..

3.       A 3V Button Cell.. Get it from a Watch repair shop. (Approx Diameter – 2cm)

4.       Push to on Push to Off Switch. This kind of switch has a spring action. Pressing it once will close the contacts and pressing it again will turn it off. You may also remove the switch from the circuit to make it simple. Just keep 2 terminals bare.

5.       A small piece of double sided tape..

Parts List

Cut the brush from its stem so that we only have the Bristle part.

Connect the motor, batteries and switch in series to that the motor turns on when the switch is pressed once and it stops when switch is pressed again.

Tape this circuit behind the brush using a double sided tape.

 Assembly

 


Well, actually thats it. Now press the switch and see how the bristle bot shivers and jumps..

img_3734.jpg

 

Here’s how my bot works:

bristle-bot-1.wmv

bristle-bot-2.wmv

Using SPI in 89V51RD2

December 26th, 2009

In this section I will be taking you to the world of SPI and we will see how to interface it with a memory device.Let us first understand what a serial communication is all about.

Serial buses occupy a significant place in embedded design.From andon displays to storage to peripherals,it is everywhere.

If your project needs a humble amount of read/write memory, serial EEPROM may be the answer. These tiny and inexpensive devices are especially useful when you need to minimize the number of I/O lines, cost, or physical size.

Why should we go for Serial Communication?

There are many different reasons to use a serial interface. The thing we found to be most beneficial of serial communications is low pin counts. Serial communications can be performed with just one I/O pin, compared to eight or more for parallel communications. It can be interfaced with embedded system peripherals, such as analog-to-digital and digital-to-analog converters, LCDs, and temperature sensors, support serial interfaces.

This allows large tasks that would normally require larger processors to be tackled with several inexpensive smaller processors.A small microcontroller will do the job which will otherwise require much more sophiscated microcontrollers.

Since we now know what advantages serial buses have got we will see the implementation of a serial bus.We can take SPI simply because it is easy to program and saves much pain for us.Of course SPI has other advantages too which we will be seeing later.

So What is SPI?

SPI Interface or Serial Peripheral Interface bus is as the name suggests will shift data out (and in) one bit at a time.It transmits data from master to slave over short distance at high speeds. SPI is nothing but a back to back connected two shift registers, shifting entirely controlled by the Master. It is as simple as that.

Unlike I2C there is no concept of changing ownership of bus,there are no slave device addresses. SPI is a much simpler protocol and because of this you can operate it at speeds greater than 10MHz (compared with the 3.4MHz maximum for I2C).

The best feature of SPI is that you can do full duplex data transfers (data in both directions at the same time) which you can not do with I2C and you can do it fast which is the best part.

Principles of SPI
The Serial Peripheral Interface is used primarily for a synchronous serial communication of host processor and peripherals. We use four lines and three lines are also possible but with some changes in software.(I personally would recommend a 4 wire interface if you want the programmimg to be easier.)The data input DI accepts data from the microcontroller when the microcontroller writes into it and the data output DO serves on the one hand the reading back of data, offers however also the possibility to cascade several devices. The data output of the preceding device then forms the data input for the next IC.

There is a MASTER and a SLAVE mode. The MASTER device provides the clock signal and determines the which SLAVE it wants to communicate with. CS and SCK are outputs from the master.

The SLAVE device receives the clock and chip select from the MASTER, CS and SCK are therefore inputs.This means there is one master, while the number of slaves is only limited by the number of SLAVE SELECT lines the master is having.

A SPI device can be a simple shift register up to an independent subsystem. The basic principle of a shift register is always present. The data are serially transferred, pumped into a shift register and are then internally available for parallel processing.The most important thing to keep in mind is SPI is bidirectional and before you read something you have to transmit a dummy data so that data is emerges out from the slave when it sees something coming from the master.

SPI Interface : Signals

SPI Interface Signals
Name Master Slave
SCK Serial clock output from master [SCK] Input to slave [SCK]
SDO Serial output from master [MOSI] Input to slave [MOSI}
SDI Serial data input from slave [MISO] Output from slave [MISO]
SS Optional slave (chip) select [SS] Slave select [SS]

Here is the block diagram for a single SPI device connection:

21.JPG

Now let us see what advantages SPI brings to you

  • Very fast > 10Mhz.
  • Simple interface
  • Supports full duplex data streaming.

Let us now see the flip side of SPI

  • The clock scheme may not be the same between devices.
  • The data length can vary from device to device.
  • Travels short distances only

Now having enough knowledge about SPI let us take the plunge.Here we go interfacing 89V51RD2 with EEPROM using SPI.Why 89V51RD2?Simply because it has a SPI inbuilt in it and will help us a long way in making our programming easier.Now the next question will be the choice of EEPROM.I went for ATMEL 93C46 which is a 3 wire serial EEPROM.Having said that it does not mean we cannot use it as a 4 wire serial EEPROM.We can definitely use it as a 4 wire serial EEPROM.We chose the easier option.i.e 4 wire configuration.

Now let us start designing the circuit for interfacing the EEPROM with our Microcontroller.

The AT93C46 must be connected to the 89V51RD2 microcontroller in either a 3-wire or 4-wire configuration.In the 3-wire configuration, the EEPROM serial data in (DI) and serial data out (DO) pins are both connected to the same microcontroller I/O pin, thereby saving a pin. In the 4 wire configuration the DI pin is connected to MOSI pin while the DO pin is connected to MISO pin.The ORG pin selects 8-bit data when grounded and 16-bit data when floating or tied to VCC.

We will now see the circuit for 4 wire configuration.This is how it looks:

11.JPG

I have grounded the ORG so that x 8 organization is selected.The ground must be common and DON’T give separate ground to the CPU and the memory device.The rest of the connections is the same as what we discussed earlier in this section.

How to Program?

I hope everybody is now ready for programming.While programming we have to be careful because the difficult part about SPI is that the standard is loose and each device implements it a little differently. This means you have to pay special attention to the datasheet when writing your interface code. All SPI settings are determined by the SPI Control Register (SPCR).This register also takes care of the clock.You need not worry about giving clock after every bit. This register can control and also decide how clock is to be given.The data is transmitted and received through SPI data register(SPDAT).STATUS register informs you whether a transmission is completed or not.So by polling the corresponding bit you can know the status of transmission.STATUS register in 89V51RD2 is SPSR.

The SPI control register (SPCR) has 8 bits, each of which control a particular SPI setting.

If you run through the datasheet of 89V51RD2 you will know how to do the settings.

Once you have your SPI Control Register set correctly you just need to figure out how long you need to pause between instructions and you are ready to go. Now that you have a feel for how SPI works, let’s take a look at the details of EEPROM.

The AT93C46 is activated using the Chip Select pin (CS) and accessed via a 3-wire serial interface consisting of Data Input (DI), Data Output (DO), and Shift Clock (SK). Upon receiving a READ instruction at DI, the address is decoded and the data is clocked out serially on the DO pin. In the WRITE cycle,for polling we bring the CS pin high and the DO pin outputs the READY/BUSY status of the part.Before programming make it a point to go through the datasheet of 93C46 and check how the data gets inside.

Before Programming make sure that the following steps are followed otherwise you will face all the problems which I faced and I promise you its going to be difficult if you have not read the datasheets thoroughly.

1.You need only one full duplex (simultaneous send / receive ) function for SPI which you call and recall with changing data to perform read/erase/ write operations.

2. Even to receive a byte you need to send a dummy byte and when SPIF is set after sending 8 bits you simply read the byte and clear SPIF.

3. I expect that you can send a series of Dummy 00s and read the busy status( as a returned byte) after any write/erase commands. The bytes will be zero when busy and one when ready.

Let me share two important problems which I faced while implementing.

The first one is that we have to send 10 bits(start bit,opcodes,address ) in an 8 bit register SPDAT.Sounds tough?But not quite so.

The solution goes like this:

Since more than 8 bits are required to control a 93C46 device, so two bytes are required.

This can be done in the following way

First Byte

| 0 | 0 | 0 | 0 | 0 | 0 | S | O1 |

where S is the start bit and O1 is the op code msb.

Second Byte

| O0 | A6 | A5 | A4 | A3 | A2 | A1 | AO |

where OP0 is op code lsb and A6-A0 are necessary address bits.
The next one is that,you must have noticed a leading zero emerging while reading before data bits come,from the timing diagram of Read function in 93C46 EEPROM datasheet.This zero bit will prevent the LSB from entering the data string. For this,this is what you have to do.In the datasheet of 89v51rd2 you can see that SPI Mode 0 expects the MISO input to be ready when the clock goes positive, where as the EEPROM shifts out the data after a short delay from the positive edge of the pulse. So, after sending the read command with SPCR=0×52, you change SPCR =0×56 (CPHA=1 ) before reading the data byte from EEPROM. This will ensure that the Master strobes data on the falling edge of the clock and by then the EEPROM will output the data to MISO, triggered by the positive edge of the clock. After reading the byte, in the same function set SPCR back to 0×52 before leaving the function.

Ok enough of theory let us do the coding.The functionality of this code is to write to the EEPROM a character and read it back and display the character in the Hyperterminal through UART.

void SPI_MasterInit(void);

unsigned char SPI_TX_CHAR(unsigned char d);

void ERASE();

void EWEN();

void WR( char EE_address,char EE_data);

unsigned char RD( char EE_address );

void main()

{

unsigned char data2;

SS=0;

UART_INIT(9600,0); //Intialising UART

SPI_MasterInit(); //Intiallising our controller as master

ERASE(); //doing a erase operation

WR(0xEC,’V’); //doing a write operation

data2=RD(0×6C); //reading the data and storing in data2

TX_CHAR(data2); //displaying the value in data2 in

//hyperterminal

while(1)

{

}

}

void SPI_MasterInit(void)

{

P1 = (1<<P1_7)|(1<<P1_5)|(1<<P1_4); // Set MOSI and SCK and SS(-) as output , all others input

SPCR = 0×52; // Enable SPI, Master, set clock rate fck/64

}

unsigned char SPI_TX_CHAR(unsigned char d)

{

SPDAT=d;

while(!(SPSR & (1<<SPSR | 0×80))); //checking for transmission to complete

SPSR=SPSR & 0×7F; //clearing SPIF

SPCR=0X52; //making CPHA=0

return SPDAT; // Return data register

}

void ERASE()

{

SS=0;

SS=1;

SPI_TX_CHAR(0×03); //sending start bit and OP1

SPI_TX_CHAR(0xFF); //sending OP0 and address

SS=0; //doing a low to high transition before checking for busy flag

SS=1;

while(MISO==0); //checking for busy flag

SS=0; //selecting chip select low

}

void EWEN()

{

SS=0;

SS=1;

SPI_TX_CHAR(0×02); //sending start bit and OP1

SPI_TX_CHAR(0×60); //sending OP0 and address

SS=0;

DELAY(20); // MUST wait 5 ms for internal cycle to complete

}

void WR( char EE_address,char EE_data)

{

SS=0;

SS=1;

EWEN(); //doing a write enable before doing a write operation

SS=0; //doing a high to low transition

SS=1;

SPI_TX_CHAR(0×02); //sending start bit and OP1

SPI_TX_CHAR( EE_address ); //sending OP0 and address

SPI_TX_CHAR(EE_data); //sending the data bits

SS=0; //doing a low to high transition before //checking for busy flag

SS=1;

while(MISO==0); //checking for busy flag

SS=0;

DELAY(20); // MUST wait 5 ms for internal cycle to complete

}

unsigned char RD( char EE_address )

{

unsigned char data1;

SS=0;

SS=1;

SPI_TX_CHAR(0×03); //sending start bit and OP1

SPI_TX_CHAR(EE_address); //sending OP0 and address

SPCR=0X56; //making CPHA=1

data1=SPI_TX_CHAR(0×00); //Transmitting dummy data for reading back

the data bits

SS=0;

return data1;

}