-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmea_to_geopose.cpp
181 lines (170 loc) · 4.68 KB
/
nmea_to_geopose.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <nmea_to_geopose/nmea_to_geopose.h>
NmeaToGeoPose::NmeaToGeoPose(ros::NodeHandle nh,ros::NodeHandle pnh)
{
nh_ = nh;
pnh_ = pnh;
pnh_.param<std::string>("input_topic", input_topic_, "/nmea_sentence");
nmea_sub_ = nh_.subscribe(input_topic_,10,&NmeaToGeoPose::nmeaSentenceCallback,this);
geopose_pub_ = pnh_.advertise<geographic_msgs::GeoPoseStamped>("geopose",1);
}
NmeaToGeoPose::~NmeaToGeoPose()
{
}
void NmeaToGeoPose::nmeaSentenceCallback(const nmea_msgs::Sentence::ConstPtr msg)
{
nmea_msgs::Sentence sentence = *msg;
if(isGprmcSentence(sentence))
{
geographic_msgs::GeoPoint geopoint;
boost::optional<std::vector<std::string> > data = splitSentence(sentence);
if(data)
{
std::string lat_str = data.get()[3];
std::string north_or_south_str = data.get()[4];
double latitude = std::stod(lat_str.substr(0,2)) + std::stod(lat_str.substr(2))/60.0;
ROS_ASSERT(north_or_south_str == "N" || north_or_south_str == "S");
if(north_or_south_str == "S")
{
latitude = latitude*-1;
}
std::string lon_str = data.get()[5];
std::string east_or_west_str = data.get()[6];
double longitude = std::stod(lon_str.substr(0,3)) + std::stod(lon_str.substr(3))/60.0;
ROS_ASSERT(east_or_west_str == "E" || east_or_west_str == "W");
if(east_or_west_str == "W")
{
longitude = longitude*-1;
}
geopoint.latitude = latitude;
geopoint.longitude = longitude;
geopoint.altitude = 0.0;
geopoint_ = geopoint;
}
}
if(isGphdtSentence(sentence))
{
boost::optional<std::vector<std::string> > data = splitSentence(sentence);
if(data)
{
if(data.get()[2] == "T")
{
double heading = std::stod(data.get()[1]);
geometry_msgs::Vector3 vec;
vec.x = 0.0;
vec.y = 0.0;
vec.z = heading/180*M_PI*-1;
geometry_msgs::Quaternion quat =
quaternion_operation::convertEulerAngleToQuaternion(vec);
quat_ = quat;
}
}
}
if(geopoint_ && quat_)
{
if(!last_timestamp_ || last_timestamp_ != msg->header.stamp)
{
last_timestamp_ = msg->header.stamp;
geographic_msgs::GeoPoseStamped geopose;
geopose.pose.position = geopoint_.get();
geopose.pose.orientation = quat_.get();
geopose.header = msg->header;
geopose_pub_.publish(geopose);
}
}
}
std::vector<std::string> NmeaToGeoPose::split(const std::string &s,char delim)
{
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (getline(ss, item, delim))
{
if (!item.empty())
{
elems.push_back(item);
}
}
return elems;
}
std::vector<std::string> NmeaToGeoPose::splitChecksum(std::string str)
{
return split(str,'*');
}
boost::optional<std::vector<std::string> > NmeaToGeoPose::splitSentence(nmea_msgs::Sentence sentence)
{
std::vector<std::string> data = splitChecksum(sentence.sentence);
if(data.size() != 2)
{
return boost::none;
}
if(calculateChecksum(data[0]) == data[1])
{
return split(data[0],',');
}
return boost::none;
}
bool NmeaToGeoPose::isGprmcSentence(nmea_msgs::Sentence sentence)
{
std::string type = sentence.sentence.substr(0,6);
if(type == "$GPRMC")
{
return true;
}
return false;
}
bool NmeaToGeoPose::isGphdtSentence(nmea_msgs::Sentence sentence)
{
std::string type = sentence.sentence.substr(0,6);
if(type == "$GPHDT")
{
return true;
}
return false;
}
std::string NmeaToGeoPose::calculateChecksum(std::string sentence)
{
uint8_t checksum;
for(int i=1; i<sentence.size(); i++)
{
int16_t c = sentence[i];
checksum ^= c;
}
uint8_t rest = checksum%16;
uint8_t quotient = (checksum-rest)/16;
std::string ret = getHexString(quotient) + getHexString(rest);
return ret;
}
std::string NmeaToGeoPose::getHexString(uint8_t value)
{
ROS_ASSERT(value <= 16);
std::string ret;
if(value == 10)
{
ret = "A";
}
else if(value == 11)
{
ret = "B";
}
else if(value == 12)
{
ret = "C";
}
else if(value == 13)
{
ret = "D";
}
else if(value == 14)
{
ret = "E";
}
else if(value == 15)
{
ret = "F";
}
else
{
ret = std::to_string(value);
}
return ret;
}