Skip to content

Commit fb9703e

Browse files
Implemented streaming operator to convert types to string
1 parent d0614f1 commit fb9703e

16 files changed

+140
-89
lines changed

include/franky/cartesian_state.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class CartesianState {
6868
return velocity_;
6969
}
7070

71+
friend inline std::ostream &operator<<(std::ostream &os, const CartesianState &cartesian_state);
72+
7173
private:
7274
RobotPose pose_;
7375
RobotVelocity velocity_;
@@ -77,4 +79,9 @@ inline CartesianState operator*(const Affine &transform, const CartesianState &c
7779
return cartesian_state.transformWith(transform);
7880
}
7981

82+
inline std::ostream &operator<<(std::ostream &os, const CartesianState &cartesian_state) {
83+
os << "CartesianState(pose=" << cartesian_state.pose_ << ", velocity=" << cartesian_state.velocity_ << ")";
84+
return os;
85+
}
86+
8087
} // namespace franky

include/franky/dynamics_limit.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class DynamicsLimit {
9494
*/
9595
const std::string desc;
9696

97+
template <typename LimitTypeStream>
98+
friend std::ostream& operator<<(std::ostream& os, const DynamicsLimit<LimitTypeStream>& dynamics_limit);
99+
97100
private:
98101
/**
99102
* @brief Validate the new value before setting it.
@@ -110,4 +113,10 @@ class DynamicsLimit {
110113
std::function<bool()> can_write_condition_; /**< Function to check if writing is allowed. */
111114
};
112115

116+
template <typename LimitTypeStream>
117+
std::ostream& operator<<(std::ostream& os, const DynamicsLimit<LimitTypeStream>& dynamics_limit) {
118+
os << dynamics_limit.value_ << " (max: " << dynamics_limit.max << ")";
119+
return os;
120+
}
121+
113122
} // namespace franky

include/franky/elbow_state.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <optional>
44
#include <array>
5+
#include <iostream>
56

67
namespace franky {
78

@@ -14,6 +15,8 @@ enum class FlipDirection {
1415
kPositive = 1
1516
};
1617

18+
std::ostream& operator<<(std::ostream& os, const FlipDirection& flip_direction);
19+
1720
/**
1821
* @brief Elbow state of the robot.
1922
*
@@ -73,6 +76,8 @@ class ElbowState {
7376
*/
7477
[[nodiscard]] inline std::optional<FlipDirection> joint_4_flip() const { return joint_4_flip_; }
7578

79+
friend std::ostream& operator<<(std::ostream& os, const ElbowState& elbow_state);
80+
7681
private:
7782
double joint_3_pos_{0.0};
7883
std::optional<FlipDirection> joint_4_flip_{std::nullopt};

include/franky/joint_state.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ class JointState {
5050
return velocity_;
5151
}
5252

53+
friend std::ostream& operator<<(std::ostream& os, const JointState& joint_state);
54+
5355
private:
5456
Vector7d position_;
5557
Vector7d velocity_;
5658
};
5759

60+
inline std::ostream& operator<<(std::ostream& os, const JointState& joint_state) {
61+
os << "JointState(position=" << joint_state.position_ << ", velocity=" << joint_state.velocity_ << ")";
62+
return os;
63+
}
64+
5865
} // namespace franky

include/franky/robot_pose.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class RobotPose {
125125
return elbow_state_;
126126
}
127127

128+
friend std::ostream& operator<<(std::ostream& os, const RobotPose& robot_pose);
129+
128130
private:
129131
Affine end_effector_pose_;
130132
std::optional<ElbowState> elbow_state_;

include/franky/robot_velocity.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class RobotVelocity {
129129
return elbow_velocity_;
130130
}
131131

132+
friend std::ostream& operator<<(std::ostream& os, const RobotVelocity& robot_velocity);
133+
132134
private:
133135
Twist end_effector_twist_;
134136
std::optional<double> elbow_velocity_ = std::nullopt;

include/franky/twist.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <Eigen/Core>
44

55
#include "franky/types.hpp"
6+
#include "franky/util.hpp"
67

