diff --git a/systems/plants/constraint/ContactWrenchConstraint.m b/systems/plants/constraint/ContactWrenchConstraint.m index 5d5ff66265ad..36a928ea2720 100644 --- a/systems/plants/constraint/ContactWrenchConstraint.m +++ b/systems/plants/constraint/ContactWrenchConstraint.m @@ -1,19 +1,13 @@ classdef ContactWrenchConstraint < RigidBodyConstraint % constrain the contact forces - % @param tspan - A 1 x 2 vector. The time span of the constraint being active - % @param robot - A RigidBodyManipulator or a TimeSteppingRigidBodyManipulator % @param num_constraint - A scalar. The number of constraints. - % @param mex_ptr - A DrakeConstraintMexPointer % @param F_size - A 1 x 2 matrix. The size of the force parameter matrix. % @param F_lb - A double matrix of size F_size. The lower bound on the % force parameters % @param F_ub - A double matrix of size F_size. The upper bound on the % force parameters properties(SetAccess = protected) - tspan % a 1x2 vector num_constraint; - robot - mex_ptr F_size; F_lb; F_ub @@ -21,18 +15,7 @@ methods function obj = ContactWrenchConstraint(robot,tspan) - obj = obj@RigidBodyConstraint(RigidBodyConstraint.ContactWrenchConstraintCategory); - if(nargin<2) - tspan = [-inf,inf]; - end - if(isempty(tspan)) - tspan = [-inf,inf]; - end - if(tspan(1)>tspan(end)) - error('Drake:ContactWrenchConstraint:tspan(1) should be no larger than tspan(end)') - end - obj.tspan = [tspan(1) tspan(end)]; - obj.robot = robot; + obj = obj@RigidBodyConstraint(RigidBodyConstraint.ContactWrenchConstraintCategory,robot,tspan); end function tspan = getTspan(obj) diff --git a/systems/plants/constraint/QuasiStaticConstraint.m b/systems/plants/constraint/QuasiStaticConstraint.m index c6184c0d85be..b654467ad217 100644 --- a/systems/plants/constraint/QuasiStaticConstraint.m +++ b/systems/plants/constraint/QuasiStaticConstraint.m @@ -1,10 +1,5 @@ classdef QuasiStaticConstraint=0 || category <=-7) + { + std::cerr<<"Drake:RigidBodyConstraint:Unsupported constraint category"<category = category; + this->type = 0; + this->robot = robot; + if(tspan(0)>tspan(1)) + { + std::cerr<<"Drake:RigidBodyConstraint:tspan(0) should be no larger than tspan(1)"<tspan[0] = tspan(0); + this->tspan[1] = tspan(1); +} + +RigidBodyConstraint::RigidBodyConstraint(const RigidBodyConstraint &rhs):category(rhs.category),type(rhs.type),robot(rhs.robot) +{ + this->tspan[0] = rhs.tspan[0]; + this->tspan[1] = rhs.tspan[1]; +} -RigidBodyConstraint::RigidBodyConstraint(const RigidBodyConstraint &rhs):category(rhs.category),type(rhs.type){}; RigidBodyConstraint::~RigidBodyConstraint(void){}; const int QuasiStaticDefaultRobotNum[1] = {0}; const std::set QuasiStaticConstraint::defaultRobotNumSet(QuasiStaticDefaultRobotNum,QuasiStaticDefaultRobotNum+1); -QuasiStaticConstraint::QuasiStaticConstraint(RigidBodyManipulator* robot, const Vector2d &tspan,const std::set &robotnumset):RigidBodyConstraint(RigidBodyConstraint::QuasiStaticConstraintCategory) +QuasiStaticConstraint::QuasiStaticConstraint(RigidBodyManipulator* robot, const Vector2d &tspan,const std::set &robotnumset):RigidBodyConstraint(RigidBodyConstraint::QuasiStaticConstraintCategory,robot,tspan) { - this->robot = robot; this->m_robotnumset = robotnumset; - if(tspan(0)>tspan(1)) - { - std::cerr<<"tspan(0) should be no larger than tspan(1)"<tspan[0] = tspan(0); - this->tspan[1] = tspan(1); this->shrinkFactor = 0.9; - this->active = false; this->num_bodies = 0; this->num_pts = 0; this->type = RigidBodyConstraint::QuasiStaticConstraintType; } -QuasiStaticConstraint::QuasiStaticConstraint(const QuasiStaticConstraint &rhs):RigidBodyConstraint(rhs),m_robotnumset(rhs.m_robotnumset),shrinkFactor(rhs.shrinkFactor),active(rhs.active),num_bodies(rhs.num_bodies),num_pts(rhs.num_pts),bodies(rhs.bodies),num_body_pts(rhs.num_body_pts),body_pts(rhs.body_pts) +QuasiStaticConstraint::QuasiStaticConstraint(const QuasiStaticConstraint &rhs):RigidBodyConstraint(rhs),m_robotnumset(rhs.m_robotnumset),shrinkFactor(rhs.shrinkFactor),num_bodies(rhs.num_bodies),num_pts(rhs.num_pts),bodies(rhs.bodies),num_body_pts(rhs.num_body_pts),body_pts(rhs.body_pts) { - this->robot = rhs.robot; - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; } QuasiStaticConstraint::~QuasiStaticConstraint() @@ -274,15 +284,8 @@ void QuasiStaticConstraint::updateRobotnum(std::set &robotnumset) this->m_robotnumset = robotnumset; } -PostureConstraint::PostureConstraint(RigidBodyManipulator* model, const Eigen::Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::PostureConstraintCategory) +PostureConstraint::PostureConstraint(RigidBodyManipulator* robot, const Eigen::Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::PostureConstraintCategory,robot,tspan) { - if(tspan(1)tspan[0] = tspan(0); - this->tspan[1] = tspan(1); - this->robot = model; int nq = this->robot->num_dof; this->lb = new double[nq]; this->ub = new double[nq]; @@ -293,9 +296,6 @@ PostureConstraint::PostureConstraint(RigidBodyManipulator* model, const Eigen::V PostureConstraint::PostureConstraint(const PostureConstraint& rhs):RigidBodyConstraint(rhs) { - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; - this->robot = rhs.robot; int nq = this->robot->num_dof; this->lb = new double[nq]; this->ub = new double[nq]; @@ -304,7 +304,6 @@ PostureConstraint::PostureConstraint(const PostureConstraint& rhs):RigidBodyCons this->lb[i] = rhs.lb[i]; this->ub[i] = rhs.ub[i]; } - this->type = RigidBodyConstraint::PostureConstraintType; } bool PostureConstraint::isTimeValid(const double* t) const @@ -356,22 +355,12 @@ PostureConstraint::~PostureConstraint() delete[] this->ub; } -MultipleTimeLinearPostureConstraint::MultipleTimeLinearPostureConstraint(RigidBodyManipulator *model, const Eigen::Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::MultipleTimeLinearPostureConstraintCategory) +MultipleTimeLinearPostureConstraint::MultipleTimeLinearPostureConstraint(RigidBodyManipulator *robot, const Eigen::Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::MultipleTimeLinearPostureConstraintCategory,robot,tspan) { - if(tspan(1)tspan[0] = tspan(0); - this->tspan[1] = tspan(1); - this->robot = model; } MultipleTimeLinearPostureConstraint::MultipleTimeLinearPostureConstraint(const MultipleTimeLinearPostureConstraint &rhs):RigidBodyConstraint(rhs) { - this->robot = rhs.robot; - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; } std::vector MultipleTimeLinearPostureConstraint::isTimeValid(const double* t, int n_breaks) const @@ -440,9 +429,8 @@ void MultipleTimeLinearPostureConstraint::eval(const double* t, int n_breaks, co } } -SingleTimeLinearPostureConstraint::SingleTimeLinearPostureConstraint(RigidBodyManipulator* robot, const VectorXi &iAfun, const VectorXi &jAvar, const VectorXd &A, const VectorXd &lb, const VectorXd &ub, const Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::SingleTimeLinearPostureConstraintCategory) +SingleTimeLinearPostureConstraint::SingleTimeLinearPostureConstraint(RigidBodyManipulator* robot, const VectorXi &iAfun, const VectorXi &jAvar, const VectorXd &A, const VectorXd &lb, const VectorXd &ub, const Vector2d &tspan):RigidBodyConstraint(RigidBodyConstraint::SingleTimeLinearPostureConstraintCategory,robot,tspan) { - this->robot = robot; this->lb = lb; this->ub = ub; this->num_constraint = this->lb.size(); @@ -487,20 +475,11 @@ SingleTimeLinearPostureConstraint::SingleTimeLinearPostureConstraint(RigidBodyMa { A_mat.insert(iAfun(i),jAvar(i)) = A(i); } - if(tspan(1)tspan[0] = tspan(0); - this->tspan[1] = tspan(1); this->type = RigidBodyConstraint::SingleTimeLinearPostureConstraintType; } SingleTimeLinearPostureConstraint::SingleTimeLinearPostureConstraint(const SingleTimeLinearPostureConstraint &rhs):RigidBodyConstraint(rhs),iAfun(rhs.iAfun),jAvar(rhs.jAvar),A(rhs.A),lb(rhs.lb),ub(rhs.ub),num_constraint(rhs.num_constraint),A_mat(rhs.A_mat) { - this->robot = rhs.robot; - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; } bool SingleTimeLinearPostureConstraint::isTimeValid(const double* t) const @@ -607,24 +586,14 @@ void SingleTimeLinearPostureConstraint::name(const double* t, std::vectortspan[0] = tspan(0); - this->tspan[1] = tspan(1); - this->robot = model; this->num_constraint = 0; } SingleTimeKinematicConstraint::SingleTimeKinematicConstraint(const SingleTimeKinematicConstraint &rhs):RigidBodyConstraint(rhs) { - this->robot = rhs.robot; this->num_constraint = rhs.num_constraint; - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; } bool SingleTimeKinematicConstraint::isTimeValid(const double* t) const { @@ -646,22 +615,12 @@ void SingleTimeKinematicConstraint::updateRobot(RigidBodyManipulator* robot) this->robot = robot; } -MultipleTimeKinematicConstraint::MultipleTimeKinematicConstraint(RigidBodyManipulator *model, const Vector2d &tspan): RigidBodyConstraint(RigidBodyConstraint::MultipleTimeKinematicConstraintCategory) +MultipleTimeKinematicConstraint::MultipleTimeKinematicConstraint(RigidBodyManipulator *robot, const Vector2d &tspan): RigidBodyConstraint(RigidBodyConstraint::MultipleTimeKinematicConstraintCategory,robot,tspan) { - if(tspan(1)tspan[0] = tspan(0); - this->tspan[1] = tspan(1); - this->robot = model; } MultipleTimeKinematicConstraint::MultipleTimeKinematicConstraint(const MultipleTimeKinematicConstraint &rhs):RigidBodyConstraint(rhs) { - this->robot = rhs.robot; - this->tspan[0] = rhs.tspan[0]; - this->tspan[1] = rhs.tspan[1]; } void MultipleTimeKinematicConstraint::eval(const double* t, int n_breaks, const MatrixXd &q, VectorXd &c, MatrixXd &dc) const diff --git a/systems/plants/constraint/RigidBodyConstraint.h b/systems/plants/constraint/RigidBodyConstraint.h index 66fce5386e0f..1e0a25947474 100644 --- a/systems/plants/constraint/RigidBodyConstraint.h +++ b/systems/plants/constraint/RigidBodyConstraint.h @@ -26,17 +26,19 @@ namespace DrakeRigidBodyConstraint{ void drakePrintMatrix(const Eigen::MatrixXd &mat); - class RigidBodyConstraint { protected: int category; int type; + RigidBodyManipulator* robot; + double tspan[2]; public: - RigidBodyConstraint(int category):category(category),type(0){}; + RigidBodyConstraint(int category,RigidBodyManipulator* robot, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); RigidBodyConstraint(const RigidBodyConstraint &rhs); int getType() const {return this->type;}; int getCategory() const {return this->category;}; + RigidBodyManipulator* getRobotPointer() const {return this->robot;} ; virtual ~RigidBodyConstraint(void) = 0; /* In each category, constraint class share the same function interface, this value needs to be in consistent with that in MATLAB*/ static const int SingleTimeKinematicConstraintCategory = -1; @@ -96,9 +98,7 @@ class RigidBodyConstraint class QuasiStaticConstraint: public RigidBodyConstraint { protected: - RigidBodyManipulator* robot; std::set m_robotnumset; - double tspan[2]; double shrinkFactor; bool active; int num_bodies; @@ -117,7 +117,6 @@ class QuasiStaticConstraint: public RigidBodyConstraint void name(const double* t,std::vector &name_str) const; virtual ~QuasiStaticConstraint(void); bool isActive() const {return this->active;}; - RigidBodyManipulator* getRobotPointer() const{return robot;}; int getNumWeights() const{return this->num_pts;}; void addContact(int num_new_bodies, const int* body, const Eigen::MatrixXd* body_pts); void setShrinkFactor(double factor); @@ -141,17 +140,14 @@ class QuasiStaticConstraint: public RigidBodyConstraint class PostureConstraint: public RigidBodyConstraint { protected: - double tspan[2]; double* lb; double* ub; - RigidBodyManipulator *robot; public: PostureConstraint(RigidBodyManipulator *model, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); PostureConstraint(const PostureConstraint& rhs); bool isTimeValid(const double* t) const; void setJointLimits(int num_idx, const int* joint_idx, const double* lb, const double* ub); void bounds(const double* t,double* joint_min, double* joint_max) const; - RigidBodyManipulator* getRobotPointer() const {return robot;} ; virtual ~PostureConstraint(void); }; @@ -175,15 +171,12 @@ class PostureConstraint: public RigidBodyConstraint class MultipleTimeLinearPostureConstraint: public RigidBodyConstraint { protected: - RigidBodyManipulator* robot; int numValidTime(const std::vector &valid_flag) const; void validTimeInd(const std::vector &valid_flag, Eigen::VectorXi &valid_t_ind) const; public: - double tspan[2]; MultipleTimeLinearPostureConstraint(RigidBodyManipulator *model, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); MultipleTimeLinearPostureConstraint(const MultipleTimeLinearPostureConstraint& rhs); std::vector isTimeValid(const double* t, int n_breaks) const; - RigidBodyManipulator* getRobotPointer() const {return robot;}; void eval(const double* t, int n_breaks, const Eigen::MatrixXd &q, Eigen::VectorXd &c,Eigen::SparseMatrix &dc) const; virtual int getNumConstraint(const double* t, int n_breaks) const = 0; virtual void feval(const double* t, int n_breaks, const Eigen::MatrixXd &q, Eigen::VectorXd &c) const = 0; @@ -221,7 +214,6 @@ class MultipleTimeLinearPostureConstraint: public RigidBodyConstraint class SingleTimeLinearPostureConstraint: public RigidBodyConstraint { protected: - RigidBodyManipulator* robot; Eigen::VectorXi iAfun; Eigen::VectorXi jAvar; Eigen::VectorXd A; @@ -229,13 +221,11 @@ class SingleTimeLinearPostureConstraint: public RigidBodyConstraint Eigen::VectorXd ub; int num_constraint; Eigen::SparseMatrix A_mat; - double tspan[2]; public: SingleTimeLinearPostureConstraint(RigidBodyManipulator* robot, const Eigen::VectorXi &iAfun, const Eigen::VectorXi &jAvar, const Eigen::VectorXd &A, const Eigen::VectorXd &lb, const Eigen::VectorXd &ub, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); SingleTimeLinearPostureConstraint(const SingleTimeLinearPostureConstraint& rhs); bool isTimeValid(const double* t) const; int getNumConstraint(const double* t) const; - RigidBodyManipulator* getRobotPointer() const{return robot;}; void bounds(const double* t, Eigen::VectorXd &lb, Eigen::VectorXd &ub) const; void feval(const double* t, const Eigen::VectorXd &q, Eigen::VectorXd &c) const; void geval(const double* t, Eigen::VectorXi &iAfun, Eigen::VectorXi &jAvar, Eigen::VectorXd &A) const; @@ -249,15 +239,12 @@ class SingleTimeLinearPostureConstraint: public RigidBodyConstraint class SingleTimeKinematicConstraint: public RigidBodyConstraint { protected: - RigidBodyManipulator *robot; int num_constraint; public: - double tspan[2]; SingleTimeKinematicConstraint(RigidBodyManipulator *model, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); SingleTimeKinematicConstraint(const SingleTimeKinematicConstraint &rhs); bool isTimeValid(const double* t) const; int getNumConstraint(const double* t) const; - RigidBodyManipulator* getRobotPointer() const{return robot;}; virtual void eval(const double* t,Eigen::VectorXd &c, Eigen::MatrixXd &dc) const = 0; virtual void bounds(const double* t, Eigen::VectorXd &lb, Eigen::VectorXd &ub) const = 0; virtual void name(const double* t, std::vector &name_str) const = 0; @@ -268,14 +255,11 @@ class SingleTimeKinematicConstraint: public RigidBodyConstraint class MultipleTimeKinematicConstraint : public RigidBodyConstraint { protected: - RigidBodyManipulator *robot; int numValidTime(const double* t,int n_breaks) const; public: - double tspan[2]; MultipleTimeKinematicConstraint(RigidBodyManipulator *model, const Eigen::Vector2d &tspan = DrakeRigidBodyConstraint::default_tspan); MultipleTimeKinematicConstraint(const MultipleTimeKinematicConstraint &rhs); std::vector isTimeValid(const double* t,int n_breaks) const; - RigidBodyManipulator* getRobotPointer() const{return robot;}; virtual int getNumConstraint(const double* t,int n_breaks) const = 0; void eval(const double* t, int n_breaks,const Eigen::MatrixXd &q,Eigen::VectorXd &c, Eigen::MatrixXd &dc) const; virtual void eval_valid(const double* valid_t, int num_valid_t,const Eigen::MatrixXd &valid_q,Eigen::VectorXd &c, Eigen::MatrixXd &dc_valid) const = 0; diff --git a/systems/plants/constraint/RigidBodyConstraint.m b/systems/plants/constraint/RigidBodyConstraint.m index 844e534384a4..aa5db575a913 100644 --- a/systems/plants/constraint/RigidBodyConstraint.m +++ b/systems/plants/constraint/RigidBodyConstraint.m @@ -6,11 +6,14 @@ % this category. % @param robot -- A RigidBodyManipulator or TimeSteppingRigidBodyManipulator % @param tspan -- A 1 x 2 double vector. The time span + % @param mex_ptr -- A DrakeConstraintMexPointer. The mex pointer of the + % RigidBodyConstraint properties(SetAccess = protected) category type robot tspan + mex_ptr end properties(Constant) @@ -63,13 +66,18 @@ if(~isa(robot,'RigidBodyManipulator') && ~isa(robot,'TimeSteppingRigidBodyManipulator')) error('Drake:RigidBodyConstraint:BadInput','robot has to be a RigidBodyManipulator or a TimeSteppingRigidBodyManipulator'); end - if(narargin<3) + obj.robot = robot; + if(nargin<3) + tspan = [-inf,inf]; + end + if(isempty(tspan)); tspan = [-inf,inf]; end tspan_size = size(tspan); if(~isnumeric(tspan) || length(tspan_size) ~= 2 || tspan_size(1) ~= 1 || tspan_size(2) ~= 2 || tspan(1)>tspan(2)) error('Drake:RigidBodyConstraint:BadInput','tspan should be a 1 x 2 vector, and tspan(1) <= tspan(2)'); end + obj.tspan = tspan; end end end \ No newline at end of file diff --git a/systems/plants/constraint/SingleTimeKinematicConstraint.m b/systems/plants/constraint/SingleTimeKinematicConstraint.m index f54a330857cf..79e04d425a9d 100644 --- a/systems/plants/constraint/SingleTimeKinematicConstraint.m +++ b/systems/plants/constraint/SingleTimeKinematicConstraint.m @@ -1,11 +1,9 @@ classdef SingleTimeKinematicConstraint < RigidBodyConstraint % An abstract class. Its eval function take a single time as input, the % constraint is enforced at that time only - % @param num_nlcon -- An int scalar. The number of nonlinear constraints - % @param mex_ptr -- A DrakeConstraintMexPointer. The mex pointer of the - % SingleTimeKinematicConstraint; - properties(SetAccess = protected) - num_nlcon + % @param num_constraint -- An int scalar. The number of nonlinear constraints + properties(SetAccess = protected,GetAccess = protected) + num_constraint end methods @@ -31,7 +29,7 @@ function n = getNumConstraint(obj,t) if(obj.isTimeValid(t)) - n = obj.num_nlcon; + n = obj.num_constraint; else n = 0; end diff --git a/systems/plants/constraint/SingleTimeLinearPostureConstraint.m b/systems/plants/constraint/SingleTimeLinearPostureConstraint.m index b7ba26c47910..68e892560c76 100644 --- a/systems/plants/constraint/SingleTimeLinearPostureConstraint.m +++ b/systems/plants/constraint/SingleTimeLinearPostureConstraint.m @@ -13,7 +13,7 @@ A lb ub - num_lcon + num_constraint A_mat end @@ -92,7 +92,7 @@ function n = getNumConstraint(obj,t) if(obj.isTimeValid(t)) - n = obj.num_lcon; + n = obj.num_constraint; else n = 0; end diff --git a/systems/plants/constraint/WorldEulerConstraint.m b/systems/plants/constraint/WorldEulerConstraint.m index f1c9b0bfb928..f7ac4368a36d 100644 --- a/systems/plants/constraint/WorldEulerConstraint.m +++ b/systems/plants/constraint/WorldEulerConstraint.m @@ -46,26 +46,21 @@ function name_str = name(obj,t) if(obj.isTimeValid(t)) name_str = cell(obj.num_constraint,1); + time_str = ''; + if(~isempty(t)) + time_str = sprintf('at time %5.2f',t); + end constraint_idx = 1; if(~obj.null_constraint_rows(1)) - name_str{constraint_idx} = sprintf('%s roll',obj.body_name); - if(~isempty(t)) - name_str{constraint_idx} = sprintf('%s at time %10.4f',name_str{constraint_idx},t); - end + name_str{constraint_idx} = sprintf('%s roll %s',obj.body_name,time_str); constraint_idx = constraint_idx+1; end if(~obj.null_constraint_rows(2)) - name_str{constraint_idx} = sprintf('%s pitch',obj.body_name); - if(~isempty(t)) - name_str{constraint_idx} = sprintf('%s at time %10.4f',name_str{constraint_idx},t); - end + name_str{constraint_idx} = sprintf('%s pitch %s',obj.body_name,t); constraint_idx = constraint_idx+1; end if(~obj.null_constraint_rows(3)) - name_str{constraint_idx} = sprintf('%s yaw',obj.body_name); - if(~isempty(t)) - name_str{constraint_idx} = sprintf('%s at time %10.4f',name_str{constraint_idx},t); - end + name_str{constraint_idx} = sprintf('%s yaw %s',obj.body_name,t); end else name_str = [];