Thursday, November 1, 2012

Firmware for uC | Communication Interface

0 comments
In the last post, we had little introduction of how the firmware is going to act. With that picture in mind, now we will go a little deeper.

Android <==> BtBee <==> USART <==> uC

USART Communication
    Atmega16 contains one USART device. In this section we'll learn how to use it for receiving information. For using any of the peripherals in the uC, we need to do 

           + Switch ON the peripheral.
           + Configure the peripheral.
           + Use it.

Switching ON the USART
    Switching ON the USART involves, setting the TXEN and RXEN bits.
UCSRB |= _(RXEN) | _(TXEN);    /*enable the device */

Configuring the USART
   Configuring involves setting the Baud-rate of the USART device. In Atmega16, it can be done by writing the calculated Prescaler value into USART Baud Rate Register(UBRR). UBRR is a 16 bit register. UBRRH and UBRRL represents the higher and lower bytes of the UBRR.
UCSRC |= _(USBS);               /* use 2 STOP bits */
UCSRC |= _(UCSZ0) | _(UCSZ1)    /* use 8 DATA bit mode */

int baud_prescale = F_CPU/(16*baud) - 1;
UBRRL = baud_prescale & 0x00FF;
UBRRH = RSHIFT(baud_prescale, 8) & 0x00FF; 

Using USART
   USART device provides a data register named USART Data Register(UDR), which act as a buffer. There are two separate register for transmission and reception. UDR refers to one when used for transmission and another for reception.
unsigned char received_data = UDR ;
UDR = data;
Interrupt 
   We have seen the snippets for transmission and reception of data through USART. Using Interrupt mechanism in conjunction with USART provides a way for efficient use of the micro-controller time. For you to realize how useful the interrupt mechanism, here is an example. Here our purpose is to receive a byte of information through USART.

   You can just copy the contents of the UDR register at any time you want. If you do so, mostly the copied content will not the correct information, but some garbage value. We have wait for the USART device to complete reception and then copy the data. It is done as follows.
unsigned char USART_receive()
{
   while(UCSRA&(1<<RXC) );
   return UDR;
}

    USART indicates the reception of data by setting the RXC bit of UCSRA register to 1. We have to continuously monitor the RXC bit, and when it becomes 1, we return the data, i.e contents of UDR.

    By enabling the Interrupt mechanism, we can make the USART to interrupt the processor when, RXC becomes one i.e. when the reception is complete. This means that, the micro-controller can do some useful job without wasting its time in monitoring a single bit.And when the processor is interrupted, it will halt the operation, whatever it does and serves the Interrupt, and after serving the interrupt it can continues is operation where is was left.

    And of course, we have code the Interrupt Service Routine, a special function which is called when the interrupt occurs. There can be several interrupts, with several priorities. In avr-gcc, the interrupt service routines are coded as follows.
ISR(USART_RXC_vect)
{
   UCSRA &= ~(1<<RXC);  /* clears the RXC bit */
   return UDR;
}
    where the ISR and USART_RXC_vect is a predefined macros by avr-gcc. Here is the list of predefined interrupt vector names specific to a controller.
   List of Interrupt Vectors

Read more ►

Firmware for uC | Introduction

0 comments
   This post marks the beginning of a new article series discussing the development of the firmware, we coded for the atmega16 micro-controller(we call it the, ORGAN) used in the project.

   The firmware will not do anything unless the android(we call it the, BRAIN) asks it to do so. After lot of thinking, and experiments interrupt driven method is chosen  for implementation. When it receives a command usually one the predefined set of values defined through these directives.

file: athena.h
/************************************************************
 * Actual operations                                        *
 * they are used as indices for                             *
 * <category>_<operation>_<direction> function pointer array  *
 ************************************************************/

/*  LOCOMOTION OPERATIONS */
#define LOCO_FORWARD      LOCO_CONT_BEGIN +  01
#define LOCO_BACKWARD     LOCO_CONT_BEGIN +  02
#define LOCO_TURN_RIGHT   LOCO_CONT_BEGIN +  03
#define LOCO_TURN_LEFT    LOCO_CONT_BEGIN +  04
#define LOCO_TURN_AROUND  LOCO_CONT_BEGIN +  05
/*  CAMERA HANDLING */
#define CAM_LOOK_UP       CAM_CONT_BEGIN + 01
#define CAM_LOOK_DOWN     CAM_CONT_BEGIN + 02
#define CAM_LOOK_RIGHT    CAM_CONT_BEGIN + 03
#define CAM_LOOK_LEFT     CAM_CONT_BEGIN + 04
/* ARM CONTROL */
#define ARM_CONTRACT      ARM_CONT_BEGIN + 01
#define ARM_DILATE        ARM_CONT_BEGIN + 02
#define ARM_UP            ARM_CONT_BEGIN + 03
#define ARM_DOWN          ARM_CONT_BEGIN + 04
where the values of <category>_CONT_BEGIN are,
#define LOCO_CONT_BEGIN 00                               /* 00 */
#define LOCO_CONT_LEN   10
#define LOCO_CONT_END   LOCO_CONT_BEGIN + LOCO_CONT_LEN  /* 10 */

