io_interface.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # FSRobo-R Package BSDL
4 # ---------
5 # Copyright (C) 2019 FUJISOFT. All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification,
8 # are permitted provided that the following conditions are met:
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation and/or
13 # other materials provided with the distribution.
14 # 3. Neither the name of the copyright holder nor the names of its contributors
15 # may be used to endorse or promote products derived from this software without
16 # specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 # --------
29 
30 import sys
31 import rospy
32 from fsrobo_r_msgs.srv import *
33 from fsrobo_r_msgs.msg import *
34 from threading import Event
35 
37  SET_DIGITAL_OUT = 1
38  SET_ADC_MODE = 2
39 
40 class IOState:
41  NO_CHANGE = -1
42  ON = 1
43  OFF = 0
44 
46  digital_states = {}
47  analog_states = {}
48 
49  def __init__(self, robot_name=None, init_node=True):
50  if robot_name == '':
51  self.ns = '/'
52  elif robot_name:
53  self.ns = '/' + robot_name + '/'
54  else:
55  self.ns = ''
56 
57  self._init_node = init_node
58  self.init_setter()
59  self.init_getter()
60 
61  def get_digital_val(self, addr):
62  return self.digital_states.get(addr, False)
63 
64  def get_digital_vals(self):
65  return self.digital_states
66 
67  def get_analog_val(self, addr):
68  return self.analog_states.get(addr, 0)
69 
70  def wait_digital_val(self, addr, val, time_out=None):
71  ev = Event()
72 
73  if isinstance(val, list):
74  val_list = val
75  else:
76  val_list = [val]
77 
78  def checker():
79  addr_list = range(addr, addr + len(val_list))
80 
81  if all(map(lambda a, v: v not in [0, 1] or self.get_digital_val(a) == v, addr_list, val_list)):
82  ev.set()
83 
84  self.digital_val_hooks.append(checker)
85  result = ev.wait(time_out)
86  self.digital_val_hooks.remove(checker)
87 
88  return result
89 
90  def set_digital_val(self, addr, *val):
91  try:
92  self.set_io(IOFunctionType.SET_DIGITAL_OUT, addr, list(val))
93  except rospy.ServiceException, e:
94  print "Service call failed: %s" % e
95 
96  def set_analog_mode(self, ch, mode):
97  try:
98  self.set_io(IOFunctionType.SET_ADC_MODE, ch, [mode])
99  except rospy.ServiceException, e:
100  print "Service call failed: %s" % e
101 
102  def callback(self, data):
103  for x in data.digital_in_states:
104  self.digital_states[x.addr] = x.state
105 
106  for x in data.digital_out_states:
107  self.digital_states[x.addr] = x.state
108 
109  for x in data.analog_in_states:
110  self.analog_states[x.ch] = x.state
111 
112  for hook in self.digital_val_hooks:
113  hook()
114 
115  def init_getter(self):
117  if self._init_node:
118  rospy.init_node('io_interface', anonymous = True)
119  rospy.Subscriber(self.ns + 'io_states', IOStates, self.callback)
120  rospy.wait_for_message(self.ns + 'io_states', IOStates, 5)
121 
122  def init_setter(self):
123  rospy.loginfo(self.ns)
124  rospy.wait_for_service(self.ns + 'set_io')
125  self.set_io = rospy.ServiceProxy(self.ns + 'set_io', SetIO)
126 
127 if __name__ == '__main__':
128  io = IOInterface()
129  io.set_analog_mode(0,1)
130  io.set_digital_val(16,1)
131  io.set_digital_val(17,0)
132  print(io.wait_digital_val(16, [1, 0], time_out=5))
133  print io.digital_states
134  print io.get_digital_val(0)
135  print io.get_digital_val(16)
136  print io.get_digital_val(17)
137 
def wait_digital_val(self, addr, val, time_out=None)
Definition: io_interface.py:70
def __init__(self, robot_name=None, init_node=True)
Definition: io_interface.py:49


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