
You just purchased a robot from Pololu and use Linux Ubuntu or Debian as the operating system, then this article is for you. First of all, the article is intended for beginners who decide to try themselves in robotics.
To begin with, they brought me a similar robot, or rather Pololu 3pi, and I began to study what kind of animal it is. From the store, a demo program was stitched into it, with which it was possible to check all the functions of the device. And he knew how to flash LEDs, rotate engines, play a classic melody through his buzzer, display text on a two-line display of 8 characters per line and finally recognize black areas under the sensors on the bottom of the device (for example, black tracks on a white background). which can move).
After studying the functionality of the device, the following thought arose: "But let's write our program into it." And then the next question was how to do it on Linux Ubuntu. The official site
www.pololu.com , foreign forums and the study of the sources of downloaded libraries helped a lot in this issue.
Now briefly tell you how and what to do to write your program to this device. Immediately I will clarify that for programming Pololu, besides the device itself, a special programmer with a USB interface is needed to connect to a computer. I used PGM03A - a wonderful and inexpensive programmer for microcontrollers based on AVR, including our ATMega328P.
We will write programs in C, but you can also write in C ++, for this you need to use the appropriate compiler and libraries. To begin with, we put the compiler, additional libraries and utilities using the “sudo apt-get install”:
gcc-avr - source code compiler in the machine code of AVR controllers, if you are writing in C ++ you need to install the appropriate compiler and libraries;
avr-libs - AVR libraries for the C language;
avra - the assembler, is necessary as a part of AVR-GCC for receiving the assembler code;
binutils-avr - additional utilities for building programs;
avrdude is a utility for recording the assembled binary hex file through the programmer in the AVR controllers.
')
These are all the libraries that are needed to build and write binary hex files to the AVR controller. Now you need to collect and write to the controller the simplest program.
For example, consider the following code written in C, it will cycle on and off the red Pololu 3 pi LED. The example was taken from the official site
www.pololu.com and will work correctly only with an ATMega328P microcontroller.
#define F_CPU 20000000UL // Baby Orangutan frequency (20MHz)
#include
#include
void delayms( uint16_t millis ) {
while ( millis ) {
_delay_ms( 1 );
millis--;
}
}
int main( void ) {
DDRD |= 1 << DDD1; // set LED pin PD1 to output
while ( 1 ) {
PORTD &= ~( 1 << PORTD1 ); // LED off
delayms( 900 ); // delay 900 ms
PORTD |= 1 << PORTD1; // LED on
delayms( 100 ); // delay 100 ms
}
return 0;
}
This code snippet needs to be placed in the BlinkLed.c file. The file extension "* .c" says that we have the source code written in the C language. Now we compile the file into machine code, for this we go to the working directory with the BlinkLed.c file and execute the following command:
$ avr-gcc -g -Wall -mcall-prologues -mmcu=atmega328p -Os -c -o BlinkLed.o BlinkLed.c
And at the output we get the file BlinkLed.o with the object code. An object code is an executable code with some interspersions of additional information for the linker, for example, a symbol table. At this step, your program has already been assembled, it remains to combine it with other object modules of the linker (if you use them) and collect a binary file with the extension * .hex, the data of which we will write to the microprocessor. In the console, you need to run the following commands:
$ avr-gcc -g -Wall -mcall-prologues -mmcu=atmega328p -Os BlinkLed.o -Wl,-gc-sections -lpololu_atmega328p -Wl,-relax -o BlinkLed.obj
$ avr-objcopy -R .eeprom -O ihex BlinkLed.obj BlinkLed.hex
A file BlinkLed.hex appeared in the directory with commands for the ATMega328P microcontroller, which now need to be flashed into it. But first you need to know in which device to send commands. To do this, we need to connect a programmer to the computer and find new devices in the / dev directory. In my case, 2 new devices appeared: / dev / ttyACM0 and / dev / ttyACM1. Now we connect the microcontroller to the programmer and execute the command for the corresponding device (the device must be turned on and the batteries must be enough for the duration of the firmware process):
$ avrdude -c avrispv2 -p m328p -P /dev/ttyACM0 -e
.
avrdude: AVR device initialized and ready to accept instructions
Reading | |||||||||||||||||||||||||||||||||||||||||||||| | 100% 0.01s
avrdude: Device signature = 0x1e654f
avrdude: erasing chip
avrdude: safemode: Fuses OK
avrdude done. Thank you.
If you saw the response from the device, then the connection test actually passed and now you can safely flash our prepared instructions. Perform in the console:
$ avrdude -p m328p -c avrisp2 -P /dev/ttyACM0 -U flash:w:BlinkLed.hex
After a few seconds, the instructions in the device will be successfully flashed, the controller will automatically restart and you will see how Pololu 3pi actively winks at you with its LED. To automate the process of building an application, you can write a Makefile.
We learned how to flash the microcontroller from the source code written in C, now for more comfortable work with the device, you need to connect the Pololu library, which already has low-level operations for controlling the components of the robot. We download the latest version of the library from the official website
www.pololu.com , at the time of this writing, the library is available at
www.pololu.com/docs/0J20/3 .
$ wget www.pololu.com/file/download/libpololu-avr-100607.zip?file_id=0J381
Unpack:
$ unzip libpololu-avr-yymmdd.zip, «yymmdd» .
Go to the root directory with the unpacked library and execute the "make" command to build the packages. After assembly, it remains to put it, but then a couple of surprises awaited me. First you need to open the file "Makefile", find 2 lines:
# LIB := /usr/lib/avr/lib
# INCLUDE_POLOLU := /usr/lib/avr/include
And uncomment them, check the existence of directories on your system and change them if necessary. After that, you can run the “sudo make install” command to install the library. At the same time, packages with the extension “* .a” for different versions of microcontrollers should be copied to “/ usr / lib / avr / lib” and to the “/ usr / lib / avr / include” header files “popolu / *. H” for connection required library functionality.
There was a second surprise waiting for me, the header files were copied to the directory specified in “NCLUDE_POLOLU”, that is, to “/ usr / lib / avr / include”, although they are searched in the directory “/ usr / lib / avr / include / pololu”. The problem was solved by creating the “/ usr / lib / avr / include / pololu” directory and re-copying all the “pololu / *. H” files into it.
Finally, we will write another small program to secure the material, using the Pololu library. To do this, we will connect the library at the beginning of the program file by writing such a line "#include <pololu / 3pi.h>". Below I place the code with comments, which must be placed in the file "MyRobot.c". As you can see, the code turned out to be less, but we used much more functionality:
#include <pololu/3pi.h> // Pololu
#include <stdlib.h> //
int main()
{
print("Hello :)"); //
lcd_goto_xy(0,1); //
int val;
while(1)
{
green_led(0); //
red_led(0); //
val = random()%3; // {0,1,2}
if(val == 0) {
green_led(1); //
red_led(1); //
}
delay_ms(1000); // 1
}
}
To automatically build and flash the program, we will create a special Makefile in which you need to place the following text, all indents should be saved as 1 tab from the beginning of the line:
DEVICE = atmega328p
AVRDUDE_DEVICE = m328p
CFLAGS = -g -Wall -mcall-prologues -mmcu = $ (DEVICE) -Os
CC = avr-gcc
OBJ2HEX = avr-objcopy
LDFLAGS = -Wl, -gc-sections -lpololu _ $ (DEVICE) -Wl, -relax
PORT = / dev / ttyACM0
AVRDUDE = avrdude
TARGET = MyRobot
all: $ (TARGET) .hex
clean:
rm -f * .o * .hex * .obj * .hex
% .hex:% .obj
$ (OBJ2HEX) -R .eeprom -O ihex $ <$ @
% .obj:% .o
$ (CC) $ (CFLAGS) $ <$ (LDFLAGS) -o $ @
install: $ (TARGET) .hex
$ (AVRDUDE) -p $ (AVRDUDE_DEVICE) -c avrisp2 -P $ (PORT) -U flash: w: $ (TARGET) .hex
Now, to build the program, just type the “make” command, and for flashing it to the device, respectively, “make install”.
It is time to go through numerous examples that you find in the unpacked library files, and already compiled hex files are in the same place, if you suddenly have problems with the compiler. As an IDE for writing C source code, I use NetBeans with the C / C ++ plugin installed.
Thanks for attention. I would be grateful if you report inaccuracies and errors in the article.