robot_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 from simple_message import SimpleMessageSocket, \
34  SimpleMessageType, JointTrajPtReplyMessage, ReplyCode, SpecialSequence, \
35  SetIOReplyMessage, IOFunctionType, \
36  SetPostureReplyMessage, GetPostureReplyMessage, \
37  SysStatReplyMessage, \
38  SetToolOffsetReplyMessage
39 from robot_controller import RobotController
40 
41 HOST, PORT = "0.0.0.0", 11003
42 
43 class RobotTCPHandler(SocketServer.BaseRequestHandler):
44  def handle(self):
45  #client = self.request
46  sock = SimpleMessageSocket(self.request)
47  address = self.client_address[0]
48  print('connected from {}'.format(address))
49  ctl = RobotController()
50 
51  msg_handlers = {
52  SimpleMessageType.SET_IO: self.on_set_io,
53  SimpleMessageType.SET_POSTURE: self.on_set_posture,
54  SimpleMessageType.GET_POSTURE: self.on_get_posture,
55  SimpleMessageType.SYS_STAT: self.on_sys_stat,
56  SimpleMessageType.SET_TOOL_OFFSET: self.on_set_tool_offset
57  }
58 
59  try:
60  while True:
61  recv_msg = sock.recv()
62 
63  msg_handler = msg_handlers.get(
64  recv_msg.msg_type, self.on_unkown_message)
65 
66  reply_msg = msg_handler(ctl, recv_msg)
67  sock.send(reply_msg)
68  finally:
69  ctl.close()
70 
71  def on_set_io(self, ctl, recv_msg):
72  print(recv_msg.fun)
73  print(recv_msg.address)
74  print(recv_msg.data_size)
75  print(recv_msg.data)
76 
77  if recv_msg.fun == IOFunctionType.SET_DIGITAL_OUT:
78  result = ctl.set_dio(
79  recv_msg.address, recv_msg.data[0:recv_msg.data_size])
80  elif recv_msg.fun == IOFunctionType.SET_ADC_MODE:
81  result = ctl.set_adc_mode(recv_msg.address, recv_msg.data[0])
82 
83  reply_msg = SetIOReplyMessage()
84  reply_msg.reply_code = ReplyCode.SUCCESS if result else ReplyCode.FAILURE
85  reply_msg.result = reply_msg.Result.SUCCESS if result else reply_msg.Result.FAILURE
86  print("received: I/O message")
87 
88  return reply_msg
89 
90  def on_set_posture(self, ctl, recv_msg):
91  print(recv_msg.posture)
92  print("received: Set posture message")
93 
94  result = ctl.set_posture(recv_msg.posture)
95 
96  reply_msg = SetPostureReplyMessage()
97  reply_msg.reply_code = ReplyCode.SUCCESS if result else ReplyCode.FAILURE
98 
99  return reply_msg
100 
101  def on_get_posture(self, ctl, recv_msg):
102  print("received: Get posture message")
103 
104  posture = ctl.get_posture()
105 
106  reply_msg = GetPostureReplyMessage()
107  if posture == None:
108  reply_msg.reply_code = ReplyCode.FAILURE
109  else:
110  reply_msg.reply_code = ReplyCode.SUCCESS
111  reply_msg.posture = posture
112 
113  return reply_msg
114 
115  def on_sys_stat(self, ctl, recv_msg):
116  print(recv_msg.stat_type)
117 
118  result = ctl.sys_stat(recv_msg.stat_type)
119 
120  reply_msg = SysStatReplyMessage()
121  reply_msg.reply_code = ReplyCode.SUCCESS if result else ReplyCode.FAILURE
122  print("result: {}".format(result))
123  reply_msg.result = result
124  print("received: sys_stat message")
125 
126  return reply_msg
127 
128  def on_set_tool_offset(self, ctl, recv_msg):
129  print("received: Set tool offset message")
130  result = ctl.set_tool_offset(recv_msg.x, recv_msg.y, recv_msg.z,
131  recv_msg.rz, recv_msg.ry, recv_msg.rx)
132  print("result: {}".format(result))
133 
134  reply_msg = SetToolOffsetReplyMessage()
135  reply_msg.reply_code = SetToolOffsetReplyMessage.Result.SUCCESS if result else SetToolOffsetReplyMessage.Result.FAILURE
136 
137  return reply_msg
138 
139  def on_unkown_message(self, ctl, recv_msg):
140  raise NotImplementedError(
141  "Unknown msg_type: {}".format(recv_msg.msg_type))
142 
143 
144 class RobotServer(SocketServer.ForkingTCPServer, object):
145  def server_bind(self):
146  self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
147  self.socket.bind(self.server_address)
148 
149 
150 if __name__ == "__main__":
151  server = RobotServer((HOST, PORT), RobotTCPHandler)
152  server.serve_forever()
def on_set_tool_offset(self, ctl, recv_msg)
def on_set_posture(self, ctl, recv_msg)
Definition: robot_server.py:90
def on_set_io(self, ctl, recv_msg)
Definition: robot_server.py:71
def on_unkown_message(self, ctl, recv_msg)
def on_get_posture(self, ctl, recv_msg)
def on_sys_stat(self, ctl, recv_msg)


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