Arduino code to control two 18v15 motor controllers via ttl

For about two weeks I have been trying to adapt this code

#include <NewSoftSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX (not used in this example)
#define txPin 4 // pin 4 connects to SMC RX
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
void setup()
{
// initialize software serial object with baud rate of 38.4 kbps
mySerial.begin(38400);
// the Simple Motor Controller must be running for at least 1 ms
// before we try to send serial data, so we delay here for 5 ms
delay(5);
// if the Simple Motor Controller has automatic baud detection
// enabled, we first need to send it the byte 0xAA (170 in decimal)
// so that it can learn the baud rate
mySerial.print(0xAA, BYTE); // send baud-indicator byte
// next we need to send the Exit Safe Start command, which
// clears the safe-start violation and lets the motor run
exitSafeStart(); // clear the safe-start violation and let the motor run
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
delay(1000);
}

To control two pololu simple motor controllers separately. This is what I have. I am very new to serial programing and most of this code I’m not sure about. As of yet, I am unable to get it to work. Any ideas? Anything at all would be great!!!

#include <NewSoftSerial.h>

#define rxPin 7 // pin 3 connects to SMC TX (not used in this example)
#define txPin 8 // pin 4 connects to SMC RX

NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);

// pululu codes
#define NEWCMD 0xAA
#define EXITSAFESTART 0x03
#define FORWARD 0x05
#define REVERSE 0x06

// device id's
 uint8_t Motor1 = 10;
 uint8_t Motor2 = 13;


void exitSafeStart(uint8_t device = 0) //if you omit the argument, it will default to whatever value you specify here
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);
  mySerial.print(EXITSAFESTART, BYTE);
}


void setMotorSpeed(uint8_t device, int speed)
{
  mySerial.print(NEWCMD, BYTE);
  mySerial.print(device, BYTE);

  if (speed < 0)
  {
    mySerial.print(REVERSE, BYTE); // motor reverse command
    speed = -speed; // make speed positive
  }
  else
  {
    mySerial.print(FORWARD, BYTE); // motor forward command
  }
  speed = constrain(speed, 0, 3200);  // speed should be a number between 0 to 3200
  mySerial.print((unsigned char)(speed & 0x1F), BYTE);
  mySerial.print((unsigned char)(speed >> 5), BYTE);
}

void setup()
{
  mySerial.begin(19200);
  delay(5);
  mySerial.print(0xAA, BYTE);
  exitSafeStart();
  }

void loop()
{
  //int speed = analogRead(A0) * 6 - 3072;  // use an LDR or potmeter or ...
  setMotorSpeed(Motor1, 3200); // full-speed forward
  delay(10);

 // int speed = analogRead(A1) * 6 - 3072;
  setMotorSpeed(Motor2, -3200); // full-speed reverse
  delay(10);
}

When asking for help, you should actually describe what the problem is instead of just saying it doesn’t work. It looks like your current code should not even compile because you omitted the argument to exitSafeStart().

Also, please let us know where you got the starting code. In this case, it looks like you got it from the Arduino Examples section of the Simple Motor Controller user’s guide:
pololu.com/docs/0J44/6.7.1

Did you ever succeed in controlling one motor with that code?

What are all the connections you have made?

–David

Yes, I apologize. The first code did come from user manual for the motor controller. It is the simple version. Yes, I was able to make one motor controller run with that code. Both motor controllers RX ports are connected to the same wire at pin eight on my arduino. the second smc tx pin is wired to txin on the first smc, and from there it connects to pin seven on the arduino. The second code I have posted was was pieced together with help from the arduino forum.

Hello.

What problem are you encountering?

- Ryan

I am trying to write a code for a arduino that will control two pololu high power simple motor controllers (18v15). the controllers on are on the same serial line. They are connected to pin 8 on the arduino using newsoftserial library. I am trying to keep the code similar to the simple arduino example provided in the motor controller’s user manual. pololu.com/docs/0J44/6.7.1 I am very new to serial commands and not too sure how to get the job done. This project that I am working on is a robotic platform with two high current motors.

What Ryan and I are asking you is what actually went wrong when you tried to compile, load, and run the code above. There are many different problems you could have had: anything from not being able to find the Upload button to the motors running too slow. Your first post in this thread (rather than your fourth) should have told us exactly what your problem was so we can help you solve it. A good way to communicate your problem is to describe the expected behavior of your system and the actual behavior.

