资讯专栏INFORMATION COLUMN

扫地机器人的模拟程序 (2)

stormgens / 2293人阅读

摘要:上一篇文章中介绍了地图模块,接着来看主模块和动作模块主模块思路主模块由一个类构成,其调用各子模块,且其属性可用于保存信息这些信息,除了之前地图模块中的和之外,还包括初始坐标现所在坐标移动路径代码动作模块思路所谓移动,在模拟程序里就是更新现所

上一篇文章中介绍了地图模块,接着来看主模块和动作模块

主模块

思路:
主模块由一个Robot类构成,其调用各子模块,且其属性可用于保存信息
这些信息,除了之前地图模块中的coordinate_list和impassable_coordinate_list之外,还包括:

初始坐标

现所在坐标

移动路径

代码:

class Robot(object):
    def __init__(self):
        from map import coordinate_list, impassable_coordinate_list
        self.coordinate_list = coordinate_list
        self.impassable_coordinate_list = impassable_coordinate_list
        self.start_coordinate = (0, 0)
        self.current_coordinate = self.start_coordinate
        self.path_log = []
        self.path_log.append(self.start_coordinate)


robot = Robot()

if __name__ == "__main__":
    pass
动作模块

思路:

所谓移动,在模拟程序里就是更新 现所在坐标 和 移动路径

考虑到先做出来,简化基本动作就只有往上下左右移动

代码:

import numpy as np


def move_up(self):
    self.current_coordinate = tuple(np.array(self.current_coordinate) + np.array([0, 1]))
    print("up")
    self.path_log.append(self.current_coordinate)


def move_down(self):
    self.current_coordinate = tuple(np.array(self.current_coordinate) + np.array([0, -1]))
    print("down")
    self.path_log.append(self.current_coordinate)


def move_left(self):
    self.current_coordinate = tuple(np.array(self.current_coordinate) + np.array([-1, 0]))
    print("left")
    self.path_log.append(self.current_coordinate)


def move_right(self):
    self.current_coordinate = tuple(np.array(self.current_coordinate) + np.array([1, 0]))
    print("right")
    self.path_log.append(self.current_coordinate)

这里用了numpy,其实不用numpy也可以,但考虑到后期复杂寻路逻辑还是会处理数组,先用起来练下手,代码也显得简洁一些?
之后在main中添加方法:

    ...
    from motion import *
    Robot.move_up = move_up
    Robot.move_down = move_down
    Robot.move_left = move_left
    Robot.move_right = move_right
感知模块

思路:
之前提到过先让机器人根据完善的地图来实现部分功能,然后再逐步改善,先让感知模块根据地图来“感知”
具体来说,如果某坐标不在coordinate_list中,或者在impassable_coordinate_list中,那么就不能通行,返回False,代码也比较简单:

def judge_up_passable(self):
    x, y = self.current_coordinate
    up_coordinate = (x, y + 1)
    if up_coordinate not in self.coordinate_list or (up_coordinate in self.impassable_coordinate_list):
        return False
    else:
        return True

def judge_down_passable(self):
    x, y = self.current_coordinate
    down_coordinate = (x, y - 1)
    if down_coordinate not in self.coordinate_list or (down_coordinate in self.impassable_coordinate_list):
        return False
    else:
        return True


def judge_left_passable(self):
    x, y = self.current_coordinate
    left_coordinate = (x - 1, y)
    if left_coordinate not in self.coordinate_list or (left_coordinate in self.impassable_coordinate_list):
        return False
    else:
        return True


def judge_right_passable(self):
    x, y = self.current_coordinate
    right_coordinate = (x + 1, y)
    if right_coordinate not in self.coordinate_list or (right_coordinate in self.impassable_coordinate_list):
        return False
    else:
        return True

之后在main中添加方法:

    ...
    from sensor import *
    Robot.judge_up_passable = judge_up_passable
    Robot.judge_down_passable = judge_down_passable
    Robot.judge_left_passable = judge_left_passable
    Robot.judge_right_passable = judge_right_passable
测试

之后可以在main后面添加测试代码:

    # 移动测试
    print(robot.current_coordinate)
    robot.move_up()
    print(robot.current_coordinate)
    print(robot.path_log)

    # 感应测试
    print(robot.judge_up_passable())
    robot.current_coordinate = (0, 0)
    robot.impassable_coordinate_list.append((0, 1))
    print(robot.judge_up_passable())

获得的结果应该是
(0, 0)
up
(0, 1)
[(0, 0), (0, 1)]
True
False

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41295.html

相关文章

  • 扫地器人模拟程序 (1)

    摘要:前言在朋友的推荐下,尝试写一个模拟的扫地机器人的程序,当做是练习工程能力和算法写这篇文章一是记录和分享思路,也希望获得更多意见和建议,欢迎评论效果本来是打算最后再贴图的,文章没啥人气,加上感冒偷个懒就先贴个图吧不知道为什么没办法直接贴图片, 前言 在朋友的推荐下,尝试写一个模拟的扫地机器人的程序,当做是练习(工程能力和算法)写这篇文章一是记录和分享思路,也希望获得更多意见和建议,欢迎评...

    tanglijun 评论0 收藏0
  • 扫地器人模拟程序 (3)

    摘要:话说我的地图就是栅格形式用点坐标来表示格子模板模型法很容易理解,就是有几种走法,按情况调用。 寻路模块 (1) 终于要挑战寻路模块,虽然我是在重复造轮子,但看一下别人的轮子怎么造也是很重要的,所以在这之前首先搜索下,看看有什么现成的思路和代码,收获如下: 两种寻路逻辑 有两种寻路逻辑, 随机碰撞和路径规划,考虑到: a. 随机碰撞似乎需要不少经验/实验数据才能达到不错的效果,我缺经验/...

    ccj659 评论0 收藏0
  • 扫地器人模拟程序 (4)

    摘要:寻路模块通过一番寻找,发现这系列文章,其不仅包含算法,连寻路算法中的一些基础知识也一并介绍了,不愧是斯坦福出品,也很感谢译者要实现点到点最短路径,还需要做一些微小的工作,下面逐个说明计算曼哈顿距离的函数目的是寻路,肯定需要一个方法来估算两点 寻路模块 (2) 通过一番寻找,发现这系列文章,其不仅包含A*算法,连寻路算法中的一些基础知识也一并介绍了,不愧是斯坦福出品,也很感谢译者要实现点...

    thekingisalwaysluc 评论0 收藏0
  • 自动化会提高测试覆盖率,那测试覆盖率是什么?

    摘要:测试覆盖率有什么优势依然是以打扫房屋为例,测试覆盖率可以度量打扫的质量指示何时该停止打扫提醒我们还有其他地方需要清理。至此,我们可以得出结论测试自动化更高的测试覆盖率。 ...

    lyning 评论0 收藏0
  • 人工智能商业化全面解析,内含趋势解读

    摘要:在全球智能新商业峰会上,亿欧公司发布中国人工智能商业落地强榜单与研究报告。一定程度上,该榜单反映了人工智能在中国各细分领域的商业化程度。 人工智能商业化是什么意思?它和商业智能有怎样的联系?本文从概念、重大发展事件、发展趋势这几个方面全面解读人工智能商业化。 121 一、概念人工智能商业化,是指人工智能走向商业化的历程,即人工智能实现商业场景的应用。人工智能的商业化进程正一步步通过各种...

    Amos 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<