Object Following Robot

wohoo…..!!

How many of them want to make a funny Robot which follows a unique colored object, for example let us take a ball.

How to build such a thing what all things should you know yeah i am coming to that point

1.image processing(OpennCV 2.0)

2. Microcontroller (AVR)

For people who don’t know about OpenCV its is an  Open Source Computer Vision  library of programming functions for real time computer vision written in C. since it is written in c language u need not learn any cheat code like do it Matlab.

pros of opencv

1.its free

2.it is platform independent

3.written in c and c++ easy to use

let us break down this problem statement into two parts

1.extract the information pertaining to the ball from the cam feed . Apply the algorithm to know if the ball is on the left hand side or on the Right hand side. Next find the distance between the ball and the bot using the size attribute size of the ball .

now using  the x-coordinate of the cog of the ball we can decide weather the ball is on the Right or Left side of the robot by compareing it with the midpoint of the x-axis of the image.if the size of the image is 320 x 240 then

if ball_x >180

ball is on the Right hand side of the bot.Now make the bot rotate right by a small angle

else if  ball_x < 140

ball is on the Left hand side of the bot .Now make the bot rotate Light by a small angle

else if  ball_x>141 &&   ball_x <179

now the bot is facing the ball

get the box size and aproximate it to know the distance between the bot and the ball. Based up on the distance now decide to weather to move the bot forward or backward .

Initially i have tracked a unique colored object only on the x-Axis here i have placed the camera on the top of the motor .now by rotating the motor left and right i have achieved tracking on x-Axis .

The camera is connected to the pc where imageprocessing is done and then based up on the  x-coordinate of the object the control information  is sent to the microcontroller(AVR)  serially.

On the micro-controller code is written to receive control information through serial port and control the rotation of the DC motor interfaced through L293D(Motor Driver).

In my First attempt to do so i have used RoboRealm.

Once i have seen this happening  with this confidence i headed towards implementing the same on a two wheeled Robot.This time i have  implemented the image processing  part using OpenCV 2.0.

Motion Gaming using Opencv and SDL lib

hello every one ,I hope every one has gone through my previous post on embedded gaming .If not please check it out to get some idea about embedded gaming.In this post i am going to write about a weekend project of mine which was inspired from the PLAYSTATION MOVE MOTION CONTROLER from sony. This is a very small attempt of mine to do some thing.

This time i have controlled the Egg Man game which you people have allready seen  in my previous post using the motion of the tennis ball in my hand .

Basically it is motion gaming which enables the game running on the PC to be controlled  according to the motion of  the unique colored ball on stick in the players hand.The first step to solve the problem statement is to be able to track the motion of the  ball with the help of image processing .i achieved the first step using opencv an open source cross platform image processing library.

Step two is to control the game using the control inputs from the image processing part of the code and moving the player on screen accordingly.This step is quite simple just redrawing the player on the game arena at a new position and adding some lines of code pertaining to the game logic makes it done.

If you wish to add sound to your game this can also be done using the sdl

i have attached the video of the game which is been controlled using the motion of the user

Embedded Gaming using AVR Microcontroller

Hello every one.

This post is about what i call “EMBEDDEDGAMING“. yeah its about writing games for your PC and controlling the using the embedded hardware.

It all started when i joined THINKLABS @SINE IITB .During the first few months of my professional life here was quite exited about my work.it was during this period where i tried to started learning how to write simple 2D games with the help of web and obviously the documentation of the open source API’s which i used.

During the initial  days it was quite tough to write the games, embedding the game logic in the code.Tough i was was not from a comp science background  finally tried and mastered this skill embedding the game logic into the code.

Before i start talking about the 2D games .i would also like to mention about VT102 control sequences.Using this control sequence we can print characters on the terminal with any of the six supported colors in foreground and background of the character.most of the standard terminals available support this protocol.using this control sequences is very easy just sent the control sequence string on to the terminal serially and get the character of your choice printed with preferred colored .

The video bellow shows a simple game written using AVR and controlled using an analog joystick connected to the ADC port of the AVR and the rendering is happening on the hyperterminal using the vt102 control sequences using the UART.

The following video demonstrates a simple game written on AVR 

