Roboclaw (2x60) always has an extra command execution
Posted: Tue Nov 27, 2018 7:30 am
Setup:
I am using an arduino to talk to two roboclaws (2x60) via packet serial. The arduino has two serial TX ports being connected to each roboclaw (i.e. Serial1 -> RoboClaw1 and Serial2 -> RoboClaw2). The 5V and GND pins from RoboClaw1 is used to power the arduino. A single voltage supply is connected to each roboclaw. When turning on the voltage supply, the roboclaws receive power and they both have their STAT1 light turn green and subsequently the arduino begins receiving power.
The Issue:
After everything is powered, I connect the arduino to a PC via USB. Once connection is established, the roboclaw that is not powering the arduino (in this case Roboclaw2) now has both the STAT1 and STAT2 lights turn green. When I send a command from PC -> Arduino, Roboclaw1 (the one powering the arduino) executes that command twice while Roboclaw2 (the one not powering the arduino) executes the command only once. I checked to make sure that the arduino is not sending duplicate signals to any of the roboclaws and it is indeed not. Any help would be greatly appreciated!
I am using an arduino to talk to two roboclaws (2x60) via packet serial. The arduino has two serial TX ports being connected to each roboclaw (i.e. Serial1 -> RoboClaw1 and Serial2 -> RoboClaw2). The 5V and GND pins from RoboClaw1 is used to power the arduino. A single voltage supply is connected to each roboclaw. When turning on the voltage supply, the roboclaws receive power and they both have their STAT1 light turn green and subsequently the arduino begins receiving power.
The Issue:
After everything is powered, I connect the arduino to a PC via USB. Once connection is established, the roboclaw that is not powering the arduino (in this case Roboclaw2) now has both the STAT1 and STAT2 lights turn green. When I send a command from PC -> Arduino, Roboclaw1 (the one powering the arduino) executes that command twice while Roboclaw2 (the one not powering the arduino) executes the command only once. I checked to make sure that the arduino is not sending duplicate signals to any of the roboclaws and it is indeed not. Any help would be greatly appreciated!
Code: Select all
#include "RoboClaw.h"
#define address_m1 0x80
#define address_m2 0x81
RoboClaw robo_x(&Serial1, 1000);
RoboClaw robo_y(&Serial2, 1000);
void setup() {
Serial.begin(38400);
robo_x.begin(38400);
robo_y.begin(38400);
}
void loop() {
while(Serial.available() > 0) {
String incoming;
incoming = Serial.readStringUntil('\n');
int beginning = incoming.indexOf("z");
String temp = incoming.substring(beginning+1, incoming.length());
int size = temp.length();
char* inputarray = new char[size+1];
strcpy(inputarray, temp.c_str());
inputarray[size] = '\0';
int controllerCMDS[8];
char* outputs = strtok(inputarray, ",");
int index = 0;
while (outputs != 0) {
controllerCMDS[index] = strtol(outputs, NULL, 10);
outputs = strtok(0, ",");
index++;
}
// Current controllerCMDS array configuration:
// index[0] = input for motor 1 of roboclaw_x
// index[1] = time delay for input 1
// index[2] = input for motor 2 of roboclaw_x
// index[3] = time delay for input 2
// index[4] = input for motor 1 of roboclaw_y
// index[5] = time delay for input 3
// and so on..
if (controllerCMDS[0] > 0) {
robo_x.ForwardM1(address_m1, controllerCMDS[0]);
delay(controllerCMDS[1]);
robo_x.ForwardM1(address_m1, 0);
} else if (controllerCMDS[0] < 0) {
robo_x.BackwardM1(address_m1, controllerCMDS[0]);
delay(controllerCMDS[1]);
robo_x.ForwardM1(address_m1, 0);
} else {
robo_x.ForwardM1(address_m1, 0);
delay(controllerCMDS[1]);
}
if (controllerCMDS[2] > 0) {
robo_x.ForwardM2(address_m1, controllerCMDS[2]);
delay(controllerCMDS[3]);
robo_x.ForwardM2(address_m1, 0);
} else if (controllerCMDS[2] < 0) {
robo_x.BackwardM2(address_m1, controllerCMDS[2]);
delay(controllerCMDS[3]);
robo_x.ForwardM2(address_m1, 0);
} else {
robo_x.ForwardM2(address_m1, 0);
delay(controllerCMDS[3]);
}
if (controllerCMDS[4] > 0) {
robo_y.ForwardM1(address_m2, controllerCMDS[4]);
delay(controllerCMDS[5]);
robo_y.ForwardM1(address_m2, 0);
} else if (controllerCMDS[4] < 0) {
robo_y.BackwardM1(address_m2, controllerCMDS[4]);
delay(controllerCMDS[5]);
robo_y.ForwardM1(address_m2, 0);
} else {
robo_y.ForwardM1(address_m2, 0);
delay(controllerCMDS[5]);
}
if (controllerCMDS[6] > 0) {
robo_y.ForwardM2(address_m2, controllerCMDS[6]);
delay(controllerCMDS[7]);
robo_y.ForwardM2(address_m2, 0);
} else if (controllerCMDS[6] < 0) {
robo_y.BackwardM2(address_m2, controllerCMDS[6]);
delay(controllerCMDS[7]);
robo_y.ForwardM2(address_m2, 0);
} else {
robo_y.ForwardM2(address_m2, 0);
delay(controllerCMDS[7]);
}
}
}