-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance_sub_1.py
84 lines (70 loc) · 1.89 KB
/
distance_sub_1.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
84
#! /usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
import rospy
from std_msgs.msg import Int32
DIR = 12
STEP =5
CW = 1
CCW = 0
EN=7
#M0=8
#M1=11
#M2=25
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(EN, GPIO.OUT)
#GPIO.setup(M0, GPIO.OUT)
#GPIO.setup(M1, GPIO.OUT)
#GPIO.setup(M2, GPIO.OUT)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
delay=0.0005
#GPIO.output(M0, GPIO.LOW)
#GPIO.output(M1, GPIO.LOW)
#GPIO.output(M2, GPIO.LOW)
GPIO.output(EN, GPIO.LOW)
def walk():
rospy.Subscriber('motor_distance',Int32,callback)
rospy.spin()
def callback(distance):
rospy.loginfo("starting motor..")
angle=(360*distance.data)/4.71
if angle > 0: ##FOR CLOCKWISE DIRECTION
rospy.loginfo("running motor %d cm forward",distance.data)
step_count=(angle)/1.8
rospy.loginfo(round(step_count))
step_count=round(step_count)
rospy.loginfo(int(step_count))
for x in range(int(step_count)):
GPIO.output(EN, GPIO.LOW)
GPIO.output(DIR, GPIO.HIGH)
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
GPIO.output(EN,GPIO.HIGH)
#sleep(0.005)
elif angle < 0: ##FOR ANTICLOCKWISE DIRECTION
angle=(angle)*(-1)
rospy.loginfo("running motor %d cm backward",distance.data)
a_step_count=(angle)/1.8
rospy.loginfo(int(a_step_count))
a_step_count=round(a_step_count)
a_step_count=int(a_step_count)
#a_step_count=a_step_count*2
rospy.loginfo(int(a_step_count))
for x in range(int(a_step_count)):
GPIO.output(EN, GPIO.LOW)
GPIO.output(DIR, GPIO.LOW)
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
GPIO.output(EN,GPIO.HIGH)
#sleep(0.005)
GPIO.output(EN,GPIO.HIGH)
if __name__=='__main__':
rospy.init_node('run',anonymous=True)
walk()
GPIO.cleanup()