Skip to content

action_align

AlignTopCornerAction #

Bases: Action

Action used to align the robot on the top corner (blue camp by default) before game start.

Source code in cogip/tools/planner/actions/action_align.py
 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
class AlignTopCornerAction(Action):
    """
    Action used to align the robot on the top corner (blue camp by default) before game start.
    """

    def __init__(
        self,
        planner: "Planner",
        strategy: Strategy,
        *,
        final_pose: models.Pose | None = None,
        reset_countdown=False,
        weight: float = 2000000.0,
    ):
        self.final_pose = final_pose
        self.reset_countdown = reset_countdown
        self.custom_weight = weight
        super().__init__("Align Top Corner action", planner, strategy)
        self.before_action_func = self.before_action
        self.after_action_func = self.after_action
        self.align_x = 700
        self.align_y = -1300
        self.border_offset = 115

    def set_avoidance(self, new_strategy: AvoidanceStrategy):
        self.logger.info(f"{self.name}: set avoidance to {new_strategy.name}")
        self.planner.shared_properties.avoidance_strategy = new_strategy.val

    async def init_start_pose(self):
        pass

    async def before_action(self):
        self.logger.info(f"{self.name}: before_action")
        self.avoidance_backup = AvoidanceStrategy(self.planner.shared_properties.avoidance_strategy)
        self.set_avoidance(AvoidanceStrategy.Disabled)
        self.disable_fixed_obstacles_backup = self.planner.shared_properties.disable_fixed_obstacles
        self.planner.shared_properties.disable_fixed_obstacles = True
        self.start_position = self.planner.start_positions.get()
        if not self.final_pose:
            self.final_pose = self.start_position

        await self.init_start_pose()

        # Align top
        pose = Pose(
            x=1100,
            y=self.start_position.y,
            O=180,
            max_speed_linear=5,
            max_speed_angular=5,
            motion_direction=MotionDirection.BACKWARD_ONLY,
            bypass_anti_blocking=True,
            timeout_ms=0,
            bypass_final_orientation=True,
            before_pose_func=self.before_align_top,
            after_pose_func=self.after_align_top,
        )
        if self.planner.shared_properties.table == TableEnum.Training:
            pose.x -= 1000
        self.poses.append(pose)

    async def before_align_top(self):
        self.logger.info(f"{self.name}: before_align_top")
        await actuators.front_arms_open(self.planner)
        await actuators.back_arms_open(self.planner)
        await actuators.front_lift_mid(self.planner)
        await actuators.back_lift_mid(self.planner)

    async def after_align_top(self):
        self.logger.info(f"{self.name}: after_align_top")
        pose_current = self.planner.pose_current
        new_pose_current = Pose(
            x=1000 - self.border_offset,
            y=pose_current.y,
            O=180,
        )
        if self.planner.shared_properties.table == TableEnum.Training:
            new_pose_current.x -= 1000
        self.planner.shared_pose_current_buffer.push(new_pose_current.x, new_pose_current.y, new_pose_current.O)
        await self.planner.sio_ns.emit("pose_start", new_pose_current.pose.model_dump())
        await asyncio.sleep(0.5)

        # Step forward from top
        pose = Pose(
            x=self.start_position.x,
            y=pose_current.y,
            O=180,
            max_speed_linear=10,
            max_speed_angular=10,
            motion_direction=MotionDirection.FORWARD_ONLY,
            bypass_final_orientation=False,
            before_pose_func=self.before_step_forward_from_top,
            after_pose_func=self.after_step_forward_from_top,
        )
        self.poses.append(pose)

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

    async def after_step_forward_from_top(self):
        self.logger.info(f"{self.name}: after_step_forward_from_top")
        pose_current = self.planner.pose_current

        # Align side
        pose = AdaptedPose(
            x=pose_current.x,
            y=-1600,
            O=90,
            max_speed_linear=5,
            max_speed_angular=20,
            motion_direction=MotionDirection.BACKWARD_ONLY,
            bypass_anti_blocking=True,
            timeout_ms=0,
            bypass_final_orientation=True,
            before_pose_func=self.before_align_side,
            after_pose_func=self.after_align_side,
        )
        self.poses.append(pose)

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

    async def after_align_side(self):
        self.logger.info(f"{self.name}: after_align_side")
        pose_current = self.planner.pose_current
        new_pose_current = AdaptedPose(
            x=pose_current.x,
            y=-1500 + self.border_offset,
            O=90,
        )
        self.planner.shared_pose_current_buffer.push(new_pose_current.x, new_pose_current.y, new_pose_current.O)
        await self.planner.sio_ns.emit("pose_start", new_pose_current.pose.model_dump())
        await asyncio.sleep(0.5)

        # Step forward from side
        pose = Pose(
            x=pose_current.x,
            y=self.start_position.y,
            O=90,
            max_speed_linear=10,
            max_speed_angular=10,
            motion_direction=MotionDirection.FORWARD_ONLY,
            bypass_final_orientation=False,
            before_pose_func=self.before_step_forward_from_side,
            after_pose_func=self.after_step_forward_from_side,
        )
        self.poses.append(pose)

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

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

        # Final pose
        pose = Pose(
            x=self.final_pose.x,
            y=self.final_pose.y,
            O=self.final_pose.O,
            max_speed_linear=10,
            max_speed_angular=10,
            motion_direction=MotionDirection.BIDIRECTIONAL,
            before_pose_func=self.before_final_pose,
            after_pose_func=self.after_final_pose,
        )
        self.poses.append(pose)

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

    async def after_final_pose(self):
        self.logger.info(f"{self.name}: after_final_pose")
        await actuators.front_arms_close(self.planner)
        await actuators.back_arms_close(self.planner)
        await actuators.front_lift_down(self.planner)
        await actuators.back_lift_down(self.planner)

    async def after_action(self):
        self.logger.info(f"{self.name}: after_action")
        self.set_avoidance(self.avoidance_backup)
        self.planner.shared_properties.disable_fixed_obstacles = self.disable_fixed_obstacles_backup
        if self.reset_countdown:
            now = datetime.now(UTC)
            self.planner.countdown_start_timestamp = now
            await self.planner.sio_ns.emit(
                "start_countdown",
                (self.planner.robot_id, self.planner.game_context.game_duration, now.isoformat(), "deepskyblue"),
            )

    def weight(self) -> float:
        return self.custom_weight

