Fawkes API  Fawkes Development Version
JointInterface.cpp
1 
2 /***************************************************************************
3  * JointInterface.cpp - Fawkes BlackBoard Interface - JointInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2013 Till Hofmann
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/JointInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class JointInterface <interfaces/JointInterface.h>
36  * JointInterface Fawkes BlackBoard Interface.
37  *
38  Storage for a single joint state.
39 
40  * @ingroup FawkesInterfaces
41  */
42 
43 
44 
45 /** Constructor */
46 JointInterface::JointInterface() : Interface()
47 {
48  data_size = sizeof(JointInterface_data_t);
49  data_ptr = malloc(data_size);
50  data = (JointInterface_data_t *)data_ptr;
51  data_ts = (interface_data_ts_t *)data_ptr;
52  memset(data_ptr, 0, data_size);
53  add_fieldinfo(IFT_FLOAT, "position", 1, &data->position);
54  add_fieldinfo(IFT_FLOAT, "velocity", 1, &data->velocity);
55  unsigned char tmp_hash[] = {0xd2, 0x74, 0x1b, 0x6a, 0x5b, 0xf, 0xa9, 0xe1, 0xb0, 0xa8, 0x47, 0x84, 0x6f, 0x8f, 0x1c, 0xab};
56  set_hash(tmp_hash);
57 }
58 
59 /** Destructor */
60 JointInterface::~JointInterface()
61 {
62  free(data_ptr);
63 }
64 /* Methods */
65 /** Get position value.
66  *
67  The joint's position in rad.
68 
69  * @return position value
70  */
71 float
73 {
74  return data->position;
75 }
76 
77 /** Get maximum length of position value.
78  * @return length of position value, can be length of the array or number of
79  * maximum number of characters for a string
80  */
81 size_t
83 {
84  return 1;
85 }
86 
87 /** Set position value.
88  *
89  The joint's position in rad.
90 
91  * @param new_position new position value
92  */
93 void
94 JointInterface::set_position(const float new_position)
95 {
96  data->position = new_position;
97  data_changed = true;
98 }
99 
100 /** Get velocity value.
101  *
102  The joint's velocity in rad/s.
103 
104  * @return velocity value
105  */
106 float
108 {
109  return data->velocity;
110 }
111 
112 /** Get maximum length of velocity value.
113  * @return length of velocity value, can be length of the array or number of
114  * maximum number of characters for a string
115  */
116 size_t
118 {
119  return 1;
120 }
121 
122 /** Set velocity value.
123  *
124  The joint's velocity in rad/s.
125 
126  * @param new_velocity new velocity value
127  */
128 void
129 JointInterface::set_velocity(const float new_velocity)
130 {
131  data->velocity = new_velocity;
132  data_changed = true;
133 }
134 
135 /* =========== message create =========== */
136 Message *
138 {
139  throw UnknownTypeException("The given type '%s' does not match any known "
140  "message type for this interface type.", type);
141 }
142 
143 
144 /** Copy values from other interface.
145  * @param other other interface to copy values from
146  */
147 void
149 {
150  const JointInterface *oi = dynamic_cast<const JointInterface *>(other);
151  if (oi == NULL) {
152  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
153  type(), other->type());
154  }
155  memcpy(data, oi->data, sizeof(JointInterface_data_t));
156 }
157 
158 const char *
159 JointInterface::enum_tostring(const char *enumtype, int val) const
160 {
161  throw UnknownTypeException("Unknown enum type %s", enumtype);
162 }
163 
164 /* =========== messages =========== */
165 /** Check if message is valid and can be enqueued.
166  * @param message Message to check
167  * @return true if the message is valid, false otherwise.
168  */
169 bool
171 {
172  return false;
173 }
174 
175 /// @cond INTERNALS
176 EXPORT_INTERFACE(JointInterface)
177 /// @endcond
178 
179 
180 } // end namespace fawkes
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:124
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
float position() const
Get position value.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
Fawkes library namespace.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:133
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:125
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
virtual void copy_values(const Interface *other)
Copy values from other interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
float velocity() const
Get velocity value.
const char * type() const
Get type of interface.
Definition: interface.cpp:651
size_t maxlenof_velocity() const
Get maximum length of velocity value.
size_t maxlenof_position() const
Get maximum length of position value.
void set_position(const float new_position)
Set position value.
float field
Definition: types.h:45
virtual Message * create_message(const char *type) const
Create message based on type name.
void set_velocity(const float new_velocity)
Set velocity value.
JointInterface Fawkes BlackBoard Interface.
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:436
const char * type() const
Get message type.
Definition: message.cpp:378