Library for all 4 motors or use multiple instances

I am working on creating a library for the Dagu 4 Motor Controller with support for included encoders and current sensors.

http://www.robotshop.com/productinfo.aspx?pc=RB-Dag-55&lang=en-US

Is it better to have the library manage all 4 motors and encoders in 1 instance or have it manage 1 set and then create multiple instances for each motor in the sketch?

I plan to be able to have motors at different speeds based on current draw. Also adjust speed to adjust tension on the tracks since this will be used with the rover 5.

Thanks for the help.

Is it better to have the library manage all 4 motors and encoders in 1 instance or have it manage 1 set and then create multiple instances for each motor in the sketch?

Multiple instances. Each instance needs to know about exactly one motor.

You could create a Motor class that knows about one motor, and a Motors class that knows about the 4 Motor instances. The sketch would then interact with the Motors instance, which would deal with the 4 Motor instances.

So a class within a class?

A class that creates 4 instances of 1 motor class.

I think that makes it more complicated than it needs to be.

I am just thinking if I use 1 motor class then how do I account for distance and speed, I guess each motor instance would tracks its own speed and distance when requested.

One more thing can a class attach an interrupt?

One more thing can a class attach an interrupt?

Yes, but…

If the function that the interrupt is to call is a member of a class, the compiler (and the Arduino at run time) has no way of figuring out which instance of the class to call the method for. So, the method that the interrupt handler is to call must be a static method (class level, not instance level).

This means that the interrupt handler method does not have access to any instance’s data because it is not called for an instance. Therefore, you must provide a way for the interrupt handler to know what instance the interrupt is for.

Now, you are probably saying “but I’ll only have one instance…”. Possibly true, but the compiler doesn’t (and can’t) know that.

There is a pattern for a class called a singleton, where the constructor for the class is private, and a “get the instance” method (public and static) is provided that returns the existing instance or creates an instance and then returns the instance. However, that is a design pattern, not any kind of keyword that the compiler could use to generate different code.

Your static interrupt handler could use this pattern to get access to the only instance of the class, or make one come into existence, so that it could have access to the instance’s data and methods.

Not a trivial design pattern to implement, and probably not something that can happen in the time constraints of an interrupt handler, or the memory constraints of the Arduino.

Thanks for the writeup, I did find this

http://arduinoetcetera.blogspot.com/2011/01/classes-within-classes-initialiser.html

Which really helped once I figured it out. Finally got it to work using the Teensy encoder class. So I did a class in a class. I got the encoders counting but 1 of the counts is negative. Not sure why, I would think 2 of them would be negative.