-
Notifications
You must be signed in to change notification settings - Fork 0
/
MOTORCONTROL
62 lines (49 loc) · 1.33 KB
/
MOTORCONTROL
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
#PUBLISHER
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from random import randint
def random_number_publisher():
rospy.init_node('random_number')
pub=rospy.Publisher('pub', Int32, queue_size=10)
rate= rospy.Rate(2)
while not rospy.is_shutdown():
random_msg=randint(0,180)
rospy.loginfo(random_msg)
pub.publish(random_msg)
rate.sleep()
if __name__=='__main__':
try:
random_number_publisher()
except rospy.ROSInterruptException:
pass
#SUBSCRIBER
#!/usr/bin/env python
import rospy
import RPi.GPIO as GPIO
from std_msgs.msg import *
servo_pin = 21
#Ajuste estes valores para obter o intervalo completo do movimento do servo
deg_0_pulse = 0.5
deg_180_pulse = 2.5
f = 50.0
# Faca alguns calculos dos parametros da largura do pulso
period = 1000/f
k = 100/period
deg_0_duty = deg_0_pulse*k
pulse_range = deg_180_pulse - deg_0_pulse
duty_range = pulse_range * k
#Iniciar o pino gpio
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin,GPIO.OUT)
pwm = GPIO.PWM(servo_pin,f)
pwm.start(0)
def callback(data):
duty = deg_0_duty + (data.data/180.0)* duty_range
pwm.ChangeDutyCycle(duty)
def random_subscriber():
rospy.init_node('sub')
rospy.Subscriber('pub',Int32, callback)
rospy.spin()
if __name__=='__main__':
random_subscriber()