MDDS30

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

MDDS30

Postby mscholl » Wed Feb 07, 2018 4:41 am

Greetings! I am hoping to get my MDDS30 tomorrow and have a few questions. I will be setting it up in two different applications.
The first will entail using a digital joystick, arduino, and potentiometer to control 2 motors. I am assuming the program below will work for this application, but need to make sure my dip switches are set appropriately. Can you please tell me where they should be set? Also, I will be connecting directly to the arduino, not thru a base shield. Do I attach both "NC" to VIN and both GRD to the grounds, or just one?
The code would be as below...

CODE: SELECT_ALL_CODE
int sw1=10;
int sw2=11;
int sw3=12;

int sw4=6;
int state1;
int state2;
int state3;
int state4;

//MOTOR A
int pwm1 = 9;
int dir1 = 8;

// Motor B
int pwm2 = 3;
int dir2 = 5;

// Speed control potentiometers
int SpeedControll = A0; 
 
// Motor Speed Values - Start at zero
int MotorSpeed1 = 0;

void setup()
{
  pinMode(sw1,INPUT_PULLUP);
  pinMode(sw2,INPUT_PULLUP);
  pinMode(sw3,INPUT_PULLUP);
  pinMode(sw4,INPUT_PULLUP);

  Serial.begin(9600);

  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(dir1, OUTPUT);
  pinMode(dir2, OUTPUT);
}

void loop()
{
  state1 = digitalRead(sw1);
  state2 = digitalRead(sw2);
  state3 = digitalRead(sw3);
  state4 = digitalRead(sw4);

  // Read the values from the potentiometers
  MotorSpeed1 = analogRead (SpeedControll);
 
  // Convert to range of 0-255
  MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 200);

  if (state1 == 0) { //Make a Right turn
    analogWrite(pwm2, 120);
    analogWrite(pwm1, 120);
    digitalWrite(dir2, HIGH);
    digitalWrite(dir1, LOW);
  }
  else if (state2 == 0) { //Forward Movement
    analogWrite(pwm1, MotorSpeed1);
    analogWrite(pwm2, MotorSpeed1);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, HIGH);
  }
  else if (state3 == 0) { //Take a left turn
    analogWrite(pwm1, 120);
    analogWrite(pwm2,120);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
  }
  else if (state4 == 0) { //Backward Movement
    analogWrite(pwm1, MotorSpeed1);
    analogWrite(pwm2, MotorSpeed1);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
  }
  else {
    analogWrite(pwm1, 0);
    analogWrite(pwm2, 0);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
  }

  Serial.print("1-");
  Serial.print(state1);
  Serial.print(" 2-");
  Serial.print(state2);
  Serial.print(" 3-");
  Serial.print(state3);
  Serial.print(" 4-");
  Serial.println(state4);
  delay(250);
}

My second application will be using an analog joystick, with a potentiometer (to further control the speed), with the driver, arduino and 2 motors. I have previously used a different brand driver using the serial packetized mode, but need to know if I can use the same program, or need to modify it for the MDDS30. Once again, I need to know the position of the dip switches. I am running the motors way below their max speed. The code is below....

CODE: SELECT_ALL_CODE
SoftwareSerial SWSerial(NOT_A_PIN, 11); // RX on no pin (unused), TX on pin 11 (to S1).
Sabertooth ST(128, SWSerial); // Address 128, and use SWSerial as the serial port.
int sensorPin = A0; 
int S2= A1;
int sensorValue = 0;
int sensorvalue2 =0;
int mapedsen2 =0;
int mapedsen1=0;

void setup()
{
  SWSerial.begin(9600);
  Serial.begin(9600);
  ST.autobaud();
}

void loop()
{
  int SPEED_POT = map(analogRead(A3), 0, 1023, 10, 50);
  //Serial.println(SPEED_POT);
  sensorValue = analogRead(sensorPin);
  sensorvalue2= analogRead(S2);

  mapedsen1 = map(sensorValue,0,1023,-SPEED_POT,SPEED_POT);
  mapedsen2 = map(sensorvalue2,0,1023,-SPEED_POT,SPEED_POT);

  // Change this 30 to 50 may be if it doesn't work with the load on it.
  int b = map(sensorvalue2,0,1023,25,-25);

  if ( (mapedsen1 >=5) && (abs(mapedsen2)<= 20) ) {
    ST.motor(1,mapedsen1);
    ST.motor(2,-mapedsen1);
  }
  else if((mapedsen1 <= -5)&&(abs(mapedsen2)<=20)) {
    ST.motor(1,mapedsen1);
    ST.motor(2,-mapedsen1);
  }
  else if((abs(mapedsen1)<= 10) && (mapedsen2 >= 0)) {
    ST.motor(1,b);
    ST.motor(2,b);
  }
  else if((abs(mapedsen1)<= 10) && (mapedsen2 <= -3)) {
    ST.motor(1,b);
    ST.motor(2,b);
  }
  else {
    ST.motor(1,0);
    ST.motor(2,0);
  }
}
mscholl
Newbie
 
Posts: 10
Joined: Sat Sep 09, 2017 8:55 pm

Re: MDDS30

Postby Idris » Thu Feb 08, 2018 3:18 pm

Hi mscoll,

For first setup, you should set your driver to PWM with independent mode (10110100), you can refer to MDDS30 User's Manual page 16.

For second setup, you want to use Serial Packetized mode. We are not sure either your previous code is compatible with ours. However you can download the library here - GitHub. For DIP switch, it can be different combination depends on the address that you set. Again, you can refer to MDDS30 User's Manual page 19 for Serial Packetized explaination.

