0


腾讯扣叮虚拟仿真实验室机器人自动导航+陨石样本收集

前言

本文为腾讯coding入门教程,具体为以下四步骤

  • 一、自动导航的思路与操作
  • 二、躲避障碍
  • 三、陨石样本收集
  • 四、退出事件

最终效果如下:


一、自动指向和到达目的地停下

1.了解项目环境

首先进入课程练习,找到中学Python的规则解读后点击大展身手进入

此时右侧右侧会出现背景和规则,主要是以下五个任务

本文主要是完成陨石样本收集和到达南极这两个任务

首先打开代码编辑,这里会初始给我们导入了他们的库,为了能够更加清晰了解这些库的作用可以打开官方网页文档,查看每个代码的含义

官方网页:https://support.qq.com/products/418580/faqs-more/?id=131014

2.自定义朝向并前往坐标

为了方便代码理解,我们自己定义函数go_to(x,y),我们后续若想前往何处,只需要在go_to()里面输入你想去的x坐标和y坐标即可,例如:

此时我们需要在函数里面写入自定义朝向函数,即左侧的“2.朝向(X,Y)”,并将Robot_Max.face_coordinate(X坐标值, Y坐标值)里的“X坐标值”和“Y坐标值”更改为x和y,如下:

  1. from codingtge import Robot_Max, coding
  2. def go_to(x,y):
  3. Robot_Max.face_coordinate(x, y)
  4. go_to(255,635)

此时点击运行,就会发现机器人会自动朝向你所指定的方向旋转,就起到了自定义朝向操作

为了能够安全到达你所指定的坐标,你在此代码后写入移动和到达目的地停下的操作,此时用到移动函数和判断语句

首先为了能够移动更加快速,在函数里加入切换为轮式状态,并以每秒15米的速度前进

速度后的0.1为持续时间,仅仅这样写入这样的代码只会使机器人移动0.1秒,所以为了能够使机器人持续移动,就要将这个移动函数放入循环里

这样写就会使机器人持续朝目的地移动,但是当遇到了新的问题:此时改如何停下?

3.到达目的地并停下

为了能够正常的停下,我的思路是:将机器人的坐标和目的地的坐标进行比对,当机器人坐标和目的地的坐标等于时,停下

在将我的思路进行编写后,得出以下代码:

  1. from codingtge import Robot_Max, coding
  2. def go_to(x,y):
  3. Robot_Max.face_coordinate(x, y)
  4. Robot_Max.state("Wheel")
  5. is_arrive = False
  6. while is_arrive == False:
  7. Robot_Max.move("Wheel", 15, 0.1)
  8. x_current = Robot_Max.get_coordinate_X()
  9. y_current = Robot_Max.get_coordinate_Y()
  10. if abs(x_current - x) <= 2 and abs(y_current - y) <= 2:
  11. is_arrive = True
  12. go_to(255,635)

在上述代码中,我写入了当到达坐标附近时就停下来的操作:开始先在while中放入未到达目的地的信号,然后使机器人持续移动,并每0.1秒记录下此时机器人的坐标,并实时与目的地坐标进行判断。由于实际肯定会有些许误差,所以为了防止一些意外操作,我便使用了以下判断:当当前x坐标与当前y坐标与目的地坐标的偏差值在2内(绝对值),即传入已到达的信号,以停下循环。(个人不太推荐函数,前期进行学习时,发觉并不是很好用,文章内我仅仅分享我自认为最好用的操作,如果有更好用的或其他思想能完成相同的项目也是很好的)

此时运行代码,就会发现机器人就可以实现自动指向和到达目的地停下的操作了!

二、躲避障碍

为了能前往错综复杂的目的地,就需要对前方路况进行一段监测,否则很有可能会撞到掩体而导致机器人动弹不得。所以要在代码内加入监测,例如当开启前方监测时:

当运行时终端会每0.1秒返回最大为100的数字

在正常行驶中,我们不仅要判断前方路段也要进行左右两方的路段,以便我们进行左右转操作时,不会误撞到左右两方的操作,故在此思路上修改代码如下:

  1. from codingtge import Robot_Max, coding
  2. def go_to(x,y):
  3. Robot_Max.face_coordinate(x, y)
  4. Robot_Max.state("Wheel")
  5. Robot_Max.ir_on("front")
  6. Robot_Max.ir_on("left")
  7. Robot_Max.ir_on("right")
  8. is_arrive = False
  9. while is_arrive == False:
  10. front = Robot_Max.get_ir_distance("front")
  11. left = Robot_Max.get_ir_distance("left")
  12. right = Robot_Max.get_ir_distance("right")
  13. if front <= 40:
  14. Robot_Max.stop()
  15. if right > left:
  16. Robot_Max.rotate_angle("Wheel", "Right", 10)
  17. else:
  18. Robot_Max.rotate_angle("Wheel", "Left", 10)
  19. continue
  20. Robot_Max.move("Wheel", 15, 0.1)
  21. x_current = Robot_Max.get_coordinate_X()
  22. y_current = Robot_Max.get_coordinate_Y()
  23. if abs(x_current - x) <= 2 and abs(y_current - y) <= 2:
  24. is_arrive = True
  25. go_to(475,460)

首先检测出前方40米是否有障碍,以提前进行障碍躲避。若有,则再判断左右两方障碍哪方更宽广一些,并往该方向旋转直到前方40米没有障碍时才进行移动操作。

