Fawkes API  Fawkes Development Version
GripperInterface.cpp
1 
2 /***************************************************************************
3  * GripperInterface.cpp - Fawkes BlackBoard Interface - GripperInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2013 Sebastian Reuter
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/GripperInterface.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 GripperInterface <interfaces/GripperInterface.h>
36  * GripperInterface Fawkes BlackBoard Interface.
37  *
38  This interface provides support for a simple gripper actuator.
39  It has been used with the Robotino Gripper.
40 
41  * @ingroup FawkesInterfaces
42  */
43 
44 
45 
46 /** Constructor */
47 GripperInterface::GripperInterface() : Interface()
48 {
49  data_size = sizeof(GripperInterface_data_t);
50  data_ptr = malloc(data_size);
51  data = (GripperInterface_data_t *)data_ptr;
52  data_ts = (interface_data_ts_t *)data_ptr;
53  memset(data_ptr, 0, data_size);
54  enum_map_GripperState[(int)OPEN] = "OPEN";
55  enum_map_GripperState[(int)CLOSED] = "CLOSED";
56  add_fieldinfo(IFT_ENUM, "gripper_state", 1, &data->gripper_state, "GripperState", &enum_map_GripperState);
57  add_messageinfo("OpenGripperMessage");
58  add_messageinfo("CloseGripperMessage");
59  unsigned char tmp_hash[] = {0xf8, 0xd6, 0x88, 0xb4, 0xfc, 0xfa, 0x1f, 0x1b, 0x20, 0x9f, 0xc, 0xd, 0x81, 0x3c, 0xba, 0xdf};
60  set_hash(tmp_hash);
61 }
62 
63 /** Destructor */
64 GripperInterface::~GripperInterface()
65 {
66  free(data_ptr);
67 }
68 /** Convert GripperState constant to string.
69  * @param value value to convert to string
70  * @return constant value as string.
71  */
72 const char *
73 GripperInterface::tostring_GripperState(GripperState value) const
74 {
75  switch (value) {
76  case OPEN: return "OPEN";
77  case CLOSED: return "CLOSED";
78  default: return "UNKNOWN";
79  }
80 }
81 /* Methods */
82 /** Get gripper_state value.
83  *
84  The current state of the gripper.
85 
86  * @return gripper_state value
87  */
89 GripperInterface::gripper_state() const
90 {
91  return (GripperInterface::GripperState)data->gripper_state;
92 }
93 
94 /** Get maximum length of gripper_state value.
95  * @return length of gripper_state value, can be length of the array or number of
96  * maximum number of characters for a string
97  */
98 size_t
99 GripperInterface::maxlenof_gripper_state() const
100 {
101  return 1;
102 }
103 
104 /** Set gripper_state value.
105  *
106  The current state of the gripper.
107 
108  * @param new_gripper_state new gripper_state value
109  */
110 void
111 GripperInterface::set_gripper_state(const GripperState new_gripper_state)
112 {
113  data->gripper_state = new_gripper_state;
114  data_changed = true;
115 }
116 
117 /* =========== message create =========== */
118 Message *
119 GripperInterface::create_message(const char *type) const
120 {
121  if ( strncmp("OpenGripperMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
122  return new OpenGripperMessage();
123  } else if ( strncmp("CloseGripperMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
124  return new CloseGripperMessage();
125  } else {
126  throw UnknownTypeException("The given type '%s' does not match any known "
127  "message type for this interface type.", type);
128  }
129 }
130 
131 
132 /** Copy values from other interface.
133  * @param other other interface to copy values from
134  */
135 void
136 GripperInterface::copy_values(const Interface *other)
137 {
138  const GripperInterface *oi = dynamic_cast<const GripperInterface *>(other);
139  if (oi == NULL) {
140  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
141  type(), other->type());
142  }
143  memcpy(data, oi->data, sizeof(GripperInterface_data_t));
144 }
145 
146 const char *
147 GripperInterface::enum_tostring(const char *enumtype, int val) const
148 {
149  if (strcmp(enumtype, "GripperState") == 0) {
150  return tostring_GripperState((GripperState)val);
151  }
152  throw UnknownTypeException("Unknown enum type %s", enumtype);
153 }
154 
155 /* =========== messages =========== */
156 /** @class GripperInterface::OpenGripperMessage <interfaces/GripperInterface.h>
157  * OpenGripperMessage Fawkes BlackBoard Interface Message.
158  *
159 
160  */
161 
162 
163 /** Constructor */
164 GripperInterface::OpenGripperMessage::OpenGripperMessage() : Message("OpenGripperMessage")
165 {
166  data_size = sizeof(OpenGripperMessage_data_t);
167  data_ptr = malloc(data_size);
168  memset(data_ptr, 0, data_size);
169  data = (OpenGripperMessage_data_t *)data_ptr;
171  enum_map_GripperState[(int)OPEN] = "OPEN";
172  enum_map_GripperState[(int)CLOSED] = "CLOSED";
173 }
174 
175 /** Destructor */
177 {
178  free(data_ptr);
179 }
180 
181 /** Copy constructor.
182  * @param m message to copy from
183  */
185 {
186  data_size = m->data_size;
187  data_ptr = malloc(data_size);
188  memcpy(data_ptr, m->data_ptr, data_size);
189  data = (OpenGripperMessage_data_t *)data_ptr;
191 }
192 
193 /* Methods */
194 /** Clone this message.
195  * Produces a message of the same type as this message and copies the
196  * data to the new message.
197  * @return clone of this message
198  */
199 Message *
201 {
202  return new GripperInterface::OpenGripperMessage(this);
203 }
204 /** @class GripperInterface::CloseGripperMessage <interfaces/GripperInterface.h>
205  * CloseGripperMessage Fawkes BlackBoard Interface Message.
206  *
207 
208  */
209 
210 
211 /** Constructor */
213 {
214  data_size = sizeof(CloseGripperMessage_data_t);
215  data_ptr = malloc(data_size);
216  memset(data_ptr, 0, data_size);
217  data = (CloseGripperMessage_data_t *)data_ptr;
219  enum_map_GripperState[(int)OPEN] = "OPEN";
220  enum_map_GripperState[(int)CLOSED] = "CLOSED";
221 }
222 
223 /** Destructor */
225 {
226  free(data_ptr);
227 }
228 
229 /** Copy constructor.
230  * @param m message to copy from
231  */
233 {
234  data_size = m->data_size;
235  data_ptr = malloc(data_size);
236  memcpy(data_ptr, m->data_ptr, data_size);
237  data = (CloseGripperMessage_data_t *)data_ptr;
239 }
240 
241 /* Methods */
242 /** Clone this message.
243  * Produces a message of the same type as this message and copies the
244  * data to the new message.
245  * @return clone of this message
246  */
247 Message *
249 {
250  return new GripperInterface::CloseGripperMessage(this);
251 }
252 /** Check if message is valid and can be enqueued.
253  * @param message Message to check
254  * @return true if the message is valid, false otherwise.
255  */
256 bool
258 {
259  const OpenGripperMessage *m0 = dynamic_cast<const OpenGripperMessage *>(message);
260  if ( m0 != NULL ) {
261  return true;
262  }
263  const CloseGripperMessage *m1 = dynamic_cast<const CloseGripperMessage *>(message);
264  if ( m1 != NULL ) {
265  return true;
266  }
267  return false;
268 }
269 
270 /// @cond INTERNALS
271 EXPORT_INTERFACE(GripperInterface)
272 /// @endcond
273 
274 
275 } // end namespace fawkes
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
CloseGripperMessage Fawkes BlackBoard Interface Message.
Fawkes library namespace.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Timestamp data, must be present and first entries for each interface data structs! This leans on time...
Definition: message.h:129
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
const char * type() const
Get type of interface.
Definition: interface.cpp:651
virtual Message * clone() const
Clone this message.
virtual Message * clone() const
Clone this message.
GripperState
Indicator of current or desired gripper state.
OpenGripperMessage Fawkes BlackBoard Interface Message.
GripperInterface Fawkes BlackBoard Interface.