Unable to sync with arduino

asked 2017-05-05 16:38:25 -0500

srivats2794 gravatar image

updated 2017-05-17 15:02:45 -0500

Hi all

I'm really new to ROS, Robotics and electronics and I'm figuring out things just now. I built a code for a basic sense and avoid. I have a cpp file, launch file, CMake file and package xml file. after I catkin make and use roslaunch, I get the error " Unable to sync with device; possible link problem or link software version mismatch such as hydro rosserial_python with groovy Arduino". I tried suggestions of baud rate, USE_USBCON and I also reinstalled rosserial and made sure its all kinetic (UBUNTU 16.04). I also changed the cable and checked on the basis of one suggestion. Still this problem persists. I have attached all my files. Kindly help even if i have made a stupid mistake.

Thank you

CPP file:

#define USE_USBCON
#include <ros/ros.h>
#include "geometry_msgs/Twist.h"
#include "std_msgs/UInt64.h"
#include "std_msgs/Int16.h"

#define LINEAR_SPEED 1.4
#define ANGULAR_SPEED 4.0
#define TURN_DURATION 500
#define REVERSE_DURATION 400
#define OBJECT_DIST_NEAR 30
#define OBJECT_DIST_SAFE 60

enum STATE { FORWARD, REVERSE, TURN };

class SenseAndAvoid 

{  
 public:
        SenseAndAvoid();

  private:
    void leftEncoderCallback(const std_msgs::UInt64::ConstPtr& msg);
    void rightEncoderCallback(const std_msgs::UInt64::ConstPtr& msg);
    void sonarCallback(const std_msgs::Int16::ConstPtr& msg);

    ros::NodeHandle nh;

    ros::Publisher vel_pub;

    ros::Subscriber left_encoder_sub;
    ros::Subscriber right_encoder_sub;
    ros::Subscriber sonar_sub;

    geometry_msgs::Twist vel_msg;

    long left_count, right_count;
    long last_count;
    STATE state; 
};

SenseAndAvoid::SenseAndAvoid()


{   

left_encoder_sub = nh.subscribe<std_msgs::UInt64>("/arduino/encoder_left_value", 10, &SenseAndAvoid::leftEncoderCallback, this);   

right_encoder_sub = nh.subscribe<std_msgs::UInt64>("/arduino/encoder_right_value", 10, &SenseAndAvoid::rightEncoderCallback, this);   

sonar_sub = nh.subscribe<std_msgs::Int16>("/arduino/sonar_2", 10, &SenseAndAvoid::sonarCallback, this);

  vel_pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 10);

  state = FORWARD;   left_count = 0;   right_count = 0; 
}


void SenseAndAvoid::leftEncoderCallback(const std_msgs::UInt64::ConstPtr& msg) 
{ left_count = msg->data; }

void SenseAndAvoid::rightEncoderCallback(const std_msgs::UInt64::ConstPtr& msg) 
{  right_count = msg->data; }

void SenseAndAvoid::sonarCallback(const std_msgs::Int16::ConstPtr& msg)
 {   
if(state == FORWARD && msg->data < OBJECT_DIST_NEAR && msg->data > 5)  
{
        ROS_INFO("REVERSE");
        state = REVERSE;
        vel_msg.linear.x = -LINEAR_SPEED;
        vel_msg.angular.z = 0;
        last_count = left_count;   
 }   
 else if (state == FORWARD) 
 {
        vel_msg.linear.x = LINEAR_SPEED;
        vel_msg.angular.z = 0;  
 }   
 else if(state == REVERSE && msg->data > OBJECT_DIST_SAFE && left_count > REVERSE_DURATION + last_count) 
 {
        ROS_INFO("TURN");
        state = TURN;
        vel_msg.linear.x = LINEAR_SPEED / 2;
        vel_msg.angular.z = -ANGULAR_SPEED;
        last_count = left_count;   
 }    
else if (state == TURN && left_count > TURN_DURATION + last_count) 
 {
        state = FORWARD;
        ROS_INFO("FORWARD");
        vel_msg.linear.x = LINEAR_SPEED;
        vel_msg.angular.z = 0;    }    vel_pub.publish(vel_msg);  
}
        int main(int argc, char **argv) {   ros::init(argc, argv, "sense_and_avoid");

      SenseAndAvoid sense_and_avoid;

      ros::spin();  
}

CMake:

cmake_minimum_required(VERSION 2.8.3)
project(lab2_sense_and_avoid)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  cv_bridge
  image_transport
)

catkin_package(
  CATKIN_DEPENDS roscpp rospy std_msgs cv_bridge image_transport
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(sense_and_avoid src/sense_and_avoid.cpp)
edit retag flag offensive close merge delete

Comments

Can you edit this to have proper code indentation so it formats properly? It's hard to follow as-is.

jwhendy gravatar image jwhendy  ( 2017-05-16 22:02:21 -0500 )edit
1

Thanks for your reply. I have edited as much as possible.

My problem is When I connect using rosrun rosserial_python serial.node.py to my arduino it connects when the baud rate is 57600 but not at 115200. So I even included the baud rate as 57600 in my ROS launch file. But even that doesnt help.

srivats2794 gravatar image srivats2794  ( 2017-05-17 15:06:24 -0500 )edit

This is typically a problem with your arduino code or the launch file that launches the rosserial_python node; it would help if those were included in your question.

ahendrix gravatar image ahendrix  ( 2017-05-17 17:48:15 -0500 )edit

To clarify, you're saying that you can rosrun directly, just not roslaunch? In that case, I agree with @ahendrix... the launch file would seem super pertinent here. Since you can connect, it's probably not the case, but permissions to /dev/tty/ACM0 can be a source of trouble sometimes.

jwhendy gravatar image jwhendy  ( 2017-05-17 20:30:32 -0500 )edit

this was my original launch file

<launch>
      <include file="$(find jet_bringup)/launch/jet_real.launch"/>
      <node name="sense_and_avoid" pkg="lab2_sense_and_avoid" type="sense_and_avoid"/>
    </launch>
srivats2794 gravatar image srivats2794  ( 2017-05-18 09:24:30 -0500 )edit

Then when my baud rate wasnt matching I added 2 more lines:

<param name="port" value="/dev/ttyACM0"/>
    <param name="baud" value="57600"/>
srivats2794 gravatar image srivats2794  ( 2017-05-18 09:26:09 -0500 )edit

And @jwhendy my rosrun is working if i run at 57600 baud rate but not at 115200. I made sure I had permissions for /dev/tty/ACM0 so that isnt the problem.

srivats2794 gravatar image srivats2794  ( 2017-05-18 09:30:51 -0500 )edit

Silly question (but no .ino file to examine): what are you putting in Serial.begin(xxx);?

jwhendy gravatar image jwhendy  ( 2017-05-18 17:09:12 -0500 )edit