Skip to content

Commit

Permalink
Merge pull request #13 from SINTEFMedtek/pickle_robot_class
Browse files Browse the repository at this point in the history
Added pickling support to the Robot class
  • Loading branch information
androst authored Mar 21, 2024
2 parents 3c828d8 + 06da755 commit 2f041bf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
29 changes: 28 additions & 1 deletion source/pyromocc/source/pyromocc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,34 @@ PYBIND11_MODULE(pyromocc, m) {
.def("configure", &Robot::configure)
.def("get_state", &Robot::getCurrentState)
.def("stop_move", &Robot::stopMove)
.def("_connect", &Robot::connect, "Connects to the robot.");
.def("_connect", &Robot::connect, "Connects to the robot.")
.def("get_comms_config", &Robot::getCommuncationConfiguration)
.def(py::pickle(
/* Get object state */
[](Robot &r) {
Manipulator manipulator = r.getCurrentState()->getManipulator();
connectionConfiguration commsConfig = r.getCommuncationConfiguration();
if (r.isConnected())
r.disconnect();
return py::make_tuple(manipulator.manipulator, manipulator.sw_version, commsConfig.host, commsConfig.port);
},

/* Create a new object and restore the state */
[](py::tuple pickled_state) {
if (pickled_state.size() != 4)
throw std::runtime_error("Invalid state");

Manipulator manipulator = { pickled_state[0].cast<ManipulatorType>(), pickled_state[1].cast<std::string>() };
/* Create a new instance of Robot */
std::unique_ptr<Robot> r = std::unique_ptr<Robot>(new Robot());
r->configure(manipulator, pickled_state[2].cast<std::string>(), pickled_state[3].cast<int>());
if (!r->connect()) {
throw std::runtime_error("Failed to unpickle, cannot connect to robot");
}

return r;
}
));

robot.def("_movej", [](Robot& self, Eigen::Ref<const Eigen::RowVectorXd> target, double acc, double vel,
double time = 0, double blendRad = 0, bool wait=false){
Expand Down
5 changes: 5 additions & 0 deletions source/romocc/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ RobotState::pointer Robot::getCurrentState() const
return mCurrentState;
}

connectionConfiguration Robot::getCommuncationConfiguration() const
{
return mCommunicationInterface->getConnectionConfig();
}

void Robot::runMotionQueue(MotionQueue queue)
{
mMotionQueue = std::move(queue);
Expand Down
2 changes: 2 additions & 0 deletions source/romocc/Robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ROMOCC_EXPORT Robot : public Object
void shutdown();

RobotState::pointer getCurrentState() const;
connectionConfiguration getCommuncationConfiguration() const;

void addUpdateSubscription(std::function<void()> updateFunction);

template <class Target>
Expand Down
5 changes: 5 additions & 0 deletions source/romocc/communication/CommunicationInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ void CommunicationInterface::setManipulator(romocc::Manipulator manipulator)
mCurrentState->setManipulator(manipulator);
}

connectionConfiguration CommunicationInterface::getConnectionConfig()
{
return connectionConfiguration{ mHost, mPort };
}

RobotState::pointer CommunicationInterface::getRobotState()
{
return mCurrentState;
Expand Down
6 changes: 6 additions & 0 deletions source/romocc/communication/CommunicationInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

namespace romocc
{
struct connectionConfiguration
{
std::string host;
int port;
};

class ROMOCC_EXPORT CommunicationInterface : public Object
{
Expand All @@ -24,6 +29,7 @@ class ROMOCC_EXPORT CommunicationInterface : public Object

void configConnection(std::string host, int port);
void setManipulator(Manipulator manipulator);
connectionConfiguration getConnectionConfig();

RobotState::pointer getRobotState();

Expand Down
5 changes: 5 additions & 0 deletions source/romocc/robotics/RobotState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,9 @@ Transform3d RobotState::jointConfigToOperationalConfig(romocc::Vector6d jointCon
return transform_to_joint(jointConfig);
}

Manipulator RobotState::getManipulator()
{
return mManipulator;
}

}
2 changes: 2 additions & 0 deletions source/romocc/robotics/RobotState.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ROMOCC_EXPORT RobotState : public Object
Vector6d operationalConfigToJointConfig(Transform3d transform);
Transform3d jointConfigToOperationalConfig(Vector6d jointConfig);

Manipulator getManipulator();

std::shared_ptr<FKSolver> getFKSolver();
std::shared_ptr<IKSolver> getIKSolver();

Expand Down

0 comments on commit 2f041bf

Please sign in to comment.