rby1-sdk
Loading...
Searching...
No Matches
state.h
1#pragma once
2
3#include <Eigen/Core>
4
5#include "rby1-sdk/export.h"
6
7namespace rb::dyn {
8
9template <int DOF>
10class Robot;
11
12template <int DOF>
13class State {
14 public:
15 template <int>
16 friend class Robot;
17
18 template <typename T, int N>
19 using ContainerType = typename std::conditional_t<(N > 0), std::array<T, (unsigned int)N>, std::vector<T>>;
20
21 unsigned int GetBaseLinkIdx() const { // NOLINT
22 return base_link_user_idx;
23 }
24
25 template <typename Derived>
26 void SetQ(const Eigen::MatrixBase<Derived>& new_q) {
27 Set<Derived::RowsAtCompileTime>(q, new_q.eval());
28 }
29
30 Eigen::Vector<double, DOF> GetQ() const { return q(utr_joint_map); }
31
32 template <typename Derived>
33 void SetQdot(const Eigen::MatrixBase<Derived>& new_qdot) {
34 Set<Derived::RowsAtCompileTime>(qdot, new_qdot.eval());
35 }
36
37 Eigen::Vector<double, DOF> GetQdot() const { return qdot(utr_joint_map); }
38
39 template <typename Derived>
40 void SetQddot(const Eigen::MatrixBase<Derived>& new_qddot) {
41 Set<Derived::RowsAtCompileTime>(qddot, new_qddot.eval());
42 }
43
44 Eigen::Vector<double, DOF> GetQddot() const { return qddot(utr_joint_map); }
45
46 template <typename Derived>
47 void SetTau(const Eigen::MatrixBase<Derived>& new_tau) {
48 Set<Derived::RowsAtCompileTime>(tau, new_tau.eval());
49 }
50
51 const Eigen::Vector<double, DOF> GetTau() const { return tau(utr_joint_map); }
52
53 const math::se3v::MatrixType& GetV0() const { return V0; }
54
55 void SetV0(const math::se3v::MatrixType& new_V0) { V0 = new_V0; }
56
57 const math::se3v::MatrixType& GetVdot0() const { return Vdot0; }
58
59 // Vdot of root link in root frame
60 void SetVdot0(const math::se3v::MatrixType& new_Vdot0) { Vdot0 = new_Vdot0; }
61
62 void SetGravity(const math::se3v::MatrixType& gravity) { Vdot0 = -gravity; }
63
64 ContainerType<std::string, DOF> GetJointNames() const { return joint_names; }
65
66 [[nodiscard]] std::vector<std::string> GetLinkNames() const { return link_names; }
67
68 private:
69 explicit State(int dof) {
70 if (!(DOF < 0 || dof == DOF)) {
71 throw std::runtime_error("State initialization failed");
72 }
73
74 if constexpr (DOF < 0) {
75 q.resize(dof);
76 qdot.resize(dof);
77 qddot.resize(dof);
78 tau.resize(dof);
79 E.resize(dof + 1, math::SE3::Identity());
80 S.resize(6, dof);
81 T.resize(dof + 1, math::SE3::Identity());
82 V.resize(6, dof);
83 Vdot.resize(6, dof);
84 F.resize(6, dof);
85 joint_names.resize(dof);
86 utr_joint_map.resize(dof);
87 rtu_joint_map.resize(dof);
88 } else {
89 E.fill(math::SE3::Identity());
90 T.fill(math::SE3::Identity());
91 }
92
93 q.setZero();
94 qdot.setZero();
95 qddot.setZero();
96 S.setZero();
97 V.setZero();
98 Vdot0.setZero();
99 F.setZero();
100 }
101
102 template <int N>
103 void Set(Eigen::Vector<double, DOF>& s, const Eigen::Vector<double, N>& i) {
104 if (s.size() == i.size()) {
105 s = i(rtu_joint_map);
106 } else if (s.size() > i.size()) {
107 s.template head<>(i.size()) = i(rtu_joint_map.template head<>(i.size()));
108 } else {
109 throw std::runtime_error("i.size cannot be greater than s.size");
110 }
111 }
112
113 ContainerType<std::string, DOF> joint_names;
114 Eigen::Vector<unsigned int, DOF> utr_joint_map; // user joint idx -> robot joint idx
115 Eigen::Vector<unsigned int, DOF> rtu_joint_map; // robot joint idx -> user joint idx
116
117 unsigned int base_link_user_idx;
118 std::vector<std::string> link_names;
119 std::vector<typename Robot<DOF>::LinkIdx_> utr_link_map; // user link idx -> robot dummy/sub link idx
120
121 public:
122 Eigen::Vector<double, DOF> q;
123 Eigen::Vector<double, DOF> qdot;
124 Eigen::Vector<double, DOF> qddot;
125 Eigen::Vector<double, DOF> tau;
126 math::se3v::MatrixType V0{math::se3v::MatrixType::Zero()};
127 math::se3v::MatrixType Vdot0{math::se3v::MatrixType::Zero()};
128
129 ContainerType<math::SE3::MatrixType, DOF> E{}; // exponential mapping
130 Eigen::Matrix<double, 6, DOF> S; // S(q_0,...q_i) in root frame
131
132 ContainerType<math::SE3::MatrixType, DOF> T{}; // product of exponential
133 Eigen::Matrix<double, 6, DOF> V;
134 Eigen::Matrix<double, 6, DOF> Vdot;
135 Eigen::Matrix<double, 6, DOF> F;
136};
137
138} // namespace rb::dyn
Definition state.h:10
Definition state.h:13