Skip to content

Commit

Permalink
Fixed configuration list structure change
Browse files Browse the repository at this point in the history
  • Loading branch information
pooyanjamshidi committed Mar 30, 2018
1 parent de9e4db commit eb11d79
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 68 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pkg_check_modules(JSONCPP jsoncpp)
SetConfig.srv
)



## Generate actions in the 'action' folder
# add_action_files(
# FILES
Expand Down
4 changes: 2 additions & 2 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<build_depend>message_generation</build_depend>
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<exec_depend>message_runtime</exec_depend>
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
Expand Down
57 changes: 0 additions & 57 deletions src/config_list.json

This file was deleted.

36 changes: 27 additions & 9 deletions src/config_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace gazebo{
class GAZEBO_VISIBLE ConfigurationPlugin : public ModelPlugin
{
protected: double power_load;
protected: float power_load;
protected: int default_config;
protected: int current_config;
protected: physics::ModelPtr model;
Expand Down Expand Up @@ -72,17 +72,36 @@ private: const char * home = getenv("HOME");
std::ifstream config_file(home + config_path, std::ifstream::binary);
json_reader.parse(config_file, this->config_list, false);

ROS_GREEN_STREAM("Configuration Plugin is fully loaded.");
}

private: float get_power_load_by_id(int config_id)
{
for (const Json::Value& config: this->config_list["configurations"])
{
if (config["config_id"] == config_id)
{
return config["power_load_w"].asFloat();
}
}

}

public: virtual void Init()
{
std::string load = config_list[std::to_string(this->default_config)]["power_load"].asString();
this->power_load = std::stod(load.c_str());
ROS_GREEN_STREAM("Init Config Manager");
this->power_load = get_power_load_by_id(this->default_config);
ROS_GREEN_STREAM(this->power_load);
SetPowerLoad(this->power_load);
ROS_GREEN_STREAM("Default configuration has been set");
}

public: bool SetPowerLoad(double power_load)
public: virtual void Reset()
{
this->Init();
}

public: bool SetPowerLoad(float power_load)
{
brass_gazebo_battery::SetLoad srv;
srv.request.power_load = power_load;
Expand All @@ -93,27 +112,26 @@ public: bool SetPowerLoad(double power_load)
return true;
}
else{
ROS_RED_STREAM("An error happened calling power load service set");
ROS_RED_STREAM("An error happened calling power load service");
return false;
}

}

public: bool SetConfiguration(brass_gazebo_config_manager::SetConfig::Request &req,
public: bool SetConfiguration(brass_gazebo_config_manager::SetConfig::Request &req,
brass_gazebo_config_manager::SetConfig::Response &res)
{
lock.lock();
this->current_config = req.current_config;
ROS_GREEN_STREAM("New configuration of the robot: " << this->current_config);
std::string load = config_list[std::to_string(this->current_config)]["power_load"].asString();
this->power_load = std::stod(load.c_str());
this->power_load = get_power_load_by_id(this->current_config);
SetPowerLoad(this->power_load);
lock.unlock();
res.result = true;
return true;
}

public: bool GetConfiguration(brass_gazebo_config_manager::GetConfig::Request &req,
public: bool GetConfiguration(brass_gazebo_config_manager::GetConfig::Request &req,
brass_gazebo_config_manager::GetConfig::Response &res)
{
lock.lock();
Expand Down

0 comments on commit eb11d79

Please sign in to comment.