forked from Nthn9211/Assistive-Technology-Capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irsensor.py
83 lines (65 loc) · 2.32 KB
/
irsensor.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import RPi.GPIO as GPIO # Import the GPIO library.
import time # Import time library
import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Twist
GPIO.setmode(GPIO.BOARD) # Set Pi to use pin number when referencing GPIO pins.
GPIO.setup(16, GPIO.IN) # Set GPIO pin 16 to input
GPIO.setup(11, GPIO.IN) # Set GPIO pin 14 to input
GPIO.setup(13, GPIO.IN) # gpio 15 as input
GPIO.setup(18, GPIO.OUT) # Set GPIO pin 18 to output mode.
GPIO.setup(15, GPIO.OUT)
bluePin = GPIO.PWM(18, 100) # Initialize PWM on pwmPin 100Hz frequency
orangePin = GPIO.PWM(15, 100) # Initialize PWM on pwmPin 100Hz frequency
bluedc=80 # set blue dc variable to 0 for 0%
orangedc=80
bluePin.start(bluedc)
orangePin.start(orangedc)
bluePin.ChangeDutyCycle(bluedc)
orangePin.ChangeDutyCycle(orangedc)
cmd_vel = Twist()
def talker():
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
cmd_vel.linear.x = 0.5
while not rospy.is_shutdown():
irsensor()
pub.publish(cmd_vel)
rate.sleep()
bluePin.stop() # stop PWM
orangePin.stop() # stop PWM
GPIO.cleanup() # resets GPIO ports used back to input modei to use pin number when referencing GPIO pins.
def irsensor():
# time.sleep(0.4)
if(GPIO.input(16) == True):
print("obstacle detected")
cmd_vel.linear.x = 0
orangedc = 76
orangePin.ChangeDutyCycle(orangedc)
bluedc = 76
bluePin.ChangeDutyCycle(bluedc)
elif (GPIO.input(11) == True):
print("obstacle detected")
cmd_vel.linear.x = 0
orangedc = 76
orangePin.ChangeDutyCycle(orangedc)
bluedc = 76
bluePin.ChangeDutyCycle(bluedc)
elif (GPIO.input(13) == True):
print("obstacle detected")
cmd_vel.linear.x = 0
orangedc = 76
orangePin.ChangeDutyCycle(orangedc)
bluedc = 76
bluePin.ChangeDutyCycle(bluedc)
else:
print("no obstacle")
cmd_vel.linear.x = 0.5
orangedc = 96.3
orangePin.ChangeDutyCycle(orangedc)
bluedc = 100
bluePin.ChangeDutyCycle(bluedc)
if __name__ == '__main__':
talker()