35 #include <sensor_msgs/NavSatFix.h>
37 #include<mission_planning/Path.h>
42 #include <postgresql/libpq-fe.h>
47 #define d2r (M_PI / 180.0) //degree to radians
64 PGconn *
conn = PQconnectdb(
"user=atlas password=atlascar dbname=atlas_navigation hostaddr=127.0.0.1 port=5432");
77 printf(
"Database Disconnected - OK\n");
93 conn = PQconnectdb(
"user=atlas password=atlascar dbname=atlas_navigation hostaddr=127.0.0.1 port=5432");
96 if (PQstatus(conn) != CONNECTION_OK)
98 printf(
"Connection to database failed");
102 printf(
"Connection to database - OK\n");
110 if (PQstatus(conn) != CONNECTION_OK)
112 printf(
"Connection to database failed");
116 printf(
"Connection to database - OK\n");
131 std::string sSQL =
"INSERT INTO random (rand_stuff) VALUES ("+ std::to_string(fname) +
")";
133 PGresult *res = PQexec(conn, sSQL.c_str());
135 if (PQresultStatus(res) != PGRES_COMMAND_OK)
137 printf(
"Insert employee record failed");
142 printf(
"Insert employee record - OK\n");
152 PGresult *res = PQexec(conn,
"DELETE FROM random");
154 if (PQresultStatus(res) != PGRES_COMMAND_OK)
156 printf(
"Delete employees record failed.");
161 printf(
"\nDelete employees record - OK\n");
178 std::string sSQL =
"INSERT INTO gps_coord_table (gps_lat,gps_lon) VALUES ("+ std::to_string(gps->latitude) +
"," +std::to_string(gps->longitude)+
")";
180 PGresult *res = PQexec(conn, sSQL.c_str());
182 if (PQresultStatus(res) != PGRES_COMMAND_OK)
184 printf(
"Insert gps coordinates record failed");
189 printf(
"Insert gps coordinates record - OK\n");
207 for (
int i = 0; i < resp.lat.size(); ++i)
209 double glat = resp.lat[i];
210 double glon = resp.lon[i];
211 std::string ginst = resp.instruction[i];
213 std::string sSQL =
"INSERT INTO waypoint_table (way_lat,way_lon,instruction) VALUES ("+ std::to_string(glat) +
"," + std::to_string(glon)+
",'" + ginst +
"')";
215 PGresult *res = PQexec(conn, sSQL.c_str());
217 if (PQresultStatus(res) != PGRES_COMMAND_OK)
219 printf(
"Insert waypoints record failed: %s", PQerrorMessage(conn));
224 printf(
"Insert waypoints record - OK\n");
243 std::string sSQL =
"INSERT INTO closest_waypoint_table (closest_index) VALUES ("+ std::to_string(closestwaypoint) +
")";
245 PGresult *res = PQexec(conn, sSQL.c_str());
247 if (PQresultStatus(res) != PGRES_COMMAND_OK)
249 printf(
"Insert Closest Waypoint record failed");
254 printf(
"Insert Closest Waypoint - OK\n");
273 std::string ginst = resp.instruction[nextinstruction];
274 std::string sSQL =
"INSERT INTO next_inst_table (next_inst) VALUES ('" + ginst +
"')";
275 PGresult *res = PQexec(conn, sSQL.c_str());
277 if (PQresultStatus(res) != PGRES_COMMAND_OK)
279 printf(
"Insert Next Instruction record failed");
284 printf(
"Insert Next Instruction - OK\n");
301 PGresult *res1 = PQexec(conn,
"DELETE FROM closest_waypoint_table");
303 if (PQresultStatus(res1) != PGRES_COMMAND_OK)
305 printf(
"Delete closest waypoint record failed.");
310 printf(
"\nDelete closest waypoint record - OK\n");
317 PGresult *res2 = PQexec(conn,
"DELETE FROM waypoint_table");
319 if (PQresultStatus(res2) != PGRES_COMMAND_OK)
321 printf(
"Delete waypoint table record failed.");
326 printf(
"\nDelete waypoint table record - OK\n");
333 PGresult *res3 = PQexec(conn,
"DELETE FROM gps_coord_table");
335 if (PQresultStatus(res3) != PGRES_COMMAND_OK)
337 printf(
"Delete gps coordinates record failed.");
342 printf(
"\nDelete gps coordinates record - OK\n");
349 PGresult *res4 = PQexec(conn,
"DELETE FROM next_inst_table");
351 if (PQresultStatus(res4) != PGRES_COMMAND_OK)
353 printf(
"Delete next instruction record failed.");
358 printf(
"\nDelete next instruction record - OK\n");
370 PGresult *res = PQexec(conn,
"SELECT * FROM destination_table ORDER BY dest_id DESC LIMIT 1;");
373 if (PQresultStatus(res) != PGRES_TUPLES_OK) {
374 printf(
"No data in table, make sure you submited a valid destination from the Webpage ! Try again");
379 rec_count = PQntuples(res);
381 printf(
"Obtained %d records.\n", rec_count);
383 std::vector<std::string> destRecord;
385 for (
int row=0; row<rec_count; row++) {
388 destRecord.push_back(PQgetvalue(res, row, 1));
389 destRecord.push_back(PQgetvalue(res, row, 2));
390 std::cout << destRecord[0] << std::endl;
391 std::cout << destRecord[1] << std::endl;
413 double haversine_km(
double lat1,
double lon1,
double lat2,
double lon2)
415 double dlon = (lon2 - lon1) *
d2r;
416 double dlat = (lat2 - lat1) *
d2r;
417 double a = pow(sin(dlat/2.0), 2) + cos(lat1*
d2r) * cos(lat2*d2r) * pow(sin(dlon/2.0), 2);
418 double c = 2 * atan2(sqrt(a), sqrt(1-a));
434 double shortestDistance(
const sensor_msgs::NavSatFixPtr &gps,
const mission_planning::Path::Response& resp){
437 std::vector<double> allDists;
443 for (
int i = 0; i < resp.lat.size(); ++i) {
452 double dist =
haversine_km(resp.lat.at(i),resp.lon.at(i),gps->latitude,gps->longitude);
453 allDists.push_back(dist);
457 int min_index = std::min_element(allDists.begin(), allDists.end()) - allDists.begin();
481 double nextInst(
const sensor_msgs::NavSatFixPtr &gps,
const mission_planning::Path::Response& resp){
483 std::vector<double> allDists;
489 for (
int i = 0; i < resp.lat.size(); ++i) {
491 double dist =
haversine_km(resp.lat.at(i),resp.lon.at(i),gps->latitude,gps->longitude);
493 allDists.push_back(dist);
498 int min_index = std::min_element(allDists.begin(), allDists.end()) - allDists.begin();
501 std::vector<double> smallerthan (allDists.size());
504 auto it = std::copy_if (allDists.begin()+min_index+1, allDists.end(), smallerthan.begin(), [](
int i){
return (i<=0.075);} );
505 smallerthan.resize(std::distance(smallerthan.begin(),it));
508 std::vector<int> posis;
510 for(
int i=0; i<smallerthan.size(); ++i)
512 auto it2=find(allDists.begin(),allDists.end(),smallerthan[i]);
514 int pos = distance(allDists.begin(), it2);
516 posis.push_back(pos);
520 std::vector<int> validinstr;
522 if(posis.size() > 0){
523 for (
int i=0;i<posis.size();i++) {
524 if(!resp.instruction[posis[i]].empty())
526 std::cout <<
"Return this instruction, or better, return the index that contains the instruction" <<
"\n";
527 validinstr.push_back(posis[i]);
531 if (validinstr.size() > 0)
533 int valid_inst = validinstr[0];
553 std::pair<double,double>
wrongDirection (
const sensor_msgs::NavSatFixPtr &gps,
const mission_planning::Path::Response &resp,
int minimumIndex){
555 double distance1 = 0;
556 double distance2 = 0;
558 if(minimumIndex+1 <= resp.lat.size()){
560 distance1 =
haversine_km(resp.lat.at(minimumIndex),resp.lon.at(minimumIndex),gps->latitude,gps->longitude);
561 distance2 =
haversine_km(resp.lat.at(minimumIndex+1),resp.lon.at(minimumIndex+1),gps->latitude,gps->longitude);
564 return std::make_pair(distance1,distance2);
618 client =
n.serviceClient<mission_planning::Path>(
"return_path");
628 mission_planning::Path::Request req;
631 std::cout <<
"Choose locations or insert coordinates Lat/Lon " << std::endl;
632 std::cout <<
"Option 1: DEM " << std::endl;
633 std::cout <<
"Option 2: IT " << std::endl;
634 std::cout <<
"Option 3: CRASTO " << std::endl;
635 std::cout <<
"Option 4: REITORIA " << std::endl;
636 std::cout <<
"Option 5: GARAGEM " << std::endl;
637 std::cout <<
"Option 6: PRAIA DA BARRA " << std::endl;
638 std::cout <<
"Option 7: CUSTOM DESTINATION (ex: 40.629418 ; -8.658260)" << std::endl;
639 std::cout <<
"Option 8: DESTINATION FROM WEBPAGE, SELECT A VALID DESTINATION FROM THE WEBPAGE FIRST THEN PRESS 8 " << std::endl;
640 int anwser;
int customLat;
int customLon;
644 req.destLat = 40.629418;
645 req.destLon = -8.658260;
648 req.destLat = 40.643282;
649 req.destLon = -8.659614;
652 req.destLat = 40.623604;
653 req.destLon = -8.657467;
656 req.destLat = 40.631689;
657 req.destLon = -8.657190;
660 req.destLat = 40.634114;
661 req.destLon = -8.657181;
664 req.destLat = 40.633250;
665 req.destLon = -8.745934;
668 std::cin >> customLat ;
670 std::cin >> customLon ;
671 req.destLat = customLat;
672 req.destLon = customLon;
676 std::string::size_type sz;
682 req.destLat = 40.635114;
683 req.destLon = -8.656467;
689 req.gpsLat = msg->latitude;
690 req.gpsLon = msg->longitude;
718 printf(
"Closest waypoint to the current gps location is: %10.6f %10.6f \n",
resp.lat[minIndex],
resp.lon[minIndex]);
719 std::cout <<
"Instruction TODO: " <<
resp.instruction[minIndex] << std::endl;
724 std::cout <<
"Upcoming Instruction: " <<
resp.instruction[nextInstruction] << std::endl;
734 static int sentinel = 0;
735 static double closeDist1;
736 static double closeDist2;
740 closeDist1 = p.first;
741 closeDist2 = p.second;
743 }
else if (sentinel == 1) {
744 if(closeDist1 !=0 && closeDist2 !=0){
746 double closeDist12 = p2.first;
747 double closeDist22= p2.second;
750 if((closeDist12 > closeDist1) && closeDist12 >= 0.075 && (closeDist22 > closeDist2) && closeDist22 >= 0.075)
754 mission_planning::Path::Request req;
756 req.destLat =
resp.lat.back();
757 req.destLon =
resp.lon.back();
760 req.gpsLat = msg->latitude;
761 req.gpsLon = msg->longitude;
766 printf(
"Recalculating Route...");
799 bool processData(
const sensor_msgs::NavSatFixPtr &gps,
const mission_planning::Path::Response&
resp){
827 mission_planning::Path::Response
resp;
841 int main(
int argc,
char** argv)
863 ros::init(argc, argv,
"gps_Subscriber");
void InsertGpsCoordRec(PGconn *conn, const sensor_msgs::NavSatFixPtr &gps)
std::pair< double, double > wrongDirection(const sensor_msgs::NavSatFixPtr &gps, const mission_planning::Path::Response &resp, int minimumIndex)
void InsertWaypointsRec(PGconn *conn, const mission_planning::Path::Response &resp)
double nextInst(const sensor_msgs::NavSatFixPtr &gps, const mission_planning::Path::Response &resp)
int main(int argc, char **argv)
void InsertClosestWaypointRec(PGconn *conn, int closestwaypoint)
ros::ServiceClient client
double haversine_km(double lat1, double lon1, double lat2, double lon2)
void RemoveAllTablesRec(PGconn *conn)
void CloseConn(PGconn *conn)
void RemoveAllEmployeeRec(PGconn *conn)
GpsTest(ros::NodeHandle nh_)
std::vector< std::string > FetchEmployeeRec(PGconn *conn)
void DBConnectionCheck(PGconn *conn)
void InsertNextInstructionRec(PGconn *conn, const mission_planning::Path::Response &resp, int nextinstruction)
void gpsCallback(const sensor_msgs::NavSatFixPtr &msg)
double shortestDistance(const sensor_msgs::NavSatFixPtr &gps, const mission_planning::Path::Response &resp)
void InsertEmployeeRec(PGconn *conn, double fname)
mission_planning::Path::Response resp
bool processData(const sensor_msgs::NavSatFixPtr &gps, const mission_planning::Path::Response &resp)