Skip to content

action_build_tribune_x2

BuildTribuneX2Action #

Bases: Action

Action used to build a 2-story tribune.

Source code in cogip/tools/planner/actions/action_build_tribune_x2.py
 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
 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
class BuildTribuneX2Action(Action):
    """
    Action used to build a 2-story tribune.
    """

    def __init__(
        self,
        planner: "Planner",
        actions: Actions,
        construction_area_id: ConstructionAreaID,
        weight: float = 2000000.0,
    ):
        self.custom_weight = weight
        super().__init__(f"BuildTribuneX2 {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 = 180
        self.shift_approach = self.shift_build + 150
        self.shift_step_back = self.shift_approach

    async def recycle(self):
        self.recycled = True

    async def before_action(self):
        logger.info(f"{self.name}: before_action - tribunes_in_robot={self.game_context.tribunes_in_robot}")
        self.start_pose = self.pose_current

        # Approach
        # Skip approach if the robot is already in front of the construction area
        match self.construction_area.O:
            case 0 | 180:
                diff = abs(self.construction_area.y - self.start_pose.y)
            case -90 | 90:
                diff = abs(self.construction_area.x - self.start_pose.x)
            case _:
                diff = 1000

        if diff >= 5:
            approach_pose = Pose(
                **get_relative_pose(
                    self.construction_area,
                    front_offset=self.shift_approach,
                    angular_offset=180,
                ).model_dump(),
                max_speed_linear=100,
                max_speed_angular=100,
                allow_reverse=True,
                before_pose_func=self.before_approach,
                after_pose_func=self.after_approach,
            )
            self.poses.append(approach_pose)
        else:
            logger.info(f"{self.name}: skip approach (diff = {diff})")
            await self.before_approach()
            await self.after_approach()

        # Build
        build_pose = Pose(
            **get_relative_pose(
                self.construction_area,
                front_offset=self.shift_build,
                angular_offset=180,
            ).model_dump(),
            max_speed_linear=80,
            max_speed_angular=80,
            bypass_final_orientation=False,
            allow_reverse=False,
            before_pose_func=self.before_build,
            after_pose_func=self.after_build,
        )
        self.poses.append(build_pose)

        # Step back
        step_back_pose = Pose(
            **get_relative_pose(
                self.construction_area,
                front_offset=self.shift_step_back,
                angular_offset=180,
            ).model_dump(),
            max_speed_linear=50,
            max_speed_angular=50,
            allow_reverse=True,
            bypass_final_orientation=False,
            before_pose_func=self.before_step_back,
            after_pose_func=self.after_step_back,
        )
        self.poses.append(step_back_pose)

    async def before_approach(self):
        logger.info(f"{self.name}: before_approach")

    async def after_approach(self):
        logger.info(f"{self.name}: after_approach")

    async def before_build(self):
        logger.info(f"{self.name}: before_build")

    async def after_build(self):
        logger.info(f"{self.name}: after_build - tribunes_in_robot={self.game_context.tribunes_in_robot}")
        await actuators.tribune_spread(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)

        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.5)

        await actuators.arms_hold2(self.planner)
        await asyncio.sleep(0.1)

        await actuators.arms_release(self.planner)
        await asyncio.sleep(0.1)

        await asyncio.gather(
            actuators.arms_close(self.planner),
            actuators.arm_left_side(self.planner),
            actuators.arm_right_side(self.planner),
        )

        self.game_context.tribunes_in_robot -= 2
        self.construction_area.tribune_level += 2

    async def before_step_back(self):
        logger.info(f"{self.name}: before_step_back")

    async def after_step_back(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.score += 12

    def weight(self) -> float:
        if self.game_context.tribunes_in_robot < 2 or self.construction_area.tribune_level != 0:
            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.OppositeSideLarge2
            and self.game_context.construction_areas[ConstructionAreaID.OppositeSideLarge3].enabled
        ):
            return 0

        return self.custom_weight