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