Robotics 2012-11-25

25 November 2012

Alright, so years ago I purchased a micro-controller to make this robot dance, an Atmel ATtiny2313, and I figured out how to program some “hello, world” style applications for it, to include making an LED blink and an attached servo swing back and forth, but time passed, and this knowledge grew faint, and meanwhile some of the parts broke in transport. Today’s exercise was to dust off the relevant cobwebs, physical and mental. Being a practical sort of guy, I originally bought not just a stand-alone micro-controller, but rather a board with the related infrastructure to power it and wire up devices to its ports…

unwired_board

… because a guy can only learn so many things at the same time, and first up was powering the thing, which entails getting somewhere between seven and twelve volts of direct current going into it, a contraption such as the following being required…

finished_soldering_plug

… which looks simple enough, except that the 9V battery hookup and the DC plug come as separate contraptions that need to be soldered together…

soldering_plug

… which entailed evicting the dog from one of his favorite spots…

dog_eating_yogurt

… so that we could eventually end up with the following rig…

testing_wired_up_board

The principal reason that I chose an Atmel micro-controller was the ability to program it with a Linux-based tool chain that used the C programming language, as opposed to the more popular PIC micro-controller for which Windows and Basic (*vomit in mouth*) seem to be required.

Meanwhile, the program transfer mechanism I received uses a printer cable, and I didn’t have a printer-cable-to-USB adapter handy, which left me in the position of using an ancient laptop I had sitting around running Ubuntu, for which I was able to apt-get the packages gcc-avr, avr-libc, and avrdude, and then build programs with…

  • avr-gcc -mmcu=attiny2313 -Os -o <program_name>.out <program_name>.c
  • avr-objcopy -j .text -O ihex <program_name>.out <program_name>.hex
  • avrdude -c pony-stk200 -p t2313 -e -U flash:w:/path/to/<program_name>.hex

… incantations I was able to invoke as quasi-magic because former-Andrew was a swell guy and actually documented the stuff he did years ago and uploaded it to GitHub. Less well documented was the plug via which the programming cable needed to be attached, but fortunately I had the good fortune to have documented this by not unplugging the constituent pieces, the arrangement of which has now been documented in photographic form. Even less well documented was the fact that after running avrdude to install a program the commencement of its execution requires unplugging the programming cable, but somehow I remembered this years later. A good memory has some advantages…


/* blink the LED on the board */
#include <avr/io.h>
#define F_CPU 8000000
#include <util/delay.h>

int sleep() {
  int i = 0;

  for (i = 0; i < 100; ++i)
    _delay_ms(10);

  return 1;
}

int main(void)
{
  // Set Port D pins as all outputs
  DDRD = 0xff;

  while (1) {
    // Set all Port D pins as LOW
    PORTD = 0;

    sleep();

    // Set all Port D pins as HIGH
    PORTD = 0xff;

    sleep();
  }

  return 0;
}
    

Now I need to write the code to coordinate the activation of the three servos and somehow wire up and bolt together the requisite electronic and mechanical infrastructure… The code is what it is… The mechanical and electrical stuff can be resolved somewhat more incrementally, first with a breadboard that can be hand-held as I “chase” after the robot, and then later with something more permanent to render the robot more autonomous… Baby steps…

Leave a Reply