但是执行以上代码操作时,会发现机器人在多次转弯后便遗失了目的地的方向,所以还需要在以上代码中再次添加转向函数,为了防止机器人在转向目的地过于频繁,则需要对代码进行一些限制,

防止出现一些意外。在以上思想上进行编写后得出以下代码:

  1. from codingtge import Robot_Max, coding
  2. def go_to(x,y):
  3. Robot_Max.face_coordinate(x, y)
  4. Robot_Max.state("Wheel")
  5. Robot_Max.ir_on("front")
  6. Robot_Max.ir_on("left")
  7. Robot_Max.ir_on("right")
  8. #限制机器人旋转过多
  9. counter = 0
  10. is_arrive = False
  11. while is_arrive == False:
  12. front = Robot_Max.get_ir_distance("front")
  13. left = Robot_Max.get_ir_distance("left")
  14. right = Robot_Max.get_ir_distance("right")
  15. if front <= 40:
  16. Robot_Max.stop()
  17. if right > left:
  18. Robot_Max.rotate_angle("Wheel", "Right", 10)
  19. else:
  20. Robot_Max.rotate_angle("Wheel", "Left", 10)
  21. continue
  22. #限制机器人在移动时判断过多(每两秒判断一次)
  23. Robot_Max.move("Wheel", 15,2)
  24. #当进行3次循环后(进行三次移动后),机器人更正一次朝向,更正后将counter重新赋值为0
  25. if counter == 3:
  26. Robot_Max.face_coordinate(x, y)
  27. counter = 0
  28. #每次循环将counter进行加1操作
  29. counter += 1
  30. x_current = Robot_Max.get_coordinate_X()
  31. y_current = Robot_Max.get_coordinate_Y()
  32. #由于之前将移动时判断修改为每两秒一次,为了防止在移动过程中出现到达目的地而没停下的场面
  33. #在后方重新加入:当坐标临近目的地时将判断更改为每0.1秒一次
  34. while abs(x_current - x) <= 30 and abs(y_current - y) <= 30:
  35. Robot_Max.move("Wheel", 15,0.1)
  36. if abs(x_current - x) <= 2 and abs(y_current - y) <= 2:
  37. is_arrive = True
  38. #当操作成立,跳出本次循环,由于定义已is_arrive为True,故循环结束
  39. break
  40. #每0.1秒判断一次坐标轴
  41. x_current = Robot_Max.get_coordinate_X()
  42. y_current = Robot_Max.get_coordinate_Y()
  43. go_to(475,460)

为了方便理解我在我新加的代码上均添加了注释

(以上代码修改可能有些过多,建议复制下来与结合注释与之前的代码进行对比分析!)

此时运行代码,可以发现机器人不仅躲避了障碍,也可以正常的到达目的地

三、陨石样本收集

陨石样本收集相较于上述代码,就简单许多了

  1. def catch(x,y):
  2. Robot_Max.equip_arm()
  3. Robot_Max.arm_action(x, y, 0, "catch")
  4. Robot_Max.arm_takein()

首先加载出机械臂再将机械臂伸向你定义的x与y坐标(z坐标填0即可),再执行抓取操作,将陨石抓取后再利用机械臂收取夹持物函数,进行陨石样本收集

总结

将以上函数编写完后,就可以很容易的完成陨石收集和到达南极的任务了!

完整代码如下

  1. from codingtge import Robot_Max, coding
  2. def go_to(x,y):
  3. Robot_Max.face_coordinate(x, y)
  4. Robot_Max.state("Wheel")
  5. Robot_Max.ir_on("front")
  6. Robot_Max.ir_on("left")
  7. Robot_Max.ir_on("right")
  8. counter = 0
  9. is_arrive = False
  10. while is_arrive == False:
  11. front = Robot_Max.get_ir_distance("front")
  12. left = Robot_Max.get_ir_distance("left")
  13. right = Robot_Max.get_ir_distance("right")
  14. if front <= 40:
  15. Robot_Max.stop()
  16. if right > left:
  17. Robot_Max.rotate_angle("Wheel", "Right", 10)
  18. else:
  19. Robot_Max.rotate_angle("Wheel", "Left", 10)
  20. continue
  21. Robot_Max.move("Wheel", 15,2)
  22. if counter == 3:
  23. Robot_Max.face_coordinate(x, y)
  24. counter = 0
  25. counter += 1
  26. x_current = Robot_Max.get_coordinate_X()
  27. y_current = Robot_Max.get_coordinate_Y()
  28. while abs(x_current - x) <= 30 and abs(y_current - y) <= 30:
  29. Robot_Max.move("Wheel", 15,0.1)
  30. if abs(x_current - x) <= 2 and abs(y_current - y) <= 2:
  31. is_arrive = True
  32. break
  33. x_current = Robot_Max.get_coordinate_X()
  34. y_current = Robot_Max.get_coordinate_Y()
  35. def catch(x,y):
  36. Robot_Max.equip_arm()
  37. Robot_Max.arm_action(x, y, 0, "catch")
  38. Robot_Max.arm_takein()
  39. go_to(475,460)
  40. catch(475,460)
  41. go_to(800,370)

以上便是本人的制作过程,如遇问题或者文章有错误可私信或者评论


本文转载自: https://blog.csdn.net/weixin_57037287/article/details/129808276
版权归原作者 暮川OvO 所有, 如有侵权,请联系我们删除。

“腾讯扣叮虚拟仿真实验室机器人自动导航+陨石样本收集”的评论:

还没有评论