-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from dic-iit/TSID/joint_regularization
Implent JointRegularizationTask in TSID
- Loading branch information
Showing
7 changed files
with
377 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/TSID/include/BipedalLocomotion/TSID/JointsTrackingTask.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @file JointsTrackingTask.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_TSID_JOINT_REGULARIZATION_TASK_H | ||
#define BIPEDAL_LOCOMOTION_TSID_JOINT_REGULARIZATION_TASK_H | ||
|
||
#include <BipedalLocomotion/TSID/Task.h> | ||
|
||
#include <LieGroupControllers/ProportionalDerivativeController.h> | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace TSID | ||
{ | ||
|
||
/** | ||
* JointsTrackingTask is a concrete implementation of the Task. Please use this element if you | ||
* want to control the desired joint position of the robot. | ||
* The task represents the following equation | ||
* \f[ | ||
* \begin{bmatrix} 0_6 & I_n \end{bmatrix} \nu = \ddot{s} ^ * | ||
* \f] | ||
* where \f$0_6\f$ and \f$I_n\f$ are the zero and the identity matrix. | ||
* The desired joint acceleration is chosen such that the joint will converge to the desired | ||
* trajectory and it is computed with a standard standard PD controller in \f$\mathbb{R}^n\f$. | ||
*/ | ||
class JointsTrackingTask : public Task | ||
{ | ||
Eigen::VectorXd m_kp; /**< Proportional gain. */ | ||
Eigen::VectorXd m_kd; /**< Derivative gain. */ | ||
Eigen::VectorXd m_jointVelocity; /**< Joints velocity in radians per second. */ | ||
Eigen::VectorXd m_jointPosition; /**< Joints position in radians */ | ||
Eigen::VectorXd m_desiredJointVelocity; /**< Desired joints velocity in radians per second. */ | ||
Eigen::VectorXd m_desiredJointAcceleration; /**< Desired joints acceleration in radians per | ||
second square. */ | ||
Eigen::VectorXd m_desiredJointPosition; /**< Desired joints position in radians. */ | ||
Eigen::VectorXd m_zero; /**< Vector containing zero elements. */ | ||
|
||
public: | ||
/** | ||
* Initialize the planner. | ||
* @param paramHandler pointer to the parameters handler. | ||
* @param variablesHandler reference to a variables handler. | ||
* @note the following parameters are required by the class | ||
* | Parameter Name | Type | Description | Mandatory | | ||
* |:----------------------------------:|:--------:|:--------------------------------------------------------------------------------------:|:---------:| | ||
* | `robot_acceleration_variable_name` | `string` | Name of the variable contained in `VariablesHandler` describing the robot acceleration | Yes | | ||
* | `kp ` | `vector` | Proportional controller gain | Yes | | ||
* | `kd` | `vector` | Derivative Gain of the controller. if not specified \f$k_d = 2 \sqrt{k_p}\f$ | No | | ||
* @return True in case of success, false otherwise. | ||
*/ | ||
bool initialize(std::weak_ptr<ParametersHandler::IParametersHandler> paramHandler, | ||
const System::VariablesHandler& variablesHandler) override; | ||
|
||
/** | ||
* Update the content of the element. | ||
* @return True in case of success, false otherwise. | ||
*/ | ||
bool update() override; | ||
|
||
/** | ||
* Set the desired setpoint. | ||
* @param jointPosition vector containing the desired joint position in radians. | ||
* @note The desired velocity and acceleration are implicitly set to zero. | ||
* @return True in case of success, false otherwise. | ||
*/ | ||
bool setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition); | ||
|
||
/** | ||
* Set the desired setpoint. | ||
* @param jointPosition vector containing the desired joint position in radians. | ||
* @param jointVelocity vector containing the desired joint velocity in radians per second. | ||
* @note The desired acceleration is implicitly set to zero. | ||
* @return True in case of success, false otherwise. | ||
*/ | ||
bool setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition, | ||
Eigen::Ref<const Eigen::VectorXd> jointVelocity); | ||
|
||
/** | ||
* Set the desired setpoint. | ||
* @param jointPosition vector containing the desired joint position in radians. | ||
* @param jointVelocity vector containing the desired joint velocity in radians per second. | ||
* @param jointAcceleration vector containing the desired joint velocity in radians per second square. | ||
* @return True in case of success, false otherwise. | ||
*/ | ||
bool setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition, | ||
Eigen::Ref<const Eigen::VectorXd> jointVelocity, | ||
Eigen::Ref<const Eigen::VectorXd> jointAcceleration); | ||
}; | ||
|
||
} // namespace TSID | ||
} // namespace BipedalLocomotion | ||
|
||
#endif // BIPEDAL_LOCOMOTION_TSID_JOINT_REGULARIZATION_TASK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/** | ||
* @file JointsTrackingTask.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2021 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#include <BipedalLocomotion/TSID/JointsTrackingTask.h> | ||
|
||
#include <iDynTree/Core/EigenHelpers.h> | ||
|
||
using namespace BipedalLocomotion::ParametersHandler; | ||
using namespace BipedalLocomotion::TSID; | ||
|
||
bool JointsTrackingTask::initialize( | ||
std::weak_ptr<ParametersHandler::IParametersHandler> paramHandler, | ||
const System::VariablesHandler& variablesHandler) | ||
{ | ||
constexpr std::string_view errorPrefix = "[JointsTrackingTask::initialize] "; | ||
|
||
if (m_kinDyn == nullptr || !m_kinDyn->isValid()) | ||
{ | ||
std::cerr << errorPrefix << "KinDynComputations object is not valid." << std::endl; | ||
return false; | ||
} | ||
|
||
auto ptr = paramHandler.lock(); | ||
if (ptr == nullptr) | ||
{ | ||
std::cerr << errorPrefix << "The parameter handler is not valid." << std::endl; | ||
return false; | ||
} | ||
|
||
System::VariablesHandler::VariableDescription robotAccelerationVariable; | ||
std::string robotAccelerationVariableName; | ||
if (!ptr->getParameter("robot_acceleration_variable_name", robotAccelerationVariableName) | ||
|| !variablesHandler.getVariable(robotAccelerationVariableName, robotAccelerationVariable)) | ||
{ | ||
std::cerr << errorPrefix << "Error while retrieving the robot acceleration variable." | ||
<< std::endl; | ||
return false; | ||
} | ||
|
||
if (robotAccelerationVariable.size != m_kinDyn->getNrOfDegreesOfFreedom() + 6) | ||
{ | ||
std::cerr << errorPrefix << "Error while retrieving the robot acceleration variable." | ||
<< std::endl; | ||
return false; | ||
} | ||
|
||
// set the gains for the controllers | ||
m_kp.resize(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_kd.resize(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
if (!ptr->getParameter("kp", m_kp)) | ||
{ | ||
std::cerr << errorPrefix << "Error while retrieving the proportional gain." << std::endl; | ||
return false; | ||
} | ||
|
||
if (!ptr->getParameter("kd", m_kd)) | ||
{ | ||
std::cout << errorPrefix << "Error while retrieving the derivative gain." << std::endl; | ||
std::cout << errorPrefix << "The default kd will be set." << std::endl; | ||
|
||
m_kd = 2 * m_kp.cwiseSqrt(); | ||
} | ||
|
||
// set the description | ||
m_description = "Joint tracking task"; | ||
|
||
// resize the matrices | ||
m_A.resize(m_kinDyn->getNrOfDegreesOfFreedom(), variablesHandler.getNumberOfVariables()); | ||
m_A.setZero(); | ||
m_b.resize(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
|
||
m_zero = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_desiredJointPosition = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_desiredJointVelocity = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_desiredJointAcceleration = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_jointPosition = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
m_jointVelocity = Eigen::VectorXd::Zero(m_kinDyn->getNrOfDegreesOfFreedom()); | ||
|
||
// A is constant | ||
// here we assume that the first robot acceleration is stored as [base_acceleration; | ||
// joint_acceleration] | ||
iDynTree::toEigen(this->subA(robotAccelerationVariable)) | ||
.rightCols(m_kinDyn->getNrOfDegreesOfFreedom()) | ||
.setIdentity(); | ||
|
||
return true; | ||
} | ||
|
||
bool JointsTrackingTask::update() | ||
{ | ||
constexpr std::string_view errorPrefix = "[JointsTrackingTask::update] "; | ||
|
||
if (!m_kinDyn->getJointPos(m_jointPosition)) | ||
{ | ||
std::cerr << errorPrefix << " Unable to get the joint position" << std::endl; | ||
return false; | ||
} | ||
|
||
if (!m_kinDyn->getJointVel(m_jointVelocity)) | ||
{ | ||
std::cerr << errorPrefix << " Unable to get the joint velocity" << std::endl; | ||
return false; | ||
} | ||
|
||
m_b = m_desiredJointAcceleration | ||
+ m_kp.asDiagonal() * (m_desiredJointPosition - m_jointPosition) | ||
+ m_kd.asDiagonal() * (m_desiredJointVelocity - m_jointVelocity); | ||
|
||
return true; | ||
} | ||
|
||
bool JointsTrackingTask::setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition) | ||
{ | ||
return this->setSetpoint(jointPosition, m_zero, m_zero); | ||
} | ||
|
||
bool JointsTrackingTask::setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition, | ||
Eigen::Ref<const Eigen::VectorXd> jointVelocity) | ||
{ | ||
return this->setSetpoint(jointPosition, jointVelocity, m_zero); | ||
} | ||
|
||
bool JointsTrackingTask::setSetpoint(Eigen::Ref<const Eigen::VectorXd> jointPosition, | ||
Eigen::Ref<const Eigen::VectorXd> jointVelocity, | ||
Eigen::Ref<const Eigen::VectorXd> jointAcceleration) | ||
{ | ||
constexpr std::string_view errorPrefix = "[JointsTrackingTask::setSetpoint] "; | ||
|
||
if (jointPosition.size() != m_kinDyn->getNrOfDegreesOfFreedom() | ||
|| jointVelocity.size() != m_kinDyn->getNrOfDegreesOfFreedom() | ||
|| jointAcceleration.size() != m_kinDyn->getNrOfDegreesOfFreedom()) | ||
{ | ||
std::cerr << errorPrefix << "Wrong size of the desired reference trajectory:" << std::endl | ||
<< "Expected size: " << m_kinDyn->getNrOfDegreesOfFreedom() << std::endl | ||
<< "Joint position size: " << jointPosition.size() << std::endl | ||
<< "Joint velocity size: " << jointVelocity.size() << std::endl | ||
<< "Joint acceleration size: " << jointAcceleration.size() << std::endl; | ||
return false; | ||
} | ||
|
||
m_desiredJointPosition = jointPosition; | ||
m_desiredJointVelocity = jointVelocity; | ||
m_desiredJointAcceleration = jointVelocity; | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.