摘要:上一篇文章中介紹了地圖模塊,接著來看主模塊和動(dòng)作模塊主模塊思路主模塊由一個(gè)類構(gòu)成,其調(diào)用各子模塊,且其屬性可用于保存信息這些信息,除了之前地圖模塊中的和之外,還包括初始坐標(biāo)現(xiàn)所在坐標(biāo)移動(dòng)路徑代碼動(dòng)作模塊思路所謂移動(dòng),在模擬程序里就是更新現(xiàn)所
上一篇文章中介紹了地圖模塊,接著來看主模塊和動(dòng)作模塊
主模塊思路:
主模塊由一個(gè)Robot類構(gòu)成,其調(diào)用各子模塊,且其屬性可用于保存信息
這些信息,除了之前地圖模塊中的coordinate_list和impassable_coordinate_list之外,還包括:
初始坐標(biāo)
現(xiàn)所在坐標(biāo)
移動(dòng)路徑
代碼:
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動(dòng)作模塊
思路:
所謂移動(dòng),在模擬程序里就是更新 現(xiàn)所在坐標(biāo) 和 移動(dòng)路徑
考慮到先做出來,簡化基本動(dòng)作就只有往上下左右移動(dòng)
代碼:
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,其實(shí)不用numpy也可以,但考慮到后期復(fù)雜尋路邏輯還是會(huì)處理數(shù)組,先用起來練下手,代碼也顯得簡潔一些?
之后在main中添加方法:
... from motion import * Robot.move_up = move_up Robot.move_down = move_down Robot.move_left = move_left Robot.move_right = move_right感知模塊
思路:
之前提到過先讓機(jī)器人根據(jù)完善的地圖來實(shí)現(xiàn)部分功能,然后再逐步改善,先讓感知模塊根據(jù)地圖來“感知”
具體來說,如果某坐標(biāo)不在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測(cè)試
之后可以在main后面添加測(cè)試代碼:
# 移動(dòng)測(cè)試 print(robot.current_coordinate) robot.move_up() print(robot.current_coordinate) print(robot.path_log) # 感應(yīng)測(cè)試 print(robot.judge_up_passable()) robot.current_coordinate = (0, 0) robot.impassable_coordinate_list.append((0, 1)) print(robot.judge_up_passable())
獲得的結(jié)果應(yīng)該是
(0, 0)
up
(0, 1)
[(0, 0), (0, 1)]
True
False
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/41295.html
摘要:前言在朋友的推薦下,嘗試寫一個(gè)模擬的掃地機(jī)器人的程序,當(dāng)做是練習(xí)工程能力和算法寫這篇文章一是記錄和分享思路,也希望獲得更多意見和建議,歡迎評(píng)論效果本來是打算最后再貼圖的,文章沒啥人氣,加上感冒偷個(gè)懶就先貼個(gè)圖吧不知道為什么沒辦法直接貼圖片, 前言 在朋友的推薦下,嘗試寫一個(gè)模擬的掃地機(jī)器人的程序,當(dāng)做是練習(xí)(工程能力和算法)寫這篇文章一是記錄和分享思路,也希望獲得更多意見和建議,歡迎評(píng)...
摘要:話說我的地圖就是柵格形式用點(diǎn)坐標(biāo)來表示格子模板模型法很容易理解,就是有幾種走法,按情況調(diào)用。 尋路模塊 (1) 終于要挑戰(zhàn)尋路模塊,雖然我是在重復(fù)造輪子,但看一下別人的輪子怎么造也是很重要的,所以在這之前首先搜索下,看看有什么現(xiàn)成的思路和代碼,收獲如下: 兩種尋路邏輯 有兩種尋路邏輯, 隨機(jī)碰撞和路徑規(guī)劃,考慮到: a. 隨機(jī)碰撞似乎需要不少經(jīng)驗(yàn)/實(shí)驗(yàn)數(shù)據(jù)才能達(dá)到不錯(cuò)的效果,我缺經(jīng)驗(yàn)/...
摘要:尋路模塊通過一番尋找,發(fā)現(xiàn)這系列文章,其不僅包含算法,連尋路算法中的一些基礎(chǔ)知識(shí)也一并介紹了,不愧是斯坦福出品,也很感謝譯者要實(shí)現(xiàn)點(diǎn)到點(diǎn)最短路徑,還需要做一些微小的工作,下面逐個(gè)說明計(jì)算曼哈頓距離的函數(shù)目的是尋路,肯定需要一個(gè)方法來估算兩點(diǎn) 尋路模塊 (2) 通過一番尋找,發(fā)現(xiàn)這系列文章,其不僅包含A*算法,連尋路算法中的一些基礎(chǔ)知識(shí)也一并介紹了,不愧是斯坦福出品,也很感謝譯者要實(shí)現(xiàn)點(diǎn)...
摘要:測(cè)試覆蓋率有什么優(yōu)勢(shì)依然是以打掃房屋為例,測(cè)試覆蓋率可以度量打掃的質(zhì)量指示何時(shí)該停止打掃提醒我們還有其他地方需要清理。至此,我們可以得出結(jié)論測(cè)試自動(dòng)化更高的測(cè)試覆蓋率。 ...
摘要:在全球智能新商業(yè)峰會(huì)上,億歐公司發(fā)布中國人工智能商業(yè)落地強(qiáng)榜單與研究報(bào)告。一定程度上,該榜單反映了人工智能在中國各細(xì)分領(lǐng)域的商業(yè)化程度。 人工智能商業(yè)化是什么意思?它和商業(yè)智能有怎樣的聯(lián)系?本文從概念、重大發(fā)展事件、發(fā)展趨勢(shì)這幾個(gè)方面全面解讀人工智能商業(yè)化。 121 一、概念人工智能商業(yè)化,是指人工智能走向商業(yè)化的歷程,即人工智能實(shí)現(xiàn)商業(yè)場(chǎng)景的應(yīng)用。人工智能的商業(yè)化進(jìn)程正一步步通過各種...
閱讀 2982·2021-11-24 09:39
閱讀 1266·2021-11-02 14:38
閱讀 4313·2021-09-10 11:26
閱讀 2852·2021-08-25 09:40
閱讀 2386·2019-08-30 15:54
閱讀 557·2019-08-30 10:56
閱讀 2864·2019-08-26 12:14
閱讀 3299·2019-08-26 12:13