Program a 3pi robot in C++

Hello. Yes, Atmel Studio supports C++ and there is nothing stopping you from using it on our programmable AVR products. Unfortunately, it is not easy to adapt our templates to C++ so you will have to start from scratch and configure Atmel Studio yourself:

  1. Create a new project in Atmel Studio and select the “GCC C++ Executable Project” template provided by Atmel. This can be found in the “C/C++” template category.
  2. In the Device Selection dialog, choose the model of AVR you are trying to program. For a recently-made 3pi, this would be the ATmega328P.
  3. Select Project > Properties… to open up the project properties window so you can properly configure your project to use the AVR libraries.
  4. Under “AVR/GNU Linker > Libraries”, click the little green icon to add the right library for your device. For ATmega328P-based devices like the 3pi, you would type “pololu_atmega328p”.
  5. Under “AVR/GNU C++ Compiler > Optimization”, check “Prepare functions for garbage collection (-ffunction-sections)”.
  6. Under “AVR/GNU Linker > Optimization”, check “Garbage collect unused sections (-Wl,–gc-sections).”

For more information about what these settings are and why you need them, see the “Using the Pololu AVR Library for your own projects” section of the Pololu AVR C/C++ Library User’s Guide.

Here is some fun code to get you started:

#include <pololu/orangutan.h>

class Output
{
  public:
  uint8_t pin;	
  Output(uint8_t pin) : pin(pin) { }
  void operator<<(uint8_t value){ set_digital_output(pin, value); }	
};	

Output led(IO_D1);

int main(void)
{
  while(1)
  {
    led << 1;
    delay_ms(300);
    led << 0;
    delay_ms(300);		
  }
}

–David