1+
2+ #include " ros/ros.h"
3+ #include " gazebo_msgs/ModelState.h"
4+ #include < geometry_msgs/Pose2D.h>
5+ #include < tf2/LinearMath/Quaternion.h>
6+
7+ class SetState {
8+ private:
9+ ros::NodeHandle nh;
10+ ros::Publisher pub_state = nh.advertise<gazebo_msgs::ModelState>(" /gazebo/set_model_state" , 1000 );
11+ ros::Subscriber sub_state = nh.subscribe(" /vectornav/ins_2d/NED_pose" , 1000 , &SetState::stateCallback, this );
12+ tf2::Quaternion q;
13+ gazebo_msgs::ModelState model_msg;
14+
15+ public:
16+ void stateCallback (const geometry_msgs::Pose2D::ConstPtr& msg);
17+ };
18+
19+ void SetState::stateCallback (const geometry_msgs::Pose2D::ConstPtr& msg){
20+ model_msg.model_name = " vtec_s3" ;
21+
22+ // NED to ENU
23+ q.setRPY (0 ,0 ,-msg->theta );
24+
25+ model_msg.pose .position .x = msg->x ;
26+ model_msg.pose .position .y = -msg->y ;
27+ model_msg.pose .position .z = 0 ;
28+
29+ model_msg.pose .orientation .x = q.x ();
30+ model_msg.pose .orientation .y = q.y ();
31+ model_msg.pose .orientation .z = q.z ();
32+ model_msg.pose .orientation .w = q.w ();
33+
34+ pub_state.publish (model_msg);
35+ }
36+
37+ int main (int argc, char **argv){
38+ ros::init (argc,argv, " gazebo_interface" );
39+ SetState state;
40+ ros::Rate rate (100 );
41+
42+ while (ros::ok){
43+ rate.sleep (); // wait 10 Hz
44+ ros::spinOnce (); // will call all the callbacks waiting to be called at that point in time
45+ }
46+
47+ return 0 ;
48+ }
0 commit comments