|
楼主 |
发表于 2021-8-4 10:47:13
|
显示全部楼层
- #!/usr/bin/python3
- #coding=utf-8
- import time
- import rospy
- from sensor_msgs.msg import Joy
- from geometry_msgs.msg import Twist
- #from can_msgs.msg import Frame
- class Joy360:
- def __init__(self):
- self.__line_x = 0
- self.__angle_z = 0
- self.__pub_cmd_vel = None #:
- self.__pub_can_vel = None
- self.__start = False
-
- pass
- def joy_callback(self,data:Joy):
- if data.buttons[10] == 1:
- self.__start = not self.__start
-
- # send_data = Frame()
- # send_data.dlc = 8
- # send_data.id = 2
- # send_data.data = [0x00, 0xDA, 0x00, 0x10, 0x00, 0x00, 0x00, 0x1F]
- # if not self.__start:
- # send_data.data[7] = 0x0F
- # self.__pub_can_vel.publish(send_data)
- # send_data.id = 3
- # self.__pub_can_vel.publish(send_data)
-
- self.__line_x = data.axes[1]*0.3
- self.__angle_z = data.axes[3]*0.3
-
- def start(self):
- print("start init this node")
- rospy.init_node("joy")
- rospy.loginfo("init node ok!!")
-
- self.__pub_cmd_vel = rospy.Publisher("/cmd_vel",Twist,queue_size=100)
- # self.__pub_can_vel = rospy.Publisher("/sent_messages",Frame,queue_size=100)
- rospy.Subscriber("joy",Joy,self.joy_callback)
-
- try:
- while not rospy.is_shutdown():
- time.sleep(0.1)
- cmd= Twist()
- cmd.linear.x=self.__line_x
- cmd.anguar.z=self.__angle_z
- if self.__start:
-
- self.__pub_cmd_vel.publish(cmd)
- except KeyboardInterrupt as e:
- print("quit by user " + str(e))
- if __name__=="__main__":
- joy_ins = Joy360()
- joy_ins.start()
复制代码
我的手柄代码如上面,我用手柄控制小乌龟是可以实现的,但是控制机器人就不可以了。 |
|