-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKill_switch_node_P1_V2.py
69 lines (58 loc) · 2.11 KB
/
Kill_switch_node_P1_V2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
#import the dependencies
import roslib;
import rospy
#import the kobuki messages
from kobuki_msgs.msg import ButtonEvent
from kobuki_msgs.msg import MotorPower
from kobuki_msgs.msg import Led
#write the callback functions for each of the messages
class Kill_switchP1:
def __init__(self):
#constructor
#initialize the subscribers
#initialize the publishers
self.button_sub = rospy.Subscriber("mobile_base/events/button",
ButtonEvent,self.ButtonEventCallback,queue_size = 1)
self.motor_pub = rospy.Publisher("/mobile_base/commands/motor_power",
MotorPower)
self.led_pub = rospy.Publisher("/mobile_base/commands/led2",Led)
#initialize the hidden variables
self.state = "initilized"
self.state_count = 0
self.led_pub.publish(1)
def ButtonEventCallback(self, data):
if ( data.state == ButtonEvent.RELEASED ):
self.state = "released"
else:
self.state = "pressed"
if ( data.button == ButtonEvent.Button0 ) :
self.button = "B1"
else:
self.button = "B2"
def safety_first(self):
while not rospy.is_shutdown():
if self.state == 'pressed':
rospy.loginfo("Button Press Detected")
if self.state_count == 0:
#Red LED and motor off
self.led_pub.publish(3)
self.motor_pub.publish(0)
self.state_count = 1
else:
#Green LED and Motor on
self.led_pub.publish(1)
self.motor_pub.publish(1)
self.state_count = 0
else:
pass
####################################################################
def main(self):
KsP1V2 = Kill_switchP1()
rospy.init_node("Kill_switchP1V2") #initializes the node
try:
KsP1V2.safety_first()
except:rospy.ROSInterruptException:
rospy.loginfo("exception")
if __name__ == '__main__':
main()