Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
JacobianPosController.h
1#pragma once
2
3#include "../filters/StateSpaceUtil.h"
4#include "../navtypes.h"
5#include "../utils/math.h"
6#include "../world_interface/data.h"
7#include "TrapezoidalVelocityProfile.h"
8
9#include <array>
10#include <functional>
11#include <loguru.hpp>
12#include <optional>
13
14#include <Eigen/Core>
15
16namespace control {
17
28template <int outputDim, int inputDim>
30public:
42 const std::function<navtypes::Vectord<outputDim>(const navtypes::Vectord<inputDim>&)>&
43 kinematicsFunc,
44 const std::function<navtypes::Matrixd<outputDim, inputDim>(
45 const navtypes::Vectord<inputDim>&)>& jacobianFunc,
46 double maxVel, double maxAccel)
47 : kinematicsFunc(kinematicsFunc),
48 jacobianFunc(jacobianFunc ? jacobianFunc
49 : std::bind(util::numericalJacobian, kinematicsFunc,
50 std::placeholders::_1, outputDim)),
51 velocityProfile(maxVel, maxAccel) {
52 CHECK_NOTNULL_F(this->kinematicsFunc);
53 CHECK_NOTNULL_F(this->jacobianFunc);
54 }
55
64 navtypes::Vectord<inputDim> getCommand(robot::types::datatime_t currTime,
65 const navtypes::Vectord<inputDim>& currPos) const {
66 double unused;
67 return getCommand(currTime, currPos, unused);
68 }
69
82 navtypes::Vectord<inputDim> getCommand(robot::types::datatime_t currTime,
83 const navtypes::Vectord<inputDim>& currPos,
84 double& cosineSim) const {
85 if (!velocityProfile.hasTarget()) {
86 return currPos;
87 }
88
89 // The jacobian of the kinematics function represents a linearization of the kinematics
90 // around the current position. We then use this approximation to find the position
91 // delta in the input space that yields the required position delta in the output space
92
93 navtypes::Vectord<outputDim> currTarget = velocityProfile.getCommand(currTime);
94 navtypes::Vectord<outputDim> currPosOutput = kinematicsFunc(currPos);
95 navtypes::Vectord<outputDim> outputPosDiff = currTarget - currPosOutput;
96 navtypes::Matrixd<outputDim, inputDim> jacobian = jacobianFunc(currPos);
97 navtypes::Vectord<inputDim> inputPosDiff =
98 jacobian.colPivHouseholderQr().solve(outputPosDiff);
99
100 // the cosine similarity of the actual and required position deltas in the output space
101 // is metric of how much the kinematics allows the commanded movement.
102 navtypes::Vectord<outputDim> trueOutputPosDiff = jacobian * inputPosDiff;
103 cosineSim = outputPosDiff.dot(trueOutputPosDiff) /
104 (outputPosDiff.norm() * trueOutputPosDiff.norm());
105
106 return currPos + inputPosDiff;
107 }
108
117 const navtypes::Vectord<inputDim>& currPos,
118 const navtypes::Vectord<outputDim>& target) {
119 navtypes::Vectord<outputDim> currPosOutput = kinematicsFunc(currPos);
120 velocityProfile.setTarget(currTime, currPosOutput, target);
121 }
122
128 bool hasTarget() const {
129 return velocityProfile.hasTarget();
130 }
131
135 void reset() {
136 velocityProfile.reset();
137 }
138
139private:
140 std::function<navtypes::Vectord<outputDim>(const navtypes::Vectord<inputDim>&)>
141 kinematicsFunc;
142 std::function<Eigen::Matrix<double, outputDim, inputDim>(
144 jacobianFunc;
146};
147
148} // namespace control
_Bind_helper< __is_socketlike< _Func >::value, _Func, _BoundArgs... >::type bind(_Func &&__f, _BoundArgs &&... __args)
JacobianPosController(const std::function< navtypes::Vectord< outputDim >(const navtypes::Vectord< inputDim > &)> &kinematicsFunc, const std::function< navtypes::Matrixd< outputDim, inputDim >(const navtypes::Vectord< inputDim > &)> &jacobianFunc, double maxVel, double maxAccel)
Construct a new controller.
Definition JacobianPosController.h:41
bool hasTarget() const
Check if a target has been set.
Definition JacobianPosController.h:128
void setTarget(robot::types::datatime_t currTime, const navtypes::Vectord< inputDim > &currPos, const navtypes::Vectord< outputDim > &target)
Set the target position of the controller.
Definition JacobianPosController.h:116
navtypes::Vectord< inputDim > getCommand(robot::types::datatime_t currTime, const navtypes::Vectord< inputDim > &currPos, double &cosineSim) const
Get the current command position in the input space.
Definition JacobianPosController.h:82
void reset()
Reset the controller, unsetting the target.
Definition JacobianPosController.h:135
navtypes::Vectord< inputDim > getCommand(robot::types::datatime_t currTime, const navtypes::Vectord< inputDim > &currPos) const
Get the current command position in the input space.
Definition JacobianPosController.h:64
Trapezoidal velocity profile in multiple dimensions.
Definition TrapezoidalVelocityProfile.h:23
std::chrono::time_point< dataclock > datatime_t
A point in time as measured by dataclock.
Definition data.h:31
A collection of utility functions and classes with common use-cases.
Definition SwerveController.cpp:145