ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ultrasonic on raspberry

asked 2016-05-05 08:24:07 -0500

Emilien gravatar image

hi, i want to measure a distance of ultrasonic from raspberry, i wrote this code but i receive some error, could you help me please?

#include "ros/ros.h"
#include <ros/time.h>
#include <std_msgs/UInt16.h>
#include <iostream>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>

#define TRIG 2
#define ECHO 3

using namespace std;

uint32_t time1=0,time2=0;
uint32_t time_diff=0;
float Range_cm=0;
volatile int flag=0;

void show_distance()
  {
    cout<<"distance= "<<time1<<" "<<time2<<" "<<time_diff<<" "<<Range_cm<<" cm\n";
    cout.flush();
    delay(1000);
    digitalWrite(2,0);
    delayMicroseconds(1);
    digitalWrite(2,1);
    delayMicroseconds(10);
    digitalWrite(2,0);
  }

void myInterrupt(void)
 {
    if(flag==0)
      {
            time1=micros();
            flag=1;

      }
    else
      {
            time2=micros();
            flag=0;
            time_diff=time2-time1;
            Range_cm=time_diff/58;
            show_distance();

       }

  }


int main(int argc, char **argv)
{
        if(wiringPiSetup()<0)
     {
       cout<<"wiringPiSetup failed !!\n";
     }
    pinMode(2,OUTPUT);
    pinMode(3,INPUT);
    pullUpDnControl(3,PUD_DOWN);
    if(wiringPiISR(3,INT_EDGE_BOTH,&myInterrupt) < 0)
            {
            cerr<<"interrupt error ["<<strerror (errno)<< "]:"<<errno<<endl;
            return 1;
            }
    digitalWrite(2,0);
    delayMicroseconds(1);
    digitalWrite(2,1);
    delayMicroseconds(10);
    digitalWrite(2,0);

    ros::init(argc, argv, "servo_publisher");
    ros::NodeHandle n;
    ros::Publisher servo_pub = n.advertise<std_msgs::UInt16>("/servo", 1000);
    ros::Rate loop_rate(40);
    int count = 0;
    while(ros::ok())
    {
        while(1)
        {
            std_msgs::UInt16 cmd_msg;
            cmd_msg.data = 180;
            ROS_INFO("%i",cmd_msg.data);
            servo_pub.publish(cmd_msg);
            sleep(1);
            cmd_msg.data = 0;
            ROS_INFO("%i",cmd_msg.data);    
            servo_pub.publish(cmd_msg);
            sleep(1);
            cmd_msg.data = 45;
            ROS_INFO("%i",cmd_msg.data);
            servo_pub.publish(cmd_msg);
            sleep(1);
            cmd_msg.data = 90;
            ROS_INFO("%i",cmd_msg.data);
            servo_pub.publish(cmd_msg);
            sleep(1);
            cmd_msg.data = 135;
            ROS_INFO("%i",cmd_msg.data);
            servo_pub.publish(cmd_msg);
            sleep(1);
        }
        ros::spinOnce();
        loop_rate.sleep();
        ++count;
    }
    return 0;
}

i receive this error:

/usr/bin/ld: warning: libboost_system.so.1.54.0, needed by /opt/ros/indigo/lib/libtf.so, may conflict with libboost_system.so.1.55.0
/usr/bin/ld: warning: libboost_thread.so.1.54.0, needed by /opt/ros/indigo/lib/libtf2_ros.so, may conflict with libboost_thread.so.1.55.0
/usr/bin/ld: warning: libboost_filesystem.so.1.54.0, needed by /opt/ros/indigo/lib/libroscpp.so, may conflict with libboost_filesystem.so.1.55.0
/usr/bin/ld: warning: libboost_regex.so.1.54.0, needed by /opt/ros/indigo/lib/librosconsole.so, may conflict with libboost_regex.so.1.55.0
CMakeFiles/control.dir/src/control.cpp.o: In function `show_distance()':
control.cpp:(.text+0xc0): undefined reference to `delay'
control.cpp:(.text+0xcc): undefined reference to `digitalWrite'
control.cpp:(.text+0xd4): undefined reference to `delayMicroseconds'
control.cpp:(.text+0xe0): undefined reference to `digitalWrite'
control.cpp:(.text+0xe8): undefined reference to `delayMicroseconds'
control.cpp:(.text+0xf4): undefined reference to `digitalWrite'
CMakeFiles/control.dir/src/control.cpp.o: In function `myInterrupt()':
control.cpp:(.text+0x144): undefined reference to `micros'
control.cpp:(.text+0x164): undefined reference to `micros'
CMakeFiles/control.dir/src/control.cpp.o: In function `main':
control.cpp:(.text+0x1f4): undefined reference to `wiringPiSetup'
control.cpp:(.text+0x220): undefined reference to `pinMode'
control.cpp:(.text+0x22c): undefined reference to `pinMode'
control.cpp:(.text+0x238): undefined reference ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-08-17 21:45:07 -0500

You need to link wiringPi library to you cmakeLists.txt. It may be on your LD_LIBRARY_PATH. If so all you need to do is in you CMakeLists.txt:

target_link_libraries( your_node wiringPi ${catkin_LIBRARIES})
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-05-05 08:24:07 -0500

Seen: 446 times

Last updated: Aug 17 '16