Creating an example project to use GPS

In this example, we are going to project the latitude and the longitude of GPS to a 2D Cartesian space. For this, we will use a function written by Chuck Gantz that converts latitudes and longitudes into UTM coordinates. The node will subscribe to the /fix topic where GPS data is sent. You can find the code in chapter8_tutorials in the c8_fixtoUTM.cpp file:

#include <ros/ros.h> 
#include <tf/transform_broadcaster.h> 
#include <nav_msgs/Odometry.h> 
#include <stdio.h> 
#include <iostream> 
#include <sensor_msgs/NavSatFix.h> 
geometry_msgs::Point global_position; 
ros::Publisher position_pub; 
void gpsCallBack(const sensor_msgs::NavSatFixConstPtr& gps) 
{ 
  double northing, easting; 
  char zone; 
  LLtoUTM(gps->latitude, gps->longitude, northing, easting , 
&zone); global_position.x = easting; global_position.y = northing; global_position.z = gps->altitude; } int main(int argc, char** argv){ ros::init(argc,argv, "fixtoUTM"); ros::NodeHandle n; ros::Subscriber gps_sub = n.subscribe("fix",10, gpsCallBack); position_pub = n.advertise<geometry_msgs::Point>
("global_position", 1); ros::Rate loop_rate(10); while(n.ok()) { ros::spinOnce(); loop_rate.sleep(); } }

First, you should declare the NavSatFix message using #include <sensor_msgs/NavSatFix.h>.

This way, we can subscribe to the /fix topic in the ros::Subscriber gps_sub = n.subscribe("fix",10, gpsCallBack) main function.

All the action happens in the gpsCallBack() function. We will use the LltoUTM() function to make the conversion from latitudes and longitudes to the UTM space. We will publish a geometry_msg/Point topic named /global_position with the UTM northing and easting coordinates and the altitude from the GPS.

To try this code, after running the GPS driver, you can use the following command:

    $ rosrun chapter8_tutorials c8_fixtoUTM
  

You can use GPS data to improve your robot localization using Kalman filters to fuse odometry and IMU with NavSatFix data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.53