I suggest that you run the simple code from the user’s guide, and use it to control two SMCs. Even though that code was made to control one SMC, it should be able to control two of them with the wiring you currently have; both motor controllers should respond to all the commands that are sent. Then you should gradually change that code, a few lines at a time, until your problem starts happening. Then you will have narrowed down the problem to a few lines of code and you should be able to figure it out.

–David

So would a code for the forward command look like this if my motor controllers were numbered 10 and 13?

mySerial.print(0xAA, BYTE);
mySerial.print(0x10, BYTE);
mySerial.print(0x85, BYTE);
mySerial.print(0xAA, BYTE);
mySerial.print(0x13, BYTE);
mySerial.print(0x85, BYTE);

No. Those two commands would be correct though if you changed 0x85 to 0x05 (all data bytes must have their MSb cleared) and added the the two data bytes to specify the speed to each command.

But I don’t understand why you are asking this question. What do you think of the strategy I proposed above? Have you succeeded in running two motors yet using unmodified sample code from the user’s guide? That is the first step of the strategy I proposed above.

–David

For that simple example code, would I have to set each controller to the same device number? Because I did try to run both off that code a while back and only one of the controllers would exit safestart and run.

No, the example code from the user’s guide sends Compact Protocol commands; these commands don’t have a device ID in it so it shouldn’t matter. --David

What would cause only one smc to respond? both smc will run when run in windows under the pololu setup software.

Hello.

If only one SMC responds, it sounds like you might have a something connected wrong, or perhaps you’re having power issues. Can you describe how you have everything in your system connected, including what you’re using as your power source?

As a first step, try the sample Arduino code from the user’s guide (unmodified) with just SMC1 and make sure it works. Then repeat the process with just SMC2 and make sure that works. Once you have verified that each SMC works alone with the sample code, try connecting both.

- Ben

Both motor controllers RX ports are connected to the same wire at pin eight on my arduino. the second smc tx pin is wired to txin on the first smc, and from there it connects to pin seven on the arduino. I have not tried each by its self. but I will try that next.

Note that when you’re having trouble getting something to work, simplification is usually the best way to track down the problem. Make things a simple as possible and then slowly add in complexity until things stop working, at which point you’ll have a pretty good idea as to what might be causing the problem.

For example, the sample code in the user’s guide does not read serial data from the SMC, so there is no need to connect the TX pins of the SMCs to anything. The only connections required are ground and Arduino TX to SMC Rx (you didn’t mention ground connections in your previous post; are you making sure all your devices have a common ground?).

- Ben

Okay, so i connected the motor controller one at a time ran the example code unmodified and one of them will not run or exit safestart via serial.

i went a head and reloaded the firmware 1.03. That had no effect.

Have you changed any of the settings on either of your controllers? Can you post some pictures of your setup (ideally, one for the system that works and one for the one that doesn’t)? How are you powering everything?

Also, when you were trying to chain things together, did you ever connect the TX pins of both controllers directly to each other or to the Arduino’s TX pin?

- Ben

These are the same settings that I have both controllers set to. One of them works and the other one does nothing. For the stand alone test the rx pin was hooked pin eight of the arduino as well as the gnd to arduino’s gnd. right now the arduino is powered off of the usb power and the smc is powered off a 24v dc battery. I just bought these controllers less than a month ago. When I had both of the motor controllers connected exactly as the picture shows in the user manual showing daisy chaining.


Are these settings wrong?

I apologize for my delayed reply. I lost track of this thread, so thank you for bumping it. Your settings look fine. Just to verify, do both controllers still function properly when controlled from the Simple Motor Control Center?

Note that the maximum recommended voltage for your controller is 24 V (though it can survive voltages up to 30 V), which is why we state in the user’s manual:

Are you taking any special precautions to limit the peak voltage of your battery? How long are your power cables.

When I asked for some pictures of your setup, I meant for you to take pictures of your physical setup (i.e. how everything is connected). And you never answered this question from my previous post: “Also, when you were trying to chain things together, did you ever connect the TX pins of both controllers directly to each other or to the Arduino’s TX pin?”

- Ben