当前位置:   article > 正文

蓝桥ROS机器人之turtlesim贪吃蛇移植到Win11中_ros 机器人 移植

ros 机器人 移植

直播课遇到点小bug,没有弄好,现在已经ok。

蓝桥ROS机器人之turtlesim贪吃蛇

这里案例只是为了说明,算法不分系统,全平台支持的!


愿意花时间所有linux案例都能移植到win10/11,主要是时间太紧张了……无奈,有时候也没必要移植。

课程中截图如下:

还测试了一个movetogoal

 

坚持写博客,也是鼓励学生写,自己怎么能不写。

如果我做不到如何要求学生做?

要求从我第一次上课就提了,7年过去了,效果很惨淡啊,多惨淡呢?

每学期课程都会提及这个要求,但印象中,真正写的学生不到10人,但是!

但是,但是!

这10人当中,有9人是之前自己就写博客的。 

响应的学生太少了…… 

也论证了如下结果:

www.zhihu.com/question/520747377/answer/2449718139

里面重要的一句话:

我原本一直觉得师生对于课程结果的重要性是对半分的,各50%。

7年工作经历下来,教师作用说有10%都很夸张,当然也没有1%那么低。

一些群聊及主动学习或者交流,多有如下情况:

 


 

 

  1. import rospy
  2. from tanksim.msg import Pose
  3. from tanksim.srv import Spawn
  4. from tanksim.srv import SetPen
  5. from geometry_msgs.msg import Twist
  6. from geometry_msgs.msg import TransformStamped
  7. import random
  8. import math
  9. tank1_pose = Pose()
  10. tanklist = []
  11. lasttank = 1
  12. nexttankIndex = 1
  13. class mySpawner:
  14. def __init__(self, tname):
  15. self.tank_name = tname
  16. self.state = 1
  17. rospy.wait_for_service('/spawn')
  18. try:
  19. client = rospy.ServiceProxy('/spawn', Spawn)
  20. x = random.randint(1, 10)
  21. y = random.randint(1, 10)
  22. theta = random.uniform(1, 3.14)
  23. name = tname
  24. _nm = client(x, y, theta, name)
  25. rospy.loginfo("tank Created [%s] [%f] [%f]", name, x, y)
  26. rospy.Subscriber(self.tank_name + '/pose', Pose, self.tank_poseCallback)
  27. self.pub = rospy.Publisher(self.tank_name + '/cmd_vel', Twist, queue_size=10)
  28. self.tank_to_follow = 1
  29. self.tank_pose = Pose()
  30. rospy.wait_for_service("/" + tname + '/set_pen')
  31. try:
  32. client = rospy.ServiceProxy("/" + tname + '/set_pen', SetPen)
  33. client(0,0,0,0,1)
  34. except rospy.ServiceException as e:
  35. print("Service call failed: %s"%e)
  36. except rospy.ServiceException as e:
  37. print("Service tp spawn a tank failed. %s", e)
  38. def tank_poseCallback(self, data):
  39. self.tank_pose = data
  40. def tank_velocity(self, msg):
  41. self.pub.publish(msg)
  42. def tank1_poseCallback(data):
  43. global tank1_pose
  44. global lasttank
  45. global tanklist
  46. global nexttankIndex
  47. tank1_pose.x = round(data.x, 4)
  48. tank1_pose.y = round(data.y, 4)
  49. tank1_pose.theta = round(data.theta, 4)
  50. for i in range(len(tanklist)):
  51. twist_data = Twist()
  52. diff = math.sqrt(pow((tank1_pose.x - tanklist[i].tank_pose.x) , 2) + pow((tank1_pose.y - tanklist[i].tank_pose.y), 2))
  53. ang = math.atan2(tank1_pose.y - tanklist[i].tank_pose.y, tank1_pose.x - tanklist[i].tank_pose.x) - tanklist[i].tank_pose.theta
  54. if(ang <= -3.14) or (ang > 3.14):
  55. ang = ang / math.pi
  56. if (tanklist[i].state == 1):
  57. if diff < 1.0:
  58. tanklist[i].state = 2
  59. tanklist[i].tank_to_follow = lasttank
  60. lasttank = i + 2
  61. rospy.loginfo("tank Changed [%s] [%f] [%f]", tanklist[i].tank_name, diff, ang)
  62. nexttankIndex += 1
  63. tanklist.append(mySpawner("tank" + str(nexttankIndex)))
  64. else:
  65. parPose = tank1_pose
  66. if(tanklist[i].tank_to_follow != 1):
  67. parPose = tanklist[tanklist[i].tank_to_follow - 2].tank_pose
  68. diff = math.sqrt(pow((parPose.x - tanklist[i].tank_pose.x) , 2) + pow((parPose.y - tanklist[i].tank_pose.y), 2))
  69. goal = math.atan2(parPose.y - tanklist[i].tank_pose.y, parPose.x - tanklist[i].tank_pose.x)
  70. ang = math.atan2(math.sin(goal - tanklist[i].tank_pose.theta), math.cos(goal - tanklist[i].tank_pose.theta))
  71. if(ang <= -3.14) or (ang > 3.14):
  72. ang = ang / (2*math.pi)
  73. if(diff < 0.8):
  74. twist_data.linear.x = 0
  75. twist_data.angular.z = 0
  76. else:
  77. twist_data.linear.x = 2.5 * diff
  78. twist_data.angular.z = 20 * ang
  79. tanklist[i].tank_velocity(twist_data)
  80. tanklist[i].oldAngle = ang
  81. def spawn_tank_fn():
  82. global nexttankIndex
  83. rospy.init_node('snake_tank', anonymous=True)
  84. rospy.Subscriber('/tank1/pose', Pose, tank1_poseCallback)
  85. rospy.wait_for_service("/tank1/set_pen")
  86. try:
  87. client = rospy.ServiceProxy('/tank1/set_pen', SetPen)
  88. client(0,0,0,0,1)
  89. except rospy.ServiceException as e:
  90. print("Service call failed: %s"%e)
  91. nexttankIndex += 1
  92. tanklist.append(mySpawner("tank" + str(nexttankIndex)))
  93. # for i in range(2,10):
  94. # tanklist.append(mySpawner("tank" + str(i)))
  95. rospy.spin()
  96. if __name__ == "__main__":
  97. spawn_tank_fn()

 


 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/804758
推荐阅读
相关标签
  

闽ICP备14008679号