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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 | class BuildTribuneX3Action(Action):
"""
Action used to build a 3-story tribune.
"""
def __init__(
self,
planner: "Planner",
actions: Actions,
construction_area_id: ConstructionAreaID,
weight: float = 2000000.0,
):
self.custom_weight = weight
super().__init__(f"BuildTribuneX3 {construction_area_id.name}", planner, actions)
self.before_action_func = self.before_action
self.construction_area = self.game_context.construction_areas[construction_area_id]
self.shift_build_x3 = 160
self.shift_build_x2 = self.shift_build_x3 + 160
self.shift_approach_x2 = self.shift_build_x2 + 130
self.shift_step_back_x2 = self.shift_build_x2 + 20
self.shift_approach_x3 = self.shift_build_x2 - 20
self.shift_step_back_x3 = self.shift_approach_x3
async def recycle(self):
self.recycled = True
def set_avoidance(self, new_strategy: AvoidanceStrategy):
logger.info(f"{self.name}: set avoidance to {new_strategy.name}")
self.game_context.avoidance_strategy = new_strategy
self.planner.shared_properties["avoidance_strategy"] = new_strategy
async def before_action(self):
logger.info(f"{self.name}: before_action - tribunes_in_robot={self.game_context.tribunes_in_robot}")
self.avoidance_backup = self.game_context.avoidance_strategy
self.start_pose = self.pose_current
# Approach x2
approach_x2_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_approach_x2,
angular_offset=180,
).model_dump(),
max_speed_linear=100,
max_speed_angular=100,
allow_reverse=True,
before_pose_func=self.before_approach_x2,
after_pose_func=self.after_approach_x2,
)
self.poses.append(approach_x2_pose)
# Build x2
build_x2_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_build_x2,
angular_offset=180,
).model_dump(),
max_speed_linear=80,
max_speed_angular=80,
allow_reverse=False,
bypass_final_orientation=False,
before_pose_func=self.before_build_x2,
after_pose_func=self.after_build_x2,
)
self.poses.append(build_x2_pose)
# Step back x2
step_back_x2_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_step_back_x2,
angular_offset=180,
).model_dump(),
max_speed_linear=50,
max_speed_angular=50,
allow_reverse=True,
before_pose_func=self.before_step_back_x2,
after_pose_func=self.after_step_back_x2,
)
self.poses.append(step_back_x2_pose)
# Approach x3
approach_x3_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_approach_x3,
angular_offset=180,
).model_dump(),
max_speed_linear=20,
max_speed_angular=20,
allow_reverse=False,
bypass_final_orientation=False,
before_pose_func=self.before_approach_x3,
after_pose_func=self.after_approach_x3,
)
self.poses.append(approach_x3_pose)
# Build x3
build_x3_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_build_x3,
angular_offset=180,
).model_dump(),
max_speed_linear=20,
max_speed_angular=20,
allow_reverse=False,
bypass_final_orientation=False,
before_pose_func=self.before_build_x3,
after_pose_func=self.after_build_x3,
)
self.poses.append(build_x3_pose)
# Step back x3
step_back_x3_pose = Pose(
**get_relative_pose(
self.construction_area,
front_offset=self.shift_step_back_x3,
angular_offset=180,
).model_dump(),
max_speed_linear=50,
max_speed_angular=50,
bypass_final_orientation=False,
allow_reverse=True,
before_pose_func=self.before_step_back_x3,
after_pose_func=self.after_step_back_x3,
)
self.poses.append(step_back_x3_pose)
async def before_approach_x2(self):
logger.info(f"{self.name}: before_approach_x2")
async def after_approach_x2(self):
logger.info(f"{self.name}: after_approach_x2")
self.construction_area.enabled = False
async def before_build_x2(self):
logger.info(f"{self.name}: before_build_x2")
self.set_avoidance(AvoidanceStrategy.Disabled)
async def after_build_x2(self):
logger.info(f"{self.name}: after_build_x2 - tribunes_in_robot={self.game_context.tribunes_in_robot}")
await actuators.lift_5(self.planner)
await asyncio.sleep(0.2)
await actuators.arm_left_side(self.planner, 1500)
await actuators.arm_right_side(self.planner, 1500)
await actuators.magnet_side_left_out(self.planner, 1500)
await actuators.magnet_side_right_out(self.planner, 1500)
await asyncio.sleep(0.2)
await actuators.magnet_center_left_in(self.planner)
await actuators.magnet_center_right_in(self.planner)
await asyncio.sleep(0.2)
await actuators.arms_hold1(self.planner)
await asyncio.sleep(0.1)
await actuators.lift_140(self.planner)
await asyncio.sleep(1.2)
self.planner.scservos.set(SCServoEnum.ARM_RIGHT, 223)
self.planner.scservos.set(SCServoEnum.ARM_LEFT, 703)
await asyncio.sleep(0.2)
await asyncio.gather(
actuators.magnet_side_left_in(self.planner),
actuators.magnet_side_right_in(self.planner),
)
await asyncio.sleep(0.2)
await actuators.lift_125(self.planner)
await asyncio.sleep(0.2)
await actuators.arms_hold2(self.planner)
await asyncio.sleep(0.1)
await actuators.arms_release(self.planner)
await asyncio.sleep(0.2)
await asyncio.gather(
actuators.arms_close(self.planner),
actuators.arm_left_side(self.planner),
actuators.arm_right_side(self.planner),
)
await asyncio.sleep(0.5)
async def before_step_back_x2(self):
logger.info(f"{self.name}: before_step_back_x2")
async def after_step_back_x2(self):
logger.info(f"{self.name}: after_step_back_x2")
await actuators.lift_0(self.planner)
await asyncio.sleep(1)
async def before_approach_x3(self):
logger.info(f"{self.name}: before_approach_x3")
await actuators.arm_right_front(self.planner)
await actuators.arm_left_front(self.planner)
await actuators.magnet_side_right_in(self.planner)
await actuators.magnet_side_left_in(self.planner)
await actuators.magnet_center_left_out(self.planner)
await actuators.magnet_center_right_out(self.planner)
async def after_approach_x3(self):
logger.info(f"{self.name}: after_approach_x3")
await actuators.lift_140(self.planner)
await asyncio.sleep(1.2)
async def before_build_x3(self):
logger.info(f"{self.name}: before_build_x3")
async def after_build_x3(self):
logger.info(f"{self.name}: after_build_x3")
await actuators.lift_125(self.planner)
await asyncio.sleep(0.2)
await asyncio.gather(
actuators.magnet_center_left_in(self.planner),
actuators.magnet_center_right_in(self.planner),
)
async def before_step_back_x3(self):
logger.info(f"{self.name}: before_step_back_x3")
async def after_step_back_x3(self):
logger.info(f"{self.name}: after_step_back - tribunes_in_robot={self.game_context.tribunes_in_robot}")
self.construction_area.enabled = True
await asyncio.gather(
actuators.arm_left_center(self.planner),
actuators.arm_right_center(self.planner),
actuators.lift_0(self.planner),
)
self.game_context.tribunes_in_robot -= 2
self.construction_area.tribune_level += 2
self.game_context.score += 24
self.set_avoidance(self.avoidance_backup)
def weight(self) -> float:
if self.game_context.tribunes_in_robot < 2 or self.construction_area.tribune_level != 1:
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomSmall
and self.game_context.tribunes[TribuneID.LocalBottom].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeBottomSmall
and self.game_context.tribunes[TribuneID.OppositeBottomSide].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge1
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge2].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge1
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge2
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeSideLarge1
and self.game_context.construction_areas[ConstructionAreaID.OppositeSideLarge2].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeSideLarge1
and self.game_context.construction_areas[ConstructionAreaID.OppositeSideLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomSmall
and self.game_context.tribunes[TribuneID.LocalBottom].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeBottomSmall
and self.game_context.tribunes[TribuneID.OppositeBottomSide].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge1
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge2].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge1
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge2
and self.game_context.construction_areas[ConstructionAreaID.LocalBottomLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeSideLarge1
and self.game_context.construction_areas[ConstructionAreaID.OppositeSideLarge2].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeSideLarge1
and self.game_context.construction_areas[ConstructionAreaID.OppositeSideLarge3].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.LocalBottomLarge3
and self.game_context.tribunes[TribuneID.LocalCenter].enabled
):
return 0
if (
self.construction_area.id == ConstructionAreaID.OppositeSideLarge3
and self.game_context.tribunes[TribuneID.OppositeCenter].enabled
):
return 0
return self.custom_weight
|