78
namespace franky {
89

@@ -90,6 +91,8 @@ class Twist {
9091
return angular_velocity_;
9192
}
9293

94+
friend std::ostream& operator<<(std::ostream& os, const Twist& twist);
95+
9396
private:
9497
Eigen::Vector3d linear_velocity_;
9598
Eigen::Vector3d angular_velocity_;
@@ -104,4 +107,9 @@ inline Twist operator*(const RotationMatrixType &rotation, const Twist &twist) {
104107
return twist.transformWith(rotation);
105108
}
106109

110+
inline std::ostream& operator<<(std::ostream& os, const Twist& twist) {
111+
os << "Twist(lin=" <<twist.linear_velocity_ << ", ang=" << twist.angular_velocity_ << ")";
112+
return os;
113+
}
114+
107115
} // namespace franky

include/franky/util.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <array>
44
#include <Eigen/Core>
5+
#include <franka/duration.h>
56

67
namespace franky {
78

@@ -46,4 +47,27 @@ std::array<double, dims> expand(const ScalarOrArray<dims> &input) {
4647
return output;
4748
}
4849

50+
template<int dims>
51+
inline std::ostream &operator<<(std::ostream &os, const Eigen::Vector<double, dims> &vec) {
52+
os << "[";
53+
for (size_t i = 0; i < dims; i++) {
54+
os << vec[i];
55+
if (i != dims - 1)
56+
os << " ";
57+
}
58+
os << "]";
59+
return os;
60+
}
61+
62+
inline std::ostream &operator<<(std::ostream &os, const Affine &affine) {
63+
os << "Affine(t=" << affine.translation().eval() << ", q=" << Eigen::Quaterniond(affine.rotation()).coeffs()
64+
<< ")";
65+
return os;
66+
}
67+
68+
inline std::ostream &operator<<(std::ostream &os, const franka::Duration &duration) {
69+
os << "Duration(" << duration.toMSec() << ")";
70+
return os;
71+
}
72+
4973
} // namespace franky

python/bind_misc.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "franky.hpp"
77

8+
#include "util.hpp"
9+
810
namespace py = pybind11;
911
using namespace pybind11::literals; // to bring in the '_a' literal
1012
using namespace franky;
@@ -23,13 +25,7 @@ void bind_misc(py::module &m) {
2325
.def(py::self *= uint64_t())
2426
.def(py::self / uint64_t())
2527
.def(py::self /= uint64_t())
26-
.def("__repr__",
27-
[](const franka::Duration &duration) {
28-
std::stringstream ss;
29-
ss << "Duration(" << duration.toMSec() << ")";
30-
return ss.str();
31-
}
32-
)
28+
.def("__repr__", strFromStream<franka::Duration>)
3329
.def(py::pickle(
3430
[](const franka::Duration &duration) { // __getstate__
3531
return py::make_tuple(duration.toMSec());

python/bind_robot.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "franky.hpp"
88

9+
#include "util.hpp"
10+
911
namespace py = pybind11;
1012
using namespace pybind11::literals; // to bring in the '_a' literal
1113
using namespace franky;
@@ -37,6 +39,7 @@ void bind_robot(py::module &m) {
3739
py::class_<DynamicsLimit<Vector7d>>(m, "VectorDynamicsLimit")
3840
.def("set", &DynamicsLimit<Vector7d>::setFrom<Array<7>>, "value"_a)
3941
.def("get", &DynamicsLimit<Vector7d>::get)
42+
.def("__repr__", strFromStream<DynamicsLimit<Vector7d>>)
4043
.def_property_readonly(
4144
"max",
4245
[](const DynamicsLimit<Vector7d> &dynamics_limit) {
@@ -47,6 +50,7 @@ void bind_robot(py::module &m) {
4750
py::class_<DynamicsLimit<double>>(m, "DoubleDynamicsLimit")
4851
.def("set", &DynamicsLimit<double>::set, "value"_a)
4952
.def("get", &DynamicsLimit<double>::get)
53+
.def("__repr__", strFromStream<DynamicsLimit<double>>)
5054
.def_readonly("max", &DynamicsLimit<double>::max)
5155
.def_readonly("desc", &DynamicsLimit<double>::desc);
5256

0 commit comments

Comments
 (0)