PingThread.cpp
Go to the documentation of this file.
00001 
00022 #include "PingThread.h"
00023 #include "cvd/thread.h"
00024 #include "ros/ros.h"
00025 #include <iostream>
00026 #include <stdio.h>
00027 #include "RosThread.h"
00028 #include "uga_tum_ardrone_gui.h"
00029 
00030 PingThread::PingThread()
00031 {
00032         line1[0] = '\0';
00033         line2[0] = '\0';
00034         keepRunning = true;
00035         started = false;
00036         measure = true;
00037     ip = std::string("");
00038 
00039         p500 = 25;
00040         p20000 = 50;
00041 
00042         rosThread = NULL;
00043         gui = NULL;
00044 }
00045 
00046 PingThread::~PingThread(void)
00047 {
00048 
00049 
00050 }
00051 
00052 void PingThread::startSystem()
00053 {
00054         keepRunning = true;
00055         start();
00056 }
00057 
00058 void PingThread::stopSystem()
00059 {
00060         keepRunning = false;
00061         join();
00062 }
00063 
00064 void PingThread::setIp(std::string newip)
00065 {
00066     bool wasRunning = started;
00067     if (wasRunning)
00068         stopSystem();
00069 
00070     ip = newip;
00071 
00072     if (wasRunning)
00073         startSystem();
00074 }
00075 
00076 double parsePingResult(std::string s)
00077 {
00078         // 20008 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.075 ms
00079         size_t pos = s.find("time=");
00080         int found = 0;
00081         float ms;
00082         if(pos != std::string::npos)
00083                 found = sscanf(s.substr(pos).c_str(),"time=%f",&ms);
00084 
00085         if(found == 1 && pos != std::string::npos)
00086                 return ms;
00087         else
00088                 return 10000;
00089 }
00090 
00091 void PingThread::run()
00092 {
00093         std::cout << "Starting PING Thread" << std::endl;
00094     started = true;
00095         sprintf(pingCommand20000,"ping -c 1 -s 20000 -w 1 %s", ip.c_str());
00096         sprintf(pingCommand500,"ping -c 1 -s 500 -w 1 %s", ip.c_str());
00097         ros::Rate r(2.0);
00098         FILE *p;
00099 
00100         while(keepRunning)
00101         {
00102                 if(measure)
00103                 {
00104                         // ping twice, with a sleep in between
00105                         p = popen(pingCommand500,"r");
00106                         fgets(line1, 200, p);
00107                         fgets(line1, 200, p);
00108                         pclose(p);
00109 
00110                         // sleep 1s
00111                         r.sleep();
00112                         if(!keepRunning) break;
00113                         r.sleep();
00114                         if(!keepRunning) break;
00115 
00116 
00117 
00118                         p = popen(pingCommand20000,"r");
00119                         fgets(line2, 200, p);
00120                         fgets(line2, 200, p);
00121                         pclose(p);
00122 
00123 
00124                         // parse results which should be in line1 and line2
00125                         double res500 = parsePingResult(line1);
00126                         double res20000 = parsePingResult(line2);
00127 
00128                         std::cout << "new ping values: 500->" << res500 << " 20000->" << res20000 << std::endl;
00129 
00130                         // clip between 10 and 1000.
00131                         res500 = std::min(1000.0,std::max(1.0,res500));
00132                         res20000 = std::min(1000.0,std::max(1.0,res20000));
00133 
00134                         // update
00135                         p500 = 0.7 * p500 + 0.3 * res500;
00136                         p20000 = 0.7 * p20000 + 0.3 * res20000;
00137 
00138                         // send
00139                         snprintf(line1,200,"pings %d %d", (int)p500, (int)p20000);
00140                         if(rosThread != NULL) rosThread->publishCommand(line1);
00141                         if(gui != NULL) gui->setPings((int)p500, (int)p20000);
00142 
00143                         // sleep 1s
00144                         r.sleep();
00145                         if(!keepRunning) break;
00146                         r.sleep();
00147                         if(!keepRunning) break;
00148                 }
00149                 else
00150                 {
00151                         r.sleep();
00152                         if(!keepRunning) break;
00153                         r.sleep();
00154                         if(!keepRunning) break;
00155                         r.sleep();
00156                         if(!keepRunning) break;
00157                         r.sleep();
00158                         if(!keepRunning) break;
00159 
00160                         // send
00161                         snprintf(line1,200,"pings %d %d", (int)p500Default, (int)p20000Default);
00162                         if(rosThread != NULL) rosThread->publishCommand(line1);
00163                         if(gui != NULL) gui->setPings((int)p500Default, (int)p20000Default);
00164                 }
00165         }
00166 
00167         std::cout << "Exiting PING Thread" << std::endl;
00168 }


uga_tum_ardrone
Author(s):
autogenerated on Sat Jun 8 2019 20:30:11