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_changed |= change_field(data->gripper_state, new_gripper_state);
114 }
115 
116 /* =========== message create =========== */
117 Message *
118 GripperInterface::create_message(const char *type) const
119 {
120  if ( strncmp("OpenGripperMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
121  return new OpenGripperMessage();
122  } else if ( strncmp("CloseGripperMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
123  return new CloseGripperMessage();
124  } else {
125  throw UnknownTypeException("The given type '%s' does not match any known "
126  "message type for this interface type.", type);
127  }
128 }
129 
130 
131 /** Copy values from other interface.
132  * @param other other interface to copy values from
133  */
134 void
135 GripperInterface::copy_values(const Interface *other)
136 {
137  const GripperInterface *oi = dynamic_cast<const GripperInterface *>(other);
138  if (oi == NULL) {
139  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
140  type(), other->type());
141  }
142  memcpy(data, oi->data, sizeof(GripperInterface_data_t));
143 }
144 
145 const char *
146 GripperInterface::enum_tostring(const char *enumtype, int val) const
147 {
148  if (strcmp(enumtype, "GripperState") == 0) {
149  return tostring_GripperState((GripperState)val);
150  }
151  throw UnknownTypeException("Unknown enum type %s", enumtype);
152 }
153 
154 /* =========== messages =========== */
155 /** @class GripperInterface::OpenGripperMessage <interfaces/GripperInterface.h>
156  * OpenGripperMessage Fawkes BlackBoard Interface Message.
157  *
158 
159  */
160 
161 
162 /** Constructor */
163 GripperInterface::OpenGripperMessage::OpenGripperMessage() : Message("OpenGripperMessage")
164 {
165  data_size = sizeof(OpenGripperMessage_data_t);
166  data_ptr = malloc(data_size);
167  memset(data_ptr, 0, data_size);
168  data = (OpenGripperMessage_data_t *)data_ptr;
170  enum_map_GripperState[(int)OPEN] = "OPEN";
171  enum_map_GripperState[(int)CLOSED] = "CLOSED";
172 }
173 
174 /** Destructor */
176 {
177  free(data_ptr);
178 }
179 
180 /** Copy constructor.
181  * @param m message to copy from
182  */
184 {
185  data_size = m->data_size;
186  data_ptr = malloc(data_size);
187  memcpy(data_ptr, m->data_ptr, data_size);
188  data = (OpenGripperMessage_data_t *)data_ptr;
190 }
191 
192 /* Methods */
193 /** Clone this message.
194  * Produces a message of the same type as this message and copies the
195  * data to the new message.
196  * @return clone of this message
197  */
198 Message *
200 {
201  return new GripperInterface::OpenGripperMessage(this);
202 }
203 /** @class GripperInterface::CloseGripperMessage <interfaces/GripperInterface.h>
204  * CloseGripperMessage Fawkes BlackBoard Interface Message.
205  *
206 
207  */
208 
209 
210 /** Constructor */
212 {
213  data_size = sizeof(CloseGripperMessage_data_t);
214  data_ptr = malloc(data_size);
215  memset(data_ptr, 0, data_size);
216  data = (CloseGripperMessage_data_t *)data_ptr;
218  enum_map_GripperState[(int)OPEN] = "OPEN";
219  enum_map_GripperState[(int)CLOSED] = "CLOSED";
220 }
221 
222 /** Destructor */
224 {
225  free(data_ptr);
226 }
227 
228 /** Copy constructor.
229  * @param m message to copy from
230  */
232 {
233  data_size = m->data_size;
234  data_ptr = malloc(data_size);
235  memcpy(data_ptr, m->data_ptr, data_size);
236  data = (CloseGripperMessage_data_t *)data_ptr;
238 }
239 
240 /* Methods */
241 /** Clone this message.
242  * Produces a message of the same type as this message and copies the
243  * data to the new message.
244  * @return clone of this message
245  */
246 Message *
248 {
249  return new GripperInterface::CloseGripperMessage(this);
250 }
251 /** Check if message is valid and can be enqueued.
252  * @param message Message to check
253  * @return true if the message is valid, false otherwise.
254  */
255 bool
257 {
258  const OpenGripperMessage *m0 = dynamic_cast<const OpenGripperMessage *>(message);
259  if ( m0 != NULL ) {
260  return true;
261  }
262  const CloseGripperMessage *m1 = dynamic_cast<const CloseGripperMessage *>(message);
263  if ( m1 != NULL ) {
264  return true;
265  }
266  return false;
267 }
268 
269 /// @cond INTERNALS
270 EXPORT_INTERFACE(GripperInterface)
271 /// @endcond
272 
273 
274 } // end namespace fawkes
CloseGripperMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
OpenGripperMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
GripperInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
GripperState
Indicator of current or desired gripper state.
@ OPEN
Gripper is open.
@ CLOSED
Gripper is closed.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
const char * type() const
Get type of interface.
Definition: interface.cpp:643
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
Fawkes library namespace.
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:134