Hi Team,
In the below code i m trying to do these steps in Order.
#1. In mqtt callback - publish a message to topic
#2. After publish trying to send Ins to LeArm Robot
#3. After this instruction again publish a message to a topic.
In this case If i comment out #1 and #3 Robot Instruction works fine. If i uncomment the #1 and #3. Publish works fine but not able to control Robot Arm. Can someone help me on this.
CODE:
String moveList[2]={String("1500:1500:1500:1500:1500:1500"), String("800:1500:1500:1500:1500:1500")};
void callback(char* topic, byte* payload, unsigned int length)
{
// Allocate the correct amount of memory for the payload copy
byte* message = (byte*)malloc(length);
// Copy the payload to the new buffer
memcpy(message,payload,length);
if (!strncmp((char *)message, "Move", length)) {
mqttStatus = mqtt.publish("orchestration/machine/LeArmRobot-6001245/updateCosmodb", "LeArmRobot-6001245:false");
if(mqttStatus){
runCommand();
}
mqttStatus = mqtt.publish("orchestration/machine/LeArmRobot-6001245/updateCosmodb", "LeArmRobot-6001245:true");
}
// Free the memory
free(message);
}
void runCommand(){
String part01,part02,part03, part04, part05, part06;
for (int i = 0;i < NUMITEMS(moveList);i++) {
part01 = getValue(i,':',0);
part02 = getValue(i,':',1);
part03 = getValue(i,':',2);
part04 = getValue(i,':',3);
part05 = getValue(i,':',4);
part06 = getValue(i,':',5);
servos[0].ID = 1;
servos[0].Position = servoM1 = part01.toInt();
servos[1].ID = 2;
servos[1].Position = servoM2 = part02.toInt();
servos[2].ID = 3;
servos[2].Position = servoM3 = part03.toInt();
servos[3].ID = 4;
servos[3].Position = servoM4 = part04.toInt();
servos[4].ID = 5;
servos[4].Position = servoM5 = part05.toInt();
servos[5].ID = 6;
servos[5].Position = servoM6 = part06.toInt();
myse.moveServos(servos,6,1000);
aOPC.processOPCCommands();
delay(4000);
}
}
String getValue(int idx, char separator, int index)
{
String res;
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = moveList[idx].length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(moveList[idx].charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
res = found>index ? moveList[idx].substring(strIndex[0], strIndex[1]) : "";
return res;
}