@@ -158,18 +158,59 @@ double JointFeatures::GetJointAcceleration(
158
158
159
159
// ///////////////////////////////////////////////
160
160
double JointFeatures::GetJointForce (
161
- const Identity &_id, const std::size_t _dof) const
161
+ const Identity &_id, const std::size_t /* _dof*/ ) const
162
162
{
163
+ double results = gz::math::NAN_D;
163
164
const auto *joint = this ->ReferenceInterface <JointInfo>(_id);
164
165
const auto *identifier = std::get_if<InternalJoint>(&joint->identifier );
166
+
165
167
if (identifier)
166
168
{
167
169
const auto *model = this ->ReferenceInterface <ModelInfo>(joint->model );
168
- return model->body ->getJointTorqueMultiDof (
169
- identifier->indexInBtModel )[_dof];
170
+ auto feedback = model->body ->getLink (
171
+ identifier->indexInBtModel ).m_jointFeedback ;
172
+ const auto &link = model->body ->getLink (identifier->indexInBtModel );
173
+ results = 0.0 ;
174
+ if (link .m_jointType == btMultibodyLink::eRevolute)
175
+ {
176
+ // According to the documentation in btMultibodyLink.h,
177
+ // m_axesTop[0] is the joint axis for revolute joints.
178
+ Eigen::Vector3d axis = convert (link .getAxisTop (0 ));
179
+ math::Vector3 axis_converted (axis[0 ], axis[1 ], axis[2 ]);
180
+ btVector3 angular = feedback->m_reactionForces .getAngular ();
181
+ math::Vector3<double > angularTorque (
182
+ angular.getX (),
183
+ angular.getY (),
184
+ angular.getZ ());
185
+ results += axis_converted.Dot (angularTorque);
186
+ #if BT_BULLET_VERSION < 326
187
+ // not always true
188
+ return results / 2.0 ;
189
+ #else
190
+ return results;
191
+ #endif
192
+ }
193
+ else if (link .m_jointType == btMultibodyLink::ePrismatic)
194
+ {
195
+ auto axis = convert (link .getAxisBottom (0 ));
196
+ math::Vector3 axis_converted (axis[0 ], axis[1 ], axis[2 ]);
197
+ btVector3 linear = feedback->m_reactionForces .getLinear ();
198
+ math::Vector3<double > linearForce (
199
+ linear.getX (),
200
+ linear.getY (),
201
+ linear.getZ ());
202
+ results += axis_converted.Dot (linearForce);
203
+ #if BT_BULLET_VERSION < 326
204
+ // Not always true see for reference:
205
+ // https://github.com/bulletphysics/bullet3/discussions/3713
206
+ // https://github.com/gazebosim/gz-physics/issues/565
207
+ return results / 2.0 ;
208
+ #else
209
+ return results;
210
+ #endif
211
+ }
170
212
}
171
-
172
- return gz::math::NAN_D;
213
+ return results;
173
214
}
174
215
175
216
// ///////////////////////////////////////////////
0 commit comments