Trying to program BabyO in C++

Hi,

I’m trying to code in C++ as I don’t really know C. I think I am all mixed up with the libraries with AVR Studio 4. I tried the following code but am getting all kinds of errors. What am I doing wrong? Thanks,

#include <pololu/orangutan>
//#include <OrangutanLEDs.h>
//#include <OrangutanAnalog.h>
//#include <OrangutanMotors.h>

// This is a C++ trial with motors and sensors

//OrangutanAnalog analog;
//OrangutanLEDs leds;
//OrangutanMotors motors;


void loop ()
{
  //note the following line could also be accomplished with:
  // int pot = analog.read(7);
  int pot = analog.readTrimpot();  // determine trimpot position
  int motorSpeed = pot/2-256;  // turn pot reading into number between -256 and 255
  if (motorSpeed == -256)
    motorSpeed = -255;  // 256 is out of range
  motors.setSpeed (motorSpeed, motorSpeed);

  int ledDelay = motorSpeed;
  if (ledDelay < 0)
    ledDelay = -ledDelay; // make delay a non-negative number
  ledDelay = 256-ledDelay;

  leds.red(HIGH);  // turn red LED on
  delay(ledDelay);  

  leds.red(LOW);  // turn red LED off
  delay(ledDelay);
}

Errors:

Build started 21.3.2012 at 19:03:06
avr-gcc  -mmcu=atmega328p -Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT test.o -MF dep/test.o.d  -c  ../test.c
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:1,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanAnalog/OrangutanAnalog.h:57: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanAnalog'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:3,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:28: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanTime'
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'get_ms':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:97: error: 'OrangutanTime' undeclared (first use in this function)
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:97: error: (Each undeclared identifier is reported only once
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:97: error: for each function it appears in.)
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:97: error: expected ';' before ':' token
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'millis':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:98: error: 'OrangutanTime' undeclared (first use in this function)
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:98: error: expected ';' before ':' token
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'delay':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:100: error: expected expression before ':' token
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'delay_ms':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:101: error: expected expression before ':' token
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'delay_us':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:102: error: expected expression before ':' token
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h: In function 'delayMicroseconds':
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanTime/OrangutanTime.h:103: error: expected expression before ':' token
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:4,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanMotors/OrangutanMotors.h: At top level:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanMotors/OrangutanMotors.h:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanMotors'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:5,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanLCD/OrangutanLCD.h:166: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanLCD'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanLEDs/OrangutanLEDs.h:27,
                 from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:6,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanLEDs/../OrangutanDigital/OrangutanDigital.h:131: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanDigital'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:6,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanLEDs/OrangutanLEDs.h:47: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanLEDs'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:8,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/PololuQTRSensors/PololuQTRSensors.h:48: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PololuQTRSensors'
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/PololuQTRSensors/PololuQTRSensors.h:167: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PololuQTRSensorsRC'
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/PololuQTRSensors/PololuQTRSensors.h:254: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PololuQTRSensorsAnalog'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:9,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/PololuWheelEncoders/PololuWheelEncoders.h:26: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PololuWheelEncoders'
In file included from c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:13,
                 from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/OrangutanServos/OrangutanServos.h:51: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'OrangutanServos'
In file included from ../test.c:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:16:45: error: pololu/OrangutanSVP/OrangutanX2.h: No such file or directory
../test.c: In function 'loop':
../test.c:17: error: 'analog' undeclared (first use in this function)
../test.c:21: error: 'motors' undeclared (first use in this function)
../test.c:28: error: 'leds' undeclared (first use in this function)
make: *** [test.o] Error 1
Build failed with 25 errors and 0 warnings...

Hello.

I see you are writing C++ code in test.c. When you’re writing C++ code, the name of your file needs to end with “.cpp”, otherwise AVR Studio will use the C compiler, avr-gcc. The C++ compiler is avr-g++.

–David

OK, that makes sense. I switched the compiler & file name, but do I need to change the “make” file too? I’m getting the following error:

Build started 22.3.2012 at 20:31:16
make: *** No rule to make target `../test.c', needed by `test.o'.  Stop.
Build failed with 1 errors and 0 warnings...

Thanks.

It sounds like you simply renamed the file on your disk but didn’t reconfigure AVR Studio 4. It’s still looking for test.c and not finding it. You should go to the “AVR GCC” pane of AVR Studio, remove test.c, and add test.cpp.

–David

OK, perfect, thanks for your patience. I think I am really close. I’m getting this error now:

Build started 23.3.2012 at 19:44:07
avr-g++  -mmcu=atmega328p -Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT test.o -MF dep/test.o.d  -c  ../test.cpp
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
In file included from ../test.cpp:1:
c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:16:45: error: pololu/OrangutanSVP/OrangutanX2.h: No such file or directory
make: *** [test.o] Error 1
Build failed with 1 errors and 1 warnings...

PS. Here’s the code, but it’s basically the same as before:

#include <pololu/orangutan>
//#include <OrangutanLEDs.h>
//#include <OrangutanAnalog.h>
//#include <OrangutanMotors.h>

// This is a C++ trial with motors and sensors

OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;


void loop ()
{
  //note the following line could also be accomplished with:
  // int pot = analog.read(7);
  int pot = analog.readTrimpot();  // determine trimpot position
  int motorSpeed = pot/2-256;  // turn pot reading into number between -256 and 255
  if (motorSpeed == -256)
    motorSpeed = -255;  // 256 is out of range
  motors.setSpeeds (motorSpeed, motorSpeed);

  int ledDelay = motorSpeed;
  if (ledDelay < 0)
    ledDelay = -ledDelay; // make delay a non-negative number
  ledDelay = 256-ledDelay;

  leds.red(HIGH);  // turn red LED on
  delay(ledDelay);  

  leds.red(LOW);  // turn red LED off
  delay(ledDelay);
}

Thanks, hope you had a great weekend!

c:/winavr-20100110/lib/gcc/../../avr/include/pololu/orangutan:16:45: error: pololu/OrangutanSVP/OrangutanX2.h: No such file or directory

Sorry, there is a typo in the file pololu/orangutan. It will be fixed in the next release of the library. For now, if you’re not using an Orangutan SVP or Orangutan X2, you can just remove the indicated line in that file.
–David

OK I removed it since I am not using an SVP or X2. Now I get the following…

Build started 24.3.2012 at 17:11:09
avr-g++  -mmcu=atmega328p -Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT test.o -MF dep/test.o.d  -c  ../test.cpp
cc1plus.exe: warning: command line option "-std=gnu99" is valid for C/ObjC but not for C++
avr-g++ -mmcu=atmega328p -Wl,-gc-sections -Wl,-Map=test.map test.o    -lpololu_atmega328p  -o test.elf
c:/winavr-20100110/bin/../lib/gcc/avr/4.3.3/../../../../avr/lib/avr5/crtm328p.o:(.init9+0x0): undefined reference to `main'
make: *** [test.elf] Error 1
Build failed with 1 errors and 1 warnings...

In almost every C++ environment except Arduino you need to define a main() function. You can just add this code to the bottom of your source file:

int main()
{
  // setup();
  while(1)
  {
    loop();
  }
}

Ooops! I guess I was in Arduino land too long. :blush: I’m trying to self-teach C++ too so I forget basic stuff like that.

Success! Thanks so much for your patience and help. Without guys like you, weekend warriors like me would never have a chance. Thanks for posting over the weekend too; that was cool.

1 Like