hi, I have a troubule with this code, this run but when write in terminal rostopic hz '/PWM' say 'no new messages', and when write rostopic echo /PWM, not display anything. please what can I do ?

asked 2020-01-08 15:51:24 -0600

Juan Carlos gravatar image

updated 2020-01-09 17:44:46 -0600

jayess gravatar image

this is mi code, I use a raspberry pi for this program.

 #!/usr/bin/env python
import rospy
import RPi.GPIO as GPIO
import numpy as np
import time
import serial
from sensor_msgs.msg import JointState

msg = JointState()
valores = []

class RC(object):

        def __init__(self):
                self.pub = rospy.Publisher('/PWM', JointState, queue_size=10)

                GPIO.setwarnings(False)
                GPIO.setmode(GPIO.BCM)
                GPIO.setup(13, GPIO.OUT)
                GPIO.setup(12, GPIO.OUT)

                self.M1 = GPIO.PWM(13, 1000) # PINS de PWM a una frecuencia de 1kHZ
                self.M2 = GPIO.PWM(12, 1000)
                self.M1.start(0) # Inicializa el Pin
                self.M2.start(0)
                self.rate = rospy.Rate(25)

        def stop(self, data):
                modo = data
                print modo

        def rc(self, data1, data2, data3):
                global valores
                modo = data3

                A = data1
                B = data2

                #Ecuaciones para linealizar y aplicar un PWM de 0 a 100
                # A cada motor
                A = (0.00208-A)/0.00113 * 100
                B = (B - 0.00112)/0.00079 * 100

                WR=B
                WL=A

                # saturacion para que los motores no reciban valores >100 y <0.0
                if WR > 99.9:
                        WR = 99.9

                if WR < 0.1:
                        WR = 0.1

                if WL > 99.9:
                        WL = 99.9

                if WL < 0.1:
                        WL = 0.1

                valores.append(WL)
                if len(valores) == 5:
                        valores = valores[-5:]
                        print valores
                        average = np.mean(valores)
                        #print average
                        valores = []

                self.M1.ChangeDutyCycle(WL)     # Motor Izquierdo
                self.M2.ChangeDutyCycle(WR)     # Motor Derecho

                msg.header.stamp = rospy.get_rostime()
                msg.velocity = [WL,WR]

                self.pub.publish(msg)

                #print msg
                self.rate.sleep()

        def control(self, data):
                modo = data
                print modo

if __name__== '__main__':
        while True:
                try:
                        rospy.init_node('remoto',anonymous=True, disable_signals=True)
                        #print "Nodo creado"
                        cv = RC()
                        GPIO.setmode(GPIO.BCM)
                        GPIO.setup(24, GPIO.IN)
                        GPIO.setup(23, GPIO.IN)
                        GPIO.setup(18, GPIO.IN)
                        #print modo

                        # Avanzar (Vmax)
                        GPIO.wait_for_edge(23, GPIO.RISING) # detecta flanco de subida
                        start = time.time()         # guardar el tiempo en ese instante
                        if GPIO.wait_for_edge(23, GPIO.FALLING): #detecta flanco de bajada
                                end = time.time()       # funcion para guardar el tiempo

                        # Girar (W)
                        GPIO.wait_for_edge(18, GPIO.RISING)
                        inicio = time.time()
                        if GPIO.wait_for_edge(18, GPIO.FALLING):
                                fin = time.time()

                        duration = end - start      #seconds to run for loop
                        tiempo = fin - inicio

                        # MODO
                        GPIO.wait_for_edge(24, GPIO.RISING)
                        A = time.time()
                        if GPIO.wait_for_edge(24, GPIO.FALLING):
                                B = time.time()

                        modo = B - A

                        if 0.0021 > modo > 0.0019:
                                cv.stop(modo)

                        if 0.0016 > modo > 0.0014:
                                cv.rc(duration, tiempo, modo)

                        if 0.0010 > modo > 0.0008:
                                cv.control(modo)

                except rospy.ROSInterruptException:
                        GPIO.cleanup()
                        print "closed"
                        pass
edit retag flag offensive close merge delete

Comments

1

Are you sure that you are calling Publish on your publisher? It looks like you only call that function if 0.0016 > modo > 0.0014:

ahendrix gravatar imageahendrix ( 2020-01-08 16:17:14 -0600 )edit

yes, i'm sure, I use for publish self.pub.publish(msg), for this reason see the topic, but when write rostopic echo, this topic not publish anything

Juan Carlos gravatar imageJuan Carlos ( 2020-01-09 11:41:19 -0600 )edit