Go Down

Topic: Serial communication with labview using Xbee (Read 700 times) previous topic - next topic

wkdxoghdi

Aug 07, 2015, 08:45 am Last Edit: Aug 07, 2015, 08:46 am by wkdxoghdi
Hello. :)
I am controlling a mobile robot using the Arduino due and labview
PWM value caculated on the labview is sent to Arduino due and then Due controls the robot.
These communicate wirelessly by xbee.

Now, I'd like to increase the communication speed between Arduino due and Labview.
but, when i set the value of the "Wait(ms)" function to below 10ms  in Labview, communication has unstability. (present my communication speed(loop time) is 10ms)

so please let me know how increase the communication speed.
thank you.

-experiment image-
https://www.youtube.com/watch?v=oZdMCdHlDhM

Code: [Select]
[hr]
#include <string.h>

int PWM1 = 5; 
int DIR1 = 4; //direc
int PWM2 = 6;       
int DIR2 = 7; //dir

float readNumber = 0;
float In1, In2, pwm, newtime, A;

float newposition1 = 0;
float newposition2 = 0;

unsigned int Aold1 = 0;
unsigned int Bnew1 = 0;

unsigned int Aold2 = 0;
unsigned int Bnew2 = 0;

volatile long encoder1Pos = 0;
volatile long encoder2Pos = 0;

int encoder1PinA = 8;//Encoder A pin
int encoder1PinB = 9;//Encoder B pin
int encoder2PinA = 11;//Encoder A pin
int encoder2PinB = 12;//Encoder B pin
int Direction1, Direction2;

float pwm1_limit=255;
float pwm2_limit=255;
float count_old = 0;
float power1_old, power1_old2, power1_old3, power1_old4, power1_old5;

void setup() {
  // put your setup code here, to run once:
  pinMode(DIR1, OUTPUT);   
  pinMode(DIR2, OUTPUT);

  Serial.begin(115200);
  Serial.flush();
  Serial2.begin(115200);
  Serial2.flush();
 
  pinMode(encoder1PinA, INPUT);
  digitalWrite(encoder1PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder1PinB, INPUT);
  digitalWrite(encoder1PinB, HIGH);       // turn on pullup resistor
 
  pinMode(encoder2PinA, INPUT);
  digitalWrite(encoder2PinA, HIGH);       // turn on pullup resistor
  pinMode(encoder2PinB, INPUT);
  digitalWrite(encoder2PinB, HIGH);       // turn on pullup resistor
 
  attachInterrupt(encoder1PinA, d1EncoderA, CHANGE);
  attachInterrupt(encoder1PinB, d1EncoderB, CHANGE);
  attachInterrupt(encoder2PinA, d2EncoderA, CHANGE);
  attachInterrupt(encoder2PinB, d2EncoderB, CHANGE);
 
}

void loop() {
    if(Serial2.available()>0){
        String first  = Serial2.readStringUntil('!');
//랩뷰에서 받는 2개의 값을 분리하기 위한 소스("!"앞까지 끊고, "@"앞까지 끊음 ex)123!456@을 //받으면 123과 456으로 나눔)
        String second = Serial2.readStringUntil('@');
//http://stackoverflow.com/questions/29504679/splitting-a-comma-separated-string-through-serial-arduino
       
       float x = first.toFloat();//위에서 분리된 문자를 float형으로 바꿔줌
       float y = second.toFloat();
       
        newposition1 = -encoder1Pos; //Encoder signal
        newposition2 = encoder2Pos; //Encoder signal
       
           count_old = 0;
       
       float power1 = x;
       float power2 = y;
           
       
 ////**************************************/////
if(power1 >= 0){
   Direction1 = LOW;
}

else{
   Direction1 = HIGH;
}
 /*////******************************************////
 
  if(power2 >= 0){
   Direction2 = HIGH;
}

else {
   Direction2 = LOW;
}
////*****************************************///////

        power1 = abs(power1);
        power2 = abs(power2);
       
 if(power1 > pwm1_limit){
    power1 = pwm1_limit;
  }   
 
if(power2 > pwm2_limit){
    power2 = pwm2_limit;
  } 
       
digitalWrite(DIR1, Direction1);  //HIGH = back, LOW = forward
digitalWrite(DIR2, Direction2);  //HIGH = back, LOW = forward

    analogWrite(PWM1, power1);//PWM Speed Control
    analogWrite(PWM2, power2);//PWM Speed Control
       
    String strValue = String(newposition1);//숫자를 문자형으로 변환
    String Q = "!";
    String strValue2 = String(newposition2);
    String stringThree = strValue + Q + strValue2;//문자 합치기
 
     Serial2.println(stringThree);//Send encoder pulse to Labview
   
   }

else {//when Serial communication stop
   float count = count_old+1;
  if(count > 10000){
        newposition1 = 0; //Encoder initialization
        newposition2 = 0; //Encoder initialization
        encoder1Pos = 0;
        encoder2Pos = 0;
              }
      else{
         newposition1 = newposition1; //Encoder signal
         newposition2 = newposition2; //Encoder signal
      }
      count_old = count;
      Serial.println(count);
}
}


////////////////////////////////////////////////////////////////////////
//Encoder Read//////////////////////////////////////////////////////////
void d1EncoderA()
{
  Bnew1^Aold1 ? encoder1Pos++ : encoder1Pos--;
  Aold1 = digitalRead(encoder1PinA);
}
void d1EncoderB()
{
  Bnew1 = digitalRead(encoder1PinB);
  Bnew1^Aold1 ? encoder1Pos++ : encoder1Pos--;
}

void d2EncoderA()
{
  Bnew2^Aold2 ? encoder2Pos++:encoder2Pos--;
  Aold2=digitalRead(encoder2PinA);
}
void d2EncoderB()
{
  Bnew2=digitalRead(encoder2PinB);
  Bnew2^Aold2 ? encoder2Pos++:encoder2Pos--;
}



PaulS

Code: [Select]
  Serial.begin(115200);
  Serial.flush();
  Serial2.begin(115200);
  Serial2.flush();

Open the port. Block until all pending output data has been sent. Why? That makes no sense.
The art of getting good answers lies in asking good questions.

Go Up