2
$\begingroup$

I have an Arduino connected to ROS through serial port. I wrote an ardiuno code to drive motors. below is my code.

//Library to communicate with I2C devices
    #include "Wire.h"
    #include <Messenger.h>
    //Contain definition of maximum limits of various data type
    #include <limits.h>
     //Messenger object
    Messenger Messenger_Handler = Messenger();

    //////////////////////////////////////////////////////////////////////////////////////
    //Motor Pin definition
    //Left Motor
    #define USE_USBCOM
    #define INA_1 7
    #define INB_1 12    
      //PWM 1 pin 
    #define PWM_1 5

    //Right Motor
    #define INA_2 11 
    #define INB_2 10

    //PWM 2 pin 
    #define PWM_2 6
    #define RESET_PIN 4

    ///////////////////////////////////////////////////////////////////////////////////////
    //Motor speed from PC
    //Motor left and right speed
    float motor_left_speed = 0;
    float motor_right_speed = 0;
    /////////////////////////////////////////////////////////////////

    //Setup serial, motors and Reset functions
    void setup()
    {

      //Init Serial port with 115200 baud rate
      Serial.begin(57600);  

      //Setup Motors
      SetupMotors();
       SetupReset();
      //Set up Messenger 
      Messenger_Handler.attach(OnMssageCompleted);
       }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Setup Motors() function

    void SetupMotors()
    {

     //Left motor
     pinMode(INA_1,OUTPUT);
     pinMode(INB_1,OUTPUT); 

     //Right Motor
     pinMode(INA_2,OUTPUT);
     pinMode(INB_2,OUTPUT);  

    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Setup Reset() function

    void SetupReset()

    {

      pinMode(RESET_PIN,OUTPUT);

      ///Conenect RESET Pins to the RESET pin of launchpad,its the 16th PIN
      digitalWrite(RESET_PIN,HIGH);


    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //MAIN LOOP

    void loop()
    {

        //Read from Serial port
        Read_From_Serial();


        //Update motor values with corresponding speed and send speed values through serial port
        Update_Motors();

    delay(1000);
      }
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Read from Serial Function

    void Read_From_Serial()

    {
       while(Serial.available() > 0)
        {

           int data = Serial.read();
           Messenger_Handler.process(data);

        } 

    }
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //OnMssg Complete function definition

    void OnMssageCompleted()
    {

      char reset[] = "r";
      char set_speed[] = "s";

      if(Messenger_Handler.checkString(reset))
      {


         Reset();

      }
      if(Messenger_Handler.checkString(set_speed))
      {

         //This will set the speed
         Set_Speed();
         return; 


      }
    }
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Set speed
    void Set_Speed()
    {

      motor_left_speed = Messenger_Handler.readLong();
      motor_right_speed = Messenger_Handler.readLong();


    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Reset function
    void Reset()
    {

      delay(1000);
      digitalWrite(RESET_PIN,LOW);

    }
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Will update both motors
    void Update_Motors()
    {

      moveRightMotor(motor_right_speed);
      moveLeftMotor(motor_left_speed);

     }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //Motor running function


    void moveRightMotor(float rightServoValue)
    {
      if (rightServoValue>0)
      {

     digitalWrite(INA_1,HIGH);
     digitalWrite(INB_1,LOW);
     analogWrite(PWM_1,rightServoValue);

      }
      else if(rightServoValue<0)
      {
     digitalWrite(INA_1,LOW);
     digitalWrite(INB_1,HIGH);
     analogWrite(PWM_1,abs(rightServoValue));

      }

      else if(rightServoValue == 0)
      {
     digitalWrite(INA_1,HIGH);
     digitalWrite(INB_1,HIGH);


      }
    }
   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    void moveLeftMotor(float leftServoValue)
    {
     if (leftServoValue > 0)
      {
    digitalWrite(INA_2,LOW);
    digitalWrite(INB_2,HIGH);
    analogWrite(PWM_2,leftServoValue);
      }
      else if(leftServoValue < 0)
      {
     digitalWrite(INA_2,HIGH);
     digitalWrite(INB_2,LOW);
     analogWrite(PWM_2,abs(leftServoValue));

      }
      else if(leftServoValue == 0)
      {

       digitalWrite(INA_2,HIGH);
       digitalWrite(INB_2,HIGH);

       }  

    }

when i load the code to arduino and start rosserial node using command

rosrun rosserial_python serial_node.py /dev/ttyACM0

ROS throws below error

[ERROR] [WallTime: 1475949610.718804] Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino

I have tried changing the baud rate and fixing it similar in ROS and arduino but nothing helped

$\endgroup$
1
  • $\begingroup$ I have a similar issue when using a development board instead of my development pc. At one moment, the development board worked 50% percent of the time but now it is just not working at all. The arduino is connected to the development board thourgh USB. $\endgroup$ – François Pilote Oct 26 '16 at 20:33
1
$\begingroup$

i suggest you take a look at Tones' answer : http://answers.ros.org/question/210875/arduino-rosserial-unable-to-sync-with-device/?answer=237868#post-id-237868

i'm using the second workaround :

  • Start the serial_node with baud rate 0, exit and restart the serial node with the correct baud rate. You have to repeat this every time the error occurs.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.