Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Help understanding classes in Arduino with library MiniMaestro

Hello,

I was experimenting with the MiniMaestro Pololu Library for Arduino.

Everything worked very perfect and the servos are doing what they are supposed to do.

Now, I am working to create some structure for my code in which it will be more simple to read, for instance, I intend to create some classes to activate different parts of my robot and of course I would like to include the amazing Pololu library MiniMaestro for this purpose.

Before pasting my code I wanted to specify my question. Usually what I learned from libraries working in different parts of code, is required to add #ifndef or #ifdef depending what we want to compile first since this causes conflicts when the same class or library when is declared twice. In this case the line which contains #include <PololuMaestro.h> is declared twice in my *.ino and *.h . I do not get any error when I compile which is good but I do not fully understand if this is due to the nature more “global” of this library, compare to my class initial_position.h

I am very new in programming and any orientation to a proper lecture to understand in deep the basics would be super helpful, my apologies if I am not clear and I would be glad to provide more details.

main.ino

> #include <PololuMaestro.h>
> 
> #ifdef SERIAL_PORT_HARDWARE_OPEN
>   #define maestroSerial SERIAL_PORT_HARDWARE_OPEN
> #else
>   #include <SoftwareSerial.h>
>   SoftwareSerial maestroSerial(0, 1);
> #endif
> 
> MiniMaestro maestro(maestroSerial);
> 
> #include "initial_position.h"
> 
> int CHANNELRA = 0;
> int CHANNELLA = 2;
> int CHANNELRP = 3;
> int CHANNELLP = 1;
> int CSPEED = 0;
> int TARGETMAX = 8000;
> int TARGETMIN = 4000;
> int TARGETMID = 6000;
> 
> initial_position InitialPosition(CHANNELRA, CHANNELLA, CHANNELRP, CHANNELLP, CSPEED, TARGETMAX, TARGETMIN, TARGETMID);
> 
> void setup() {
>   // put your setup code here, to run once:
>   maestroSerial.begin(9600);
>   InitialPosition.ZeroArms();
> 
> 
> }
> 
> void loop() {
> }

initial_position.h

> #include <PololuMaestro.h>
> 
> class initial_position {
> 
>     int ChannelRightArm;
>     int ChannelLeftArm;
>     int ChannelRightPalm;
>     int ChannelLeftPalm;
>     int SpeedC;
>     int TargetMax;
>     int TargetMin;
>     int TargetMid;
> 
>   public:
> 
>     initial_position(int _ChannelRightArm, int _ChannelLeftArm, int _ChannelRightPalm, int _ChannelLeftPalm, int _SpeedC, int _TargetMax, int _TargetMin, int _TargetMid    )
>     {
> 
>       ChannelRightArm = _ChannelRightArm;
>       ChannelLeftArm = _ChannelLeftArm ;
>       ChannelRightPalm = _ChannelRightPalm;
>       ChannelLeftPalm = _ChannelLeftPalm;
>       SpeedC = _SpeedC ;
>       TargetMax = _TargetMax ;
>       TargetMin = _TargetMin ;
>       TargetMid = _TargetMid ;
>     }
> 
>     void ZeroArms() {
> 
>       //robot returns to original position
> 
>       //left palm in channel 1
> 
>       maestro.setTarget(ChannelLeftPalm,  TargetMid);
> 
>       //right palm in channel 3
> 
>       maestro.setTarget(ChannelRightArm,  TargetMid);
> 
>       //right arm in channel 0,
> 
>       maestro.setTarget(ChannelRightArm,  TargetMin);
> 
>       //left arm in channel 2
> 
>       maestro.setTarget(ChannelLeftArm, TargetMax);
>     }
> };

Hello.

The method you’re describing, using #ifndef followed by #define to avoid double inclusion, is better known as #include guard. The #include guard prevents the content of a file from being copied twice by the compiler. Another method to combat double inclusion is using #pragma once, which is used in PololuMaestro.h. Writing #pragma once tells the compiler that the file should be included once in a single compilation (the reason why you’re not getting a compiler issue in this situation). You might find this thread on stackoverflow helpful in understanding where to add header files in your program.

- Amanda

Thank you Amanda!