Libraries in Libraries?

Hey All,

I’m writing some software to make my Arduino control a 3 axis cartesian bot (think CNC machine). Anyway, here’s the basic setup that is causing me problems:

I have 3 classes:

  1. LinearAxis
  2. LimitSwitch
  3. RepStepper (my version of the stepper class)

i want the LinearAxis class to have a RepStepper instance and two LimitSwitch instances. I’m getting this error:

LinearAxis.h:18: error: ‘RepStepper’ does not name a type
LinearAxis.h:20: error: ‘LimitSwitch’ does not name a type
LinearAxis.h:21: error: ‘LimitSwitch’ does not name a type

I’m somewhat inexperienced when it comes to C++, but I have tons of experience programming PHP and OO design, so i’m guessing its some sort of syntactical thing I’m missing.

the code is all located here: http://reprap.svn.sourceforge.net/viewvc/reprap/trunk/users/hoeken/arduino/

the sketch is located here: http://reprap.svn.sourceforge.net/viewvc/reprap/trunk/users/hoeken/arduino/firmware/HoekensCustom/

thanks in advance for your help.

~Zach

You need the right #include's within the library (i.e. LinearAxis.h should include RepStepper.h), and you need to have #include's for all the libraries in your sketch.

yay! that worked.

now i have another problem:

In function 'void setup()': error: request for member 'x' in 'bot', which is of non-class type 'CartesianBot ()()'

here's the code i'm trying to do:

//init our steppers
      bot.x.stepper = RepStepper(X_DIR_PIN, X_STEP_PIN);
      bot.y.stepper = RepStepper(Y_DIR_PIN, Y_STEP_PIN);
      bot.z.stepper = RepStepper(Z_DIR_PIN, Z_STEP_PIN);

basically, i have a CartesianBot class that contains 3 LinearAxis instances which each contain a RepStepper and 2 LimitSwitch instances. Any ideas on how I would go about that so I can initialize them at runtime?

Now you're getting to the fun parts of C++. It gets a little strange, but not too bad. You need to do something like this:

In LinearAxis.h:

class LinearAxis { public: LinearAxis(int dir_pin, int step_pin, int min_pin, int max_pin); private: RepStepper stepper; LimitSwitch min_switch, max_switch; };

In LinearAxis.cpp:

LinearAxis::LinearAxis(int dir_pin, int step_pin, int min_pin, int max_pin) : stepper(dir_pin, step_pin), min_switch(min_pin), max_switch(max_pin) { // any other initialization }

Basically, constructors can't be called in the middle of code, but only when you're declaring a variable or in the weird syntax above where they hang off the end of another constructor.

I just wrote a simple introduction to writing Arduino libraries, I think this could be a good topic for the advanced tutorial. :)

okay, i think i get it... so basically whats happening is that with that funky syntax, when i do something in my main sketch like this:

LinearAxis x(dir_pin, step_pin, min_pin, max_pin);

it would then internally call the LinearAxis constructor, as well as the stepper, min/max switch constructors, etc? i'm gonna give that a try! i'll report back here when/if i can get it working!

thanks mellis!

Exactly. I hope it works. Let us know if you have any problems.

yup, that worked!

now, the only thing that i need to figure out is how to do the microsecond resolution for my steppers =)