A sparse MPC solver for walking motion generation.
oru_walk.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @author Antonio Paolillo
4  * @author Dimitar Dimitrov
5  * @author Alexander Sherikov
6  */
7 
8 #include <qi/os.hpp>
9 
10 #include "oru_walk.h"
11 
12 
13 /**
14  * Constructor for oru_walk object
15  * @param broker The parent broker
16  * @param name The name of the module
17  */
18 oru_walk::oru_walk(ALPtr<ALBroker> broker, const string& name) :
19  ALModule(broker, name),
20  access_sensor_values (ALPtr<ALMemoryFastAccess>(new ALMemoryFastAccess())),
21  wp (broker)
22 {
23  setModuleDescription("Orebro University: NAO walking module");
24 
25 
26  // advertise functions
27  functionName( "setStiffness" , getName(), "change stiffness of all joint");
28  addParam( "value", "new stiffness value from 0.0 to 1.0");
29  BIND_METHOD( oru_walk::setStiffness );
30 
31  functionName( "initPosition", getName() , "initialize robot position");
32  BIND_METHOD( oru_walk::initPosition );
33 
34  functionName( "walk", getName() , "walk");
35  BIND_METHOD( oru_walk::walk );
36 
37  functionName( "stopWalking", getName() , "stopWalking");
38  BIND_METHOD( oru_walk::stopWalkingRemote );
39 
40  solver = NULL;
41 }
42 
43 
44 
45 /**
46  * Destructor for oru_walk object
47  */
49 {
50  setStiffness(0.0f);
51  // Remove the postProcess call back connection
52  stopWalking ("Module destroyed.\n");
53  if (solver != NULL)
54  {
55  delete solver;
56  solver = NULL;
57  }
58 }
59 
60 
61 
62 // Initialisation of ALmemory fast access, DCM commands, Alias, stiffness, ...
63 /**
64  * @brief
65  */
67 {
68  bool isDCMRunning;
69 
70 
71  // Is the DCM running ?
72  try
73  {
74  isDCMRunning = getParentBroker()->getProxy("ALLauncher")->call<bool>("isModulePresent", std::string("DCM"));
75  }
76  catch (ALError& e)
77  {
78  ORUW_THROW_ERROR("Error when connecting to DCM: ", e);
79  }
80 
81  if (!isDCMRunning)
82  {
83  ORUW_THROW("Error no DCM running");
84  }
85 
86  try
87  {
88  // Get the DCM proxy
89  dcm_proxy = getParentBroker()->getDcmProxy();
90  }
91  catch (ALError& e)
92  {
93  ORUW_THROW_ERROR("Impossible to create DCM Proxy: ", e);
94  }
95 
96 
97  try
98  {
99  // Get the memory proxy
100  memory_proxy = getParentBroker()->getMemoryProxy();
101  }
102  catch (ALError& e)
103  {
104  ORUW_THROW_ERROR("Impossible to create memory proxy: ", e);
105  }
106 
107 
108 
109 // Initialisation of ALmemory fast access, DCM commands, Alias, stiffness, ...
110 
111  vector <string> joint_names(JOINTS_NUM, "Device/SubDeviceList/");
112  joint_names[HEAD_PITCH] += "HeadPitch";
113  joint_names[HEAD_YAW] += "HeadYaw";
114 
115  joint_names[L_ANKLE_PITCH] += "LAnklePitch";
116  joint_names[L_ANKLE_ROLL] += "LAnkleRoll";
117  joint_names[L_ELBOW_ROLL] += "LElbowRoll";
118  joint_names[L_ELBOW_YAW] += "LElbowYaw";
119  joint_names[L_HIP_PITCH] += "LHipPitch";
120  joint_names[L_HIP_ROLL] += "LHipRoll";
121  joint_names[L_HIP_YAW_PITCH] += "LHipYawPitch";
122  joint_names[L_KNEE_PITCH] += "LKneePitch";
123  joint_names[L_SHOULDER_PITCH] += "LShoulderPitch";
124  joint_names[L_SHOULDER_ROLL] += "LShoulderRoll";
125  joint_names[L_WRIST_YAW] += "LWristYaw";
126 
127  joint_names[R_ANKLE_PITCH] += "RAnklePitch";
128  joint_names[R_ANKLE_ROLL] += "RAnkleRoll";
129  joint_names[R_ELBOW_ROLL] += "RElbowRoll";
130  joint_names[R_ELBOW_YAW] += "RElbowYaw";
131  joint_names[R_HIP_PITCH] += "RHipPitch";
132  joint_names[R_HIP_ROLL] += "RHipRoll";
133  joint_names[R_HIP_YAW_PITCH] += "RHipYawPitch";
134  joint_names[R_KNEE_PITCH] += "RKneePitch";
135  joint_names[R_SHOULDER_PITCH] += "RShoulderPitch";
136  joint_names[R_SHOULDER_ROLL] += "RShoulderRoll";
137  joint_names[R_WRIST_YAW] += "RWristYaw";
138 
139 
140  initFastRead(joint_names);
141  initFastWrite(joint_names);
143 }
144 
145 
146 /**
147  * @brief Set stiffness of joints.
148  *
149  * @param[in] stiffnessValue value of stiffness [0;1]
150  */
151 void oru_walk::setStiffness(const float &stiffnessValue)
152 {
153  ALValue stiffnessCommands;
154 
155 
156  if ((stiffnessValue < 0) || (stiffnessValue > 1))
157  {
158  ORUW_THROW("Wrong parameters");
159  }
160 
161 
162  // Prepare one dcm command:
163  // it will linearly "Merge" all joint stiffness
164  // from last value to "stiffnessValue" in 1 seconde
165  stiffnessCommands.arraySetSize(3);
166  stiffnessCommands[0] = std::string("jointStiffness");
167  stiffnessCommands[1] = std::string("Merge");
168  stiffnessCommands[2].arraySetSize(1);
169  stiffnessCommands[2][0].arraySetSize(2);
170  stiffnessCommands[2][0][0] = stiffnessValue;
171 
172 
173  /// @attention Hardcoded parameter!
174  unsigned int stiffness_change_time = 1000;
175  try
176  {
177  stiffnessCommands[2][0][1] = dcm_proxy->getTime(stiffness_change_time);
178  }
179  catch (const ALError &e)
180  {
181  ORUW_THROW_ERROR("Error on DCM getTime : ", e);
182  }
183 
184 
185  try
186  {
187  dcm_proxy->set(stiffnessCommands);
188  }
189  catch (const ALError &e)
190  {
191  ORUW_THROW_ERROR("Error when sending stiffness to DCM : ", e);
192  }
193 
194  qi::os::msleep(stiffness_change_time);
195  qiLogInfo ("module.oru_walk", "Execution of setStiffness() is finished.");
196 }
197 
198 
199 
200 /**
201  * @brief
202  */
204 {
205  /// @attention Hardcoded parameter!
206  unsigned int init_time = 1200;
207 
208 
209  ALValue initPositionCommands;
210 
211  initPositionCommands.arraySetSize(6);
212  initPositionCommands[0] = string("jointActuator");
213  initPositionCommands[1] = string("ClearAll");
214  initPositionCommands[2] = string("time-separate");
215  initPositionCommands[3] = 0;
216  initPositionCommands[4].arraySetSize(1);
217  initPositionCommands[5].arraySetSize(JOINTS_NUM);
218  for (int i = 0; i < JOINTS_NUM; i++)
219  {
220  initPositionCommands[5][i].arraySetSize(1);
221  }
222  initJointAngles (initPositionCommands[5]);
223  for (int i = 0; i < LOWER_JOINTS_NUM; i++)
224  {
225  ref_joint_angles[i] = initPositionCommands[5][i][0];
226  }
227 
228 
229 
230  // set time
231  try
232  {
233  initPositionCommands[4][0] = dcm_proxy->getTime(init_time);
234  }
235  catch (const ALError &e)
236  {
237  ORUW_THROW_ERROR("Error on DCM getTime : ", e);
238  }
239 
240 
241  // send commands
242  try
243  {
244  dcm_proxy->setAlias(initPositionCommands);
245  }
246  catch (const AL::ALError &e)
247  {
248  ORUW_THROW_ERROR("Error with DCM setAlias : ", e);
249  }
250 
251  qi::os::msleep(init_time);
252  qiLogInfo ("module.oru_walk", "Execution of initPosition() is finished.");
253 }
void setStiffness(const float &)
Set stiffness of joints.
Definition: oru_walk.cpp:151
void initJointAngles(ALValue &)
#define ORUW_THROW(message)
Definition: oru_walk.h:65
void walk()
Construct and initialize all necessary classes and register callback function.
Definition: walking.cpp:17
void initFastWrite(const vector< string > &)
Initializes variables, that are necessary for fast sending of parameters to DCM.
smpc::solver * solver
Definition: oru_walk.h:138
#define ORUW_THROW_ERROR(message, error)
Definition: oru_walk.h:66
ALPtr< DCMProxy > dcm_proxy
Definition: oru_walk.h:143
double ref_joint_angles[LOWER_JOINTS_NUM]
Definition: oru_walk.h:135
void stopWalkingRemote()
An interface function that is called remotely to stop the execution.
Definition: walking.cpp:454
void initWalkCommands()
Initialize commands, that will be sent to DCM.
ALPtr< ALMemoryProxy > memory_proxy
Definition: oru_walk.h:144
void stopWalking(const char *)
Unregister callback and log a message.
Definition: walking.cpp:426
void initFastRead(const vector< string > &)
Initializes variables, that are necessary for fast reading of data from memory.
~oru_walk()
Definition: oru_walk.cpp:48
oru_walk(ALPtr< ALBroker > broker, const string &name)
Definition: oru_walk.cpp:18
void init()
Definition: oru_walk.cpp:66
void initPosition()
Definition: oru_walk.cpp:203