ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

!/usr/bin/env python

from time import sleep
import serial
import rospy
import tf
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Point, Pose, Quaternion, Twist, Vector3
import serial.tools.list_ports as port

ser = None

speed = 0
angle = 0
Drive = 0
Turn = 0


class Kangaroo:
     def __init__(self, debug=False ,ser= None):
          self.debug = debug
          self.source = None
          self.status = True
          self.ser = ser
          self.Drive = None
          self.Turn = None

     def callback(self,data):
          print data
          # speed = translate(data.linear.x,-1,1,-100,100)
          speed = data.linear.x * 100 + speed
          self.Drive = "D,p" + str(int(speed)) +"\r\n"
          # angle = translate(-data.angular.z,-1,1,100,-100)
          angle = -data.angular.z * 100 + angle
          self.Turn = "T,p" + str(int(angle))+"\r\n"



     def listener( self ):
          rospy.init_node('listener', )
          rospy.Subscriber("/cmd_vel", Twist, self.callback)

          print "\nSpeed =", self.Drive
          ser.write("D,home")
          ser.write(self.Drive)

          print "\nAngle =", self.Turn
          ser.write("T,home")
          ser.write(self.Turn)

          # sleep(.1) # Delay for one tenth of a second
          # print ser.readline()


          # sleep(.1) # Delay for one tenth of a second
          # print ser.readline()
          # print ser.readline()
          # ser.write("D,getp")
          #print ser.readline()

          rospy.spin()


 if __name__ == '__main__':
     print "\nInit kangaroo x2....\n"

     print "\nDetecting kangaroo x2....\n"
     portlist = list(port.comports())
     print portlist
     address = ''
     for p in portlist:
          print p
          if ' USB2.0-Serial' in str(p):
               address = str(p).split(" ")
       print "\nAddress found @"
       print address[0]

       ser = serial.Serial(address[0], 9600)  # Establish the connection on a specific port
       print ser.readline()
       ser.write("D,home")
       ser.write("T,home")
       sleep(.1)  # Delay for one tenth of a second
       ser.write("D,p0")
       sleep(.1)  # Delay for one tenth of a second
       ser.write("T,p0")
       sleep(.1)  # Delay for one tenth of a second

       speed = 0
       angle = 0
       kangaroo = Kangaroo(debug=True , ser=ser)
       kangaroo.listener()

!/usr/bin/env python

Hey guys , i am also facing just a problem like that . Callback is the function that i get the cmd_vel .

I need the callback to somehow return the captured values OR callback being able to inherit the 'ser' (Serial)

from time import sleep
import serial
import rospy
import tf
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Point, Pose, Quaternion, Twist, Vector3
import serial.tools.list_ports as port

ser = None

speed = 0
angle = 0
Drive = 0
Turn = 0


class Kangaroo:
     def __init__(self, debug=False ,ser= None):
          self.debug = debug
          self.source = None
          self.status = True
          self.ser = ser
          self.Drive = None
          self.Turn = None

     def callback(self,data):
          print data
          # speed = translate(data.linear.x,-1,1,-100,100)
          speed = data.linear.x * 100 + speed
          self.Drive = "D,p" + str(int(speed)) +"\r\n"
          # angle = translate(-data.angular.z,-1,1,100,-100)
          angle = -data.angular.z * 100 + angle
          self.Turn = "T,p" + str(int(angle))+"\r\n"



     def listener( self ):
          rospy.init_node('listener', )
          rospy.Subscriber("/cmd_vel", Twist, self.callback)

          print "\nSpeed =", self.Drive
          ser.write("D,home")
          ser.write(self.Drive)

          print "\nAngle =", self.Turn
          ser.write("T,home")
          ser.write(self.Turn)

          # sleep(.1) # Delay for one tenth of a second
          # print ser.readline()


          # sleep(.1) # Delay for one tenth of a second
          # print ser.readline()
          # print ser.readline()
          # ser.write("D,getp")
          #print ser.readline()

          rospy.spin()


 if __name__ == '__main__':
     print "\nInit kangaroo x2....\n"

     print "\nDetecting kangaroo x2....\n"
     portlist = list(port.comports())
     print portlist
     address = ''
     for p in portlist:
          print p
          if ' USB2.0-Serial' in str(p):
               address = str(p).split(" ")
       print "\nAddress found @"
       print address[0]

       ser = serial.Serial(address[0], 9600)  # Establish the connection on a specific port
       print ser.readline()
       ser.write("D,home")
       ser.write("T,home")
       sleep(.1)  # Delay for one tenth of a second
       ser.write("D,p0")
       sleep(.1)  # Delay for one tenth of a second
       ser.write("T,p0")
       sleep(.1)  # Delay for one tenth of a second

       speed = 0
       angle = 0
       kangaroo = Kangaroo(debug=True , ser=ser)
       kangaroo.listener()