I am using Pololu Micro Serial Servo Controller and a Pololu Low-Voltage Dual Serial Motor Controller with a Arduino Diecimilla board. I can successfully command the 2 motors or the 2 servos but not both intermixed. I need to stop the motors then command the servos. Otherwise I may get unpredictable outcomes.
I have connected both the Pololu Micro Serial Servo Controller and a Pololu Low-Voltage Dual Serial Motor Controller to pin 1 (TX) on the Arduino Diecimilla board. Should I be using a serial library and have each controller on separate pins?
Sample code for RobotShop Rover v1.0
#include <Wire.h>
#define MIN_POS0 1800 // the minuimum pulse width for your servos
#define MAX_POS0 4200 // maximum pulse width for your servos
#define MIN_POS1 2700 // the minuimum pulse width for your servos
#define MAX_POS1 4000 // maximum pulse width for your servos
#define srfAddress 0x70 // Address of the SRF08
#define cmdByte 0x00 // Command byte
#define lightByte 0x01 // Byte to read light sensor
#define rangeByte 0x02 // Byte for start of ranging data
// set servo speed, goes from 1 to 127
int servoSpeed = 120; // 1 = fastest 127 = slowest
int motor_reset = 2; //digital pin 2 assigned to motor reset
int irpin = 3;
int speakerOut = 9;
int idistance = 0;
int period;
int timeUp = 680; // 1000000/(2 * frequency)
int pos0 = 3000;
int pos1 = 2500;
struct t_mtab { char c, pat; } ;
struct t_mtab morsetab[] = {
} ;
#define N_MORSE (sizeof(morsetab)/sizeof(morsetab[0]))
#define SPEED (10)
#define DOTLEN (1200/SPEED)
#define DASHLEN (3*(1200/SPEED))
int distance = 0; // variable for storing the distance (cm)
byte highByte = 0x00; // Stores high byte from ranging
byte lowByte = 0x00; // Stored low byte from ranging
void
dash()
{
digitalWrite(speakerOut, LOW);
period = DASHLEN;
while (period > 0)
{
digitalWrite(speakerOut, HIGH);
delayMicroseconds(timeUp);
digitalWrite(speakerOut, LOW);
delayMicroseconds(timeUp);
--period;
}
digitalWrite(speakerOut, LOW);
delay(DOTLEN);
}
void
dit()
{
digitalWrite(speakerOut, LOW);
period = DOTLEN;
while (period > 0)
{
digitalWrite(speakerOut, HIGH);
delayMicroseconds(timeUp);
digitalWrite(speakerOut, LOW);
delayMicroseconds(timeUp);
--period;
}
digitalWrite(speakerOut, LOW);
delay(DOTLEN);
}
void
send(char c)
{
int i ;
if (c == ' ') {
Serial.print(c) ;
delay(7*DOTLEN) ;
return ;
}
for (i=0; i<N_MORSE; i++) {
if (morsetab[i].c == c) {
unsigned char p = morsetab[i].pat ;
Serial.print(morsetab[i].c) ;
while (p != 1) {
if (p & 1)
dash() ;
else
dit() ;
p = p / 2 ;
}
delay(2*DOTLEN) ;
return ;
}
}
/* if we drop off the end, then we send a space */
Serial.print("?") ;
}
void
sendmsg(char *str)
{
while (*str)
send(*str++) ;
Serial.println("");
}
void setup()
{
pinMode(motor_reset, OUTPUT);
pinMode(speakerOut, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600);
delay(100);
// reset motor controller
digitalWrite(motor_reset, LOW);
delay(50);
digitalWrite(motor_reset, HIGH);
delay(50);
// set servo pin and speed
if (servoSpeed >= 1 && servoSpeed <= 127)
{
servoSetSpeed(0, servoSpeed);
servoSetSpeed(1, servoSpeed);
}
int softRev = getSoft(); // Calls function to get software revision
Serial.println(softRev);
}
void loop()
{
pos0 = 2900;
if (pos0 > MAX_POS0)
pos0 = MAX_POS0;
if (pos0 < MIN_POS0)
pos0 = MIN_POS0;
servoSetPosition(0, pos0);
delay(25);
pos1 = 2700;
if (pos1 > MAX_POS1)
pos1 = MAX_POS1;
if (pos1 < MIN_POS1)
pos1 = MIN_POS1;
servoSetPosition(1, pos1);
delay(25);
motorforward();
irdetection();
delay(100); // Wait before looping
// sonicdetection();
// delay(5000);
}
void itoa(int n, int type)
{
}
int getRange(){ // This function gets a ranging from the SRF08
int range = 0;
Wire.beginTransmission(srfAddress); // Start communticating with SRF08
Wire.send(cmdByte); // Send Command Byte
Wire.send(0x51); // Send 0x51 to start a ranging
Wire.endTransmission();
delay(100); // Wait for ranging to be complete
Wire.beginTransmission(srfAddress); // start communicating with SRFmodule
Wire.send(rangeByte); // Call the register for start of ranging data
Wire.endTransmission();
Wire.requestFrom(srfAddress, 2); // Request 2 bytes from SRF module
while(Wire.available() < 2); // Wait for data to arrive
highByte = Wire.receive(); // Get high byte
lowByte = Wire.receive(); // Get low byte
range = (highByte << 8) + lowByte; // Put them together
return(range); // Returns Range
}
int getLight(){ // Function to get light reading
Wire.beginTransmission(srfAddress);
Wire.send(lightByte); // Call register to get light reading
Wire.endTransmission();
Wire.requestFrom(srfAddress, 1); // Request 1 byte
while(Wire.available() < 0); // While byte available
int lightRead = Wire.receive(); // Get light reading
return(lightRead); // Returns lightRead
}
int getSoft(){ // Function to get software revision
Wire.beginTransmission(srfAddress); // Begin communication with the SRF module
Wire.send(cmdByte); // Sends the command bit, when this bit is read it returns the software revision
Wire.endTransmission();
Wire.requestFrom(srfAddress, 1); // Request 1 byte
while(Wire.available() < 0); // While byte available
int software = Wire.receive(); // Get byte
return(software);
}
void irdetection()
{
idistance = analogRead(irpin); // Interface with the Sharp IR Sensor
itoa(idistance, 'R');
int rangeData = getRange(); // Calls a function to get range
itoa(rangeData, 'P');
int lightData = getLight(); // Call function to get light reading and store in lightData
itoa(lightData, 'L');
if (idistance >= 425 && idistance <= 575) // Detecting objects within roughly 10cm
{
motorstop(); // stop the rover
delay(100);
sendmsg("SOS") ;
delay(100);
motorreverse(); // reverse the motors for 1 second
delay(1000);
motorstop(); // stop the motors
delay(1000); // wait 1 second
rotateccw(); // rotate for 2 seconds
delay(1000);
motorstop(); // stop the motors
delay(2000);
sonicdetection();
}
}
void sonicdetection()
{
pos0 = 2900;
servoSetPosition(0, pos0);
delay(25);
pos1 = 2700;
servoSetPosition(1, pos1);
delay(100);
int rangeData = 0;
int j = MAX_POS0;
do
{
if (j < MIN_POS0)
j = MIN_POS0;
servoSetPosition(0, j);
delay(100);
// int softRev = getSoft();
itoa(j, 'H');
rangeData = getRange(); // Calls a function to get range
itoa(rangeData, 'S');
j = j - 50;
} while (j > MIN_POS0);
delay(100); // wait 100 milli seconds before looping again
}
//subroutine motor forward
void motorforward()
{
//left motor
unsigned char buff1[6];
buff1[0]=0x80; //start byte - do not change
buff1[1]=0x00; //Device type byte
buff1[2]=0x01; //Motor number
buff1[3]=0x45; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff1[i], BYTE);}
//right motor
unsigned char buff2[6];
buff2[0]=0x80; //start byte - do not change
buff2[1]=0x00; //Device type byte
buff2[2]=0x03; //Motor number and direction byte; motor two=02,03
buff2[3]=0x45; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff2[i], BYTE);}
}
//subroutine reverse at half speed
void motorreverse()
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80; //start byte - do not change
buff3[1]=0x00; //Device type byte
buff3[2]=0x00; //Motor number and direction byte; motor one =00,01
buff3[3]=0x35; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff3[i], BYTE);}
//right motor
unsigned char buff4[6];
buff4[0]=0x80; //start byte - do not change
buff4[1]=0x00; //Device type byte
buff4[2]=0x02; //Motor number and direction byte; motor two=02,03
buff4[3]=0x35; //Motor speed "0 to 128" (ex 100 is 64 in hex)
for(int i=0; i<4; i++) {Serial.print(buff4[i], BYTE);}
}
//Motor all stop
void motorstop()
{
//left motor
unsigned char buff3[6];
buff3[0]=0x80; //start byte - do not change
buff3[1]=0x00; //Device type byte