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)-1);
99  data->frame[sizeof(data->frame)-1] = 0;
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 Laser1080Interface::distances(unsigned int index) const
125 {
126  if (index > 1080) {
127  throw Exception("Index value %u out of bounds (0..1080)", 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 1080;
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 Laser1080Interface::set_distances(const float * new_distances)
150 {
151  memcpy(data->distances, new_distances, sizeof(float) * 1080);
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 Laser1080Interface::set_distances(unsigned int index, const float new_distances)
164 {
165  if (index > 1080) {
166  throw Exception("Index value %u out of bounds (0..1080)", 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 Laser1080Interface::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 Laser1080Interface *oi = dynamic_cast<const Laser1080Interface *>(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(Laser1080Interface_data_t));
227 }
228 
229 const char *
230 Laser1080Interface::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(Laser1080Interface)
248 /// @endcond
249 
250 
251 } // end namespace fawkes
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:125
bool is_clockwise_angle() const
Get clockwise_angle value.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:41
Laser1080Interface Fawkes BlackBoard Interface.
void set_hash(unsigned char *ihash)
Set hash.
Definition: interface.cpp:316
virtual void copy_values(const Interface *other)
Copy values from other interface.
Fawkes library namespace.
string field
Definition: types.h:48
size_t maxlenof_frame() const
Get maximum length of frame value.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:78
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:135
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:126
char * frame() const
Get frame value.
bool data_changed
Indicator if data has changed.
Definition: interface.h:226
const char * type() const
Get type of interface.
Definition: interface.cpp:640
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
Base class for exceptions in Fawkes.
Definition: exception.h:35
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:46
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:410
boolean field
Definition: types.h:37
size_t maxlenof_distances() const
Get maximum length of distances value.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.