snark
commands.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_ACTUATORS_QUICKSET_ptcr_COMMANDS_H_
20 #define SNARK_ACTUATORS_QUICKSET_ptcr_COMMANDS_H_
21 
22 #include <sstream>
23 #include <boost/array.hpp>
24 #include <comma/base/exception.h>
25 #include <comma/packed/byte.h>
26 #include <comma/packed/little_endian.h>
27 #include <comma/packed/struct.h>
28 
29 namespace snark { namespace quickset { namespace ptcr { namespace commands {
30 
31 struct pan_status: public comma::packed::packed_struct< pan_status, 1 >
32 {
33  comma::packed::byte value;
34 
35  bool cwsl() const { return 0x80 & *value.data(); }
36  bool ccwsl() const { return 0x40 & *value.data(); }
37  bool cwhl() const { return 0x20 & *value.data(); }
38  bool ccwhl() const { return 0x10 & *value.data(); }
39  bool to() const { return 0x8 & *value.data(); }
40  bool de() const { return 0x4 & *value.data(); }
41  bool fault() const { return de() || to(); }
42  std::string to_string() const { std::ostringstream oss; oss << cwsl() << ccwsl() << cwhl() << ccwhl() << to() << de() << 0 << 0; return oss.str(); }
43 };
44 
45 struct tilt_status: public comma::packed::packed_struct< tilt_status, 1 >
46 {
47  comma::packed::byte value;
48 
49  bool usl() const { return 0x80 & *value.data(); }
50  bool dsl() const { return 0x40 & *value.data(); }
51  bool uhl() const { return 0x20 & *value.data(); }
52  bool dhl() const { return 0x10 & *value.data(); }
53  bool to() const { return 0x8 & *value.data(); }
54  bool de() const { return 0x4 & *value.data(); }
55  bool fault() const { return de() || to(); }
56  std::string to_string() const { std::ostringstream oss; oss << usl() << dsl() << uhl() << dhl() << to() << de() << 0 << 0; return oss.str(); }
57 };
58 
59 struct general_status: public comma::packed::packed_struct< general_status, 1 >
60 {
61  comma::packed::byte value;
62 
63  bool con() const { return 0x80 & *value.data(); }
64  bool exec() const { return 0x40 & *value.data(); }
65  bool des() const { return 0x20 & *value.data(); }
66  bool oslr() const { return 0x10 & *value.data(); }
67  bool cwm() const { return 0x8 & *value.data(); }
68  bool ccwm() const { return 0x4 & *value.data(); }
69  bool upm() const { return 0x2 & *value.data(); }
70  bool dwnm() const { return 0x1 & *value.data(); }
71  std::string to_string() const { std::ostringstream oss; oss << con() << exec() << des() << oslr() << cwm() << ccwm() << upm() << dwnm(); return oss.str(); }
72 };
73 
74 struct get_status : public comma::packed::packed_struct< get_status, 7 >
75 {
76  struct command
77  {
78  static const unsigned char pdir = 0x80;
79  static const unsigned char tdir = 0x40;
80  static const unsigned char pslo = 0x10;
81  static const unsigned char tslo = 0x08;
82  static const unsigned char osl = 0x04;
83  static const unsigned char stop = 0x02;
84  static const unsigned char res = 0x01;
85  };
86 
87  enum { id = 0x31 }; // static const unsigned char id = 0x31;
88  comma::packed::byte command;
89  comma::packed::byte pan_jog_command;
90  comma::packed::byte tilt_jog_command;
91  comma::packed::byte zoom1_jog;
92  comma::packed::byte focus1_jog;
93  comma::packed::byte zoom2_jog;
94  comma::packed::byte focus2_jog;
95 
96  get_status() { ::memset( command.data(), 0, size ); }
97 
98  struct response : public comma::packed::packed_struct< get_status::response, 15 >
99  {
100  typedef get_status command;
101  enum { id = command::id }; // static const unsigned char id = command::id;
102  comma::packed::int24 pan;
103  comma::packed::int24 tilt;
104  pan_status response_pan_status;
105  tilt_status response_tilt_status;
106  general_status status;
107  comma::packed::byte zoom1position;
108  comma::packed::byte focus1position;
109  comma::packed::byte zoom2position;
110  comma::packed::byte focus2position;
111  comma::packed::byte camera1count;
112  comma::packed::byte camera2count;
113 
114  response() { camera1count = 0; camera2count = 0; } // quick and dirty: no camera for us
115 
116  bool operator==( const response& rhs ) const { return ::memcmp( data(), rhs.data(), size - 6 ) == 0; } // no camera status for now
117  bool operator!=( const response& rhs ) const { return !operator==( rhs ); }
118  };
119 };
120 
121 struct move_to : public comma::packed::packed_struct< move_to, 6 >
122 {
123  enum { id = 0x33 }; // static const unsigned char id = 0x33;
124  comma::packed::int24 pan;
125  comma::packed::int24 tilt;
126 
127  struct response : public comma::packed::packed_struct< move_to::response, 13 >
128  {
129  typedef move_to command;
130  enum { id = command::id }; // static const unsigned char id = command::id;
131  comma::packed::int24 pan;
132  comma::packed::int24 tilt;
133  pan_status response_pan_status;
134  tilt_status response_tilt_status;
135  general_status status;
136  comma::packed::byte zoom1position;
137  comma::packed::byte focus1position;
138  comma::packed::byte zoom2position;
139  comma::packed::byte focus2position;
140  };
141 };
142 
143 struct move_to_delta : public comma::packed::packed_struct< move_to_delta, 6 >
144 {
145  enum { id = 0x34 }; // static const unsigned char id = 0x34;
146  comma::packed::int24 pan;
147  comma::packed::int24 tilt;
148 
149  struct response : public comma::packed::packed_struct< move_to_delta::response, 13 >
150  {
151  typedef move_to_delta command;
152  enum { id = command::id }; // static const unsigned char id = command::id;
153  comma::packed::int24 pan;
154  comma::packed::int24 tilt;
155  pan_status response_pan_status;
156  tilt_status response_tilt_status;
157  general_status status;
158  comma::packed::byte zoom1position;
159  comma::packed::byte focus1position;
160  comma::packed::byte zoom2position;
161  comma::packed::byte focus2position;
162  };
163 };
164 
165 struct get_limits : public comma::packed::packed_struct< get_limits, 1 >
166 {
167  enum { id = 0x81 }; // static const unsigned char id = 0x81;
168  struct direction { enum values { clockwise = 0, right = clockwise, counterclockwise = 1, left = counterclockwise, up = 2, down = 3 }; };
169  static const unsigned char query = 0x80;
170  comma::packed::byte direction_byte;
171  get_limits() { direction_byte = query; }
172  get_limits( direction::values a ) { direction_byte = query | a; }
173 
174  struct response : public comma::packed::packed_struct< response, 4 >
175  {
176  typedef get_limits command;
177  enum { id = command::id }; // static const unsigned char id = command::id;
178  comma::packed::byte direction;
179  comma::packed::int24 value;
180  };
181 };
182 
183 struct set_limits : public comma::packed::packed_struct< set_limits, 4 >
184 {
185  enum { id = get_limits::id }; // static const unsigned char id = get_limits::id;
186  typedef get_limits::direction direction;
187  comma::packed::byte direction_byte;
188  comma::packed::int24 value;
189  set_limits() {}
190  set_limits( direction::values a, int v ) { direction_byte = a; value = v; }
191 
192  struct response : public comma::packed::packed_struct< response, 4 >
193  {
194  typedef set_limits command;
195  enum { id = command::id }; // static const unsigned char id = command::id;
196  comma::packed::byte direction;
197  comma::packed::int24 value;
198  };
199 };
200 
201 struct set_camera : public comma::packed::packed_struct< set_camera, 2 >
202 {
203  enum { id = 0x64 }; // static const unsigned char id = get_limits::id;
204  boost::array< comma::packed::byte, 2 > flags;
205  set_camera() { flags[0] = flags[1] = 0; }
206 
207  struct response : public comma::packed::packed_struct< response, 2 >
208  {
209  typedef set_camera command;
210  enum { id = command::id }; // static const unsigned char id = command::id;
211  boost::array< comma::packed::byte, 2 > flags;
212  response() { flags[0] = flags[1] = 0; }
213  };
214 };
215 
216 } } } } // namespace snark { namespace quickset { namespace ptcr { namespace commands {
217 
218 #endif // #ifndef SNARK_ACTUATORS_QUICKSET_ptcr_COMMANDS_H_