Coming to the part of writing 2D games .I have opted to use SLD gaming API  is a cross-platform, free and open source multimedia library written in C.its a properly documented and lots of community support is also available online.

The paradigm which i choose to develop and control the game is as follows

The 2D game will be running on the PC and will receive control information serially through the comport available on the pc if a laptop is used we can use USB to serial converter to get a comport on your laptop.

The embedded hardware connected to the pc will send the control information according to the position of the joystick.

Many other interfaces can be done to the AVR like accelerometer and RC5 remote to controle the game.

The following video shows game Control using joystick interfaced to AVR:

Now how about controling the game Control using RC5 Remote interfaced to AVR:

Finally we have  game Control using accelerometer interfaced to AVR:

Interfacing ps2 keyboard to atmega128/atmega64

Interfacing  ps2 keyboard to Uniboard(Atmega64/Atmega128)

Hardware needed :

  1. Uniboard
  2. Ps2 female connector
  3. 4 burg wires

AVR  Peripherals used:

  1. External interrupts
  2. Io ports
  3. UART(for debugging)

Ps2 key board implements a bidirectional synchronous serial communication .Most of the ps2 keyboards use 6-pin Mini-DIN (PS/2) connector which is shown in the following image.

Pin configuration of 6-pin Mini-DIN (PS/2):

  1. Data
  2. NC
  3. Ground
  4. Vcc(+5v)
  5. Clock
  6. NC

                                        Ps2 female connector 

All the data originating from the keyboard is transmitted one byte at time using the synchronous serial communication .when a key is pressed on the keyboard a unique scan code associated with the key is shipped out using the synchronous serial communication i.e. using the clock and data lines.

The keyboard generates the clock in this case whose frequency is 2000hz i.e. baud rate=2000 bits per second.

On every falling edge of the clock generated by the keyboard on the clock pin a new bit is shipped out on to the data pin which can be sampled to read the data bit by bit.

Ps2 keyboard used a serial frame of 11 bits each

  1. 1 start bit (Active Low)
  2. 8 data bits ( LSB First)
  3. 1 parity (Odd Parity)
  4. 1 Stop bit (Active High)

The timing diagram of the synchronous serial communication used by the keyboard is shown bellow

Fine so as every key has unique scan code which will be shipped out serially through the data pin bit bit by bit on every falling edge of the clock that’s it is it that simple…?.

No. To enable the host device to know the press and release of the key the keyboard sends the scan code twice in the following fashion.

Key pressed                                                                                                   key Released
Scan Code=11 bits                                                            Break Code + Scan Code=22 bits

 Total( key press + key released)=33 bits.

Break code is same for all the keys and  it indicate the release of the key and now to identify which key is released scan code corresponding to the key released is also shipped out again.

Ok enough theory let us do some bang bang on the keyboard now (write code)

Its very simple we just need to implement the synchronous serial communication using an external interrupt and an io pin.

Ok now tell me what should I do with external interrupt and  i/0 pin

Step one

Configure the external interrupt for falling edge and connect the clock pin to it. Since on every key press 33 bits will be shipped out of the data pin serially on the falling edge of the clock which is connected to the external interrupt pin .

On each key event(press +released) the external interrupt   ISR  will be executed 33 times of this 33 only first 11 times for which the ISR gets Executed is of our interest  to decode the scan code.

The First bit can be neglected since it is the start bit and the last two can also be neglected If you want to quickly decode the scan code .

The code to implement  the above mentioned algorithm can be found in  Ps2_lib.h

Function list:

  1. 1.       void Ps2_Init(void);

This function does the initialization job for the external interrupt and the io pin

  1. 2.       INT8U Ps2_Get_Data(void);

This function can be used to read data from the ps2 keyboard.it will wait till a key  is pressed on the keyboard and then returns the character corresponding to the key.

NOTE:

To change the  hardware configuration of the lib please look into the ps2_lib.h

/*******************************HARDWARE CONFIGRATION****************************************/

///DATA PIN CONNECTED TO ANY GPIO PIN

#define datapinno 0x07                                        // Value of data port when the data pin is high

#define dataport PORTB                                        // Enter the data port here

#define dataddr DDRB

#define datapin PINB