Hope this helps.

Thanks.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MDDS30

Postby mscholl » Tue Feb 13, 2018 1:02 pm

Thanks for the reply- I have unhooked my MDDS10 and hooked in the MDDS30. I have the NC from both groves going to my "VIN" and the GRD on both groves going to GRD on my arduino. But it is not powering up my arduino. My dip switches are just as they are on page 16 of the manual. What have I done wrong and how do I get power to my arduino?
mscholl
Newbie
 
Posts: 10
Joined: Sat Sep 09, 2017 8:55 pm

Re: MDDS30

Postby Idris » Tue Feb 13, 2018 4:44 pm

Hi mscholl,

NC means Not Connected. We do not put power at Grove connector because we want to avoid mistakes. FYI, grove standard power can be 5V or 3.3V. So we do not want user connect 5V to 3.3V.

We recommend you to have different power source for Arduino. In case you still want to get power from MDDS30, you can connect through pin 5V at RC connector to 5V Arduino. This 5V pin can supply up to 500mA only.

IMG_6660.JPG
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MDDS30

Postby mscholl » Wed Feb 14, 2018 10:19 am

Alrighty.....we are getting close. Got it to power on, and for the motors to run forward, backward, turn right, turn left. My dip switches are set 10110110 as I have a lead acid battery. The problem is the PWM. It is not recognizing my PWM- If I turn it down more than half way, I get no motor output. If I turn it up past half way, the speed stays the same on high. When I pull it up on the monitor under serial print, the speed only shows 0 or 1. I have checked all connections. Is this a dip switch issue?
Thanks
CODE: SELECT_ALL_CODE
int sw1=10;
int sw2=11;
int sw3=12;

int sw4=6;
int state1;
int state2;
int state3;
int state4;

//MOTOR A
int pwm1 = 9;
int dir1 = 8;


// Motor B
int pwm2 = 3;
int dir2 = 5;

// Speed control potentiometers
 
int SpeedControll = A0; 
 
 
// Motor Speed Values - Start at zero
 
int MotorSpeed1 = 0;

void setup() {
  pinMode(sw1,INPUT_PULLUP);
  pinMode(sw2,INPUT_PULLUP);
  pinMode(sw3,INPUT_PULLUP);
  pinMode(sw4,INPUT_PULLUP);

  Serial.begin(9600);

  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(dir1, OUTPUT);
  pinMode(dir2, OUTPUT);
}

void loop() {
  state1 = digitalRead(sw1);
  state2 = digitalRead(sw2);
  state3 = digitalRead(sw3);
  state4 = digitalRead(sw4);

  // Read the values from the potentiometers
   
  MotorSpeed1 = analogRead (SpeedControll);
 
 
  // Convert to range of 0-255
 
  MotorSpeed1 = map(MotorSpeed1, 0, 1023, 0, 255);


  if (state1 == 0) { //Make a Right turn
    analogWrite(pwm2, MotorSpeed1);
    analogWrite(pwm1, MotorSpeed1);
    digitalWrite(dir2, HIGH);
    digitalWrite(dir1, LOW);
     }
  else if (state2 == 0) { //Forward Movement
    analogWrite(pwm1, MotorSpeed1);
    analogWrite(pwm2, MotorSpeed1);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, HIGH);
  }
  else if (state3 == 0) { //Take a left turn
    analogWrite(pwm1, MotorSpeed1);
    analogWrite(pwm2,MotorSpeed1);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
  }
  else if (state4 == 0) { //Backward Movement
    analogWrite(pwm1, MotorSpeed1);
    analogWrite(pwm2, MotorSpeed1);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
   
  }
  else {
    analogWrite(pwm1, 0);
    analogWrite(pwm2, 0);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
     }

  Serial.print("1-");
  Serial.print(state1);
  Serial.print(" 2-");
  Serial.print(state2);
  Serial.print(" 3-");
  Serial.print(state3);
  Serial.print(" 4-");
  Serial.println(state4);
  delay(250);
}
mscholl
Newbie
 
Posts: 10
Joined: Sat Sep 09, 2017 8:55 pm

Re: MDDS30

Postby Idris » Wed Feb 14, 2018 2:15 pm

Hi mscholl,

Good progress. Regarding the PWM issue, could you email to support@cytron.io for updates? In case you are urgent to complete your project, you can consider to use other mode e.g. Serial Simplified or Serial Packetized which involve only 1 I/O. For more detail you can refer to MDDS30 User's Manual.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MDDS30

Postby Idris » Thu Feb 15, 2018 1:46 pm

Hi mscholl,

For Serial Packetized, on hardware/driver side, you need to set the DIP switch as stated in MDDS30 User's Manual page 19 under section 8.4 Serial Packetized. For example 11100000, you can refer to page 20. Then connect
MDDS30 IN4 - Arduino pin 4
MDDS30 GND - Arduino GND

If you want power direct from MDDS30 to Arduino, connect
MDDS30 5V - Arduino 5V

Then, at software side, you need to download MDDS30 library from GitHub link - https://github.com/CytronTechnologies/Cytron_SmartDriveDuo as a ZIP file, and install to Arduino IDE. You can refer to https://www.arduino.cc/en/Guide/Libraries on how to install Arduino libraries (Importing a .zip Library).

Open MDDS30 example code - MDDSSerialPacketTest

1.png

And upload to the Arduino Uno.

2.png

How this example works? You can read on the comment provided (at top) in this example. Try to understand how it works, then slowly add 4 switch and 1 potentiometer code to control your motor.

I hope this explanation helps.

