ocra-recipes
Doxygen documentation for the ocra-recipes repository
ControllerServer.cpp
Go to the documentation of this file.
2 
3 using namespace ocra_recipes;
4 
6  SOLVER_TYPE solver,
7  bool usingInterprocessCommunication,
8  bool useOdometry)
9 : controllerType(ctrlType)
10 , solverType(solver)
11 , usingComs(usingInterprocessCommunication)
12 , usingOdometry(useOdometry)
13 {
14 }
15 
17 {
18  // if(taskManagerSet)
19  // taskManagerSet->clearSet();
20 }
21 
23 {
24  bool res = true;
26  firstRun = true;
27 
28  rState = RobotState(model->nbDofs());
29  if(model)
30  {
31  // Set the solver.
32  switch (solverType)
33  {
34  case QUADPROG:
35  {
36  std::cout << "Using QuadProg++ "<<std::endl;
37  internalSolver = std::make_shared<ocra::OneLevelSolverWithQuadProg>();
38  }break;
39 
40  case QPOASES:
41  {
42  std::cout << "Using qpOASES"<<std::endl;
43  internalSolver = std::make_shared<ocra::OneLevelSolverWithQPOASES>();
44  }break;
45 
46  default:
47  {
48  std::cout << "Using qpOASES [Default]"<<std::endl;
49  internalSolver = std::make_shared<ocra::OneLevelSolverWithQPOASES>();
50  }break;
51  }
52 
53  // Construct the desired controller.
54  switch (controllerType)
55  {
56  case WOCRA_CONTROLLER:
57  {
58  std::cout << "Constructing a WOCRA Controller "<<std::endl;
59  bool useReducedProblem = false;
60  controller = std::make_shared<wocra::WocraController>("WocraController", model, std::static_pointer_cast<ocra::OneLevelSolver>(internalSolver), useReducedProblem);
61  }break;
62 
63  case HOCRA_CONTROLLER:
64  {
65  std::cout << "Constructing a HOCRA Controller "<<std::endl;
66  bool useReducedProblem = false;
67  controller = std::make_shared<hocra::HocraController>("HocraController", model, std::static_pointer_cast<ocra::OneLevelSolver>(internalSolver), useReducedProblem);
68  }break;
69 
70  default:
71  {
72  std::cout << "Constructing a WOCRA Controller [Default]"<<std::endl;
73  bool useReducedProblem = false;
74  controller = std::make_shared<wocra::WocraController>("WocraController", model, std::static_pointer_cast<ocra::OneLevelSolver>(internalSolver), useReducedProblem);
75  }break;
76  }
77  }
78 
79  if(usingComs)
80  {
81  serverComs = std::make_shared<ServerCommunications>(controller, model);
82  res &= serverComs->open();
83  res &= statesPort.open("/ControllerServer/states:o");
84  }
85 
86  res &= bool(model);
87  res &= bool(controller);
88 
89  // WARNING! If useOdometry is true we must call updateModel during initialization explicitly after ControllerServer::initialize()
90  if (!this->usingOdometry) {
91  // Setting up initial contact state. Both feet on the ground.
92  // TODO: The initial contact state should be obtained from the initial configuration of the robot automatically.
93  if (!model->hasFixedRoot()) {
94  this->controller->setContactState(1,1);
95  }
96  updateModel();
97  }
98  return res;
99 }
100 
101 const Eigen::VectorXd& ControllerServer::computeTorques()
102 {
104  return tau;
105 }
106 
107 void ControllerServer::computeTorques(Eigen::VectorXd& torques)
108 {
109  updateModel();
110  controller->computeOutput(torques);
111 }
112 
114 {
116  if (model->hasFixedRoot()){
117  model->setState(rState.q, rState.qd);
118  }else{
119  model->setState(rState.H_root, rState.q, rState.T_root, rState.qd);
120  }
121  if (!statesPort.write(rState)) {
122  OCRA_ERROR("Couldn't write robot state for client. Not really doing anything about it, except reporting.");
123  }
124 }
125 
126 bool ControllerServer::addTasksFromXmlFile(const std::string& filePath)
127 {
128  ocra::TaskConstructionManager factory(model, controller, filePath);
129  return true;
130 }
131 
132 bool ControllerServer::addTasks(std::vector<ocra::TaskBuilderOptions>& taskOptions)
133 {
134  ocra::TaskConstructionManager factory(model, controller, taskOptions);
135  return true;
136 }
137 
138 void ControllerServer::setRegularizationTermWeights(double wDdq, double wTau, double wFc)
139 {
140  if ( controllerType == WOCRA_CONTROLLER ) {
141  std::dynamic_pointer_cast<wocra::WocraController>(controller)->setVariableMinimizationWeights(wDdq, wTau, wFc);
142  }
143 }
Eigen::VectorXd qd
Definition: RobotState.h:42
Eigen::Displacementd H_root
Definition: RobotState.h:43
ControllerServer(CONTROLLER_TYPE ctrlType, SOLVER_TYPE solver, bool usingInterprocessCommunication=true, bool useOdometry=false)
#define OCRA_ERROR(msg)
Definition: ErrorsHelper.h:32
ocra::Controller::Ptr controller
ServerCommunications::Ptr serverComs
void setRegularizationTermWeights(double wDdq, double wTau, double wFc)
const Eigen::VectorXd & computeTorques()
bool addTasks(std::vector< ocra::TaskBuilderOptions > &tmOpts)
virtual std::shared_ptr< Model > loadRobotModel()=0
Eigen::VectorXd q
Definition: RobotState.h:41
virtual void getRobotState(Eigen::VectorXd &q, Eigen::VectorXd &qd, Eigen::Displacementd &H_root, Eigen::Twistd &T_root)=0
Eigen::Twistd T_root
Definition: RobotState.h:44
A generic abstract class the solvers that can be used in the wOcra Controller.
Wocra Controller based on LQP solver for the ocra framework.
bool addTasksFromXmlFile(const std::string &filePath)
A portable class for sending robot state information over yarp.
Definition: RobotState.h:26