Go Down

Topic: Programming Multiple Motor Drivers (Sparkfun Monster Moto shields) (Read 859 times) previous topic - next topic

patplanet

Hello,
I recently purchased 3 dual DC motor controllers from Sparkfun (Monster Moto Shields) to control 6 DC motors. I was able to successfully program 1 motor controller to control 1 or 2 DC motors using Sparkfun's example code (SEE BELOW), however, I would like to control 3 motor drivers (ultimately 6 DC motors) simultaneously. How can I adapt the code to accomodate the 3 monster moto shields?

Sparkfun's example code:
Code: [Select]
/*  MonsterMoto Shield Example Sketch
  date: 5/24/11
  code by: Jim Lindblom
  hardware by: Nate Bernstein
  SparkFun Electronics
 
 License: CC-SA 3.0, feel free to use this code however you'd like.
 Please improve upon it! Let me know how you've made it better.
 
 This is really simple example code to get you some basic
 functionality with the MonsterMoto Shield. The MonsterMote uses
 two VNH2SP30 high-current full-bridge motor drivers.
 
 Use the motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
 function to get motors going in either CW, CCW, BRAKEVCC, or
 BRAKEGND. Use motorOff(int motor) to turn a specific motor off.
 
 The motor variable in each function should be either a 0 or a 1.
 pwm in the motorGo function should be a value between 0 and 255.
 */
#define BRAKEVCC 0
#define CW   1
#define CCW  2
#define BRAKEGND 3
#define CS_THRESHOLD 100

/*  VNH2SP30 pin definitions
 xxx[0] controls '1' outputs
 xxx[1] controls '2' outputs */
int inApin[2] = {7, 4};  // INA: Clockwise input
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input
int pwmpin[2] = {5, 6}; // PWM input
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin)

int statpin = 13;

void setup()
{
  Serial.begin(9600);
 
  pinMode(statpin, OUTPUT);

  // Initialize digital pins as outputs
  for (int i=0; i<2; i++)
  {
    pinMode(inApin[i], OUTPUT);
    pinMode(inBpin[i], OUTPUT);
    pinMode(pwmpin[i], OUTPUT);
  }
  // Initialize braked
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  // motorGo(0, CW, 1023);
  // motorGo(1, CCW, 1023);
}

void loop()
{
  motorGo(0, CW, 128);
  motorGo(1, CCW, 128);
  delay(1500);

  motorGo(0, CCW, 1023);
  motorGo(1, CW, 1023);
  delay(750);
 
  if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD))
    digitalWrite(statpin, HIGH);
}

void motorOff(int motor)
{
  // Initialize braked
  for (int i=0; i<2; i++)
  {
    digitalWrite(inApin[i], LOW);
    digitalWrite(inBpin[i], LOW);
  }
  analogWrite(pwmpin[motor], 0);
}

/* motorGo() will set a motor going in a specific direction
 the motor will continue going in that direction, at that speed
 until told to do otherwise.
 
 motor: this should be either 0 or 1, will selet which of the two
 motors to be controlled
 
 direct: Should be between 0 and 3, with the following result
 0: Brake to VCC
 1: Clockwise
 2: CounterClockwise
 3: Brake to GND
 
 pwm: should be a value between ? and 1023, higher the number, the faster
 it'll go
 */
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)
{
  if (motor <= 1)
  {
    if (direct <=4)
    {
      // Set inA[motor]
      if (direct <=1)
        digitalWrite(inApin[motor], HIGH);
      else
        digitalWrite(inApin[motor], LOW);

      // Set inB[motor]
      if ((direct==0)||(direct==2))
        digitalWrite(inBpin[motor], HIGH);
      else
        digitalWrite(inBpin[motor], LOW);
     
 analogWrite(pwmpin[motor], pwm); 
    }
  }
}


willsor

Did you ever figure this out? Im interested in the same idea, although I'm wanting to control 2 steppers from 2 Sparkfun Monster Moto's.

Cheers

MorganS

Steppers? What steppers?

The Monster shield is unsuitable for steppers.
"The problem is in the code you didn't post."

3Dsim

I have similar problem in our current study. We must be able to control 5 dc motors using 3 monster moto shields. Did you ever figure this out? I don't know what to do about this problem. If yes, would you share the solution? Thank you very much

MorganS

3Dsim, it is impossible to answer your question properly without more information.

It should be easy to screw 3 of those shields to a plank of wood and wire them up. Power wiring will be important since you obviously have a lot of high current motors.

If you only have small motors then the Adafruit motor shield is stackable.

Each motor on the monster moto requires 3 pins to control it. One of these must be a PWM pin. Plus 1 analog pin for current feedback if you need that feature. That is 15-20 pins. More than an UNO but you probably already know that.
"The problem is in the code you didn't post."

3Dsim

MorganS, hi. We have been using a dual Monster Moto Shield Driver together with an Arduino Uno to control two DC motors. Then another Monster Moto Shield driver and an Arduino Uno to control the other two dc motors. And we also did the same for the last dc motor. So, there are three monster moto shield and three arduino unos to control all the dc motors. Our problem is that we don't know how to control the three motor drivers simultaneously.

MorganS

It is best to get all the shields controlled by one Arduino.

If you can combine or hardwire some pins then maybe an Uno can do it. For example, maybe Motor2 always drives forward but you need to control its speed: then you wire A2 to 5V, B2 to ground and connect only the PWM pin to the Arduino.

If you really need all 20 control and sense pins then upgrade to a Mega or a Teensy 3.6.

Even if you want to use 3 Arduinos controlled by a central "master" the Uno is a poor choice because it only has one hardware Serial.
"The problem is in the code you didn't post."

Go Up