state_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 os
34 import time
35 import math
36 from simple_message import SimpleMessageSocket, JointPositionMessage, IOStateMessage, StatusMessage, TriStates, RobotMode, WrenchMessage
37 from force_sensor import ForceSensor
38 from info_catch_client import InfoCatchClient
39 import traceback
40 
41 HOST, PORT = "0.0.0.0", 11002
42 
43 class StateTCPHandler(SocketServer.BaseRequestHandler):
44  def handle(self):
45  sock = SimpleMessageSocket(self.request)
46  address = self.client_address[0]
47  client = InfoCatchClient()
48  client.connect(
49  [InfoCatchClient.Label.I000,
50  InfoCatchClient.Label.R200,
51  InfoCatchClient.Label.M000,
52  InfoCatchClient.Label.M001,
53  InfoCatchClient.Label.M100,
54  InfoCatchClient.Label.M102,
55  InfoCatchClient.Label.F000,
56  InfoCatchClient.Label.F200,
57  InfoCatchClient.Label.F300,
58  ])
59  self._fs = ForceSensor()
60  try:
61  while True:
62  print('receive from shared memory')
63  info = client.recv()
64  print("send message")
65 
66  msg = self.make_joint_position(info)
67  sock.send(msg)
68 
69  msg = self.make_io_state(info)
70  sock.send(msg)
71 
72  msg = self.make_robot_status(info)
73  sock.send(msg)
74 
75  msg = self.make_wrench(info)
76  if msg:
77  sock.send(msg)
78 
79  except Exception as e:
80  print(e)
81  traceback.print_exc()
82  client.close()
83 
84  def make_joint_position(self, info):
85  msg = JointPositionMessage()
86  msg.joint_data = info[InfoCatchClient.Label.R200] + [0, 0, 0, 0]
87  print(msg.joint_data)
88  return msg
89 
90  def make_io_state(self, info):
91  msg = IOStateMessage()
92  msg.digital = info[InfoCatchClient.Label.M000] + info[InfoCatchClient.Label.M001]
93  # info catch server doesn't support read analog data
94  msg.analog = [0]
95  print(msg.digital)
96  print(msg.analog)
97  return msg
98 
99  def make_robot_status(self, info):
100  hw_stat = BitAdapter(info[InfoCatchClient.Label.M100][0])
101  sw_stat = BitAdapter(info[InfoCatchClient.Label.M102][0])
102 
103  servo_on = hw_stat.as_bool(0)
104  e_stop = hw_stat.as_bool(1)
105  sys_err = sw_stat.as_bool(3)
106  stat_code = sw_stat.subseq(4, 4)
107  err_code = sw_stat.subseq(8, 8)
108  auto_mode = os.path.exists('/tmp/auto_ready')
109 
110  msg = StatusMessage()
111  msg.mode = RobotMode.AUTO if auto_mode else RobotMode.MANUAL
112  msg.e_stopped = TriStates.from_bool(e_stop)
113  msg.drives_powered = TriStates.from_bool(servo_on)
114  msg.motion_possible = TriStates.from_bool(auto_mode and servo_on and not sys_err)
115  msg.in_motion = TriStates.UNKNOWN
116  msg.in_error = TriStates.from_bool(sys_err)
117  print('{} {} {} {} {} {}'.format(auto_mode, servo_on,
118  e_stop, sys_err, stat_code, err_code))
119  if stat_code in [SysStat.NORMAL_ERROR,
120  SysStat.CRITICAL_ERROR,
121  SysStat.USER_NORMAL_ERROR,
122  SysStat.USER_CRITICAL_ERROR]:
123  msg.error_code = stat_code << 8 | err_code
124  else:
125  msg.error_code = 0
126 
127  return msg
128 
129  def make_wrench(self, info):
130  self._fs.read(info)
131  if self._fs.is_valid():
132  msg = WrenchMessage()
133  msg.force = [self._fs.fx, self._fs.fy, self._fs.fz]
134  msg.torque = [self._fs.mx, self._fs.my, self._fs.mz]
135  print('wrench: {} {}'.format(msg.force, msg.torque))
136  return msg
137  else:
138  return None
139 
140 class SysStat:
141  BOOT = 0
142  INIT = 1
143  READY = 2
144  INC = 3
145  TEACH = 4
146  JOG = 5
147  RUN = 6
148  PAUSE = 7
149  NORMAL_ERROR = 10
150  CRITICAL_ERROR = 11
151  USER_NORMAL_ERROR = 12
152  USER_CRITICAL_ERROR = 13
153 
155  def __init__(self, num):
156  self._num = num
157 
158  def as_bool(self, n):
159  return bool((self._num >> n) & 1)
160 
161  def subseq(self, n, bit_len):
162  mask = (1 << (n + bit_len)) - 1
163  return (self._num & mask) >> n
164 
165 class StateServer(SocketServer.ThreadingTCPServer, object):
166  def server_bind(self):
167  self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
168  self.socket.bind(self.server_address)
169 
170 if __name__ == "__main__":
171  server = StateServer((HOST, PORT), StateTCPHandler)
172  server.serve_forever()
def make_robot_status(self, info)
Definition: state_server.py:99
def make_io_state(self, info)
Definition: state_server.py:90
def __init__(self, num)
def make_joint_position(self, info)
Definition: state_server.py:84
def subseq(self, n, bit_len)


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