Choreonoid  1.8
Device.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_DEVICE_H
7 #define CNOID_BODY_DEVICE_H
8 
9 #include <cnoid/ClonableReferenced>
10 #include <cnoid/EigenTypes>
11 #include <cnoid/Signal>
12 #include <string>
13 #include "exportdecl.h"
14 
15 namespace cnoid {
16 
17 class Body;
18 class Link;
19 
20 class CNOID_EXPORT DeviceState : public ClonableReferenced
21 {
22 protected:
25 
26 public:
27  virtual ~DeviceState() { }
28 
29  virtual const char* typeName() const = 0;
30  virtual void copyStateFrom(const DeviceState& other) = 0;
31  virtual DeviceState* cloneState() const = 0;
32 
36  virtual int stateSize() const = 0;
37 
42  virtual const double* readState(const double* buf) = 0;
43 
48  virtual double* writeState(double* out_buf) const = 0;
49 };
51 
52 
53 class CNOID_EXPORT Device : public DeviceState
54 {
55  struct NonState {
56  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
57  int index; // automatically assigned
58  int id; // pre-defined id
59  std::string name;
60  Link* link;
61  Isometry3 T_local;
62  const Isometry3& const_T_local() const { return T_local; }
63  Signal<void()> sigStateChanged;
64  Signal<void(double time)> sigTimeChanged;
65  };
66 
67  NonState* ns;
68 
69 protected:
70  Device();
71  Device(const Device& org, bool copyStateOnly = false);
72  void copySpecFrom(const Device* other);
73 
74 public:
75  virtual ~Device();
76 
77  Device* clone() const {
78  return static_cast<Device*>(doClone(nullptr));
79  }
80  Device* clone(CloneMap& cloneMap) const {
81  return static_cast<Device*>(doClone(&cloneMap));
82  }
83 
84  virtual bool copyFrom(const Device* other);
85 
86  void setIndex(int index) { ns->index = index; }
87  void setId(int id) { ns->id = id; }
88  void setName(const std::string& name) { ns->name = name; }
89  void setLink(Link* link) { ns->link = link; }
90 
91  virtual void forEachActualType(std::function<bool(const std::type_info& type)> func);
92 
93  bool hasStateOnly() const { return (ns != 0); }
94 
95  int index() const { return ns->index; }
96  int id() const { return ns->id; }
97  const std::string& name() const { return ns->name; }
98 
99  const Link* link() const { return ns->link; }
100  Link* link() { return ns->link; }
101 
102  const Body* body() const;
103  Body* body();
104 
105  Isometry3& T_local() { return ns->T_local; }
106  const Isometry3& T_local() const { return ns->T_local; }
107  const Isometry3& localPosition() const { return ns->T_local; }
108  template<class Scalar, int Mode, int Options>
109  void setLocalPosition(const Eigen::Transform<Scalar, 3, Mode, Options>& T) {
110  ns->T_local = T.template cast<Isometry3::Scalar>();
111  }
112 
113  Isometry3::ConstLinearPart R_local() const { return ns->const_T_local().linear(); }
114  Isometry3::LinearPart R_local() { return ns->T_local.linear(); }
115  Isometry3::ConstLinearPart localRotation() const { return ns->const_T_local().linear(); }
116  template<typename Derived>
117  void setLocalRotation(const Eigen::MatrixBase<Derived>& R) { ns->T_local.linear() = R; }
118 
119  Isometry3::ConstTranslationPart p_local() const { return ns->const_T_local().translation(); }
120  Isometry3::TranslationPart p_local() { return ns->T_local.translation(); }
121  Isometry3::ConstTranslationPart localTranslation() const { return ns->const_T_local().translation(); }
122  template<typename Derived>
123  void setLocalTranslation(const Eigen::MatrixBase<Derived>& p) { ns->T_local.translation() = p; }
124 
125  virtual void clearState();
126 
127  virtual bool on() const;
128  virtual void on(bool on);
129 
131  return ns->sigStateChanged;
132  }
133 
135  ns->sigStateChanged();
136  }
137 
138  SignalProxy<void(double time)> sigTimeChanged() {
139  return ns->sigTimeChanged;
140  }
141 
142  void notifyTimeChange(double time) {
143  ns->sigTimeChanged(time);
144  }
145 
146  [[deprecated("Please use T_local() instead")]]
147  const Isometry3& T_local_org() const { return ns->T_local; };
148  [[deprecated]]
149  void setLocalAttitude(const Isometry3& Ta);
150  [[deprecated]]
151  double cycle() const { return 20.0; }
152  [[deprecated]]
153  void setCycle(double msec) { }
154 };
155 
157 
158 }
159 
160 #endif
cnoid::Device::R_local
Isometry3::ConstLinearPart R_local() const
Definition: Device.h:113
cnoid::Device::hasStateOnly
bool hasStateOnly() const
Definition: Device.h:93
cnoid::Device::setLink
void setLink(Link *link)
Definition: Device.h:89
cnoid::Device::setLocalPosition
void setLocalPosition(const Eigen::Transform< Scalar, 3, Mode, Options > &T)
Definition: Device.h:109
cnoid::Device::T_local
const Isometry3 & T_local() const
Definition: Device.h:106
cnoid::Device::index
int index() const
Definition: Device.h:95
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::DeviceState::~DeviceState
virtual ~DeviceState()
Definition: Device.h:27
cnoid::Device::localTranslation
Isometry3::ConstTranslationPart localTranslation() const
Definition: Device.h:121
cnoid::Device::setLocalRotation
void setLocalRotation(const Eigen::MatrixBase< Derived > &R)
Definition: Device.h:117
cnoid::Device::clone
Device * clone() const
Definition: Device.h:77
cnoid::Device::localRotation
Isometry3::ConstLinearPart localRotation() const
Definition: Device.h:115
cnoid::Device::notifyTimeChange
void notifyTimeChange(double time)
Definition: Device.h:142
cnoid::ref_ptr< DeviceState >
cnoid::Device::setName
void setName(const std::string &name)
Definition: Device.h:88
cnoid::DeviceStatePtr
ref_ptr< DeviceState > DeviceStatePtr
Definition: Device.h:50
cnoid::Device::setId
void setId(int id)
Definition: Device.h:87
cnoid::Device::setIndex
void setIndex(int index)
Definition: Device.h:86
cnoid::Device::p_local
Isometry3::ConstTranslationPart p_local() const
Definition: Device.h:119
cnoid::Device::R_local
Isometry3::LinearPart R_local()
Definition: Device.h:114
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::ClonableReferenced
Definition: ClonableReferenced.h:10
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::Device::name
const std::string & name() const
Definition: Device.h:97
cnoid::DeviceState
Definition: Device.h:20
cnoid::Device::clone
Device * clone(CloneMap &cloneMap) const
Definition: Device.h:80
cnoid::Device::link
Link * link()
Definition: Device.h:100
cnoid::Device::localPosition
const Isometry3 & localPosition() const
Definition: Device.h:107
cnoid::Device::sigTimeChanged
SignalProxy< void(double time)> sigTimeChanged()
Definition: Device.h:138
cnoid::Device::cycle
double cycle() const
Definition: Device.h:151
cnoid::Body
Definition: Body.h:28
cnoid::Device::id
int id() const
Definition: Device.h:96
cnoid::Signal
Definition: Signal.h:165
cnoid::DeviceState::DeviceState
DeviceState(const DeviceState &)
Definition: Device.h:24
cnoid::Device
Definition: Device.h:53
cnoid::DeviceState::DeviceState
DeviceState()
Definition: Device.h:23
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::Device::setCycle
void setCycle(double msec)
Definition: Device.h:153
cnoid::Device::notifyStateChange
void notifyStateChange()
Definition: Device.h:134
cnoid::Device::sigStateChanged
SignalProxy< void()> sigStateChanged()
Definition: Device.h:130
cnoid::Device::T_local
Isometry3 & T_local()
Definition: Device.h:105
cnoid::Device::p_local
Isometry3::TranslationPart p_local()
Definition: Device.h:120
cnoid::Device::setLocalTranslation
void setLocalTranslation(const Eigen::MatrixBase< Derived > &p)
Definition: Device.h:123
cnoid::DevicePtr
ref_ptr< Device > DevicePtr
Definition: Device.h:156
cnoid::Device::T_local_org
const Isometry3 & T_local_org() const
Definition: Device.h:147
cnoid::Device::link
const Link * link() const
Definition: Device.h:99