Give your Hexbug a Real Brain!
Chapter 5 - Control Software -
As mentioned earlier, the PIC microcontroller chip was chosen for this project because of the excellent support, availability, and low cost from Microchip. Microchip also provides a free software development package for it called MPLAB. (It’s truly FREE, not just for 30 or 60 days as some companies’ evaluation packages are.) Both the program and user’s guide can be downloaded from their site. You will have to buy the programmer though.
Any one of these tools will be suitable for programming the bug. They are a very good value for the money. (There are also third party suppliers of programmers.)
My personal recommendation is the PICkit-2 Starter Kit. Besides the programmer module and MPLAB software, the kit includes a demo PC board with a PIC16F690 chip, A series of 12 Lessons on assembly programming including source files, and a HI-TECH PICC™ LITE ‘C’ Compiler. With the additional software provided, the programmer module can also be used to analyze serial communications and as a three channel logic analyzer.
More information about MPLAB can be found on the right..
More information about the PICkit-2 can be found on the right.
If you are not familiar with the PIC assembly language and would like to get started, there is a good, free online tutorial, “PIC Assembly Language for the Complete Beginner” by Michael A. Covington at: http://www.covingtoninnovations.com/noppp/picassem2004.pdf
Why write in assembly language? Freedom. Though you have to handle all the details yourself, once they are set up, you can “have it your way”. Basic, C, and other high level languages do take care of a lot of details for you, but in relatively simple projects, there aren’t that many inter-connected details that need it.
Using assembly language with PIC chips is more like ‘writing hardware’. The PIC’s instruction set is made of a few (35) small, fast efficient instructions out of which bigger function can be made. These then can be re-used. It’s a little like creating your own ‘higher level’ project-specific language.
This implementation uses the PICkit-2 programming module. As this is a first/test version, the software for the Bug is very basic, and does not use interrupts so they are disabled in the initialization routine. Future versions may use them- this is just a starting point. The code is broken up into several sections- Initialization, Subroutines, and Behavior.
(If you create a new, or modified program and Bug behavior, please e-mail it and its ‘personality’ description so it can be posted for others to try. If you wish, your contact information will be posted with it)
To conserve power, the A/D converter and other analog functions are disabled. If the ANSEL (Analog-Select) register is not cleared, ports GP0,1,2 & 4 will be in analog input mode, and will read digitally as a ‘0’. The watchdog timer is enabled to provide its maximum timeout of 2.3 seconds. By using the internal oscillator two pins are freed up. It runs at 4 mhz. The master clear pin (MCLR) is assigned to be used as a digital input, and here is a tricky detail that can be easily over looked when also using the ICSP function.( See-
KBSearch/KB_StdProb.aspx?ID=SQ6UJ9A003LD4 on microchip’s website for more details.)
The MCLR pin is used by the PICkit-2 to start the programming cycle. When the chip is programmed for the first time, the MCLR pin will be in the default master clear mode. On the next programming cycle, the previously installed initialization routine reprograms the pin as a digital input before the PICkit-2 programmer can put it into programming mode. The programming cycle will fail, and it might appear that the chip is bad. The only way out of this problem is to erase the whole chip, then reprogram it. You did remember to read and save internal clock’s factory calibration value didn’t you?
There is another way around this problem. A small delay is inserted into the code before the initialization routines are run. This will allow the PICkit-2 to gain control of the MCLR pin before it can be changed.
With the initialization details completed, the next elements needed are the subroutines. Some of these are made from lower level ones. Their details can be found in the assembly code listing, so the following is a simplified list of the major ones, and their functionality:
The details of these subroutines can be found in the assembly code listing.
[Click HERE to down load the basic assembly code .DOC of .ASM file]
[Click HERE to down load the basic assembly code .DOC of .HEX file]
Main Code: Test & Personality-
Now for the main ‘personality’ code- For this version, the first thing the Bug should do is test its systems. These should not start until we want them to, so the first part will be to blink the LED and wait for the antennae to be touched. It will then make some sounds, turn the LED On then Off, run the motors forward, reverse and forward again, turn the LED On then Off again, output its battery voltage level as a series of tones, and go back to blinking the LED and waiting for the antennae to be touched. Here is a sample of the code; ( Note that by having created subroutines first, 23 of the 36 instructions are either a subroutine call, or a ‘MOV’e the Literal value into the ‘W’orking register,”MOVLW” )
The next block of code is the bugs ‘personality’. Look through the code listing for more details. Here is a basic outline:
Start walking, start timer
If Antenna bump-
Stop; Count bumps; after 5th bump, complain
After 10 bumps, throw a ‘tantrum’-
Move back and forth with sounds;
Sleep for 5 seconds
If no bumps occur for 30sec, celebrate-
Reverse circle with sounds
Sleep for 10 seconds
Wake; reset counters;
Go back to Main Loop-
Some simple additions coming in the near future:
1) Monitor the battery voltage and have the Bug ‘cry’ when its batteries get low.
2) Have the bug respond to light levels- The PIC chip could be put into lowest power ‘Sleep’ mode and would be awakened by the Watch Dog Timer (WDT). If the light level was high enough, the bug would start wandering around. When the light level dropped, the bug could go back into ‘Sleep’ mode.
1]The micro-robotic Hexbug is the result of collaboration between Ignition (Plano, TX; ), an industrial design and development firm, and Innovation First Inc. (Greenville, TX; ), a product engineering company. RadioShack is the exclusive North American retailer of the Hexbug. Bandai, the leading toy manufacturer in Japan, also recently signed a deal for global distribution rights, with plans for other products to follow. “We had a rough idea for this product, but Ignition brought it to life,” says Joel Carter, VP of marketing at Innovation First. “They transformed our concept into a viable market-ready product and helped us to create an entirely new product category.”