AlignTopCornerCameraAction #

Bases: AlignTopCornerAction

Action used to align the robot on the top corner (blue camp by default) before game start. Set initial pose from camera detection.

Source code in cogip/tools/planner/actions/action_align.py
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
class AlignTopCornerCameraAction(AlignTopCornerAction):
    """
    Action used to align the robot on the top corner (blue camp by default) before game start.
    Set initial pose from camera detection.
    """

    def __init__(
        self,
        planner: "Planner",
        strategy: Strategy,
        *,
        final_pose: models.Pose | None = None,
        reset_countdown=False,
        weight: float = 2000000.0,
    ):
        super().__init__(
            planner,
            strategy,
            final_pose=final_pose,
            reset_countdown=reset_countdown,
            weight=weight,
        )
        self.name = "Align Top Corner Camera action"

    async def init_start_pose(self):
        # On start, the robot is inside the Top start area, oriented toward the Aruco tag (only useful for simulation).
        init_pose = AdaptedPose(
            x=550 + self.planner.shared_properties.robot_length / 2,
            y=-1050 - self.planner.shared_properties.robot_width / 2,
            O=110,
        )
        if self.planner.shared_properties.table == TableEnum.Training:
            init_pose.x -= 1000

        self.logger.info(f"{self.name}: Emitting initial pose for camera detection: {init_pose.pose}")
        self.planner.shared_pose_current_buffer.push(init_pose.x, init_pose.y, init_pose.O)
        await self.planner.sio_ns.emit("pose_start", init_pose.pose.model_dump())
        await asyncio.sleep(0.5)

        current_pose = await get_robot_position(self.planner)
        if current_pose:
            self.logger.info(f"{self.name}: Camera detected pose: {current_pose}")
        else:
            self.logger.warning(f"{self.name}: No camera detection, using default start pose")
            current_pose = AdaptedPose(
                x=550 + self.planner.shared_properties.robot_length / 2,
                y=-1050 - self.planner.shared_properties.robot_width / 2,
                O=90,
            )
            if self.planner.shared_properties.table == TableEnum.Training:
                current_pose.x -= 1000
        self.planner.shared_pose_current_buffer.push(current_pose.x, current_pose.y, current_pose.O)
        await self.planner.sio_ns.emit("pose_start", current_pose.pose.model_dump())
        await asyncio.sleep(0.5)