#ifndef CHEETAH_SOFTWARE_SPARSECMPC_H #define CHEETAH_SOFTWARE_SPARSECMPC_H #include "GraphSearch.h" #include "cppTypes.h" #include "../../../third-party/JCQP/SparseMatrixMath.h" struct BblockID { u32 foot; u32 timestep; }; class SparseCMPC { public: SparseCMPC(); void run(); // setup methods template void setRobotParameters(Mat3& inertia, T mass, T maxForce) { _Ibody = inertia.template cast(); _mass = mass; _maxForce = maxForce; } void setFriction(double mu) { _mu = mu; } template void setWeights(Vec12& weights, T alpha) { _weights = weights.template cast(); _alpha = alpha; } template void setX0(Vec3 p, Vec3 v, Vec4 q, Vec3 w) { _p0 = p.template cast(); _v0 = v.template cast(); _q0 = q.template cast(); _w0 = w.template cast(); } void setContactTrajectory(ContactState* contacts, std::size_t length) { _contactTrajectory.resize(length); for(std::size_t i = 0; i < length; i++) { _contactTrajectory[i] = contacts[i]; } } void setStateTrajectory(vectorAligned>& traj) { _stateTrajectory = traj; } template void setDtTrajectory(std::vector& traj) { _dtTrajectory.clear(); _dtTrajectory.reserve(traj.size()); for(auto pt : traj) _dtTrajectory.push_back(pt); } template void setFeet(Vec12& feet) { _pFeet = feet.template cast(); } // Eigen::Matrix& getResult() { // return _result; // } Vec12 getResult(); private: void buildX0(); void buildCT(); void buildDT(); void c2d(u32 trajIdx, u32 bBlockStartIdx, u32 block_count); u32 getStateIndex(u32 trajIdx); u32 getControlIndex(u32 bBlockIdx); u32 addConstraint(u32 size); void addConstraintTriple(double value, u32 row, u32 col); void addX0Constraint(); void addDynamicsConstraints(); void addForceConstraints(); void addFrictionConstraints(); void addQuadraticStateCost(); void addLinearStateCost(); void addQuadraticControlCost(); void runSolver(); void runSolverOSQP(); // inputs Mat3 _Ibody; Vec12 _weights; double _mass, _maxForce, _mu, _alpha; Vec3 _p0, _v0, _w0, _rpy0; Vec4 _q0; Vec12 _x0; Vec12 _pFeet, _g; // input trajectories std::vector _contactTrajectory; vectorAligned> _stateTrajectory; std::vector _dtTrajectory; // intermediates vectorAligned> _aMat; std::vector _bBlockIds; vectorAligned> _bBlocks; std::vector _contactCounts; std::vector _runningContactCounts; std::vector> _constraintTriples, _costTriples; std::vector _lb, _ub, _linearCost; Eigen::Matrix _result; u32 _trajectoryLength; u32 _bBlockCount; u32 _constraintCount; }; #endif //CHEETAH_SOFTWARE_SPARSECMPC_H