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