Hello Guys,
I am Chetan, and I am doing 2 DOF using x sim, arduino and Pololu simple motor controller 18v25. I did all the connections and everything, and there is no error in the program but still the motors are not running...

so can you guys please help me out with this program. Is there anybody who did this on Pololu motor controller....?
Its the same program by racingmat...but changed accordingly to pololu motor controllers....
#include <SoftwareSerial.h>
#define BRAKEVCC 0
#define STOP 0
#define BRAKEGND 3
////////////////////////////////////////////////////////////////////////////////
#define pwmMax 255 // or less, if you want to lower the maximum motor's speed
// defining the range of potentiometer's rotation
const int potMini=20;
const int potMaxi=620;
////////////////////////////////////////////////////////////////////////////////
#define motLeft 0
#define motRight 1
#define potL A0
#define potR A5
int DataValueL=512; //middle position 0-1024
int DataValueR=512; //middle position 0-1024
#define rxPin1 3 // pin 3 connects to smcSerial TX (not used in this example)
#define txPin1 4 // pin 4 connects to smcSerial RX
SoftwareSerial smcSerial1(rxPin1, txPin1);
#define rxPin2 7 // pin 3 connects to smcSerial TX (not used in this example)
#define txPin2 8 // pin 4 connects to smcSerial RX
SoftwareSerial smcSerial2(rxPin2, txPin2);
////////////////////////////////////////////////////////////////////////////////
// INITIALIZATION
////////////////////////////////////////////////////////////////////////////////
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
smcSerial1.write(0x83);
smcSerial2.write(0x83);
}
void setup()
{
// initialize software serial object with baud rate of 19.2 kbps
smcSerial1.begin(19200);
smcSerial2.begin(19200);
// 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
smcSerial1.write(0xAA); // send baud-indicator byte
smcSerial2.write(0xAA);
// 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
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Loop ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void loop()
{
int sensorL,sensorR;
readSerialData(); // DataValueR & L contain the last order received (if there is no newer received, the last is kept)
// the previous order will still be used by the PID regulation MotorMotion Function
sensorR = analogRead(potR); // range 0-1024
sensorL = analogRead(potL); // range 0-1024
motorMotion(motRight,sensorR,DataValueR);
motorMotion(motLeft,sensorL,DataValueL);
}
////////////////////////////////////////////////////////////////////////////////
// Procedure: wait for complete trame
////////////////////////////////////////////////////////////////////////////////
void readSerialData()
{
byte Data[3]={
'0','0','0' };
// keep this function short, because the loop has to be short to keep the control over the motors
if (Serial.available()>2){
//parse the buffer : test if the byte is the first of the order "R"
Data[0]=Serial.read();
if (Data[0]=='L'){
Data[1]=Serial.read();
Data[2]=Serial.read();
// call the function that converts the hexa in decimal and that maps the range
DataValueR=NormalizeData(Data);
}
if (Data[0]=='R'){
Data[1]=Serial.read();
Data[2]=Serial.read();
// call the function that converts the hexa in decimal and maps the range
DataValueL=NormalizeData(Data);
}
}
if (Serial.available()>16) Serial.flush();
}
////////////////////////////////////////////////////////
void motorMotion(int numMot,int actualPos,int targetPos)
////////////////////////////////////////////////////////
{
int Tol=20; // no order to move will be sent to the motor if the target is close to the actual position
// this prevents short jittering moves
//could be a parameter read from a pot on an analogic pin
// the highest value, the calmest the simulator would be (less moves)
int gap;
int pwm;
int brakingDistance=30;
// security concern : targetPos has to be within the mechanically authorized range
targetPos=constrain(targetPos,potMini+brakingDistance,potMaxi-brakingDistance);
gap=abs(targetPos-actualPos);
if (gap<= Tol) {
motorOff(numMot); //too near to move
}
else {
// PID : calculates speed according to distance
pwm=195;
if (gap>50) pwm=215;
if (gap>75) pwm=235;
if (gap>100) pwm=255;
pwm=map(pwm, 0, 255, 0, 3200); //adjust the value according to pwmMax for mechanical debugging purpose !
// if motor is outside from the range, send motor back to the limit !
// go forward (up)
if ((actualPos<potMini) || (actualPos<targetPos)) motorGo(numMot, pwm);
// go reverse (down)
if ((actualPos>potMaxi) || (actualPos>targetPos)) motorRev(numMot, pwm);
}
}
////////////////////////////////////////////////////////////////////////////////
void motorOff(int motor){ //Brake Ground : free wheel actually
////////////////////////////////////////////////////////////////////////////////
smcSerial1.write(0xE0);
smcSerial2.write(0xE0);
//analogWrite(pwmpin[motor], 0);
}
void motorGo(uint8_t motor, int speed)
{
if (motor == 0)
{ smcSerial2.write(0x85); // full-speed forward
smcSerial2.write(speed & 0x1F);
smcSerial2.write(speed >> 5);
}
else
{ smcSerial1.write(0x85); // full-speed forward
smcSerial1.write(speed & 0x1F);
smcSerial1.write(speed >> 5);
}
}
void motorRev(uint8_t motor, int speed)
{
if (motor == 0)
{ smcSerial2.write(0x86); // full-speed forward
smcSerial2.write(speed & 0x1F);
smcSerial2.write(speed >> 5);
}
else
{ smcSerial1.write(0x86); // full-speed forward
smcSerial1.write(speed & 0x1F);
smcSerial1.write(speed >> 5);
}
}
////////////////////////////////////////////////////////////////////////////////
// Function: convert Hex to Dec
////////////////////////////////////////////////////////////////////////////////
int NormalizeData(byte x[3])
////////////////////////////////////////////////////////////////////////////////
{
int result;
if ((x[2]==13) || (x[2]=='R') || (x[2]=='L')) //only a LSB and Carrier Return or 'L' or 'R' in case of value below 16 (ie one CHAR and not 2)
{
x[2]=x[1]; //move MSB to LSB
x[1]='0'; //clear MSB
}
for (int i=1; i<3; i++)
{
if (x[i]>47 && x[i]<58 ){//for x0 to x9
x[i]=x[i]-48;
}
if (x[i]>64 && x[i]<71 ){//for xA to xF
x[i]=(x[i]-65)+10;
}
}
// map the range from Xsim (0 <-> 255) to the mechanically authorized range (potMini <-> potMaxi)
result=map((x[1]*16+x[2]),0,255,potMini,potMaxi);
return result;
}