Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Split magnetometer node into C++ class and header #817

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion igvc_gazebo/nodes/magnetometer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(mag_republisher main.cpp)
add_executable(mag_republisher Magnetometer.cpp)
add_dependencies(mag_republisher ${catkin_EXPORTED_TARGETS})
target_link_libraries(mag_republisher ${catkin_LIBRARIES})

Expand Down
36 changes: 36 additions & 0 deletions igvc_gazebo/nodes/magnetometer/Magnetometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "Magnetometer.h"

#include <ros/ros.h>
#include <geometry_msgs/Vector3Stamped.h>
#include <sensor_msgs/MagneticField.h>

Magnetometer::Magnetometer()
{
ros::NodeHandle nh;
ros::NodeHandle pnh("~");
std::string sub_topic = pnh.param("mag_sub_topic", std::string("/magnetometer/vector"));
std::string pub_topic = pnh.param("mag_pub_topic", std::string("/magnetometer_mag"));
g_mag_field_covar = pnh.param("mag_field_variance", 1e-6);
g_mag_field_pub = nh.advertise<sensor_msgs::MagneticField>(pub_topic, 10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are still using the g_ prefix, while the member variable does not have it. Remove the g_ prefix.

ros::Subscriber scan_sub = nh.subscribe(sub_topic, 1, magCallback);
}

void magCallback(const geometry_msgs::Vector3Stamped& msg)
{
sensor_msgs::MagneticField magnet_msg;
magnet_msg.header.seq = msg.header.seq;
magnet_msg.header.stamp = msg.header.stamp;
magnet_msg.header.frame_id = msg.header.frame_id;
magnet_msg.magnetic_field.x = msg.vector.x;
magnet_msg.magnetic_field.y = msg.vector.y;
magnet_msg.magnetic_field.z = msg.vector.z;
magnet_msg.magnetic_field_covariance = { g_mag_field_covar, 0, 0, 0, g_mag_field_covar, 0, 0, 0, g_mag_field_covar };
g_mag_field_pub.publish(magnet_msg);
}

int main(int argc, char** argv)
{
ros::init(argc, argv, "mag_republisher");
Magnetometer m;
ros::spin();
}
18 changes: 18 additions & 0 deletions igvc_gazebo/nodes/magnetometer/Magnetometer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Magnetometer.h
#ifndef Magnetometer_H
#define Magnetometer_H

#include <ros/ros.h>
#include <sensor_msgs/MagneticField.h>

class Magnetometer
{

public:
Magnetometer::Magnetometer();
private:
ros::Publisher mag_field_pub;
static double mag_field_covar;
void magCallback(const geometry_msgs::Vector3Stamped& msg);
};
#endif
32 changes: 0 additions & 32 deletions igvc_gazebo/nodes/magnetometer/main.cpp

This file was deleted.