Go Down

Topic: advice on controlling two motor controllers through arduino (Read 1 time) previous topic - next topic

talhayounas

hi
i am trying to make a wheel chair. The logic is to connect two motors to arduino through motor controllers.
I have purchased Pololu 18 v 15 fully assembled motor controllers and arduino mega.Both controllers are working fine individually means if i connect any one of them to arduino and burn the code(provided in end) its behaviour is natural its speed inceases and decreases with the argument in setMotorSpeed now i want to connect both controllers at the same time to make wheelchair control . I am trying "daisy chaining" and different code ,the code uploads and nothing happens i think ther is some problem with connections can anyone help me making connections !! i am also providing the code I am using

code to control one motor(working 100 %)

(
#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);
}
)

code for two controllers (doubted)

(
#include <NewSoftSerial.h> 
#include <LiquidCrystal.h>
#define rxPin 3  // pin 3 connects to SMC TX  (not used in this example)
#define txPin 7 // pin 6 connects to SMC RX 
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin); 
int sensorPin = 0;     
// required to allow motors to move 


 
  void setMotorSpeed(int speed, unsigned char deviceId)
{
  mySerial.print(0xAA, BYTE); // first byte of Pololu protocol
  mySerial.print(deviceId, BYTE); // determine which controller gets the command

  if (speed < 0)
  {
    mySerial.print(0x06, BYTE); // Pololu protocol version of motor reverse command
    speed = -speed; // make speed positive
  }
  else
  {
    mySerial.print(0x05, BYTE); // Pololu protocol version of 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);
// lcd.begin(16, 2);
 

  // 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

   
void loop() 
{

setMotorSpeed(300, 0xb);  // full-speed forward for controller 0xb
setMotorSpeed(400, 0xd);  // full-speed forward for controller 0xd

  delay(2000);  // wait for 2 seconds

setMotorSpeed(-300, 0xb);  // full-speed reverse for controller 0xb
  setMotorSpeed(-400, 0xd);  // full-speed reverse for controller 0xd

  delay(2000);  // wait for 2 seconds
}
Plz help me I have a deadline to do dis project and it is very near

Grumpy_Mike

Please go back and modify that post. Select the code and hit the # icon. Then it will appear in a box and it is much easier to read.

However, what you want to do has a bit of a flaw, two motors will not run exactly at the same speed even when powered under the same conditions, are you aware of this?

You are using the delay function between each motor command, this means that the controller is just doing nothing. Look at the blink without delay example in your arduino IDE under the examples menu item to see how to not use the delay, which is what you want to do.

Go Up