*I may not active for few days, we have CNY holiday here. :)
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MDDS30

Postby mscholl » Tue Feb 20, 2018 4:22 am

Hi! I hope CNY was good! I am not that great at arduino and the concept of simple serial is not simple. I have a program that someone wrote for me for simple serial with a different motor controller. Can we just modify it to run with the cytron motor driver? or how quickly can I get a functioning one?

CODE: SELECT_ALL_CODE
/*
Reads digital input pins to determine button states. Based on current motor speeds and directions, the program will smoothly
transition to the requested state (ex: transition from accelerating forward to turning/spinning left).

Reads an analog input pin (A0) connected to a potentiometer, maps the result to a range from 0 to 255
and uses the result to set the max speed.

Second analog pin (A2) connected to second potentiometer to provide steering trim control to attempt to compensate for variations
in speed between the motors.
*/
 
 /* v2.2 6/3/2015 - adds logic for steering left/right trim */
 
 // The circuit:
 // * analog pin 0 - throttle/governor potentiometer signal (middle/white wire).
 //   Center pin of the potentiometer goes to the analog pin.
 //   side pins of the potentiometer go to +5V (red) and ground (black).
   
 // * analog pin 2 - steering trim potentiometer signal (middle/white wire).
 //   Center pin of the potentiometer goes to the analog pin.
 //   side pins of the potentiometer go to +5V (red) and ground (black).

 // * Digital pin 2 - Emergency Stop button
 // * Digital pin 3 - Forward button
 // * Digital pin 4 - Reverse button
 // * Digital pin 5 - Left turn button
 // * Digital pin 6 - Right turn Button
 // * Digital pin 11 - Serial output to Sabertooth
 
 #include <Servo.h>
 #include <SoftwareSerial.h>
 
 Servo Sabertooth; // We'll name the Sabertooth object Sabertooth.
                         // For how to configure the Sabertooth, see the DIP Switch Wizard for
                         //   http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
                         // Be sure to select Simplified Serial Mode for use with this library.
                         // This sample uses a baud rate of 9600.
                         //
                         // Connections to make:
                         //   Arduino D11    ->  Sabertooth S1
                         //     Note: Serial output to Sabertooth is moved from TX01 (default) to D11 for easier debugging.
                         //   Arduino GND    ->  Sabertooth 0V
                         //   Arduino VIN    ->  Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
                         //   D2             ->  Emergency Stop Signal |
                         //   D3             ->  Forward Button Signal | The other wire from each of the digital input
                         //   D4             ->  Reverse Button Signal | buttons will be connected to a ground bus, which
                         //   D5             ->  Left Button Signal    | will in turn be connected to the gnd pin of the
                         //   D6             ->  Right Button Signal   | Arduino.
 
                         //
                         //   0V             ->  Governor/potentiometer lead 1 - black (ground)
                         //   A0             ->  Governor/potentiometer lead 2 - white (signal)
                         //   5v             ->  Governor/potentiometer lead 3 - red (+)
                          //
                         //   0V             ->  Trim/potentiometer lead 1 - black (ground)
                         //   A2             ->  Trim/potentiometer lead 2 - white (signal)
                         //   5v             ->  Trim/potentiometer lead 3 - red (+)
                                                 
 SoftwareSerial mySerial(10, 11); // RX, TX
 
//*********************************************************************************
//*** Adjustable parameters:                                                     **
const byte ndebug      = 0; //*** 0=run (debug messages off) 1=debug messages on **
const byte accelFactor = 2; //*** accelerate increment                           **
const byte decelFactor = 4; //*** decelerate increment                           **
const byte trimSens    = 6;//*** trim sensitivity: 128 = highest sensitivity    **
//*********************************************************************************

// These constants shouldn't change. 

const byte deadStop1 = 64;  // command value at center/stopped for motor 1
const byte deadStop2 = 192; // command value at center/stopped for motor 2

//****************************************************************************
// Initialize work variables:

int   motorControl1 = deadStop1; 
int   motorControl2 = deadStop2;
byte  motorByte1 = 0;
byte  motorByte2 = 0;

byte  rawSpeed1 = 0;
byte  rawSpeed2 = 0;
int   speedDifference = 0;
byte  direction1 = 1;    // '1' or '0' (Forward or Reverse) are valid values;
byte  direction2 = 1;


byte buttonState = 0;        // value read from the button
byte buttonPinNumber;        // input pin associated with this button
byte buttonPressedIndicator = 0; // Flag to indicate if any button pressed or not

float potValue   = 0;     // value read from the potentiometer
int   trimAdjustment = 0;

// These values are the max command values as determined by the throttle (potentiomemter) setting
byte maxFwdSpeed1 = 0;       
byte maxFwdSpeed2 = 0;       
byte maxRevSpeed1 = 0;       
byte maxRevSpeed2 = 0;

//****************************************************************************

void setup()
{
  delay(2000); // Give the Sabertooth time to intialize.
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  mySerial.begin(9600);
 
 //Note: Pin 1 is the Arduino serial output which connects to the Sabertooth S1 terminal. 
 
 // Define the input pins (which correspond to button jacks)
  pinMode(2, INPUT_PULLUP); // Emergency Stop
  digitalWrite(2, HIGH);
 
  pinMode(3, INPUT_PULLUP); // Forward
  digitalWrite(3, HIGH);
 
  pinMode(4, INPUT_PULLUP); // Reverse
  digitalWrite(4, HIGH);
 
  pinMode(5, INPUT_PULLUP); // Left
  digitalWrite(5, HIGH);

  pinMode(6, INPUT_PULLUP); // Right
  digitalWrite(6, HIGH);
 
  Sabertooth.attach(1);
 
};

