simple_message.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # FSRobo-R Package BSDL
5 # ---------
6 # Copyright (C) 2019 FUJISOFT. All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification,
9 # are permitted provided that the following conditions are met:
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation and/or
14 # other materials provided with the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its contributors
16 # may be used to endorse or promote products derived from this software without
17 # specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # ---------
30 
31 from struct import pack,unpack
32 
33 class TriStates:
34  UNKNOWN = -1
35  ON = TRUE = ENABLED = HIGH = 1
36  OFF = FALSE = DISABLED = LOW = 0
37 
38  @classmethod
39  def from_bool(self, b):
40  return self.TRUE if b else self.FALSE
41 
43  START_TRAJECTORY_DOWNLOAD = -1
44  START_TRAJECTORY_STREAMING = -2
45  END_TRAJECTORY = -3
46  STOP_TRAJECTORY = -4
47 
49  JOINT_POSITION = 10
50  JOINT_TRAJ_PT = 11
51  STATUS = 13
52  SET_IO = 9001
53  IO_STATE = 9003
54  EXECUTE_PROGRAM = 9004
55  SET_POSTURE = 9005
56  GET_POSTURE = 9006
57  WRENCH = 9007
58  SYS_STAT = 9008
59  SET_TOOL_OFFSET = 9009
60 
62  INVALID = 0
63  TOPIC = 1
64  SERVICE_REQUEST = 2
65  SERVICE_REPLY = 3
66 
67 class ReplyCode:
68  INVALID = UNUSED = 0
69  SUCCESS = 1
70  FAILURE = 2
71 
72 class RobotMode:
73  UNKNOWN = -1
74  MANUAL = 1
75  AUTO = 2
76 
78  SET_DIGITAL_OUT = 1
79  SET_ADC_MODE = 2
80 
81 class IOState:
82  NO_CHANGE = -1
83  ON = 1
84  OFF = 0
85 
86 class SimpleMessage(object):
87  PREFIX_SIZE = 4
88  HEADER_SIZE = 12
89  # Prefix
90  length = 0
91  # Header
92  msg_type = 0
93  comm_type = 0
94  reply_code = 0
95  body = []
96 
97  def make_body(self):
98  return ""
99 
100  def load_body(self, body):
101  pass
102 
103  def dump(self):
104  body = self.make_body()
105  prefix = pack("i", SimpleMessage.HEADER_SIZE + len(body))
106  header = pack("3i", self.msg_type, self.comm_type, self.reply_code)
107  return prefix + header + self.make_body()
108 
109  def load(self, data):
110  self.length, self.msg_type, self.comm_type, self.reply_code = unpack('4i', data[:16])
111  self.load_body(data[16:])
112 
113 
114 def recvBytes(socket, num):
115  MAX_SIZE = 4096
116 
117  remain = num
118  data = ""
119 
120  while remain > 0:
121  print("recv: {}".format(remain))
122  buf = socket.recv(MAX_SIZE if remain > MAX_SIZE else remain)
123 
124  buf_size = len(buf)
125 
126  if buf_size == 0: # TODO handling error
127  assert()
128 
129  remain -= buf_size
130  data += buf
131 
132  return data
133 
134 def sendBytes(socket, buf):
135  buf_size = len(buf)
136  remain = buf_size
137 
138  while remain > 0:
139  print("send: {}".format(remain))
140  i = buf_size - remain
141  send_size = socket.send(buf[i:])
142 
143  if send_size == 0: # TODO handling error
144  assert()
145 
146  remain -= send_size
147 
149  "TODO docstring"
150  sequence = 0
151 
152  def __init__(self):
153  self.msg_type = SimpleMessageType.JOINT_POSITION
154  self.comm_type = CommunicationType.TOPIC
155  self.joint_data = [0] * 10
156 
157  def make_body(self):
158  return pack("i10f", self.sequence, *self.joint_data)
159 
161  "TODO docstring"
162 
163  def __init__(self):
164  self.msg_type = SimpleMessageType.JOINT_TRAJ_PT
165  self.comm_type = CommunicationType.SERVICE_REQUEST
166  self.sequence = 0
167  self.joint_data = [0] * 10
168  self.velocity = 0
169  self.duration = 0
170 
171  def make_body(self):
172  return pack('i10f', self.sequence, *self.joint_data) \
173  + pack('2f', self.velocity, self.duration)
174 
175  def load_body(self, body):
176  self.sequence, = unpack('i', body[0:4])
177  self.joint_data = list(unpack('10f', body[4:44]))
178  self.velocity, self.duration = unpack('2f', body[44:52])
179 
181  "TODO docstring"
182 
183  def __init__(self):
184  self.msg_type = SimpleMessageType.JOINT_TRAJ_PT
185  self.comm_type = CommunicationType.SERVICE_REPLY
186  self.sequence = 0
187  self.joint_data = [0] * 10
188 
189  def make_body(self):
190  return pack('i10f', self.sequence, *self.joint_data)
191 
192  def load_body(self, body):
193  self.sequence, = unpack('i', body[0:4])
194  self.joint_data = list(unpack('10f', body[4:44]))
195 
197  "TODO docstring"
198 
199  def __init__(self):
200  self.msg_type = SimpleMessageType.STATUS
201  self.comm_type = CommunicationType.TOPIC
202  self.mode = RobotMode.UNKNOWN
203  self.e_stopped = TriStates.UNKNOWN
204  self.drives_powered = TriStates.UNKNOWN
205  self.motion_possible = TriStates.UNKNOWN
206  self.in_motion = TriStates.UNKNOWN
207  self.in_error = TriStates.UNKNOWN
208  self.error_code = 0
209 
210  def make_body(self):
211  return pack("7i",
212  self.drives_powered,
213  self.e_stopped,
214  self.error_code,
215  self.in_error,
216  self.in_motion,
217  self.mode,
218  self.motion_possible)
219 
221  "TODO docstring"
222 
223  def __init__(self):
224  self.msg_type = 9001
225  self.comm_type = CommunicationType.SERVICE_REQUEST
226  self.fun = 0
227  self.address = 0
228  self.data_size = 0
229  self.data = [0] * 32
230 
231  def make_body(self):
232  return pack('ii32i', self.fun, self.address, *self.data)
233 
234  def load_body(self, body):
235  self.fun, self.address, self.data_size = unpack('3i', body[0:12])
236  self.data = list(unpack('32i', body[12:140]))
237 
239  "TODO docstring"
240 
241  class Result:
242  FAILURE = 0
243  SUCCESS = 1
244 
245  def __init__(self):
246  self.msg_type = 9002
247  self.comm_type = CommunicationType.SERVICE_REPLY
248  self.result = self.Result.SUCCESS
249 
250  def make_body(self):
251  return pack('i', self.result)
252 
253  def load_body(self, body):
254  self.result = unpack('i', body[0:4])
255 
257  "TODO docstring"
258 
259  def __init__(self):
260  self.msg_type = SimpleMessageType.IO_STATE
261  self.comm_type = CommunicationType.TOPIC
262  self.digital = [0] * 2
263  self.analog = [0]
264 
265  def make_body(self):
266  return pack("2i", *self.digital) + pack("i", *self.analog)
267 
269  "TODO docstring"
270 
271  def __init__(self):
272  self.msg_type = SimpleMessageType.WRENCH
273  self.comm_type = CommunicationType.TOPIC
274  self.force = [0] * 3
275  self.torque = [0] * 3
276 
277  def make_body(self):
278  return pack("3f", *self.force) + pack("3f", *self.torque)
279 
280 
282  "TODO docstring"
283 
284  def __init__(self):
285  self.msg_type = SimpleMessageType.EXECUTE_PROGRAM
286  self.comm_type = CommunicationType.SERVICE_REQUEST
287  self.name = ''
288  self.param = ''
289 
290  def make_body(self):
291  return self.name + pack('i', len(self.name))
292 
293  def load_body(self, body):
294  param_size, = unpack('i', body[-4:])
295  self.param = body[-(param_size + 4):-4]
296  name_size, = unpack('i', body[-(param_size + 4 * 2):-(param_size + 4)])
297  self.name = body[0:name_size]
298 
300  "TODO docstring"
301 
302  class Result:
303  FAILURE = 0
304  SUCCESS = 1
305 
306  def __init__(self):
307  self.msg_type = SimpleMessageType.EXECUTE_PROGRAM
308  self.comm_type = CommunicationType.SERVICE_REPLY
309  self.result = self.Result.SUCCESS
310 
311  def make_body(self):
312  return pack('i', self.result)
313 
314  def load_body(self, body):
315  self.result, = unpack('i', body[0:4])
316 
318  "TODO docstring"
319 
320  def __init__(self):
321  self.msg_type = SimpleMessageType.SET_POSTURE
322  self.comm_type = CommunicationType.SERVICE_REQUEST
323  self.posture = 0
324 
325  def make_body(self):
326  return pack('i', self.posture)
327 
328  def load_body(self, body):
329  self.posture, = unpack('i', body[0:4])
330 
332  "TODO docstring"
333 
334  def __init__(self):
335  self.msg_type = SimpleMessageType.SET_POSTURE
336  self.comm_type = CommunicationType.SERVICE_REPLY
337  self.posture = 0
338 
339  def make_body(self):
340  return pack('i', self.posture)
341 
342  def load_body(self, body):
343  self.posture, = unpack('i', body[0:4])
344 
346  "TODO docstring"
347 
348  def __init__(self):
349  self.msg_type = SimpleMessageType.GET_POSTURE
350  self.comm_type = CommunicationType.SERVICE_REQUEST
351  self.posture = 0
352 
353  def make_body(self):
354  return pack('i', self.posture)
355 
356  def load_body(self, body):
357  self.posture, = unpack('i', body[0:4])
358 
360  "TODO docstring"
361 
362  def __init__(self):
363  self.msg_type = SimpleMessageType.GET_POSTURE
364  self.comm_type = CommunicationType.SERVICE_REPLY
365  self.posture = 0
366 
367  def make_body(self):
368  return pack('i', self.posture)
369 
370  def load_body(self, body):
371  self.posture, = unpack('i', body[0:4])
372 
374  "TODO docstring"
375 
376  class StatType:
377  EMS_STATE = 1
378  MOTION_ID = 2
379  INC_MODE = 3
380  ABS_LOST = 4
381  MOTION_REQUEST = 5
382  MOTION_STATE = 6
383  SPI_ERROR = 7
384  SERVO_CONTROL = 8
385  PULSE = 0x8000
386 
387  def __init__(self):
388  self.msg_type = SimpleMessageType.SYS_STAT
389  self.comm_type = CommunicationType.SERVICE_REQUEST
390  self.stat_type = 0
391  self.result = 0
392 
393  def make_body(self):
394  return pack('2i', self.stat_type, self.result)
395 
396  def load_body(self, body):
397  self.stat_type, self.result = unpack('2i', body[0:8])
398 
400  "TODO docstring"
401 
402  class StatType:
403  EMS_STATE = 1
404  MOTION_ID = 2
405  INC_MODE = 3
406  ABS_LOST = 4
407  MOTION_REQUEST = 5
408  MOTION_STATE = 6
409  SPI_ERROR = 7
410  SERVO_CONTROL = 8
411  PULSE = 0x8000
412 
413  def __init__(self):
414  self.msg_type = SimpleMessageType.SYS_STAT
415  self.comm_type = CommunicationType.SERVICE_REPLY
416  self.stat_type = 0
417  self.result = 0
418 
419  def make_body(self):
420  return pack('2i', self.stat_type, self.result)
421 
422  def load_body(self, body):
423  self.stat_type, self.result = unpack('2i', body[0:8])
424 
426  "TODO docstring"
427 
428  def __init__(self):
429  self.msg_type = SimpleMessageType.SET_TOOL_OFFSET
430  self.comm_type = CommunicationType.SERVICE_REQUEST
431  self.x = 0
432  self.y = 0
433  self.z = 0
434  self.rz = 0
435  self.rx = 0
436  self.ry = 0
437 
438  def make_body(self):
439  return pack('6f', self.x, self.y, self.z, self.rz, self.ry, self.rx)
440 
441  def load_body(self, body):
442  self.x, self.y, self.z, self.rz, self.ry, self.rx = unpack('6f', body)
443 
445  "TODO docstring"
446 
447  class Result:
448  FAILURE = 0
449  SUCCESS = 1
450 
451  def __init__(self):
452  self.msg_type = SimpleMessageType.SET_TOOL_OFFSET
453  self.comm_type = CommunicationType.SERVICE_REPLY
454  self.result = self.Result.SUCCESS
455 
456  def make_body(self):
457  return pack('i', self.result)
458 
459  def load_body(self, body):
460  self.result = unpack('i', body[0:4])
461 
462 
464  MAX_BUFFER_SIZE = 4096
465 
466  def __init__(self, sock):
467  self._sock = sock
468 
469  def send(self, message):
470  self._send_bytes(message.dump())
471 
472  def recv(self):
473  data = self._recv_message_bytes()
474  #print("{:02x}".format(ord(data[0])))
475  print(":".join("{:02x}".format(ord(c)) for c in data))
476  prefix, msg_type = unpack("2i", data[0:8])
477 
478  msg_class = {
479  SimpleMessageType.JOINT_TRAJ_PT: JointTrajPtMessage,
480  SimpleMessageType.STATUS: StatusMessage,
481  SimpleMessageType.SET_IO: SetIOMessage,
482  SimpleMessageType.EXECUTE_PROGRAM: ExecuteProgramMessage,
483  SimpleMessageType.SET_POSTURE: SetPostureMessage,
484  SimpleMessageType.GET_POSTURE: GetPostureMessage,
485  SimpleMessageType.SYS_STAT: SysStatMessage,
486  SimpleMessageType.SET_TOOL_OFFSET: SetToolOffsetMessage,
487  }.get(msg_type, SimpleMessage)
488 
489  msg = msg_class()
490  msg.load(data)
491 
492  return msg
493 
495  prefix = self._recv_bytes(4)
496  data_size, = unpack("I", prefix)
497  data = self._recv_bytes(data_size)
498 
499  return prefix + data
500 
501  def _recv_bytes(self, num):
502  remain = num
503  data = ""
504 
505  while remain > 0:
506  print("recv: {}".format(remain))
507  buf = self._sock.recv(self.MAX_BUFFER_SIZE if remain > self.MAX_BUFFER_SIZE else remain)
508 
509  buf_size = len(buf)
510 
511  if buf_size == 0: # TODO handling error
512  assert()
513 
514  remain -= buf_size
515  data += buf
516 
517  return data
518 
519  def _send_bytes(self, data):
520 
521  data_size = len(data)
522  remain = data_size
523 
524  while remain > 0:
525  print("send: {}".format(remain))
526  i = data_size - remain
527  send_size = self._sock.send(data[i:])
528 
529  if send_size == 0: # TODO handling error
530  assert()
531 
532  remain -= send_size
def sendBytes(socket, buf)
def recvBytes(socket, num)


fsrobo_r_driver
Author(s): F-ROSROBO
autogenerated on Sun Feb 9 2020 03:58:29