anandvgeorge

gpio_test.py

May 6th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. import roslib
  4. import sys
  5. import RPi.GPIO as GPIO
  6. roslib.load_manifest('camera')
  7. from camera.msg import ball
  8. from std_msgs.msg import Int8
  9.  
  10. # DIFF_DRIVE CONTROL
  11.  
  12. EN_LEFT = 18   # EN_LEFT   : BCM18 : 12
  13. IN1_LEFT = 14  # IN1_LEFT  : BCM14 : 8
  14. IN2_LEFT = 15  # IN2_LEFT  : BCM15 : 10
  15.  
  16. EN_RIGHT = 13  # EN_RIGHT  : BCM13 : 33
  17. IN1_RIGHT = 5  # IN1_RIGHT : BCM5  : 29
  18. IN2_RIGHT = 6  # IN2_RIGHT : BCM6  : 31
  19.  
  20. MOVE = 1 #moving -- initial state
  21.  
  22. def applyVelocity(LEFT, RIGHT):
  23.   GPIO.output(IN1_LEFT, 1)
  24.   pwm_LEFT.start(LEFT)
  25.  
  26.   GPIO.output(IN2_RIGHT, 1)
  27.   pwm_RIGHT.start(RIGHT)
  28.  
  29.   print("\nvl:%f\tvr:%f" %(LEFT, RIGHT))
  30.  
  31. def move(data):
  32.   #print "got STOP packet"
  33.   global MOVE
  34.   #print data
  35.   MOVE = str(data)
  36.   #MOVE = int(MOVE)
  37.   #print(type(MOVE))
  38.   #print("\nmove " + MOVE)
  39.   #print MOVE
  40.   #callback(50)
  41.  
  42. def callback(data):
  43.   global MOVE
  44.   #print "callback MOVE value:"
  45.   #print MOVE
  46.   if MOVE == 1 or MOVE == "data: 1":
  47.       # change reference to (320, 240)
  48.       data.x -= 320
  49.       data.y -= 240
  50.       #print("\nX:%d\tY:%d\tRadius:%d" % (data.x, data.y, data.radius))
  51.  
  52.       x = data.x
  53.       vc = 30
  54.       x = x * 30 / 320 # x in range(-50, 50)
  55.       vl = vc + x
  56.       vr = vc - x
  57.  
  58.       # print("\nX:%f\tvl:%f\tvr:%f" %(x, vl, vr))
  59.       applyVelocity(vl, vr)
  60.    
  61.   elif MOVE == 0 or MOVE == "data: 0":
  62.       #print "\napplying 0 velocity"
  63.       applyVelocity(0, 0)
  64.   else:
  65.       print "WTF Happening!?"
  66.       applyVelocity(70, 5)
  67.  
  68. def listener():
  69.     rospy.init_node('listener', anonymous=True)
  70.     rospy.Subscriber("ball_pos", ball, callback)
  71.     rospy.Subscriber("stop", Int8, move)
  72.     print("\nokay")
  73.     # spin() simply keeps python from exiting until this node is stopped
  74.     rospy.spin()
  75.  
  76. if __name__ == '__main__':
  77.     # Initialize to use BCM numbering.
  78.     GPIO.setmode(GPIO.BCM)
  79.  
  80.     # We want pin 18 to be an output pin, and it should start out with a low value.
  81.     GPIO.setup(IN1_LEFT, GPIO.OUT, initial=GPIO.LOW)
  82.     GPIO.setup(IN2_LEFT, GPIO.OUT, initial=GPIO.LOW)
  83.     GPIO.setup(EN_LEFT, GPIO.OUT, initial=GPIO.LOW)
  84.     pwm_LEFT = GPIO.PWM(EN_LEFT, 50) # frequency
  85.  
  86.     GPIO.setup(IN1_RIGHT, GPIO.OUT, initial=GPIO.LOW)
  87.     GPIO.setup(IN2_RIGHT, GPIO.OUT, initial=GPIO.LOW)
  88.     GPIO.setup(EN_RIGHT, GPIO.OUT, initial=GPIO.LOW)
  89.     pwm_RIGHT = GPIO.PWM(EN_RIGHT, 50) # frequency
  90.          
  91.     listener()
Add Comment
Please, Sign In to add comment