Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Added watchdog #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion wesley/ros/src/commander/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "exit_handlers.h"
#include <string>
#include <unistd.h>

#include <signal.h>
#include <watchdog.h>
//#include <thread.h> Include if using watchdog
using std::string;

#define grasp() executeBinary("rostopic pub -1 /arm/put/point wesley/arm_point '{direct_mode: false, cmd: grasp}'", "");
Expand Down Expand Up @@ -116,6 +118,11 @@ int executeBinary(string binaryName, string prefix, string mode ){
if(f == 0){
return -2;
}
/**
* Uncomment this to turn on watchdogging
//WatchDog watch;
//std::thread(watch,binaryName);
*/
//Get return value, don't ask why it's this but it is. It's from the stack overflow on popen.
//
// http://bytes.com/topic/c/answers/131694-pclose-returning-termination-status-command#post472837
Expand All @@ -126,3 +133,5 @@ int executeBinary(string binaryName, string prefix, string mode ){
// man 2 waitpid) did not shed any extra light on this.
return pclose(f)/256;
}


28 changes: 28 additions & 0 deletions wesley/ros/src/commander/src/watchdog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "watchdog.h"
#include <unistd.h>
#include <stdio.h>
#include <sstream>
#include <thread>
#include <chrono>
using std::stringstream;

void WatchDog::execute_pkill(string name){
stringstream ss;
ss << "pkill " << name;
FILE * f = popen(ss.str().c_str(),"r");
pclose(f);
}

void WatchDog::tick(){
amountCalled++;
}

void WatchDog::operator()(string binaryName){
amountCalled = 0;
while(amountCalled < checkAmount){
tick();
std::this_thread::sleep_for(std::chrono::seconds(seconds_to_sleep));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires c++11

}
execute_pkill(binaryName);
}

33 changes: 33 additions & 0 deletions wesley/ros/src/commander/src/watchdog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <string>
#include <unistd.h>
#include <signal.h>
#include <thread>
#include <map>
#include <stdio.h>
using std::map;
using std::string;

#ifndef WATCHDOG_H
#define WATCHDOG_H
/**
* Watch dog is a functor object that should be called as such
* WatchDog watch;
* std::thread(watch,binaryName);
*/
class WatchDog{
string name;
int amountCalled;
int checkAmount; /*<The time in near-seconds for the thread to be alive before it's killed */
const int seconds_to_sleep = 1;
//Tick through each process updating the amount of times it's been called in call_map and kills it if it's been checked checkAmount times
void tick();
void execute_pkill(string name);

public:
WatchDog(){checkAmount=1000;}
WatchDog(int amountOfTicksBeforeKilled){checkAmount=amountOfTicksBeforeKilled;}
void operator()(string binaryName);

};
#endif