void loop()
{
  // check throttle potentiometer setting:
  potValue = analogRead(A0);           

  // set max command values based on throttle (potentiometer) setting
  maxRevSpeed1 = map(potValue, 0,1023,63,1);
  maxFwdSpeed1 = map(potValue, 0,1023,65,127);
  maxRevSpeed2 = map(potValue, 0,1023,191,128);
  maxFwdSpeed2 = map(potValue, 0,1023,193,255);
 
  //******************************************
 
  // check steering trim potentiometer setting:
  potValue = analogRead(A2);
 
  // set steering correction based on trim (potentiometer) setting
  trimAdjustment = map(potValue, 0,1023,(-1 * trimSens),trimSens);
    if (ndebug == 1){
     Serial.print("\t potValue = " );                       
     Serial.println(potValue);
     Serial.print("\t trimAdjustment = " );                       
     Serial.println(trimAdjustment);
   };
  //******************************************

  if (ndebug == 1){
   // print the results to the serial monitor:
   Serial.print("\t MRS1 = " );                       
   Serial.print(maxRevSpeed1);     
   Serial.print("\t MFS1 = " );                       
   Serial.println(maxFwdSpeed1);   
   Serial.print("\t MRS2 = " );                       
   Serial.print(maxRevSpeed2);     
   Serial.print("\t MFS2 = " );                       
   Serial.println(maxFwdSpeed2); 
  };
 
  buttonPressedIndicator = 0; // reset button pressed indicator
 
  // poll the motion buttons (pins 2-6):
  // Note that having E-stop assigned to pin #2 with break after processing ensures that it will
  // always have first priority.
 
  for (buttonPinNumber = 2; buttonPinNumber <= 6; ++buttonPinNumber)
  {
    buttonState = digitalRead(buttonPinNumber);
    if (buttonState == LOW)
    {
      buttonPressedIndicator = ++buttonPressedIndicator;
      buttonPressedFunction(buttonPinNumber);
      if (ndebug == 1)
      {
       Serial.println("Button pressed - Breaking loop ***" );
      };
      break;
    };
  };

 // Compute equivalent motor speeds so that left vs right can be compared.
 if ((motorControl1 == 0 or motorControl1 == deadStop1) and (motorControl2 == 0 or motorControl2 == deadStop2))
 { //1
   rawSpeed1 = 0;
   rawSpeed2 = 0;
   if (ndebug == 1)
   {
    Serial.println("rawSpeeds = 0." ); 
   };
 } //1
 else
 { //1
   if (ndebug == 1)
   {
    Serial.println("Calculating rawSpeeds." ); 
    Serial.print("\t");
     Serial.print("MC1 = ");
     Serial.print(motorControl1);       
     Serial.print("\t");
     Serial.print("MC2 = "); 
     Serial.println(motorControl2);
   }; 
   rawSpeed1 = abs(deadStop1 - motorControl1);
   rawSpeed2 = abs(deadStop2 - motorControl2);
   
   if (motorControl1 >= deadStop1)
   { //2
    direction1 = '1';
   } //2
   else
   { //2
    direction1 = '0';
   }; //2
 
   if (motorControl2 >= deadStop2)
   { //2
    direction2 = '1';
   } //2
   else
   { //2
    direction2 = 0;
   }; //2
 
   if (ndebug == 1)
   {
    Serial.print("\t");
    Serial.print("rawSpeed1: ");
    Serial.print(rawSpeed1);       
    Serial.print("\t");
    Serial.print("dir 1: ");
    Serial.print(direction1);
    Serial.print("\t");
    Serial.print("rawSpeed2: "); 
    Serial.print(rawSpeed2);
    Serial.print("\t");
    Serial.print("dir 2: ");
    Serial.println(direction2);
   };
  }; //1
     
  if (ndebug == 1)
  {
   Serial.print("MFS1 - MC1: ");
   Serial.print("\t");
   Serial.println(maxFwdSpeed1 - motorControl1);
   Serial.print("maxFwdSpeed2 - motorControl2: ");
   Serial.print("\t");
   Serial.println(maxFwdSpeed2 - motorControl2);
  };

  if (buttonPressedIndicator == 0)
  { // 0 If no buttons have been pressed decelerate.
     
   if ((motorControl1 == 0 or motorControl1 == deadStop1) and (motorControl2 == 0 or motorControl2 == deadStop2))
   { //1
    motorControl1 = deadStop1;
    motorControl2 = deadStop2;
    if (ndebug == 1)
    {       
     Serial.println("Stopped." );
    };
   } //1
   else
   { //1
    if (ndebug == 1)
    { 
     Serial.println("No button pressed - decelerate." );
    };
    decelerateFunction();
   }; //1   
     sendCommandFunction();
  }; // 0   
 
  // This just blinks the LED once to indicate the completion of each iteration of the loop.
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(150);              // wait for 150 millseconds
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW

  // wait 20 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  //delay(20);   
   if (ndebug == 1)
   {
    Serial.println("End Void Loop *****" );
   //  delay(100);
   };
}; // End of Void Loop *****************************************************************************************************
// *******************

// Functions: ***********************************************************************************************************

