motion_server.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 import SocketServer
32 import socket
33 import threading
34 import Queue
35 from simple_message import SimpleMessageSocket, \
36  SimpleMessageType, JointTrajPtReplyMessage, ReplyCode, \
37  SpecialSequence, ExecuteProgramReplyMessage
38 from robot_controller import RobotController
39 import math
40 
41 HOST, PORT = "0.0.0.0", 11000
42 
43 class MotionThread(threading.Thread):
44  def __init__(self, ctl):
45  super(MotionThread, self).__init__()
46  self._ctl = ctl
47  self._q_move = Queue.Queue()
48  self._q_abort = Queue.Queue()
49  self.daemon = True
50 
51  def run(self):
52  while True:
53  try:
54  self._q_abort.get(True, 0.001)
55  self._ctl.abort()
56  with self._q_move.mutex:
57  self._q_move.queue.clear()
58  except Queue.Empty:
59  pass
60  used_buffer_size = self._ctl.sys_stat(5)
61  if used_buffer_size < 4:
62  try:
63  joints, speed = self._q_move.get(False)
64  self._ctl.move(joints, speed)
65  except Queue.Empty:
66  pass
67 
68  def move(self, joints, speed):
69  self._q_move.put((joints, speed))
70 
71  def abort(self):
72  self._q_abort.put(True)
73 
74 class MotionTCPHandler(SocketServer.BaseRequestHandler):
75  def handle(self):
76  #client = self.request
77  sock = SimpleMessageSocket(self.request)
78  address = self.client_address[0]
79  print('connected with {}'.format(address))
80  ctl = RobotController()
81  motion_thread = MotionThread(ctl)
82  motion_thread.start()
83 
84  msg_handlers = {
85  SimpleMessageType.JOINT_TRAJ_PT: self.on_joint_traj_pt,
86  SimpleMessageType.EXECUTE_PROGRAM: self.on_execute_program
87  }
88 
89  try:
90  while True:
91  recv_msg = sock.recv()
92 
93  msg_handler = msg_handlers.get(
94  recv_msg.msg_type, self.on_unkown_message)
95 
96  reply_msg = msg_handler(ctl, recv_msg, motion_thread)
97  sock.send(reply_msg)
98  finally:
99  ctl.close()
100 
101  def on_joint_traj_pt(self, ctl, recv_msg, motion_thread):
102  joint_deg = map(math.degrees, recv_msg.joint_data[:6])
103  #print(recv_msg.sequence)
104  #print(recv_msg.joint_data)
105 
106  if recv_msg.sequence == SpecialSequence.STOP_TRAJECTORY:
107  motion_thread.abort()
108  print("abort move command")
109  else:
110  speed = recv_msg.velocity * 100
111  if speed < 0.1:
112  speed = 0.1
113  motion_thread.move(joint_deg, speed)
114 
115  reply_msg = JointTrajPtReplyMessage()
116  reply_msg.reply_code = ReplyCode.SUCCESS
117 
118  return reply_msg
119 
120  def on_execute_program(self, ctl, recv_msg, motion_thread):
121  print(recv_msg.name)
122  print(recv_msg.param)
123  print("received: Execute program message")
124 
125  result = ctl.exec_program(recv_msg.name, recv_msg.param)
126 
127  reply_msg = ExecuteProgramReplyMessage()
128  reply_msg.reply_code = ReplyCode.SUCCESS
129  reply_msg.result = reply_msg.Result.SUCCESS if result else reply_msg.Result.FAILURE
130 
131  return reply_msg
132 
133  def on_unkown_message(self, ctl, recv_msg, motion_thread):
134  raise NotImplementedError(
135  "Unknown msg_type: {}".format(recv_msg.msg_type))
136 
137 
138 class MotionServer(SocketServer.ForkingTCPServer, object):
139  def server_bind(self):
140  self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
141  self.socket.bind(self.server_address)
142 
143 
144 if __name__ == "__main__":
145  server = MotionServer((HOST, PORT), MotionTCPHandler)
146  server.serve_forever()
def on_unkown_message(self, ctl, recv_msg, motion_thread)
def on_joint_traj_pt(self, ctl, recv_msg, motion_thread)
def move(self, joints, speed)
def on_execute_program(self, ctl, recv_msg, motion_thread)


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