Controller more tha...
 
Notifications
Clear all

Controller more than one stepper motor with the same controller

18 Posts
7 Users
9 Likes
1,189 Views
DaveE
(@davee)
Prominent Member
Joined: 2 years ago
Posts: 788
 

Hi @frogandtoad,

   No need to apologise ... you pointed out a possible alternative hardware approach, i.e. servos ... which may or may not be useful in this case.

As for the software aspects, to a large extent I was leaving that for "sermon 2, the sequel", which others like yourself and @will are better placed to administer, but it clearly depends upon hardware choices ... and also "sermon 1" was long enough! Of course an object orientated approach could well ameliorate a few headaches.

I've never really played with servos, but I assumed they were generally kept 'powered on' during operation, even if they only occasionally moved intentionally, so that they were always maintained the position. This means the 'position reference voltage' or equivalent must be available at all times for every motor. I haven't come across a servo motor that is 'smart' enough to remember the value, but they might exist.

I acknowledged that with either stepper or servo motors, if the loading force is smaller than the mechanical friction, then it may be possible to power off the majority of the motors, but that is clearly not true in all cases, and generally feels like a slippery slope to frustration, if it 'nearly, but not always' works.

Best wishes to all, Dave


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Noble Member
Joined: 4 years ago
Posts: 1506
 

@inst-tech

Posted by: @inst-tech

@frogandtoad  Indeed, software implementation might be easier than using hardware logic to multiplex a servo control signal as the highest number of analog multiplexers I've seen is about 1:16 .. a far cry from the 54 lines of control @ed has spec'ed out. I appears to me that you could set up a class, and call it Servo as an array, and just detach all the servos except the the one you want to control, and attach it..Could that be done? and if so.. how would one go about setting that code up. Could you possibly write a simple code to illustrate this function?

kind regards,

LouisR

Yes, I believe it could be done, at least from a software administration point of view. Given that I thought the original question was for a set of servo motors, and you've asked the question, the following is *one approach* that we could use to do it:

#include <Servo.h>

struct Station : public Servo
 {
  Station(String name) : stationName(name) 
    {} 

  String stationName;
 };

 struct StationController
  {
   String activeStation;
   static const int MAX_STATIONS = 3; // As many as memory will allow

   Station stations[MAX_STATIONS] = {Station("shed"), Station("garage"), Station("outhouse")};

   // Automatically detaches previously attached station
   void atachStation(String station) {
     detachStation();

     for(int index(0); index < MAX_STATIONS; index++) {
       if(station.equals(stations[index].stationName)) {
         stations[index].attach(index);
         activeStation = stations[index].stationName;
        }
      } 
    }

   void detachStation() {
     for(int index(0); index < MAX_STATIONS; index++) {
       if(stations[index].attached()) {
         stations[index].detach();
        }
      } 
    }

    /* add functions as required */
  };

void setup() {
  Serial.begin(9600);

  StationController controller;

  controller.atachStation("outhouse");
  Serial.println(controller.activeStation);

  controller.atachStation("garage");
  Serial.println(controller.activeStation);
 }

void loop() {
  // ...
 }

In a nutshell, I've just created an improved servo object by inheriting all of its original functionality, and encapsulating an identifier (a name in this example), within it, so we can query it during runtime to make our decisions.

I used a helper class to encapsulate the updated servo object, to provide the workflow that is a little more appealing in the setup() or loop(), depending on how you intend to use it.  I would normally wack both classes in a header and implementation file (basically turning the code into a library), allowing for a clean main file where we can just focus on logic.

Again, this is just one version, and the first one that popped into my head - We could make a number of improvements (dynamically add to the array, etc...), but this will hopefully get the point across.

We could approach steppers in a similar fashion, but thinking about it again, it may well require not only shift registers, but also a multiplexer - I know Bill created a video on a multiplexer a while back which might interest the OP, as might the I2C videos Bill created regarding making your own I2C devices.

Please feel free to ask any questions about the code.

Cheers


   
Inst-Tech reacted
ReplyQuote
Inst-Tech
(@inst-tech)
Reputable Member
Joined: 1 year ago
Posts: 363
 

@frogandtoad  I'll try it out on my TinkerCad simulator and see how it works..I'll report back with a fritzing, and schematic drw of how I put it together. This maybe very useful for future projects, but it's certainly great for my understanding of how classes work, and how to think about them..Thanks for your help in getting me started..my goal is to be a better programmer, so my hardware projects will be both efficient and workable.

Regards,

LouisR 

LouisR


   
frogandtoad reacted
ReplyQuote
Page 2 / 2