当前位置:   article > 正文

Webots控制两轮差速机器人从一个点到另一个点_webots小车

webots小车

新建一个控制器,命名为get_to_goal.py

Webots运行视频如下:

Webots编写控制程序使二轮差速小车导航至目标点

控制器代码如下

  1. from controller import Supervisor
  2. import math
  3. import random
  4. robot = Supervisor()
  5. # get the time step of the current world.
  6. timestep = int(robot.getBasicTimeStep())
  7. #获取电机
  8. motor_right = robot.getDevice("motorRight")
  9. motor_left = robot.getDevice("motorLeft")
  10. #设置电机模式与电机初始速度
  11. motor_right.setPosition(float("inf"))
  12. motor_right.setVelocity(0.0)
  13. motor_left.setPosition(float("inf"))
  14. motor_left.setVelocity(0.0)
  15. name = robot.getFromDef("car")
  16. # 格式化x和z,保留小数点后四位
  17. # robot_x = float("{:.4f}".format(x))
  18. # robot_z = float("{:.4f}".format(z))
  19. # print("X坐标:", robot_x)
  20. # print("Z坐标:", robot_z)
  21. # 假设机器人当前在原点且朝向与y轴负方向一致
  22. CURRENT_ANGLE = -math.pi / 2
  23. # 定义移动速度和角速度
  24. LINEAR_SPEED = 3.14 # 线性速度,单位:米/
  25. def get_to_goal(name, target_x, target_z, CURRENT_ANGLE):
  26. robot_x, robot_z = car_position(name)
  27. # 目标点与机器人之间的相对角度
  28. # math.atan2返回值的范围是从 -π 到 π(不包括π),表示的是 (x, y) 点与原点的连线与正x轴之间的夹角(以弧度为单位)
  29. target_angle = math.atan2(target_z - robot_z, target_x - robot_x)
  30. print("目标点与机器人之间的相对角度:", target_angle)
  31. angle_difference1 = target_angle - CURRENT_ANGLE
  32. # 到达目标点所需的角度差, 未考虑angle_difference1=NaN
  33. if angle_difference1 > math.pi:
  34. angle_difference = angle_difference1 - 2 * math.pi
  35. print("到达目标点所需的角度差:", angle_difference)
  36. else:
  37. angle_difference = angle_difference1
  38. print("到达目标点所需的角度差:", angle_difference)
  39. rotate_to_target_angle(name, -angle_difference) # 仿真方向与正常方向相反
  40. move_to_target(name, target_x, target_z)
  41. def car_angle(name):
  42. rotation = name.getField("rotation")
  43. WorldRot = rotation.getSFRotation()
  44. qx, qy, qz, qw = WorldRot
  45. # theta = 2 * math.acos(qw)
  46. return qw
  47. def car_position(name):
  48. trans = name.getField("translation")
  49. WorldCoord = trans.getSFVec3f()
  50. x, _, z = WorldCoord
  51. return x, z
  52. # 首先旋转到目标角度
  53. def rotate_to_target_angle(name, target_angle):
  54. # 对应仿真左转
  55. if target_angle - car_angle(name) > 0:
  56. motor_left.setVelocity(LINEAR_SPEED/8)
  57. motor_right.setVelocity(-LINEAR_SPEED/8)
  58. while robot.step(timestep) != -1:
  59. if car_angle(name) >= target_angle:
  60. motor_left.setVelocity(0.)
  61. motor_right.setVelocity(0.)
  62. return
  63. # 对应仿真右转 car_angle为负
  64. if target_angle - car_angle(name) < 0:
  65. motor_left.setVelocity(-LINEAR_SPEED/8)
  66. motor_right.setVelocity(LINEAR_SPEED/8)
  67. while robot.step(timestep) != -1:
  68. # print("car_angle" + str(car_angle(name)))
  69. if car_angle(name) <= target_angle:
  70. print("car_angle" + str(car_angle(name)))
  71. print("target_angle" + str(target_angle))
  72. motor_left.setVelocity(0.)
  73. motor_right.setVelocity(0.)
  74. return
  75. # 旋转到目标角度后,直线移动到目标点
  76. def move_to_target(name, TARGET_X, TARGET_Y):
  77. left_speed = LINEAR_SPEED
  78. right_speed = LINEAR_SPEED
  79. # 负号对应仿真前进直线移动
  80. motor_left.setVelocity(-left_speed)
  81. motor_right.setVelocity(-right_speed)
  82. while robot.step(timestep) != -1:
  83. x, z = car_position(name)
  84. dis = math.sqrt((TARGET_X - x) ** 2 + (TARGET_Y - z) ** 2)
  85. # print("dis:", dis)
  86. if dis <= 0.01:
  87. print("x:" + str(x))
  88. print("z:" + str(z))
  89. motor_left.setVelocity(0)
  90. motor_right.setVelocity(0)
  91. return
  92. while True:
  93. x = random.uniform(0, 0.4)
  94. y = random.uniform(0, 0.4)
  95. print("x:" + str(x))
  96. print("y:" + str(y))
  97. get_to_goal(name,x, y, CURRENT_ANGLE)

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

闽ICP备14008679号