em_state_phidget.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import rospy
19 from cob_phidgets.msg import AnalogSensor
20 from cob_phidgets.msg import DigitalSensor
21 from cob_msgs.msg import PowerState
22 from cob_msgs.msg import EmergencyStopState
23 
24 class EmState():
25  ST_EM_FREE = 0
26  ST_EM_ACTIVE = 1
27  ST_EM_CONFIRMED = 2
28 
30  def __init__(self):
31  self.last_update = None
32  self.em_caused_by_button = False
33 
34  self.last_rear_em_state = False
35  self.last_front_em_state = False
36 
37  self.em_stop_status = EmState.ST_EM_ACTIVE
38  self.em_msg = EmergencyStopState()
39 
40  self.sub_digital_sensors = rospy.Subscriber("digital_sensors", DigitalSensor, self.phidget_cb)
41  self.pub_em_state = rospy.Publisher('em_stop_state', EmergencyStopState, queue_size=1)
42 
43  def phidget_cb(self, msg):
44  front_em_active = False
45  rear_em_active = False
46  got_message = False
47 
48  for i in range(0, len(msg.uri)):
49  if msg.uri[i] == "em_stop_laser_rear":
50  rear_em_active = not msg.state[i]
51  got_message = True
52  if msg.uri[i] == "em_stop_laser_front":
53  front_em_active = not msg.state[i]
54  got_message = True
55 
56  if got_message:
57  self.last_update = rospy.Time.now()
58 
59  if (front_em_active and rear_em_active) and (not self.last_front_em_state and not self.last_rear_em_state):
60  self.em_msg.emergency_button_stop = True
61  self.em_caused_by_button = True
62  elif (not front_em_active and not rear_em_active) and (self.last_front_em_state and self.last_rear_em_state):
63  self.em_msg.emergency_button_stop = False
64  self.em_caused_by_button = False
65  elif (front_em_active is not rear_em_active) and self.em_caused_by_button :
66  self.em_msg.emergency_button_stop = False
67  self.em_caused_by_button = False
68  self.em_msg.scanner_stop = (front_em_active or rear_em_active)
69  else:
70  self.em_msg.scanner_stop = (front_em_active or rear_em_active);
71 
72  em_signal = self.em_msg.scanner_stop or self.em_msg.emergency_button_stop
73 
74  if self.em_stop_status == EmState.ST_EM_FREE:
75  if em_signal:
76  rospy.logdebug("Emergency stop was issued")
77  self.em_stop_status = EmergencyStopState.EMSTOP
78 
79  elif self.em_stop_status == EmState.ST_EM_ACTIVE:
80  if not em_signal:
81  rospy.logdebug("Emergency stop was confirmed")
82  self.em_stop_status = EmergencyStopState.EMCONFIRMED
83  elif self.em_stop_status == EmState.ST_EM_CONFIRMED:
84  if em_signal:
85  rospy.logdebug("Emergency stop issued")
86  self.em_stop_status = EmergencyStopState.EMSTOP
87  else:
88  rospy.logdebug("Emergency stop released")
89  self.em_stop_status = EmergencyStopState.EMFREE
90 
91  self.em_msg.emergency_state = self.em_stop_status
92 
93  self.last_rear_em_state = rear_em_active
94  self.last_front_em_state = front_em_active
95 
96  def publish(self):
97  if self.last_update is not None:
98  self.pub_em_state.publish(self.em_msg)
99 
100 
101 if __name__ == "__main__":
102  rospy.init_node("em_state_phidget")
103  emsp = EMStatePhidget()
104  rospy.loginfo("em_state_phidget running")
105  rate = rospy.Rate(10)
106  while not rospy.is_shutdown():
107  emsp.publish()
108  rate.sleep()


cob_phidget_em_state
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:44