-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock_to_middle.py
More file actions
74 lines (56 loc) · 3.37 KB
/
block_to_middle.py
File metadata and controls
74 lines (56 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""ブロック置き場→中点のゲーム動作モジュール.
ブロック置き場→中点のゲーム動作のコマンド生成やコスト計算をする
パラメータは https://github.com/KatLab-MiyazakiUniv/etrobocon2022/pull/89 を参照
@author mutotaka0426
"""
from game_motion import GameMotion
class BlockToMiddle(GameMotion):
"""ブロック置き場→中点のゲーム動作クラス."""
def __init__(self, angle: int, with_block: bool) -> None:
"""BlockToMiddleのコンストラクタ.
Args:
angle: 方向転換の角度
with_block: ブロックを保持している場合True
"""
if with_block: # ブロックを保持している場合
self.__rotation_angle = GameMotion.ROTATION_BLOCK_TABLE[abs(angle)]["angle"]
self.__rotation_pwm = GameMotion.ROTATION_BLOCK_PWM
self.__rotation_time = GameMotion.ROTATION_BLOCK_TABLE[abs(angle)]["time"]
else: # ブロックを保持していない場合
self.__rotation_angle = GameMotion.ROTATION_NO_BLOCK_TABLE[abs(angle)]["angle"]
self.__rotation_pwm = GameMotion.ROTATION_NO_BLOCK_PWM
self.__rotation_time = GameMotion.ROTATION_NO_BLOCK_TABLE[abs(angle)]["time"]
self.__direct_rotation = "clockwise" if angle > 0 else "anticlockwise"
self.__motion_time = 0.8094
self.__success_rate = 1.0
def generate_command(self) -> str:
"""ブロック置き場→中点のゲーム動作に必要なコマンドを生成するメソッド.
Returns:
str: コマンド
"""
command_list = "" # コマンドのリストを格納する文字列
if self.__rotation_angle != 0: # 回頭角度が0の場合は回頭のコマンドを生成しない
# 回頭を安定させるために、回頭の前後にスリープを入れる
command_list += "SL,%d\n" % (GameMotion.SLEEP_TIME * 1000)
command_list += "RT,%d,%d,%s\n" % (self.__rotation_angle,
self.__rotation_pwm, self.__direct_rotation)
command_list += "SL,%d\n" % (GameMotion.SLEEP_TIME * 1000)
command_list += "CS,BLACK,70\n" # エッジを認識するまで直進
command_list += "DS,10,70\n" # 走行体がエッジに乗るまで直進
# エッジ切り替えのコマンドは生成しないが,計算上はエッジをnoneにする
self.current_edge = "none"
return command_list.replace("\n", ",ブロック置き場→中点\n", 1) # 最初の行の末尾に",ブロック置き場→中点"を追加する
def get_cost(self) -> float:
"""ブロック置き場→中点のゲーム動作のコストを計算するメソッド.
Returns:
float: コスト
"""
m_time = self.__motion_time # m_time: 回頭や調整動作込みの動作時間
# 動作時間に回頭時間を足す(成功率に変動はなし)
m_time += self.__rotation_time
# 回頭している場合,回頭前後のスリープ時間を足す
if self.__rotation_angle != 0:
m_time += GameMotion.SLEEP_TIME * 2
# 動作時間 * 成功率 + 最大計測時間 * 失敗率
cost = m_time*self.__success_rate+GameMotion.MAX_TIME*(1-self.__success_rate)
return cost