#define CAM_CONT_BEGIN  LOCO_CONT_END                    /* 10 */
#define CAM_CONT_LEN    10
#define CAM_CONT_END    CAM_CONT_BEGIN + CAM_CONT_LEN    /* 20 */

#define ARM_CONT_BEGIN  CAM_CONT_END                     /* 20 */
#define ARM_CONT_LEN    10
#define ARM_CONT_END    ARM_CONT_BEGIN + ARM_CONT_LEN    /* 30 */
So when the brain send one of these commands to the organ, the organ do the job for a fixed time frame and stop. (do not worry about the communication interface b/w brain & organ. we will come to that in next post.) What is now important is, firmware does the job for a fixed time duration when it is asked to do.


Read more ►

Saturday, October 27, 2012

AVR development in Linux

0 comments

Install necessary packages 

    $ sudo apt-get install gcc-avr
    $ sudo apt-get install avr-libc
    $ sudo apt-get install avrdude

Sample code

 #include
 void main()
 {

 }

Compiling code


   $ avr-gcc -Os -mmcu=$2 file_name.c -o file_name.elf
   $ avr-objcopy -j .text -O ihex file_name.elf file_name.hex

Uploading code

    $ sudo avrdude -p part_name -c programmer_id \
    >-U flash:w:file_name.hex
           + part_name => m16, m8
           + prog_id => usbasp
    + "flash" => programs flash
           + "w" => write mode 
  + shld be run as super user => "sudo" 

Compiler optimization

   specify an option -Ox while compiling (avr-gcc -Ox ... )
  + x => optimization levels
  + "-Os" => "s"=> small - for smaller code

 Made a video of LED blinking @ 900ms  

FUSE BITS:

 # read  
    $ avrdude -p m16 -c usbasp -U fuse_name:r:file_name.txt:h
        
        + fuse_name => lfuse,hfuse,lock  
   + "r" => read
        + fuse value "h" in hex format
       + stored in file_name.txt file  

 # write
          $ avrdude -p m16 -c usbasp -U fuse_name:w:0x82:m
          
        + "0x82" => sample fuse value
        + "m" => immediate ( use value in cmd directly )  
 + "w" => write

 Note: make use of AVR fuse CALC available online
Read more ►

Sunday, October 21, 2012

Setting up Git and Gitorious.org

2 comments
1. Backup any existing ssh keys
     $ cd ~/.ssh/
     $ mkdir key_backup
     $ mv *.pub ./key_backup/

2. Generate private ssh keys
     $ ssh-keygen -t rsa -C "developer.rps@gmail.com"
     $ Enter file in which to save the key: filename
     $ Enter passphrase : *****

3. Add private ssh key to git.gitorious.org account
     $ sudo apt-get install xclip
     $ xclip -sel clip < ~/.ssh/filename.pub

       - go to account setting in the user page of git.gitorious.org
       - Go to your Account Settings
       - Click ssh keys
       - Click "Add SSH key"
       - Paste your key into keyfield
       - Click Add key

4. Check whether ssh key is successfully added
     $  ssh -v git@gitorious.org


5.1. Clone the remote repo
     $ mkdir ~/workspace/
     $ cd ~/workspace
     $ git clone git://gitorious.org/athena/athena


5.2. Create remote repo from existing local repo
     $ mkdir ~/workspace/
     $ cd ~/workspace
     $ # add files if not already exist
     $ git init
     $ git remote add origin git@gitorious.org:athena/athena
     $ git commit * -m "some message"
     $ git push origin master


6. Adding files to remote repo
     $ cd ~/workspace/athena
     $ nano someFile.txt

7. Add some msg to it, save and exit
     $ git add someFile.txt
     $ git status
     $ git commit -a
     $ git remote set-url --push origin git@gitorious.org:athena/athena.git
     $ git push origin master #only for the first time,
     $ #next time onwards just use "git push")

   If the step 7 throws an error like this,
          $ Permission denied(publickey).fatal:The remote end hung up unexpectedly

  do the following, then try pushing
     $ ssh-add ~/.ssh/id_rsa.pub
     $ git push

