Society of Robots
Search and Index
MISC
 Parts List
 Robot Forum
 Chat
 Member Pages
 Axon MCU
 Robot Books
 Shop
 Contact

SKILLS
 How To Build
  A Robot
  Tutorial

 Calculators
 Mechanics
 Programming
 Miscellaneous

 Robots
 Space

HARDWARE
 Actuators
 Batteries
 Electronics
 Materials
 Microcontrollers
 Sensors

SCIENCE
 Robot Journals
 Robot Theory
 Conferences


    - THE $50 ROBOT -

    STEP-BY-STEP ROBOT TUTORIAL
    STEP 4: PROGRAM THE ROBOT

    Step by Step Robot Tutorial

    Build a quality robot for under $50 - on your own!

    Programming Your Robot
    In step 4 of the step-by-step robot tutorial we will learn how to program your robot. There are three major steps to programming a robot:

    1) Write the program. Cant program you say? No problem, I've written it already for you! But don't worry you wont get off the hook that easy. I've written the program so that a beginner can edit and change it to how you like. In the future, hopefully others will write source code and share for you to use.

    2) Compile the program. Again, this is as easy as pushing the button labeled 'build'. Seriously. Compiling converts your program into a file that your microcontroller can understand.

    3) Upload the program to your microcontroller. Again, just plug in the programmer, and push the button that says 'program'. Easy, no?



    Installing Software
    But before we can start programming your robot, you need some software.

    Ideally, I would like to give you just one single program to download and everything will work right away. This is the idea of WinAVR (22.8mb), as when you download it (yes, please download and install it), you get everything you need in one go. It will install a bunch of useful programs all in one location, including GNU Compiler Collection (GCC), GNU Binutils, avr-libc, avrdude, avrice, and a few others. Its all seamlessly built in, so I wont even bother you about them. For more information, check out the WinAVR user manual.

    Don't install Programmers Notepad, its useless if you use AVR Studio, and has been know to be buggy in previous versions.

    WinAVR



    The next program I will have you download is an IDE (Integrated Development Environment). Atmel (the producer of the microcontroller you are using) offers this software, AVR Studio, for free. This IDE has a built in editor, compiler, uploader, etc all in one package.

    AVR Studio

    Atmel normally requires you to enter annoying registration info like your email address and phone number, but lucky you I bypassed it to offer you the direct link (suckers!).

    Please download and install in this order:
    1 AVR Studio, version 4.13, build 528 (73.8mb)
    2 AVR Studio 4.13 SP1 (build 557) (37mb)
    3 AVR Studio 4.13 SP2 (build 571) (45mb)

    If that link breaks, or if you don't mind telling Atmel your home address for no reason at all, go here to download the latest AVR Studio software.
    And the untested v4.14 build 589 here (89mb).



    Now that we have installed all the essential software, lets set up AVR Studio. Find the new AVR Studio 4 icon in your start menu, click, and you should immediately see this window come up:

    Select New Project

    Select 'New Project'



    The next window that comes up will look like this:

    Select These Options in AVR Studio

    Copy the options that I have chosen. Make sure there are NO spaces in the file name.

    For example, do not do 'C:\Documents and Settings\User\Desktop . . .'

    Also, the project file name must be the same name as the initial file. There is a 'feature' (bug) in AVR Studio that will cause you problems otherwise.

    After clicking Next, another window should come up:

    Choose the Debug Platform

    Select AVR Simulator, and choose ATmega8 on the right. Click Finish. AVR Studio should now load and look something like this:

    AVR Studio



    SOURCE CODE
    Now you need to download the source code. Currently, I offer just one program for your robot, but plan to come out with others later:

    Photovore v1.zip (March 10th, 2007) - Case-based photovore program

    Source Code

    Empty the contents into the proper folder. For example, for Photovore v1, empty all files into C:\My_Robots\Photovore_1

    Optional: You can also download AVRlib (1.5mb), which is a huge library of free AVR source code to make your life easier. For more information, view the online documentation for AVRlib.



    Now we need to add in the source code that you downloaded for AVR Studio. Right click 'Source Files' and left click 'Add Existing Source File(s)...'.

    Add Source Files

    Locate Photovore_v1.c in your Photovore folder, and add it. You could also just drag and drop. The source code file is the one you would want to edit if you wanted to reprogram what actions your robot takes.

    Just like with the source files, now right click 'Header Files' and left click 'Add Existing Header File(s)...'.

    Locate SoR_Utils.h in your Photovore folder, and add it. This header file is filled with a lot of useful functions I wrote up for you.



    Now we must set up the options to compile your source code. Go to Project -> Configuration Options.

    Go to Configuration Options

    Check the box that says 'Use External Makefile' and then browse to locate the makefile (the file in the downloaded source code labeled 'makefile'). Then push OK.

    Configuration Options



    The final programming step is to compile the code. This converts your code written in C to machine code in a .hex that only your specific microcontroller can read. You should do a Rebuild All the first time you compile the downloaded source code. But after that, if you only make changes to the .c source file, you only need to do the faster Build command.

    Rebuild All

    If all went well, you should see this message after compiling:

    Rebuild All

    If you get errors, and you are new to programming, it may be a little difficult to debug at first.



    Now we need to upload the .hex file you just compiled to the microcontroller via the AVR STK Serial Port Dongle Programmer. To do this, you need to download the newest version of PonyProg2000: v2.06f BETA. For more info on the program, check the PonyProg Documentation.

    PonyProg2000

    After loading up PonyProg2000 again, you want to select 'Open Device File.' This is the beige folder looking button at the top left. Then go to your folder that has all your source code, and select/open the .hex file.

    Open Device File

    You might also need to configure which COM port you have the programmer plugged in to (serial port), as well as select ATmega8 as the microcontroller. My laptop has like 8 different USB ports so I had to try a few before it worked (I'm using a USB to serial adaptor).

    Configure COM Port and AVR

    If you aren't sure which COM port to use, click:
    Start->Settings->Control Panel->System

    A new window will come up called 'System Properties'. Open the Hardware tab and click device manager. You should see this:

    Identify COM Port



    Plug in your AVR STK Serial Port Dongle Programmer to both your computer and your robot if it isn't already from Step 3C. AFTER plugging your robot in, turn on the microcontroller by flipping the ON/OFF switch, or plugging in the battery if it doesn't have one. If you were to unplug the programmer, make sure you turn off your robot first.

    Now select 'Write All' from the drop down menu. A Yes/No window will come up, select Yes. Wait while the program uploads.

    Upload the .hex File

    If you didn't select the correct port to use for your programmer (because you still aren't sure), or you forgot to turn your robot on, you will get this error:

    Port Error Message

    Try plugging in your programmer to a different port of your computer and then push Retry. Try each COM port until it works.

    What the error means is that it is not detecting the programmer for at least one of these reasons:
    A) programmer not powered up (check voltages)
    B) you are telling it to use the wrong port (check device manager)
    C) you have another program running that is using the same port (close other programs)



    update: There is a known problem with PonyProg2000 that it won't work with a USB to serial adaptor. You might also be interested in this forum post,

    After programming, hopefully, your robot now suddenly tries to commit suicide by driving off your table. Your are done! Congratulations!

    Optional: Dont want to risk the problems some are having with PonyProg2000? Want to upload your program to your robot in under 30 seconds? You could buy the much more expensive (~$34) AVR ISP 2 programmer that is supported by Atmel (Digikey part # ATAVRISP2-ND). Its a much better programmer and supports many other Atmel microcontrollers. You also wouldn't need the PonyProg software and instead could directly connect with AVR Studio:

    Connect with AVR ISP

    If you decide to use this programmer, please check out this post concerning pinouts of the programmer.

    update 2009: You can also try the cheaper but less functional Pocket Programmer at sparkfun (see parts list on first page). It uses a 10 pin connection, but most microcontrollers today only come with 6 pin connections (including the Axon). Just keep that in mind. update 2010: Users have reported using the more affordable 6-pin ISP programmer by Pololu successfully. It works fine in AVR Studio, and includes a basic oscope, too.


    Common Compiling Error
    I repeatedly see this question asked in the forum. If you see the below error when you compile, it is because you didn't follow the directions carefully enough. Make sure you name your files and folders properly.

    Photovore_v1.c:108: fatal error: opening dependency file
     .dep/Photovore_v1.o.d: No such file or directory
    compilation terminated.
    make: *** [Photovore_v1.o] Error 1
    Build failed with 1 errors and 0 warnings...
    


    Further Reading, More Source Code, and Upgrades
    To begin your quest for changing my source code, you may be interested in reading my robot programming tutorials. You may also be interested in reading this great post that explains really well on how I programmed the servos. I wish you luck!

    Many of my future robots will now use this microcontroller, so everyone can benefit from source code that I plan to post for future robots. If you would like to document and share your robot and source code, or see what others have done, feel free to check out the member written tutorials.

    Questions?
    Got questions or comments? Post them on the robot forum! Try a forum search on '$50' to read questions that have already been asked before posting your question. I also highly recommend reading this post, as he seems to have found several problems I havent had time to address yet . . .

    Have you finished making your $50 Robot and want to share your experiences? Or want to see what others have done for ideas? Check out this $50 robot forum post.

    The $50 Robot with Sharp IR Rangefinder The $50 Robot with a UART

    Was that too easy? Want to upgrade your $50 robot?
    Go check out how to add sharp IR to your $50 Robot >>>
    Or how to upgrade your $50 Robot microcontroller >>>
    Or how to add UART functionality to your $50 Robot >>>
    Or how to add a bootloader to your $50 Robot >>>



Get Your Ad Here

Has this site helped you with your robot? Give us credit - link back, and help others in the forums!
Society of Robots copyright 2005-2014
forum SMF post simple machines