// Send Command:
  void sendCommandFunction(){
   if (ndebug == 1)
    {   
     Serial.println("Send Command function *****" );
    };
   
   
   // Ensure that command values are within valid range:
   
   if (motorControl1 < 1)
    {
       motorControl1 = 1;
    };
     
    if (motorControl1 > 127)
    {
       motorControl1 = 127;
    };
     
   if (motorControl2 < 128)
    {
      motorControl2 = 128;
    };
     
   if (motorControl2 > 255)
    {
      motorControl2 = 255;
    };

   if (ndebug == 1)
   {
    Serial.print("\t");
    Serial.print("MC1: " );
    Serial.print(motorControl1);
    Serial.print("\t");
    Serial.print("MC2: " );
    Serial.println(motorControl2);
   };
   
   // convert command values to byte format for serial communication
   
   motorByte1 = motorControl1;
   motorByte2 = motorControl2 ;
   
   // Trim correction to track straight in forward or reverse
   
   if (rawSpeed1 > abs(trimAdjustment)) // this ensures that trim correction won't cause motor to overshoot zero.
   {
    if (ndebug == 1)
    {
     Serial.print("Trim Adjust - Pin: ");   
     Serial.println(buttonPinNumber);   
    };
     
     switch (buttonPinNumber) {
    case  3: //Forward motion
     if (trimAdjustment < 0) // left trim - slow down motor 1
     {
      if (ndebug == 1)
      {
       Serial.println("Fwd motion - left trim");   
      };
      motorByte1 = motorControl1 + trimAdjustment;
      motorByte2 = motorControl2 ;
     } 
     else // right trim - slow down motor 2
     {
      if (ndebug == 1)
      {
       Serial.println("Fwd motion - right trim");   
      };
      motorByte1 = motorControl1 ;
      motorByte2 = motorControl2 - trimAdjustment;
     };
     break;
   
    case  4: // Reverse motion
     if (trimAdjustment < 0) // left trim - slow motor 1 (increase value)
     {
      if (ndebug == 1)
      {
       Serial.println("Rev motion - left trim");   
      };     motorByte1 = motorControl1 - trimAdjustment;
      motorByte2 = motorControl2 ;
     } 
     else // right trim - slow motor 2 (increase value)
     {
      if (ndebug == 1)
      {
       Serial.println("Rev motion - right trim");   
      };
      motorByte1 = motorControl1 ;
      motorByte2 = motorControl2 + trimAdjustment;
     };   
     break;
    }; //End switch
   }; 

   if (ndebug == 1)
   {
    Serial.print("\t");
    Serial.print("motorByte1: " );
    Serial.print(motorByte1);
    Serial.print("\t");
    Serial.print("motorByte2: " );
    Serial.println(motorByte2);
   };   
 
   //Send motor commands to Sabertooth     
   mySerial.write(motorByte1);
   mySerial.write(motorByte2);   
   
   if (ndebug == 1)
   {
    Serial.println(" ");   
    Serial.println("End Send Command Function *******");   
   };
  };
// End of Send Command Function ******************************************************************************************

//*****************************

