40 #include <curl/curl.h>
41 #include <boost/asio.hpp>
55 #include <mission_planning/Path.h>
68 typedef boost::shared_ptr<GpsLocation>
Ptr;
75 GpsLocation(
double latitude,
double longitude,
string maneuver){
76 this->latitude = latitude;
77 this->longitude = longitude;
78 this->maneuver = maneuver;
88 if(loc->maneuver.empty())
89 o<<
"Lat:" <<loc->latitude<<
" Lon: "<<loc->longitude<<
" Man: (none)";
91 o<<
"Lat:" <<loc->latitude<<
" Lon: "<<loc->longitude<<
" Man: "<<loc->maneuver;
107 vector<double>
Decompress(
string encodedPoints,
int precision){
109 int len = encodedPoints.length();
113 vector<double> array;
115 while (index < len) {
121 b = (int)encodedPoints[index++] - 63;
123 result |= (b & 31) << shift;
126 int dlat= ((result & 1) ? ~(result >> 1) : (result >> 1));
132 b = (int)encodedPoints[index++]- 63;
133 result |= (b & 31) << shift;
136 int dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
139 array.push_back(lat*pow(10,-precision));
140 array.push_back(lng*pow(10,-precision));
150 data->append((
const char*)ptr, size * nmemb);
166 mission_planning::Path::Request &req,
167 mission_planning::Path::Response &resp)
169 double startLat = req.gpsLat;
170 double startLon = req.gpsLon;
171 double destLat = req.destLat;
172 double destLon = req.destLon;
177 string htmlquery =
"http://193.137.172.18:5000/viaroute?loc="+to_string(startLat)+
","+to_string(startLon)+
"&loc="+to_string(destLat)+
","+to_string(destLon)+
"&instructions=true";
191 curl = curl_easy_init();
199 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
202 curl_easy_setopt(curl, CURLOPT_URL, htmlquery.c_str());
206 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
209 res = curl_easy_perform(curl);
212 fprintf(stderr,
"curl_easy_perform() failed: %s\n",
213 curl_easy_strerror(res));
216 curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &http_code);
219 curl_easy_cleanup(curl);
220 curl_global_cleanup();
228 if (http_code == 200){
230 std::cout <<
"\nServer OK, JSON file acquired " << std::endl;
235 if (jsonReader.
parse(data, jsonData))
237 std::cout <<
"Successfully parsed JSON data" << std::endl;
238 std::cout <<
"\nJSON data received:" << std::endl;
243 if (invalid_coord == 207){printf(
"Error no route found between the requested points, Check Gps Signal\n");exit(0);}
247 Json::Value encodedLine = jsonData[
"route_geometry"];
248 cout <<
"FIELD STATUS = " << encodedLine <<
"\n" << endl;
251 vector<pair<double,double> > waypoints;
254 for (
int i = 0; i < data.size(); i+=2) {
255 waypoints.push_back(make_pair(data[i],data[i+1]));
257 printf (
"%lf , %lf \n",data[i],data[i+1]);
262 string turnInstruction [18]={
"NoTurn",
"GoStraight",
"TurnSlightRight",
"TurnRight",
"TurnSharpRight",
"UTurn",
263 "TurnSharpLeft",
"TurnLeft",
"TurnSlightLeft",
"ReachViaLocation",
"HeadOn",
"EnterRoundAbout",
264 "LeaveRoundAbout",
"StayOnRoundAbout",
"StartAtEndOfStreet",
"ReachedYourDestination",
265 "EnterAgainstAllowedDirection",
"LeaveAgainstAllowedDirection"};
268 Json::Value instructions = jsonData[
"route_instructions"];
270 cout <<
"Size of instructions = "<< instructions.
size() << endl;
276 std::vector<int> instPos;
277 std::vector<string>instTodo;
279 for (
int i = 0; i < instructions.
size(); i++) {
282 instPos.push_back(instructions[i][3].asInt());
285 string dir = instructions[i][0].
asString();
286 if(dir.find(
"-")!=std::string::npos)
288 int a = dir.find(
"-");
289 string found = dir.substr(0,a);
290 string found2 = dir.substr(a+1,dir.size());
292 string fullInst = (turnInstruction[atoi(found.c_str())] +
" and " + turnInstruction[atoi(found2.c_str())]);
294 instTodo.push_back(fullInst);
299 int dirOK = atoi(dir.c_str());
302 instTodo.push_back(turnInstruction[dirOK]);
318 cout<<
"Waypoints size: "<<waypoints.size()<<endl;
321 std::vector<GpsLocation::Ptr> locations;
326 for (
int i = 0; i < waypoints.size(); i++) {
328 vector<int>::iterator it = find(instPos.begin(),instPos.end(),i);
329 int index = it - instPos.begin();
331 if(it!=instPos.end()){
335 locations.push_back(loc1);
340 locations.push_back(loc1);
346 cout <<
"Size of locations: " << locations.size() << endl;
347 for (
int i = 0; i < locations.size(); ++i) {
352 cout << location << endl;
359 resp.lat.push_back((*locations[i]).latitude);
360 resp.lon.push_back((*locations[i]).longitude);
361 resp.instruction.push_back((*locations[i]).maneuver);
364 ROS_INFO(
"Request complete");
370 std::cout <<
"Could not parse HTTP data as JSON" << std::endl;
371 std::cout <<
"HTTP data was:\n" << data << std::endl;
372 ROS_ERROR(
"Could not parse HTTP, failed to send request");
383 int main(
int argc,
char** argv)
387 ros::init(argc, argv,
"mission_response_server");
391 ros::ServiceServer server = nh.advertiseService(
"return_path",&
return_path);
392 ROS_INFO(
"Ready to send mission!");
int main(int argc, char **argv)
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
vector< double > Decompress(string encodedPoints, int precision)
GpsLocation(double latitude, double longitude, string maneuver)
boost::shared_ptr< GpsLocation > Ptr
ostream & operator<<(ostream &o, const GpsLocation::Ptr &loc)
size_t write_to_string(void *ptr, size_t size, size_t nmemb, std::string *data)
std::string asString() const
Embedded zeroes are possible.
std::string toStyledString() const
Unserialize a JSON document into a Value.
ArrayIndex size() const
Number of values in array or object.
bool return_path(mission_planning::Path::Request &req, mission_planning::Path::Response &resp)