///CLOCK PIN CONNECTED TO EXTERNAL INTERRUPT

#define external_interrupt_no 6

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

—————Bug reports and hints for improving these pages are very welcome——————

project code link for AVR-GCC compiler(winavr)

 Download Code            

Video :

 

AVR Bootloader(Based on avr109 application note)

hello every one hope you all are having great time .In this post we will try and comprehend how bootloader’s are written .

For many hobbyist  out their using AVR Microcontroller  bootloader is a black box. Many questions arise once you start using a bootloader.i have listed bellow few of them.

1.what exactly is happening in the bootloader code ……?

2.how does it work…..?

3.what is this boot switch why do i need to press this switch to program the controller ……..?

well ,same questions we bugging even me when i started using a bootloader to program some xyz board.Then i decided to write a basic bootloader to program an AVR microcontroller  using the  UART.

their are many open source bootloaders available on net and few of them are available as application notes on the atmel site .i decided to start with avr109 based bootloader .AVR109 based bootloader protocol was much simpler compared to the protocol of  stk500 based bootloader.

Let us start with few basics about the flash memory which you need to understand before diving into the bootloader code.

The flash memory is divided into two parts whose size is fixed

1. Read while write(RWW)

2. non Read while write(NRWW)

The main difference between the RWW and NRWW section is:

  • when erasing or writing a page in RWW section,the NRWW can be read during the operation.Hence the CPU is not halted.
  • when erasing or writing a page in NRWW section,the CPU is halted during the entire operation.

The flash memory is organised in term of pages.The page size in ATMEGA64 is 128 words.

Any update to the flash memory of the microcontroller happens in terms of pages either writing or erasing.The same thing happens even in the EEPROM memory.

For a Embedded Devveloper the flash memory is divided into two parts

1.application section

2.bootloader loader section

One should also know that the size of the these sections can  be configured using the fuse bits(BOOTSZ)

The following table illustrates the effect of  the BOOTSZ fuse

The application code is present in the RWW section while the BootLoader code is present in the NRWW section.while the application code can also extend into the NRWW section,but this should be avoided.if you don’t require a bootloader entire flash memory can be used for application code.Their are bootlock bits as well to enable the protection of the code resident on the flash memory.

The steps involved in programming an AVR using a Bootloader are as follows:

step 1:Entering into the bootloader

This can be done by jumping to the starting address of the bootloader from the application code by using a function pointer(if written in c language).Alternatively it can also be done with the help pf the BOOTRST flag which is present in the HFUSE.

by default this flag is 1 (unprogrammed).we need to program this flag i.e clear this flag to make the AVR to jump to starting address of the bootloader upon RESET.

Steps to load a page and erase a page in the flash memory is :

1.fill the page buffer(unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address))

2.Page Write(boot_page_write())

and to erase a page (boot_page_erase())

winavr provides the api’s to do most of the stuff relating to the the page load,page write & page erase (avr/boot.h avr/pgmspace.h)

/* AVR-GCC/avr-libc */
# include <avr/boot.h>

# include <avr/pgmspace.h>

The above two lines of code should enable you to use the api’s

The bootloader uses input from the external switch to enter into the bootlaoder

if(SWITCH==PRESSED)

{

START BOOTLOADER

}

ELSE

{

JUMP TO APPLICATION CODE

}

The above mentioned condition is at the starting of the bootloader .To enter into the bootloader we need to restart the AVR and then press the designated external switch to enter into the bootloader part.

In this case i have used sw1 of uniboard connected to PD6

Initially burn the bootloader code into the AVR using a usb or serial programmer .The .hex file can be found in the download section of this post.

NOTE:before buring the code into the AVR change the value of HFUSE 0xCC

instructions to use the bootloader:

1.press the reset switch when leds are blinking press sw1 to make the AVR to start the bootlaoder.

2.once the bootloader is started the leds on portc will stop blinking .This indicated the bootloader is started and it is waiting for the commands from UART1 at 19200 baud rate 8 data bits and 1 stop bits

3.To program the avr on windows we can use saina prog which is a freeware

can be downloaded from the following site

4.The following settings need to be done on saina prog change the name of the comport according to the number of the comport connected to the board.

Download code: