Issues with seg-faults and node not publishing anything [closed]
I hate asking about seg-fault but it's just part of the story. Below is my code:
cmake_minimum_required(VERSION 2.8.3)
project(maple)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
sensor_msgs
urdf
message_generation
)
add_message_files(
DIRECTORY
msg
FILES
Euler_Thrust.msg
ESCsArray.msg
)
generate_messages(
DEPENDENCIES
std_msgs
sensor_msgs
)
catkin_package(CATKIN_DEPENDS message_runtime)
include_directories(include ${catkin_INCLUDE_DIRS})
${catkin_EXPORTED_TARGETS})
add_executable(teleop_keyboard src/teleop_keyboard.cpp)
target_link_libraries(teleop_keyboard ${catkin_LIBRARIES})
add_dependencies(teleop_keyboard ${${PROJECT_NAME}_EXPORTED_TARGETS}
${catkin_EXPORTED_TARGETS})
add_executable(P_2 P_2/P_2.cpp)
target_link_libraries(P_2 ${catkin_LIBRARIES})
add_dependencies(P_2 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
add_executable(ROS_PCA9685 P_2/ROS_PCA9685.cpp)
add_library(PCA9685_D P_2/PCA9685_D.cpp)
target_link_libraries(ROS_PCA9685 PCA9685_D ${catkin_LIBRARIES})
add_dependencies(ROS_PCA9685 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
P_2.cpp:
#include <vector>
#include <algorithm>
#include <ros/ros.h>
#include <sensor_msgs/Imu.h>
#include <maple/ESCsArray.h>
#include <maple/Euler_Thrust.h>
#include <trajectory_msgs/MultiDOFJointTrajectory.h>
#include <math.h>
#define dt 0.01
class P_2 {
public:
std::vector<double> imu_, euler_t;
void KeyboardCallback(const maple::Euler_Thrust::ConstPtr& msg){
euler_t.clear();
euler_t.push_back((msg->psi)*3.14159265359/180);
euler_t.push_back((msg->phi)*3.14159265359/180);
euler_t.push_back((msg->theta)*3.14159265359/180);
euler_t.push_back(msg->thrust);
}
void imuCallback(const sensor_msgs::Imu::ConstPtr &imu) {
imu_.clear();
imu_.push_back(imu->orientation.x);
imu_.push_back(imu->orientation.y);
imu_.push_back(imu->orientation.z);
imu_.push_back(imu->orientation.w);
imu_.push_back(imu->angular_velocity.x);
imu_.push_back(imu->angular_velocity.y);
imu_.push_back(imu->angular_velocity.z);
return;
}
P_2() {
ros::NodeHandle nh;
ros::Publisher escs_pub = nh.advertise<maple::ESCsArray>("ESCs",1);
ros::Subscriber imu_sub = nh.subscribe("imu", 1, &P_2::imuCallback, this);
ros::Subscriber DOF_sub = nh.subscribe("KeyboardTeleop", 1, &P_2::KeyboardCallback, this);
maple::ESCsArray msg;
//NOTE: this is where math is supposed to happen with the imu_ and euler_t vectors,
//but a seg fault results since the two vectors never populate for some reason
//The value 2.0 was pushed into the vector ESCs_ for debugging purposes
msg.ESCs_.push_back(2.0);
escs_pub.publish(msg);
}
~P_2() {}
};
int main(int argc, char **argv){
ros::init(argc, argv, "P_2");
//NOTE: also for some reason the main function seems to be stuck on the init argument
P_2 p_2;
ros::spin();
return 0;
}
ESCsArray.msg:
float64[] ESCs_
Euler_Thrust.msg:
float64 thrust
float64 psi #roll
float64 phi #pitch
float64 theta #yaw
Aside from certain kinks in the code, this is my first time working with cmake and I kinda winged it. It seems to work, but I am not too sure if it does properly. The subscriber to the KeyboardTeleop topic is working fine as I verified it with rostopic echo, but it never seems to reach the subscriber in the P_2 node. SImilarly, the node P_2 initializes but does not publish data on the ESCs topic as indicated by doing rosnode list (no /ESCs topic). I'm out of ideas and I'm not sure what is wrong with my code. Any help is appreciated.
Can you please update your question with a copy and paste of the error?
There is no error with the code, the node just isn’t publishing or subscribing to anything. I managed to get the topics to show (it was a scoping issue) but the set fault remains. It is obviously due to when the imu_ and Euler_t vectors are used they are not populated. Any ideas?
Duplicate of q#278225.