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 to `pullUpDnControl'
control.cpp:(.text+0x248): undefined reference to `wiringPiISR'
control.cpp:(.text+0x304): undefined reference to `digitalWrite'
control.cpp:(.text+0x30c): undefined reference to `delayMicroseconds'
control.cpp:(.text+0x318): undefined reference to `digitalWrite'
control.cpp:(.text+0x320): undefined reference to `delayMicroseconds'
control.cpp:(.text+0x32c): undefined reference to `digitalWrite'
collect2: error: ld returned 1 exit status
control/CMakeFiles/control.dir/build.make:107: recipe for target '/home/pi/catkin_ws/devel/lib/control/control' failed
make[2]: *** [/home/pi/catkin_ws/devel/lib/control/control] Error 1
CMakeFiles/Makefile2:521: recipe for target 'control/CMakeFiles/control.dir/all' failed
make[1]: *** [control/CMakeFiles/control.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
Originally posted by Emilien on ROS Answers with karma: 167 on 2016-05-05
Post score: 0