3#include "../kinematics/ArmKinematics.h"
4#include "../navtypes.h"
5#include "../utils/time.h"
6#include "../world_interface/data.h"
27template <
unsigned int N>
40 const double safetyFactor)
41 : kin(kin_obj), safetyFactor(safetyFactor) {
42 CHECK_F(safetyFactor > 0.0 && safetyFactor < 1.0);
55 : kin(
std::
move(other.kin)), safetyFactor(other.safetyFactor) {
57 mutableFields =
std::move(other.mutableFields);
75 if (!mutableFields.has_value()) {
76 mutableFields.emplace();
79 Eigen::Vector2d newSetPoint = kin.jointPosToEEPos(currJointPos);
80 mutableFields->setpoint = normalizeEEWithinRadius(newSetPoint);
84 mutableFields.reset();
97 double safetyFactor) {
101 double maxRadius = kin_obj.
getSegLens().sum() * safetyFactor;
102 return eeRadius <= maxRadius;
115 Eigen::Vector2d newSetPoint = kin.jointPosToEEPos(targetJointPos);
117 mutableFields->setpoint = normalizeEEWithinRadius(newSetPoint);
131 pos = mutableFields->setpoint;
132 if (mutableFields->velTimestamp.has_value()) {
135 pos += mutableFields->velocity * dt;
140 return normalizeEEWithinRadius(pos);
152 const navtypes::Vectord<N>& jointPos) {
154 if (mutableFields->velTimestamp.has_value()) {
157 if (targetVel == 0.0 && mutableFields->velocity(1) == 0.0) {
158 Eigen::Vector2d newSetPoint = kin.jointPosToEEPos(jointPos);
159 mutableFields->setpoint = normalizeEEWithinRadius(newSetPoint);
164 mutableFields->setpoint = normalizeEEWithinRadius(
165 mutableFields->setpoint + mutableFields->velocity * dt);
169 mutableFields->velocity(0) = targetVel;
170 mutableFields->velTimestamp = currTime;
182 const navtypes::Vectord<N>& jointPos) {
184 if (mutableFields->velTimestamp.has_value()) {
187 if (mutableFields->velocity(0) == 0.0 && targetVel == 0.0) {
188 Eigen::Vector2d newSetPoint = kin.jointPosToEEPos(jointPos);
189 mutableFields->setpoint = normalizeEEWithinRadius(newSetPoint);
194 mutableFields->setpoint = normalizeEEWithinRadius(
195 mutableFields->setpoint + mutableFields->velocity * dt);
199 mutableFields->velocity(1) = targetVel;
200 mutableFields->velTimestamp = currTime;
212 const navtypes::Vectord<N>& currJointPos) {
218 bool success =
false;
219 navtypes::Vectord<N> jp = kin.eePosToJointPos(newPos, currJointPos, success);
220 mutableFields->velTimestamp = currTime;
222 LOG_F(WARNING,
"IK Failure!");
223 mutableFields->velocity.setZero();
225 mutableFields->setpoint = newPos;
231 struct MutableFields {
232 Eigen::Vector2d setpoint;
233 Eigen::Vector2d velocity;
234 std::optional<robot::types::datatime_t> velTimestamp;
237 std::optional<MutableFields> mutableFields;
240 const double safetyFactor;
248 Eigen::Vector2d normalizeEEWithinRadius(Eigen::Vector2d eePos) {
249 double radius = kin.
getSegLens().sum() * safetyFactor;
250 if (eePos.norm() > (radius + 1e-4)) {
257 (eePos - mutableFields->setpoint).dot(mutableFields->setpoint);
258 double differenceNorm = (eePos - mutableFields->setpoint).squaredNorm();
259 double discriminant =
261 differenceNorm * (mutableFields->setpoint.squaredNorm() -
std::pow(radius, 2));
262 double a = (-diffDotProd +
std::sqrt(discriminant)) / differenceNorm;
264 eePos = (1 - a) * mutableFields->setpoint + a * eePos;
complex< _Tp > pow(const complex< _Tp > &, int)
complex< _Tp > sqrt(const complex< _Tp > &)
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
void set_x_vel(robot::types::datatime_t currTime, double targetVel, const navtypes::Vectord< N > &jointPos)
Sets the x velocity for the end effector and returns the new command.
Definition PlanarArmController.h:151
Eigen::Vector2d get_setpoint(robot::types::datatime_t currTime)
Gets the current end effector setpoint / target position.
Definition PlanarArmController.h:126
navtypes::Vectord< N > getCommand(robot::types::datatime_t currTime, const navtypes::Vectord< N > &currJointPos)
Get the current command for the arm.
Definition PlanarArmController.h:211
void set_setpoint(const navtypes::Vectord< N > &targetJointPos)
Sets the end effector setpoint / target position.
Definition PlanarArmController.h:114
void set_y_vel(robot::types::datatime_t currTime, double targetVel, const navtypes::Vectord< N > &jointPos)
Sets the y velocity for the end effector and returns the new command.
Definition PlanarArmController.h:181
PlanarArmController(const kinematics::ArmKinematics< 2, N > &kin_obj, const double safetyFactor)
Construct a new controller object.
Definition PlanarArmController.h:39
static bool is_setpoint_valid(const navtypes::Vectord< N > &targetJointPos, kinematics::ArmKinematics< 2, N > kin_obj, double safetyFactor)
Returns whether the target joint positions are within the arm controller's radius limit.
Definition PlanarArmController.h:95
bool tryInitController(const navtypes::Vectord< N > &currJointPos)
Instantiates the PlanarArmController with the current joint positions, returning true if the joint po...
Definition PlanarArmController.h:71
PlanarArmController(PlanarArmController &&other)
Constructs a copy of an existing PlanarArmController object.
Definition PlanarArmController.h:54
A class that combines the forward and inverse kinematics of an arm.
Definition ArmKinematics.h:19
navtypes::Vectord< N > getSegLens() const override
Get the segment lengths for this arm.
Definition ArmKinematics.h:29
navtypes::Vectord< D > jointPosToEEPos(const navtypes::Vectord< N > &jointPos) const override
Given a joint position, calculate the current EE position.
Definition ArmKinematics.h:33
constexpr unsigned int getNumSegments() const
Get the number of segments in this arm.
Definition ForwardArmKinematics.h:25
std::chrono::time_point< dataclock > datatime_t
A point in time as measured by dataclock.
Definition data.h:31
double durationToSec(std::chrono::duration< Rep, Period > dur)
Convert a duration to seconds, as a double.
Definition time.h:18