8. Getting files from remote repo
     $ git pull



7. Start a new branch
     $ git branch feature-A
     $ git branch -a

8. Select new branch
     $ git checkout feature-A

9. Push commits on new branch to remote repo
     $ git push origin feature-A

10. Merge new branch with master
     $ git checkout master
     $ git merge feature-A

11. Delete a local branch
     $ git branch -D feature-A

12. Delete a remote branch
     $ git push origin --delete feature-A
Read more ►

Saturday, October 6, 2012

Software design - Proposal 01

1 comments
 Starting with a crude idea and refine it step by step..
   
    Requirements
        + accept a command
        + process and understand it
        + do it

    Refined 01
        + accept voice command
        + convert it to text
        + understand the text --> mostly pickup an object
        + find the object by looking around
        + go nearer to the object
        + pick it up if possible
        + track back to the initial position

    Refined 02
        + detect the object - DETECTION
        + calculate the distance and size of the object - GUESSING
        + track the object - TRACKING
        + go to the object
        + pick up the object if possible.
        + return back to the initial position or who commanded.
           
Before doing anything it is important to write the firmware for the uC.
    I'm planning to implement the firmware as a finite state machine. again the specification for the firmware are

        + LOCOMOTION OPERATIONS
        - LOCO_FORWARD
        - LOCO_BACKWARD
        - LOCO_TURN_RIGHT
        - LOCO_TURN_LEFT
        - LOCO_TURN_AROUND
    + CAMERA HANDLING
        - CAM_LOOK_UP
        - CAM_LOOK_DOWN
        - CAM_LOOK_RIGHT
        - CAM_LOOK_LEFT
    + ARM CONTROL
        - ARM_CONTRACT
        - ARM_DILATE
        - ARM_UP
        - ARM_DOWN
   

    these are some example operations performed by the uC when commanded by the BRAIN
    other than these there are several house-keeping functions. as previously said the uC ATmega16 is selected. the command is received thro the USART
    _____________         _________________
    | uC | USART|  <----> | BtBee | BRAIN |
    """""""""""""         """""""""""""""""
Read more ►

Tuesday, September 25, 2012

Getting ready for first review

0 comments
   Weeks went and we have finished voice recognition in Android portion of the project in Android . So we are planning to report it on first review. So next plan is to code image processing utils. here after. And also we are now familiar with Git SCM. We use Gitorious repo for our project, here is it: Repository for Athena[https://gitorious.org/athena/athena]
Read more ►

Monday, September 24, 2012

Git Simple Commands

0 comments

I keep forgetting how to push files and pull them down. So, this post is for my reference to use GIT.

Adding files to the remote repo


1. Adding the folder(or file) => git add <filename>
2. Checking status of git (confirm whether it is staged) => git status
3. Commiting => git commit -a



4. Pushing the files into remote repo => git push




5. Listing the files (not necessary)



Pulling down files from remote to local repo



Read more ►

Thursday, September 6, 2012

Started with Speech Recognition

0 comments

Couldn't finish up with the bluetooth part of the project. Will deal with it on a good day ( hopefully next week). Luckily, I'm gonna have 9 days OFF. Next week, TCS and HCL companies are coming to the college for Campus Selection. A lot of people get selected in that (here, every Btech student's last hope is TCS), so, we were given a week off for preparation. Anyways its not my issue. Coming back to Speech Recognition.

I used the speech recognition API provided by Android - simple, easy and perfect. Works better than what i thought. One drawback is that it uses an Internet (google) service to identify speech and convert it to words. We are planning to use Pocket-Sphinx in the future where the signal processing is done on-board but the drawback in that is that it has high RAM requirements. It takes quite some time to recognize words from speech even when I tested it on my PC (4GB RAM). Lets see about it in the future. For now, this will do.

Some of the images in my app:

 



As you can see above, there is a list of strings returned. Actually the intent returns an ArrayList<String>, which is added to the ListView as ArrayAdapter. I'm planning to store all the possible commands (to be sent from Android->uC) in the database and when the user gives a command through speech, we iterate through the ArrayList returned by the intent using ArrayList.Iterator() and look for a possible match in the database. It requires an efficient algorithm to search the database and find the match. Then, the matched command is sent to the uC. I believe that this idea is better than OK. 

A chunk of code for iterating through the ArrayList and looking for Integers:


  1. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  2. {
  3.         if(requestCode==check && resultCode==RESULT_OK)
  4.         {
  5. ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
  6.                        
  7.         while(results.iterator().hasNext())
  8.         {
  9.                 String temp=results.iterator().next().toString();
  10.                                
  11.                 if(isInteger(temp))
  12.                 {
  13.                         tvSpeechResult.setText(temp);
  14.                         break;
  15.                 }
  16.         }      
  17.                                
  18.         }
  19.         super.onActivityResult(requestCode, resultCode, data);
  20. }
  21.        
  22. public boolean isInteger(String ip)
  23. {
  24.        
  25. try {
  26.                        
  27.         Integer.parseInt(ip);
  28.        
  29.         return true;
  30. } catch (Exception e) {
  31.         return false;
  32.     }
  33.                
  34.                
  35. }



Hoping to get back to the Bluetooth API soon...


Read more ►

Sunday, September 2, 2012

Working on Bluetooth API

0 comments

Still working on the Bluetooth app for android. Unfortunately Bluetooth protocol is complicated. Going through the Bluetooth Chat sample code. So far so good. Took some screenshots












 
 



So far the app can enable/disable bluetooth, display paired devices, get hold of a device , print its name and its address. Just need to add communication functionality. I hope to complete this app tonite. 


Read more ►

Friday, August 31, 2012

[01 Sep'12] some-junk-notes

0 comments
+ running a openCV program under GDB
+ seperation of project arch into different units
             -   servo handling unit(camera)
             -   driver unit(locomotion)
             -   image processing unit
             -   voice recognition unit.
             -   interaction interface unit                 
                   e.g: when we want to align the robot chase with camera,
                         we need servo & locomotion unit to interact.

+  Ques: orders are given obviously but can the orders be asked by slave.
     e.g:
         servo and stepper operate at different resolutions, so the solution is
             i. standardize the unit of measurement b/w them.
            ii. create a f/b loop, so that slave uC can query the android app to get
                the error with servo due to stepper movement

from all these one thing is clear calibration is the most important that is to be considered first.

+ and the project workflow should stick to the plan.
Read more ►

Thursday, August 30, 2012

[30 Aug'12] Tested BtBee

0 comments
         Today we have tested the BtBee module that we bought from SimpleLabs (Chennai). We planned to demonstrate the voice recognition part of the application on coming monday.
         and here are something that came to my mind.
  • we have decided to use stepper motors for driving the bot
  • got to design the software design soon
  • there is no user interface required 
  • clean modularity is very important
  • going to write segments of the programs as separate program, for testing and then integrate them accordingly
  • image processing module gonna be the main thread. at a only one functionality is being run by the microcontroller. for example only the stepper functionality will be running until the bot reaches the proximity of the object to pickup
  • voice recognition: trying to access the DSP of Android phone and using pocket sphinx for voice input.
  • image processing thread
  • acquire the image 
  • find the object of interest 
  • center the object in the pic. i.e move the camera so that the object comes to the center of the image.
  • then accordingly align the wheels of the motor, so that it can go straight to the object
  • take three/four snapshots at different point as the bot moves and use kinematics and some simple maths to find the size of the object
  • can we use the ultrasonic range finder for measuring the distance between the bot and the object.
  • move towards the object so that we can grab it.
  • pick it up and return to the initial position where the command was given
  • initially we have planned to use three servos for arm control and now we try to test with a single servo grabber.
  • so everything gotta be highly modularized.
  • the command are saved in command files
                         like
                         camera.upwards = CAM_UP
                         camera.left = CAM_LEFT
                         .....
                         etc..
  • can we use GPS for tracing the path back? no GPS is power hungry and adds more complexity to the project.
  • learn how to construct a delay circuit so that only one pin can be used to drive a stepper motor using four delay elements in series. you got it??
Read more ►

Saturday, August 11, 2012

Experiment with OpenCV

0 comments
    We have started the process for making our project. Here is a demonstration, that I have done this evening. This video summarizes, a tutorial written in OpenCV doc.



   The program demonstrated loads and plays a video present in the current directory and provides a slider to position the current frame. Happy Coding !!!

You can read the original post on my blog
Read more ►

Friday, August 10, 2012

Zeroth Review - Second attempt

0 comments
   As the first entry into zeroth review was screwed due to the absense of a block diagram in the abstract, this week we were supposed to present our ideas. I got to thank two people, who supported our ideas and gave us a chance to attempt.
 
Read more ►

Sunday, July 29, 2012

Slides Preparation Going on.

0 comments
 These are the pin out details for atmega16 uC and L293D motor-driver.

Read more ►
 

Copyright © Project Athena Design by O Pregador | Blogger Theme by Blogger Template de luxo | Powered by Blogger