// Button pressed:   
  void buttonPressedFunction(int buttonPinNumber){ // 0
   buttonState = digitalRead(buttonPinNumber); 
   if (ndebug == 1){
    Serial.println("buttonPressedFunction: *****" );
    Serial.print("pin: " );                       
    Serial.print(buttonPinNumber);
    Serial.print("\t"); 
    Serial.print("buttonState = " ); 
    Serial.println(buttonState);
    };
   
   if (motorControl1 == 0)
   {
    motorControl1 = deadStop1;
   };
   if (motorControl2 == 0)
   {
    motorControl2 = deadStop2;
   };

   switch (buttonPinNumber) { //1
    // 1. Determine which button was pressed
    // 2. Compare current motor speeds and directions.
    // 3. Take appropriate action.
      case 2: // Emergency Stop ***********************************************************************************
      motorControl1 = deadStop1;
      motorControl2 = deadStop2;
 
      if (ndebug == 1)
      {
       Serial.println("E-stop sent.");
      };
     break;
     
     case 3:  // Forward button pressed*************************************************************;
      if (ndebug == 1)
      {
        Serial.println("Accel forward.");
      };
       
      accelerateForward();
       
     break;
     
     case 4:  // Reverse *********************************************************
      if (ndebug == 1)
      {
       Serial.println("Accel in reverse.");
      };
       
      accelerateReverse();
           
      break;
     
     case 5: // Left ****************************************************************************
      if (ndebug == 1)
      {
       Serial.println("Left button pressed");
      };
     
      leftButtonPressed();
         
      break;
     
     case 6: // Right button pressed. **********************************************************************
     
       if (ndebug == 1)
       {
        Serial.println("Right button pressed.");
       };
         
       rightButtonPressed(); 
     
       break;
     
    }; //1 End Switch *****************************************************
   
     sendCommandFunction();
     
     if (ndebug == 1)
     {
      Serial.println("End of buttonPressFunction ******");
     };
   
     digitalWrite(buttonPinNumber, HIGH);
     
  }; // 0
  // End of buttonPressedFunction ******************************************************************************************
 
  // Accelerate Forward ****************************************************************************************************
 
  void accelerateForward(){  //0
       if (ndebug == 1)
       {
        Serial.println("accelerateForward function");
       };   
       
       speedDifference = abs((maxFwdSpeed1 - motorControl1) - (maxFwdSpeed2 - motorControl2)); // compare motor speeds
       
       if (speedDifference != 0) // speeds mismatched
       { //1
        if (ndebug == 1)
        {
         Serial.println("Motor spd mismatch");
        };
        if (speedDifference < accelFactor) // speeds mismatched by < accelFactor
        { //2
         if (ndebug == 1)
         {
          Serial.print("Spd mismatch = ");
          Serial.println(speedDifference);
         };
         if ((maxFwdSpeed1 - motorControl1) > (maxFwdSpeed2 - motorControl2)) // left motor slow
         { //3
          motorControl1 = motorControl1 + speedDifference; //speed up motor 1 to match motor 2
          if (ndebug == 1)
          {
           Serial.println("MC1 adjusted");
          };
         } //3
         else
         { //3
          motorControl2 = motorControl2 + speedDifference; 
          if (ndebug == 1)
          {
           Serial.println("MC2 adjusted");
          };
         }; //3
        }; //2
       }; //1
         
               
       // Motor speeds equal ***********************************************
       if (speedDifference == 0)   
       { // 1 
        if (ndebug == 1)
         {
           Serial.println("rawSpeed1 == rawSpeed2");
         };
           
        if (motorControl1 < deadStop1)   
        {
         motorControl1 = (motorControl1 + decelFactor);
        }
        else
        {
         motorControl1 = (motorControl1 + accelFactor);
        };
           
        if (motorControl1 == deadStop1) // Skip dead spot.
        { //3
         motorControl1 = (motorControl1 + accelFactor);
         if (ndebug == 1)
         {
          Serial.println("Motor1 - skip dead spot");
         };
        }; //3
 
        if (motorControl1 > maxFwdSpeed1) // Check for overspeed
        { //3
         motorControl1 = maxFwdSpeed1;
         if (ndebug == 1)
         {
          Serial.println("Motor1 - Governed to MFS1");
          Serial.print("motorControl2 = ");
          Serial.println(motorControl2);
         };
        }; //3
         
        if (motorControl2 < deadStop2)   
        {
         motorControl2 = (motorControl2 + decelFactor);
        }
        else
        {
         motorControl2 = (motorControl2 + accelFactor);
        };     
             
        if (motorControl2 == deadStop2) // Skip dead spot.
        { //3
         motorControl2 = (motorControl2 + accelFactor);
         if (ndebug == 1)
         {
          Serial.println("Motor2 - skip dead spot");
         };
        }; //3
           
        if (motorControl2 > maxFwdSpeed2) // Check for overspeed
        { //3
         motorControl2 = maxFwdSpeed2;
         if (ndebug == 1)
         {
          Serial.println("Motor2 - Governed to MFS2");
         };
        }; //3
           
        return;
       }; //1
/*        else
       { //2
        if (motorControl1 > maxFwdSpeed1) // Check for overspeed
         {
           motorControl1 = maxFwdSpeed1;
           motorControl2 = maxFwdSpeed2; 
         };
         return;
        }; //2
       }; //1  */
       
      // Left motor slow *************************************************
       
      if ((maxFwdSpeed1 - motorControl1) > (maxFwdSpeed2 - motorControl2))
      { //1
       if (ndebug == 1)
       {
        Serial.println("motor 1 slow");
       };

       motorControl1 = (motorControl1 + accelFactor);
       if (motorControl1 == deadStop1) // Skip dead spot.
       { //2
        motorControl1 = (motorControl1 + accelFactor);
       }; //2
       if (motorControl1 > maxFwdSpeed1) // Check for overspeed.
       { //2
        motorControl1 = maxFwdSpeed1;
       }; //2
       return;
      } //1
      else
      { //1 - right motor slow
       if (ndebug == 1)
       {
        Serial.println("motor 2 slow");
       };
         
       motorControl2 = (motorControl2 + accelFactor);
       if (motorControl2 == deadStop2) // Skip dead spot.
       { //2
        motorControl2 = (deadStop2 + accelFactor);
       }; //2
       if (motorControl2 > maxFwdSpeed2) // Check for overspeed.
       { //2
        motorControl2 = maxFwdSpeed2;
       }; //2
       return;
      }; //1 
     }; //0
 
  // End of accelerateForward *********************************************************************************************************

  // Accelerate Reverse ***************************************************************************************************************
 
  void accelerateReverse(){  //0
       if (ndebug == 1)
        {
          Serial.println("accelerateReverse function");
        };
       
       if ((maxFwdSpeed1 - motorControl1) == (maxFwdSpeed2 - motorControl2))   
       { // 1 
        if (ndebug == 1)
        {
         Serial.println("rawSpeed1 == rawSpeed2");
        };
                     
        if (motorControl1 > deadStop2)   
        {
         motorControl1 = (motorControl1 - decelFactor);
        }
        else
        {
         motorControl1 = (motorControl1 - accelFactor);
        };   
             
        if (motorControl1 == deadStop1) // Skip dead spot.
        {
         motorControl1 = (deadStop1 - accelFactor);
        };
         
        if (motorControl1 < maxRevSpeed1)
        {
         motorControl1 = maxRevSpeed1;
        };
           
        if (motorControl2 > deadStop2)   
        {
         motorControl2 = (motorControl2 - decelFactor);
        }
        else
        {
         motorControl2 = (motorControl2 - accelFactor);
        }; 
           
        if (motorControl2 == deadStop2) // Skip dead spot.
        {
         motorControl2 = (deadStop2 - accelFactor);
        };
                   
        if (motorControl2 < maxRevSpeed2)
        {
         motorControl2 = maxRevSpeed2;
        };
     
        return;
       };
       
      // Left motor fast ************************************************
       
      if ((maxFwdSpeed1 - motorControl1) < (maxFwdSpeed2 - motorControl2))   
      { //1
       if (ndebug == 1)
       {
        Serial.println("left motor fast");
       };
       motorControl1 = (motorControl1 - accelFactor);
       if (motorControl1 == deadStop1) // Skip dead spot.
       {  //4
        motorControl1 = (deadStop1 - accelFactor);
       }; //4
                   
       if (motorControl1 < maxRevSpeed1) //Check for overspeed
       {
        motorControl1 = maxRevSpeed1;
       };
       return;
      } //1
      else
      { //1 Left motor slow
       motorControl2 = (motorControl2 - accelFactor);
       if (motorControl2 == deadStop2) // Skip dead spot.
       {  //3
        motorControl2 = (deadStop2 - accelFactor);
       }; //3
                   
       if (motorControl2 < maxRevSpeed2) //Check for overspeed
       {
        motorControl2 = maxRevSpeed2;
       };
      }; //1
      return;     
     }; //0
 
  //End of accelerateReverse Function *************************************************************************************************
 
  // leftButtonPressed Function *******************************************************************************************************
 
  void leftButtonPressed(){
   if (ndebug == 1)
   {
    Serial.println("leftButtonPressed function");
   };
   
   if (rawSpeed1 == rawSpeed2)
   { //1
    if (ndebug == 1)
    {
     Serial.println("rawSpeed1 == rawSpeed2");
     Serial.print(rawSpeed1);
     Serial.print("\t");
     Serial.println(rawSpeed1);
    };
         
    if ((direction1 != direction2) or (rawSpeed1 == 0))
    { //2
     if (ndebug == 1)
     {
      if (direction1 != direction2)
      {
       Serial.println("dir 1 != dir 2");
      };
      if (rawSpeed1 == 0)
      {
       Serial.println("rawSpeed1 == 0");
      };
     };
     
     motorControl1 = (motorControl1 - accelFactor);
     motorControl2 = (motorControl2 + accelFactor);
             
     if (motorControl1 == deadStop1) // Skip dead spot
     {  //3
      motorControl1 = (deadStop1 - accelFactor);
     }; //3
     
     if (motorControl1 < maxRevSpeed1) //Check for overspeed
     {
      motorControl1 = maxRevSpeed1;
     };
     
     if (motorControl2 == deadStop2) // Skip dead spot.
     {  //3
      motorControl2 = (deadStop2 + accelFactor);
     }; //3
     
     if (motorControl2 > maxFwdSpeed2) //Check for overspeed
     { //3
      motorControl2 = maxFwdSpeed2;
     }; //3
 
     return;
    }; //2
 
    if (direction1 == direction2)
    { //2
     if (ndebug == 1)
     {
      Serial.print("dir 1 == dir 2: ");
      Serial.println(direction2);
     };
             
     if (direction1 == 1)
     { //3
      motorControl1 = (motorControl1 - decelFactor);
      if (motorControl1 == deadStop1) // Skip dead spot
      { //4
       motorControl1 = (deadStop1 - decelFactor);
      }; //4
      if (motorControl1 < maxRevSpeed1) //Check for overspeed
      { //4
       motorControl1 = maxRevSpeed1;
      }; //4
      return;
     } //3
     else // direction = 0
     { //3
      motorControl2 = (motorControl2 + decelFactor);
      if (motorControl2 == deadStop2) // Skip dead spot
      { //4
       motorControl2 = (deadStop2 + decelFactor);
      }; //5
      if (motorControl2 > maxFwdSpeed2) //Check for overspeed
      { //5
       motorControl2 = maxFwdSpeed2;
      }; //4
      return;
     }; //3
    }; //2
     
    if (direction1 != direction2)
    { //2
     if (motorControl2 < maxFwdSpeed2)
     { //3
      motorControl2 = (motorControl2 + accelFactor);
      if (motorControl2 == deadStop2) // Skip dead spot.
      {  //4
       motorControl2 = (deadStop2 + accelFactor);
      }; //4
      if (motorControl2 > maxFwdSpeed2) //Check for overspeed
      { //4
       motorControl2 = maxFwdSpeed2;
      }; //4
      return;
     }; //3
    }; //2
   } // 1
   else
   { // 1 rawSpeed1 != rawSpeed2
    if (rawSpeed1 < rawSpeed2)
    { //2
     if (ndebug == 1)
     {
      Serial.println("rawSpeed1 < rawSpeed2");
     };
     if (motorControl1 > maxRevSpeed1)
     { //3
      motorControl1 = (motorControl1 - accelFactor);
      if (motorControl1 == deadStop1) // Skip dead spot.
      { //4
       motorControl1 = (deadStop1 - accelFactor);
      }; //4
      if (motorControl1 < maxRevSpeed1) //Check for overspeed
      { //4
       motorControl1 = maxRevSpeed1;
      }; //4     
     }; //3
     return;   
    }; //2
         
    if (rawSpeed1 > rawSpeed2)
    { //2
     if (ndebug == 1)
     {
      Serial.println("rawSpeed1 > rawSpeed2");
     };
           
     if (motorControl2 < maxFwdSpeed2)
     {//3
      motorControl2 = (motorControl2 + accelFactor);
      if (motorControl2 == deadStop2) // Skip dead spot.
      { //4
       motorControl2 = (deadStop2 + accelFactor);
      }; //4
      if (motorControl2 > maxFwdSpeed2) //Check for overspeed
      { //4
       motorControl2 = maxRevSpeed2;
      }; //4   
     }; //3   
     return;   
    }; //2       
   }; //1
         
   if (ndebug == 1)
   {
    Serial.println("No action taken.");
   };
  }; //0 End of leftButtonPressed Functiion ***********************************************************************************************
 
 
  //  rightButtonPressed Functiion ***********************************************************************************************
   
  void rightButtonPressed(){ //0
   
          if (rawSpeed1 == rawSpeed2)
        { //1
         if (ndebug == 1)
         {
          Serial.println("rawSpeed1 == rawSpeed2");
          Serial.print(rawSpeed1);
          Serial.print("\t");
          Serial.println(rawSpeed1);
         };
         
         if ((direction1 != direction2) or (rawSpeed1 == 0))
          { //2
           if (ndebug == 1)
            {Serial.println("dir 1 != dir 2");};
            if (motorControl1 < maxFwdSpeed1)
            { //3
             motorControl1 = (motorControl1 + accelFactor);
             motorControl2 = (motorControl2 - accelFactor);
             
             if (motorControl1 == 0 or motorControl1 == deadStop1) // Skip dead spot.
              {  //4
               motorControl1 = (deadStop1 + accelFactor);
              }; //4
             if (motorControl2 == 0 or motorControl2 == deadStop2) // Skip dead spot.
              {  //4
               motorControl2 = (deadStop2 - accelFactor);
              }; //4
            }; //3
            if (ndebug == 1)
             {
               Serial.println("Break");
             };
           return;
          }; //2
         
        if (direction1 == direction2)
          { //2
           if (ndebug == 1)
           {
            Serial.print("dir 1 == dir 2 : ");
            Serial.println(direction2);
           };
             
          if (direction1 == 0)
           { //3
            if (motorControl1 < maxFwdSpeed1)
             {//4
              motorControl1 = (motorControl1 + accelFactor);
              if (motorControl1 == 0 or motorControl1 == deadStop1) // Skip dead spot.
               {  //5
                motorControl1 = (deadStop1 + accelFactor);
               }; //5
             };//4
            return;
           } //3
          else
          { //3
            if (motorControl2 > maxRevSpeed2)
             { //4
              motorControl2 = (motorControl2 - accelFactor);
              if (motorControl2 == 0 or motorControl2 == deadStop2) // Skip dead spot.
              {  //5
               motorControl2 = (deadStop2 - accelFactor);
              }; //5
              if (motorControl2 < maxRevSpeed2) // Govern
              {  //5
               motorControl2 = maxRevSpeed2;
              }; //5
              return;
            }; //4
          }; //3
         }; //2
        } // 1
         else
        { // 1 rawSpeed1 != rawSpeed2
         if (rawSpeed1 < rawSpeed2)
          { //2
           if (ndebug == 1)
            {Serial.println("rawSpeed1 < rawSpeed2");};
           if (motorControl1 < maxFwdSpeed1)
            { //3
             motorControl1 = (motorControl1 + accelFactor);
             if (motorControl1 == 0 or motorControl1 == deadStop1) // Skip dead spot.
             { //4
              motorControl1 = (deadStop1 + accelFactor);
             }; //4
             return;         
            }; //3
          }; //2
         
          if (rawSpeed1 > rawSpeed2)
          { //2
           if (ndebug == 1)
            {Serial.println("rawSpeed1 > rawSpeed2");};
           
           if (motorControl2 > maxRevSpeed2)
            {//3
             motorControl2 = (motorControl2 - accelFactor);
             if (motorControl2 == deadStop2) // Skip dead spot.
             { //4
              motorControl2 = (deadStop2 - accelFactor);
             }; //4
             if (motorControl2 < maxRevSpeed2) // Govern
             {  //4
              motorControl2 = maxRevSpeed2;
             }; //4
            return; 
           }; //3       
         }; //2       
        }; //1
         
       if (ndebug == 1)
         {Serial.println("No action taken.");};

 
  }; //0 End of rightButtonPressed Function **********************************************************************************************
 
  // Decelerate:
  void decelerateFunction(){
   if (ndebug == 1)
    {   
     Serial.println("Decel function" );
    };
   if (motorControl1 > deadStop1)
    { //3
     motorControl1 = (motorControl1 - decelFactor);
     if (motorControl1 < deadStop1) // Makes sure that command value doesn't overshoot dead stop value.
     { //4
      motorControl1 = deadStop1;
     }; //4
    }; //3
   
   if (motorControl1 < deadStop1)
    { //3
     motorControl1 = (motorControl1 + decelFactor);
     if (motorControl1 > deadStop1) // Makes sure that command value doesn't overshoot dead stop value.
     { //4
      motorControl1 = deadStop1;
     }; //4
    }; //3
 
    if (motorControl2 > deadStop2)
    { //3 
     motorControl2 = (motorControl2 - decelFactor);
     if (motorControl2 < deadStop2) // Makes sure that command value doesn't overshoot dead stop value.
     { //4
      motorControl2 = deadStop2;
     }; //4
    };  //3
   
    if (motorControl2 < deadStop2)
    {  //3
     motorControl2 = (motorControl2 + decelFactor);   
     if (motorControl2 > deadStop2) // Makes sure that command value doesn't overshoot dead stop value.
     { //4
      motorControl2 = deadStop2;
     }; //4
    }; //3
  }; //2

// ****************************************************
mscholl
Newbie
 
Posts: 10
Joined: Sat Sep 09, 2017 8:55 pm

Re: MDDS30

Postby Idris » Tue Feb 20, 2018 10:38 am

Hi mscholl,

It takes some time for me to go through, understand and modify the coding. Any updates I will reply here.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MDDS30

Postby Idris » Tue Feb 20, 2018 5:52 pm

Hi mscholl,

Did you try Sabertooth Simplified Serial code with SmartDriveDuo-30? I just check the library and it looks quite same. You can set the DIP switch at SmartDriveDuo-30 to 11001100 (refer to page 18 at MDDS30 User's Manual). Connect IN1 to Arduino pin 11. Don't forget GND and 5V too.

Please share any updates here. Thanks.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 384
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Next

Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 3 guests

cron