Skip to content

Commit

Permalink
robManipulator: LoadRobot(filename) now look for suffix .json and loa…
Browse files Browse the repository at this point in the history
…ds as json if this the case, as .rob otherwise
  • Loading branch information
adeguet1 committed Nov 5, 2024
1 parent de6d31a commit 96a7a11
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
49 changes: 44 additions & 5 deletions cisstRobot/code/robManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,62 @@ void robManipulator::DeleteTools()
tools.clear();
}

robManipulator::Errno robManipulator::LoadRobot( const std::string& filename ){

if( filename.empty() ){
robManipulator::Errno robManipulator::LoadRobot(const std::string & filename)
{
if (filename.empty()) {
mLastError = "robManipulator::LoadRobot: no configuration file";
CMN_LOG_RUN_ERROR << mLastError << std::endl;
return robManipulator::EFAILURE;
}

std::ifstream ifs;
ifs.open( filename.data() );
if(!ifs){
ifs.open(filename.data());
if (!ifs) {
mLastError = "robManipulator::LoadRobot: couldn't open configuration file "
+ filename;
CMN_LOG_RUN_ERROR << mLastError << std::endl;
return robManipulator::EFAILURE;
}

// find extension, search for json
size_t dot = filename.find_last_of(".");
std::string extension = "";
if (dot != std::string::npos) {
extension = filename.substr(dot, filename.size() - dot);
}

if (extension != ".json") {
return LoadRobot(ifs);
}

// otherwise json
#if CISST_HAS_JSON
Json::Reader jsonReader;
Json::Value jsonConfig;
if (!jsonReader.parse(ifs, jsonConfig)) {
mLastError = "robManipulator::LoadRobot: syntax error while parsing json file "
+ filename + "\n" + jsonReader.getFormattedErrorMessages();
CMN_LOG_RUN_ERROR << mLastError << std::endl;
return robManipulator::EFAILURE;
} else {
// dVRK files have a DH field
Json::Value jsonDH = jsonConfig["DH"];
if (jsonDH.isNull()) {
return LoadRobot(jsonConfig);
} else {
return LoadRobot(jsonDH);
}
}
#else
mLastError = "robManipulator::LoadRobot: cisst was compiled without JSON support, can't load "
+ filename;
CMN_LOG_RUN_ERROR << mLastError << std::endl;
return robManipulator::EFAILURE;
#endif
}

robManipulator::Errno robManipulator::LoadRobot(std::istream & ifs)
{
size_t N; // the number of links
{
std::string line;
Expand Down
8 changes: 7 additions & 1 deletion cisstRobot/robManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ class CISST_EXPORT robManipulator{


//! Load the kinematics and the dynamics of the robot
virtual robManipulator::Errno LoadRobot( const std::string& linkfile );
/** If the file name ends with .json it will open the file
and then call the overloaded method LoadRobot for Json::Value.
Otherwise, assumes it's the .rob format and calls the overloaded
method LoadRobot for istream. */
virtual robManipulator::Errno LoadRobot(const std::string & linkfile);

virtual robManipulator::Errno LoadRobot(std::istream & ifs);

#if CISST_HAS_JSON
//! Load the kinematics and the dynamtics of the robot from a JSON file
Expand Down

0 comments on commit 96a7a11

Please sign in to comment.