SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
GenericPrintables.hpp
1 /* This file is part of scl, a control and simulation library
2 for robots and biomechanical models.
3 
4 scl is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8 
9 Alternatively, you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 scl is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public
20 License and a copy of the GNU General Public License along with
21 scl. If not, see <http://www.gnu.org/licenses/>.
22  */
23 /* \file GenericPrintables.hpp
24  *
25  * Created on: Sep 19, 2011
26  *
27  * Copyright (C) 2011
28  *
29  * Author: Samir Menon <smenon@stanford.edu>
30  */
31 
32 #ifndef GENERICPRINTABLES_HPP_
33 #define GENERICPRINTABLES_HPP_
34 
35 #include <scl/data_structs/SRobotIO.hpp>
36 #include <scl/data_structs/SRigidBody.hpp>
37 #include <scl/data_structs/SRobotParsed.hpp>
38 #include <scl/control/data_structs/SControllerBase.hpp>
39 #include <scl/control/task/data_structs/STaskBase.hpp>
40 #include <scl/control/task/data_structs/SControllerMultiTask.hpp>
41 
42 #include <scl/Singletons.hpp>
43 
44 #include <sutil/CRegisteredPrintables.hpp>
45 
46 #include <stdexcept>
47 #include <iomanip>
48 
49 #ifdef DEBUG
50 #include <cassert>
51 #endif
52 
53 /* **************************************************************************
54  * Starting namespace sutil
55  *
56  * These are a set of printable object definitions for objects in the database
57  * Creating separate printable objects enables accessing the objects's values
58  * through callbacks
59  * Directly accessing the objects might be hard for certain situations
60  * (Like from another process or a gui) and callbacks are good there.
61  * ************************************************************************* */
62 namespace sutil
63 {
64  template <>
65  void printToStream<scl::SRobotParsed>(
66  std::ostream& ostr,
67  const scl::SRobotParsed& arg_data
68  )
69  {
70  ostr<<"\n Name: "<<arg_data.name_;
71  ostr<<"("<<arg_data.getType()<<")";
72  ostr<<"\n Dof: "<<arg_data.dof_;
73  ostr<<"\n Jlim_max : "<<arg_data.gc_pos_limit_max_.transpose();
74  ostr<<"\n Jlim_min : "<<arg_data.gc_pos_limit_min_.transpose();
75  ostr<<"\n Actuator_force_max : "<<arg_data.actuator_forces_max_.transpose();
76  ostr<<"\n Actuator_force_min : "<<arg_data.actuator_forces_min_.transpose();
77  ostr<<"\n Flag_gc_pos_limits : "<<arg_data.flag_apply_gc_pos_limits_;
78  ostr<<"\n Flag_gc_damping : "<<arg_data.flag_apply_gc_damping_;
79  ostr<<"\n Flag_force_limits : "<<arg_data.flag_apply_actuator_force_limits_;
80  ostr<<"\n Flag_vel_limits : "<<arg_data.flag_apply_actuator_vel_limits_;
81  ostr<<"\n Flag_accel_limits : "<<arg_data.flag_apply_actuator_acc_limits_;
82  ostr<<std::endl;
83  }
84 
85  template <>
86  void printToStream<scl::SRobotIO>(
87  std::ostream& ostr,
88  const scl::SRobotIO& arg_data
89  )
90  {
91  ostr<<"\n Name: "<<arg_data.name_;
92  ostr<<"("<<arg_data.getType()<<")";
93  ostr<<"\nDof: "<<arg_data.dof_;
94  ostr<<"\nHas been init: "<<arg_data.has_been_init_;
95  ostr<<"\n q: "<<arg_data.sensors_.q_.transpose();
96  ostr<<"\n dq: "<<arg_data.sensors_.dq_.transpose();
97  ostr<<"\nddq: "<<arg_data.sensors_.ddq_.transpose();
98  ostr<<"\nMeasured force: "<<arg_data.sensors_.force_gc_measured_.transpose();
99  ostr<<"\n Command force: "<<arg_data.actuators_.force_gc_commanded_.transpose();
100  //NOTE TODO : Implement a loop to print the external forces.
101  //ostr<<"\nMeasured force: "<<arg_data.sensors_.forces_external_;
102  ostr<<std::endl;
103  }
104 
105  template <>
106  void printToStream<scl::SRigidBody>(
107  std::ostream& ostr,
108  const scl::SRigidBody& arg_data
109  )
110  {
111  ostr<<"\n Name: "<<arg_data.name_;
112  ostr<<"("<<arg_data.getType()<<")";
113  ostr<<"\n Init : "<<arg_data.has_been_init_;
114  ostr<<"\n Id : "<<arg_data.link_id_;
115  ostr<<"\n Parent : "<<arg_data.parent_name_;
116  ostr<<"\n Robot : "<<arg_data.robot_name_;
117  ostr<<"\n Joint : "<<arg_data.joint_name_;
118  ostr<<"\n Jtype : "<<arg_data.joint_type_;
119  ostr<<"\n Jlim : ["<<arg_data.joint_limit_lower_<<", "<<arg_data.joint_limit_upper_<<"]";
120  ostr<<"\n Com : "<<arg_data.com_.transpose();
121  ostr<<"\n Inertia: "<<arg_data.inertia_;
122  ostr<<"\n PosPar : "<<arg_data.pos_in_parent_.transpose();
123  ostr<<"\n OriPar : [w xyz] ["<<arg_data.ori_parent_quat_.w()
124  <<", "<<arg_data.ori_parent_quat_.x()
125  <<", "<<arg_data.ori_parent_quat_.y()
126  <<", "<<arg_data.ori_parent_quat_.z()<<"]";
127  ostr<<std::endl;
128  }
129 
130  template <>
131  void printToStream<scl::STaskBase>(
132  std::ostream& ostr,
133  const scl::STaskBase& arg_data
134  )
135  {
136  ostr<<"\n Name: "<<arg_data.name_;
137  ostr<<"("<<arg_data.getType()<<")";
138  ostr<<". Parent("<<arg_data.parent_controller_->name_<<")";
139  ostr<<"\n Init/Active : "<<arg_data.has_been_init_<<"/"<<arg_data.has_been_activated_;
140  ostr<<"\n Priority : "<<arg_data.priority_;
141  ostr<<"\n F_task : "<<arg_data.force_task_.transpose();
142  ostr<<"\n F_gc : "<<arg_data.force_gc_.transpose();
143  ostr<<std::endl;
144  }
145 
146  template <>
147  void printToStream<scl::SControllerMultiTask>(
148  std::ostream& ostr,
149  const scl::SControllerMultiTask& arg_data
150  )
151  {
152  ostr<<"\n Name: "<<arg_data.name_;
153  ostr<<"("<<arg_data.getType()<<")";
154  ostr<<"\n Init : "<<arg_data.has_been_init_;
155  ostr<<std::endl;
156  unsigned int i=0;
158  for(it = arg_data.tasks_.begin(), ite = arg_data.tasks_.end();
159  it!=ite; ++it)
160  {
161 #ifdef DEBUG
162  assert(NULL != *it);
163 #endif
164  const scl::STaskBase& task = **it;
165  ostr<<"\n === Task #"<<i<<":"; i++;
166  printToStream<scl::STaskBase>(ostr, task);
167  }
168  }
169 
170  template <>
171  void printToStream<Eigen::Vector3d >(
172  std::ostream& ostr,
173  const Eigen::Vector3d& arg_eigvec
174  )
175  { ostr<<arg_eigvec.transpose()<<std::flush; }
176 }
177 
178 /* **************************************************************************
179  * Starting namespace scl
180  *
181  * Here we define callback adders for generic objects (like robots)
182  * ************************************************************************* */
183 namespace scl
184 {
185  bool addRobotPrintables()
186  {
187  bool flag;
188  try
189  {
190  std::cout<<"\n\n*** Robot printables ***"
191  <<"\n\nscl>> print <printable name>"
192  <<"\n\n"<<std::setw(10)<< "Printable information"<<std::setw(45)<<"Printable name\n";
193  //Add all the robot links as printables.
195  for(it = scl::CDatabase::getData()->s_parser_.robots_.begin(),
196  ite = scl::CDatabase::getData()->s_parser_.robots_.end();
197  it!=ite;++it)
198  {
199  scl::SRobotParsed& rob = *it;
200 
201  //NOTE: In the database, the robot's parsed and IO data structures
202  //are both indexed by the robot name. So we add "Parsed" here at the
203  //end to distinguish between the. (You may pick your own convention).
204  flag = sutil::printables::add(rob.name_+std::string("Parsed"),rob);
205  if(false == flag)
206  {throw(std::runtime_error(std::string("Could not add a printable: Robot: ")+
207  rob.name_)); }
208  else
209  { std::cout<<"\n"<<std::setw(10)<< "Printable: Robot: "<<std::setw(51)<<rob.name_+std::string("Parsed"); }
210 
211  //Also add the links
213  for(itl = rob.rb_tree_.begin(), itle = rob.rb_tree_.end();
214  itl!=itle; ++itl)
215  {
216  scl::SRigidBody& l = *itl;
217  flag = sutil::printables::add(l.name_,l);
218  if(false == flag)
219  {throw(std::runtime_error(std::string("Could not add a printable: Robot: ")+
220  rob.name_+". Link: "+l.name_)); }
221  else
222  { std::cout<<"\n"<<std::setw(10)<<"Printable: "<<rob.name_<<" "<<l.getType()<<std::setw(40)<<l.name_; }
223  }
224  }
225 
226  //Add all the IO data structures for all the robots
228  for(iti = scl::CDatabase::getData()->s_io_.io_data_.begin(),
229  itie = scl::CDatabase::getData()->s_io_.io_data_.end();
230  iti!=itie; ++iti)
231  {
232  scl::SRobotIO& io = *iti;
233  flag = sutil::printables::add(io.name_,io);
234  if(false == flag)
235  {throw(std::runtime_error(std::string("Could not add a printable: Robot: ")+
236  io.name_)); }
237  else
238  { std::cout<<"\n"<<std::setw(10)<<"Printable: Robot: "<<std::setw(51)<<io.name_; }
239  }
240 
241  //Add all the task controller data structures for all the robots
243  for(itct = scl::CDatabase::getData()->s_controller_.controllers_.begin(),
244  itcte = scl::CDatabase::getData()->s_controller_.controllers_.end();
245  itct!=itcte; ++itct)
246  {
247  const scl::SControllerMultiTask* ctrl = dynamic_cast<scl::SControllerMultiTask*>(*itct);
248  if(S_NULL == ctrl)
249  { continue; } //Move on. Not a task controller.
250  flag = sutil::printables::add(ctrl->name_,*ctrl);
251  if(false == flag)
252  {throw(std::runtime_error(std::string("Could not add a printable: Task Controller: ")+
253  ctrl->name_)); }
254  else
255  { std::cout<<"\n"<<std::setw(10)<<"Printable: Task Controller: "<<std::setw(41)<<ctrl->name_; }
256  }
257 
258  //Add all the task controller data structures for all the robots
260  for(itctt = scl::CDatabase::getData()->s_controller_.tasks_.begin(),
261  itctte = scl::CDatabase::getData()->s_controller_.tasks_.end();
262  itctt!=itctte; ++itctt)
263  {
264  const scl::STaskBase* task = *itctt;
265 #ifdef DEBUG
266  assert(S_NULL != task);
267 #else
268  if(S_NULL == task)
269  { continue; } //Move on. Not a task.
270 #endif
271  flag = sutil::printables::add(task->name_,*task);
272  if(false == flag)
273  {throw(std::runtime_error(std::string("Could not add a printable: Task Controller: ")+
274  task->name_)); }
275  else
276  { std::cout<<"\n"<<std::setw(10)<<"Printable: Task: "<<std::setw(52)<<task->name_; }
277  }
278 
279  //Add the UI points
280  for(unsigned int i=0;i<SCL_NUM_UI_POINTS;++i)
281  {
282  std::stringstream ss; ss<<"ui"<<i;
283  std::string name(ss.str());
284  flag = sutil::printables::add(name,scl::CDatabase::getData()->s_gui_.ui_point_[i]);
285  if(false == flag)
286  {throw(std::runtime_error(std::string("Could not add a printable: ")+name)); }
287  else
288  { std::cout<<"\n"<<std::setw(10)<<"Printable: UI-point "<<std::setw(49)<<name; }
289  }
290  std::cout<<std::endl<<std::endl;
291  }
292  catch(std::exception &e)
293  {
294  std::cout<<"addRobotPrintables() : Error: "<<e.what();
295  return false;
296  }
297  return true;
298  }
299 }
300 #endif /* GENERICPRINTABLES_HPP_ */
Definition: STaskBase.hpp:69
Definition: SRobotIO.hpp:107
Definition: SControllerMultiTask.hpp:54
Definition: SRobotParsed.hpp:51
Definition: SRigidBody.hpp:94
#define SCL_NUM_UI_POINTS
Definition: SDatabase.hpp:170
Definition: CMappedList.hpp:277
virtual const std::string & getType() const
Definition: SObject.hpp:60
static SDataStruct * getData()
Definition: CSingleton.hpp:87
sutil::CMappedTree< std::string, SRigidBody > rb_tree_
Definition: SRobotParsed.hpp:56
std::string name_
Definition: SObject.hpp:69
Definition: CMappedTree.hpp:66
Definition: CMappedList.hpp:381
bool add(const std::string &arg_name, const ObjectType &arg_obj)
Definition: CRegisteredPrintables.hpp:102