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

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

主模块

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

  1. 初始坐标
  2. 现所在坐标
  3. 移动路径

代码:

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

动作模块

思路:

  1. 所谓移动,在模拟程序里就是更新 现所在坐标 和 移动路径
  2. 考虑到先做出来,简化基本动作就只有往上下左右移动

代码:

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://segmentfault.com/a/1190000012969412
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