Fawkes API  Fawkes Development Version
Laser360Interface.cpp
1 
2 /***************************************************************************
3  * Laser360Interface.cpp - Fawkes BlackBoard Interface - Laser360Interface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2008-2009 Tim Niemueller
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/Laser360Interface.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 Laser360Interface <interfaces/Laser360Interface.h>
36  * Laser360Interface Fawkes BlackBoard Interface.
37  *
38  This interface provides access to data of a laser scanner that produces
39  360 beams per scan. The inter-beam distance is 1 deg, 0 deg is
40  "forward", i.e. in the Fawkes coordinate system pointing towards
41  the cartesian point (1,0). The direction in which the angle
42  grows is indicated by the clockwise_angle field.
43 
44  * @ingroup FawkesInterfaces
45  */
46 
47 
48 
49 /** Constructor */
50 Laser360Interface::Laser360Interface() : Interface()
51 {
52  data_size = sizeof(Laser360Interface_data_t);
53  data_ptr = malloc(data_size);
54  data = (Laser360Interface_data_t *)data_ptr;
55  data_ts = (interface_data_ts_t *)data_ptr;
56  memset(data_ptr, 0, data_size);
57  add_fieldinfo(IFT_STRING, "frame", 32, data->frame);
58  add_fieldinfo(IFT_FLOAT, "distances", 360, &data->distances);
59  add_fieldinfo(IFT_BOOL, "clockwise_angle", 1, &data->clockwise_angle);
60  unsigned char tmp_hash[] = {0x5c, 0x1, 0x85, 0x24, 0x85, 0x28, 0x1f, 0xc6, 0xae, 0x4c, 0x46, 0x66, 0xe9, 0xcb, 0xe9, 0x4e};
61  set_hash(tmp_hash);
62 }
63 
64 /** Destructor */
65 Laser360Interface::~Laser360Interface()
66 {
67  free(data_ptr);
68 }
69 /* Methods */
70 /** Get frame value.
71  *
72  Coordinate frame in which the data is presented.
73 
74  * @return frame value
75  */
76 char *
78 {
79  return data->frame;
80 }
81 
82 /** Get maximum length of frame value.
83  * @return length of frame value, can be length of the array or number of
84  * maximum number of characters for a string
85  */
86 size_t
88 {
89  return 32;
90 }
91 
92 /** Set frame value.
93  *
94  Coordinate frame in which the data is presented.
95 
96  * @param new_frame new frame value
97  */
98 void
99 Laser360Interface::set_frame(const char * new_frame)
100 {
101  strncpy(data->frame, new_frame, sizeof(data->frame));
102  data_changed = true;
103 }
104 
105 /** Get distances value.
106  *
107  The distances in meter of the beams.
108 
109  * @return distances value
110  */
111 float *
113 {
114  return data->distances;
115 }
116 
117 /** Get distances value at given index.
118  *
119  The distances in meter of the beams.
120 
121  * @param index index of value
122  * @return distances value
123  * @exception Exception thrown if index is out of bounds
124  */
125 float
126 Laser360Interface::distances(unsigned int index) const
127 {
128  if (index > 360) {
129  throw Exception("Index value %u out of bounds (0..360)", index);
130  }
131  return data->distances[index];
132 }
133 
134 /** Get maximum length of distances value.
135  * @return length of distances value, can be length of the array or number of
136  * maximum number of characters for a string
137  */
138 size_t
140 {
141  return 360;
142 }
143 
144 /** Set distances value.
145  *
146  The distances in meter of the beams.
147 
148  * @param new_distances new distances value
149  */
150 void
151 Laser360Interface::set_distances(const float * new_distances)
152 {
153  memcpy(data->distances, new_distances, sizeof(float) * 360);
154  data_changed = true;
155 }
156 
157 /** Set distances value at given index.
158  *
159  The distances in meter of the beams.
160 
161  * @param new_distances new distances value
162  * @param index index for of the value
163  */
164 void
165 Laser360Interface::set_distances(unsigned int index, const float new_distances)
166 {
167  if (index > 360) {
168  throw Exception("Index value %u out of bounds (0..360)", index);
169  }
170  data->distances[index] = new_distances;
171  data_changed = true;
172 }
173 /** Get clockwise_angle value.
174  *
175  True if the angle grows clockwise.
176 
177  * @return clockwise_angle value
178  */
179 bool
181 {
182  return data->clockwise_angle;
183 }
184 
185 /** Get maximum length of clockwise_angle value.
186  * @return length of clockwise_angle value, can be length of the array or number of
187  * maximum number of characters for a string
188  */
189 size_t
191 {
192  return 1;
193 }
194 
195 /** Set clockwise_angle value.
196  *
197  True if the angle grows clockwise.
198 
199  * @param new_clockwise_angle new clockwise_angle value
200  */
201 void
202 Laser360Interface::set_clockwise_angle(const bool new_clockwise_angle)
203 {
204  data->clockwise_angle = new_clockwise_angle;
205  data_changed = true;
206 }
207 
208 /* =========== message create =========== */
209 Message *
211 {
212  throw UnknownTypeException("The given type '%s' does not match any known "
213  "message type for this interface type.", type);
214 }
215 
216 
217 /** Copy values from other interface.
218  * @param other other interface to copy values from
219  */
220 void
222 {
223  const Laser360Interface *oi = dynamic_cast<const Laser360Interface *>(other);
224  if (oi == NULL) {
225  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
226  type(), other->type());
227  }
228  memcpy(data, oi->data, sizeof(Laser360Interface_data_t));
229 }
230 
231 const char *
232 Laser360Interface::enum_tostring(const char *enumtype, int val) const
233 {
234  throw UnknownTypeException("Unknown enum type %s", enumtype);
235 }
236 
237 /* =========== messages =========== */
238 /** Check if message is valid and can be enqueued.
239  * @param message Message to check
240  * @return true if the message is valid, false otherwise.
241  */
242 bool
244 {
245  return false;
246 }
247 
248 /// @cond INTERNALS
249 EXPORT_INTERFACE(Laser360Interface)
250 /// @endcond
251 
252 
253 } // end namespace fawkes
Laser360Interface Fawkes BlackBoard Interface.
virtual void copy_values(const Interface *other)
Copy values from other interface.
size_t maxlenof_frame() const
Get maximum length of frame value.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:44
void set_frame(const char *new_frame)
Set frame value.
bool is_clockwise_angle() const
Get clockwise_angle value.
void set_clockwise_angle(const bool new_clockwise_angle)
Set clockwise_angle value.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:314
Fawkes library namespace.
unsigned int data_size
Minimal data size to hold data storage.
Definition: interface.h:221
string field
Definition: types.h:47
virtual const char * enum_tostring(const char *enumtype, int val) const
Convert arbitrary enum value to string.
void set_distances(unsigned int index, const float new_distances)
Set distances value at given index.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
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 field info list.
Definition: interface.cpp:335
size_t maxlenof_clockwise_angle() const
Get maximum length of clockwise_angle value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:222
const char * type() const
Get type of interface.
Definition: interface.cpp:651
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:220
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual Message * create_message(const char *type) const
Create message based on type name.
float field
Definition: types.h:45
interface_data_ts_t * data_ts
Pointer to data casted to timestamp struct.
Definition: interface.h:224
float * distances() const
Get distances value.
char * frame() const
Get frame value.
boolean field
Definition: types.h:36
size_t maxlenof_distances() const
Get maximum length of distances value.