Choreonoid  1.8
MprControllerItemBase.h
Go to the documentation of this file.
1 #ifndef CNOID_MANIPULATOR_PLUGIN_MPR_CONTROLLR_ITEM_BASE_H
2 #define CNOID_MANIPULATOR_PLUGIN_MPR_CONTROLLR_ITEM_BASE_H
3 
4 #include <cnoid/ControllerItem>
5 #include "MprProgram.h"
6 #include "MprVariable.h"
7 #include <typeindex>
8 #include <memory>
9 #include "exportdecl.h"
10 
11 namespace cnoid {
12 
13 class MprProgramItemBase;
14 class MprProgram;
15 class MprStatement;
16 class LinkKinematicsKit;
17 class MprVariableSet;
18 
19 class CNOID_EXPORT MprControllerItemBase : public ControllerItem
20 {
21 public:
22  static void initializeClass(ExtensionManager* ext);
23 
26  virtual ~MprControllerItemBase();
27 
28  virtual double timeStep() const override;
29  double speedRatio() const;
30  void setSpeedRatio(double r);
31 
32  virtual bool initialize(ControllerIO* io) override final;
33  virtual bool start() override final;
34  virtual void input() override final;
35  virtual bool control() override final;
36  virtual void output() override final;
37  virtual void stop() override final;
38 
39 protected:
40  template<class StatementType>
41  void registerStatementInterpreter(std::function<bool(StatementType* statement)> interpret){
42  registerStatementInterpreter(
43  typeid(StatementType),
44  [interpret](MprStatement* statement){
45  return interpret(static_cast<StatementType*>(statement)); });
46  }
47  void registerBaseStatementInterpreters();
48 
49  void setResidentInputFunction(std::function<void()> input);
50 
56  MprProgramItemBase* getStartupProgramItem();
57 
62  MprProgram* getStartupProgram();
63 
64  MprProgram* getCurrentProgram();
65  MprProgram::iterator getCurrentIterator();
66  void setCurrent(MprProgram::iterator iter);
67  void setCurrent(
68  MprProgram* program, MprProgram::iterator iter, MprProgram::iterator upperNext);
69 
70  MprProgram* findProgram(const std::string& name);
71 
72  LinkKinematicsKit* linkKinematicsKitForControl();
73 
74  void pushControlFunctions(
75  std::function<bool()> control, std::function<void()> input = nullptr, std::function<void()> output = nullptr);
76 
77  void pushOutputOnceFunction(std::function<void()> outputOnce);
78 
79  virtual bool onInitialize(ControllerIO* io);
80  virtual bool onStart();
81  virtual bool onStop();
82 
83  // Virtual functions for customizing variables
84  virtual bool initializeVariables();
85  void setVariableListSync(MprVariableList* listInGui, MprVariableList* listInController);
86  virtual stdx::optional<MprVariable::Value> evalExpressionAsVariableValue(
87  std::string::const_iterator& io_expressionBegin, std::string::const_iterator expressionEnd);
88  virtual std::function<bool(MprVariable::Value value)> evalExpressionAsVariableToAssginValue(
89  const std::string& expression);
90 
91  virtual void onDisconnectedFromRoot() override;
92  virtual void doPutProperties(PutPropertyFunction& putProperty) override;
93  virtual bool store(Archive& archive) override;
94  virtual bool restore(const Archive& archive) override;
95 
96 private:
97  void registerStatementInterpreter(
98  std::type_index statementType, const std::function<bool(MprStatement* statement)>& interpret);
99 
100  class Impl;
101  Impl* impl;
102 };
103 
105 
106 
107 class CNOID_EXPORT MprControllerLog : public Referenced
108 {
109 public:
111  const std::string& topLevelProgramName() const { return *topLevelProgramName_; }
112  std::shared_ptr<const std::string> sharedTopLevelProgramName() const { return topLevelProgramName_; }
113  const std::vector<int>& hierachicalPosition() const { return hierachicalPosition_; }
114  bool isErrorState() const { return isErrorState_; }
115 
116 private:
117  std::shared_ptr<std::string> topLevelProgramName_;
118  std::vector<int> hierachicalPosition_;
119  bool isErrorState_;
120 
121  friend class MprControllerItemBase;
122 };
123 
125 
126 }
127 
128 #endif
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::PutPropertyFunction
Definition: PutPropertyFunction.h:51
cnoid::MprProgramItemBase
Definition: MprProgramItemBase.h:16
cnoid::Archive
Definition: Archive.h:22
cnoid::MprControllerItemBasePtr
ref_ptr< MprControllerItemBase > MprControllerItemBasePtr
Definition: MprControllerItemBase.h:104
cnoid::MprControllerLog::isErrorState
bool isErrorState() const
Definition: MprControllerItemBase.h:114
cnoid::ControllerIO
Definition: ControllerIO.h:16
cnoid::ControllerItem
Definition: ControllerItem.h:20
cnoid::ref_ptr
Definition: Referenced.h:103
cnoid::MprVariable::Value
stdx::variant< int, double, bool, std::string > Value
Definition: MprVariable.h:18
cnoid::MprControllerLog::sharedTopLevelProgramName
std::shared_ptr< const std::string > sharedTopLevelProgramName() const
Definition: MprControllerItemBase.h:112
MprVariable.h
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::LinkKinematicsKit
Definition: LinkKinematicsKit.h:23
cnoid::MprStatement
Definition: MprStatement.h:16
cnoid::MprVariableList
Definition: MprVariableList.h:12
cnoid::Referenced
Definition: Referenced.h:54
cnoid::MprProgram
Definition: MprProgram.h:21
cnoid::MprControllerLog::hierachicalPosition
const std::vector< int > & hierachicalPosition() const
Definition: MprControllerItemBase.h:113
cnoid::MprControllerLog
Definition: MprControllerItemBase.h:107
cnoid::MprControllerItemBase::registerStatementInterpreter
void registerStatementInterpreter(std::function< bool(StatementType *statement)> interpret)
Definition: MprControllerItemBase.h:41
cnoid::MprControllerLogPtr
ref_ptr< MprControllerLog > MprControllerLogPtr
Definition: MprControllerItemBase.h:124
cnoid::MprProgram::iterator
StatementContainer::iterator iterator
Definition: MprProgram.h:25
cnoid::MprControllerLog::topLevelProgramName
const std::string & topLevelProgramName() const
Definition: MprControllerItemBase.h:111
cnoid::MprControllerItemBase
Definition: MprControllerItemBase.h:19
MprProgram.h