Skip to content

base_actions

AlignAction #

Bases: Action

Action used align the robot before game start.

Source code in cogip/tools/planner/actions/base_actions.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
class AlignAction(Action):
    """
    Action used align the robot before game start.
    """

    def __init__(self, planner: "Planner", actions: Actions):
        super().__init__("Align action", planner, actions)
        self.before_action_func = self.init_poses

    def set_avoidance(self, new_strategy: AvoidanceStrategy):
        self.game_context.avoidance_strategy = new_strategy
        self.planner.shared_properties["avoidance_strategy"] = new_strategy

    async def init_poses(self):
        self.start_pose = self.planner.pose_current.model_copy()
        self.start_avoidance = self.game_context.avoidance_strategy

        await asyncio.gather(
            actuators.bottom_grip_close(self.planner),
            actuators.top_grip_close(self.planner),
            actuators.arm_panel_close(self.planner),
            actuators.cart_in(self.planner),
            asyncio.sleep(0.5),
        )
        await asyncio.gather(
            actuators.bottom_lift_up(self.planner),
            actuators.top_lift_up(self.planner),
        )

        if Camp().adapt_y(self.start_pose.y) > 0:
            # Do not do alignment if the robot is in the opposite start position because it is not in a corner
            return

        pose1 = AdaptedPose(
            x=self.start_pose.x,
            y=-1700 + self.game_context.properties.robot_length / 2,
            O=0,
            max_speed_linear=5,
            max_speed_angular=5,
            allow_reverse=True,
            bypass_anti_blocking=True,
            timeout_ms=0,  # TODO
            bypass_final_orientation=True,
            before_pose_func=self.before_pose1,
            after_pose_func=self.after_pose1,
        )
        self.poses.append(pose1)

    async def before_pose1(self):
        self.set_avoidance(AvoidanceStrategy.Disabled)

    async def after_pose1(self):
        self.set_avoidance(self.start_avoidance)
        current_pose = self.planner.pose_current.model_copy()
        current_pose.y = Camp().adapt_y(-1500 + self.game_context.properties.robot_length / 2)
        current_pose.O = Camp().adapt_angle(90)
        await self.planner.sio_ns.emit("pose_start", current_pose.model_dump())

        pose2 = AdaptedPose(
            x=current_pose.x,
            y=-1250,
            O=180 if current_pose.x > 0 else 0,
            max_speed_linear=20,
            max_speed_angular=20,
            allow_reverse=False,
        )
        self.poses.append(pose2)

        if current_pose.x > 0:
            x = 1200 - self.game_context.properties.robot_length / 2
        else:
            x = -1200 + self.game_context.properties.robot_length / 2
        pose3 = AdaptedPose(
            x=x,
            y=-1250,
            O=0,
            max_speed_linear=5,
            max_speed_angular=5,
            allow_reverse=True,
            bypass_anti_blocking=True,
            timeout_ms=0,  # TODO
            bypass_final_orientation=True,
            before_pose_func=self.before_pose3,
            after_pose_func=self.after_pose3,
        )
        self.poses.append(pose3)

    async def before_pose3(self):
        self.set_avoidance(AvoidanceStrategy.Disabled)

    async def after_pose3(self):
        self.set_avoidance(self.start_avoidance)
        current_pose = self.planner.pose_current.model_copy()
        if current_pose.x > 0:
            current_pose.x = 1000 - self.game_context.properties.robot_length / 2
        else:
            current_pose.x = -1000 + self.game_context.properties.robot_length / 2
        current_pose.O = 180 if current_pose.x > 0 else 0
        await self.planner.sio_ns.emit("pose_start", current_pose.model_dump())

        pose4 = Pose(
            x=730 if current_pose.x > 0 else -730,
            y=current_pose.y,
            O=current_pose.O,
            max_speed_linear=33,
            max_speed_angular=33,
            allow_reverse=False,
        )
        self.poses.append(pose4)

        pose5 = Pose(
            x=self.start_pose.x,
            y=self.start_pose.y,
            O=self.start_pose.O,
            max_speed_linear=33,
            max_speed_angular=33,
            allow_reverse=True,
        )
        self.poses.append(pose5)

    def weight(self) -> float:
        return 1000000.0

DropInDropoffZoneAction #

Bases: Action

Drop plants from the lower grips in a dropoff zone.

Source code in cogip/tools/planner/actions/base_actions.py
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
class DropInDropoffZoneAction(Action):
    """
    Drop plants from the lower grips in a dropoff zone.
    """

    def __init__(self, planner: "Planner", actions: Actions, dropoff_zone_id: artifacts.DropoffZoneID, slot: int):
        super().__init__("DropInDropoffZone action", planner, actions)
        self.dropoff_zone = self.game_context.dropoff_zones[dropoff_zone_id]
        self.slot = slot
        self.push_pot: PushPotAction | None = None
        self.after_action_func = self.after_action

        match dropoff_zone_id:
            case artifacts.DropoffZoneID.Top:
                x = self.dropoff_zone.x + (self.slot - 1) * 125 - self.game_context.properties.robot_length / 2
                y = -1180
                if self.slot == 2:
                    # Prepare PushPot action
                    self.push_pot = PushPotAction(self.planner, self.actions, artifacts.PotSupplyID.LocalTop)
                    x2 = self.push_pot.poses[0].x
                    y2 = self.push_pot.poses[0].y
                else:
                    x2 = x - 150
                    y2 = y
                angle = 0
            case artifacts.DropoffZoneID.Bottom:
                x = self.dropoff_zone.x - (self.slot - 1) * 125 + self.game_context.properties.robot_length / 2
                y = -1180
                if self.slot == 2 and self.game_context._table == TableEnum.Training:
                    # Prepare PushPot action
                    self.push_pot = PushPotAction(self.planner, self.actions, artifacts.PotSupplyID.LocalMiddle)
                    x2 = self.push_pot.poses[0].x
                    y2 = self.push_pot.poses[0].y
                else:
                    x2 = x + 150
                    y2 = y
                angle = 180
            case artifacts.DropoffZoneID.Opposite:
                x = x2 = self.dropoff_zone.x
                y = self.dropoff_zone.y + (self.slot - 1) * 125 - self.game_context.properties.robot_length / 2
                y2 = y - 150
                angle = 90

        self.poses.append(
            AdaptedPose(
                x=x,
                y=y,
                O=angle,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=False,
                after_pose_func=self.after_pose1,
            )
        )

        self.poses.append(
            AdaptedPose(
                x=x2,
                y=y2,
                O=angle,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=True,
                after_pose_func=self.after_pose2,
            )
        )

    async def after_pose1(self):
        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state
            and self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state
        ):
            # Do not count plant without pot because we do not know its color
            self.game_context.score += 3  # Plant valid
            self.game_context.score += 1  # Plant in pot

        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state
            and self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state
        ):
            # Do not count plant without pot because we do not know its color
            self.game_context.score += 3  # Plant valid
            self.game_context.score += 1  # Plant in pot

        await actuators.bottom_grip_open(self.planner)
        await actuators.cart_magnet_off(self.planner)
        await asyncio.sleep(0.1)
        await actuators.cart_in(self.planner)

    async def after_pose2(self):
        self.dropoff_zone.free_slots -= 1
        if BoolSensorEnum.BOTTOM_GRIP_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state = False
        if BoolSensorEnum.MAGNET_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state = False
        if BoolSensorEnum.MAGNET_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state = False
        if BoolSensorEnum.BOTTOM_GRIP_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state = False

    async def after_action(self):
        if self.push_pot:
            self.actions.append(self.push_pot)

    def weight(self) -> float:
        if not (
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state
            and self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state
        ):
            return 0

        if self.slot + 1 == self.dropoff_zone.free_slots:
            return 1000000.0
        return 0

DropInPlanterAction #

Bases: Action

Drop plants from the upper grips in a planter.

Source code in cogip/tools/planner/actions/base_actions.py
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
class DropInPlanterAction(Action):
    """
    Drop plants from the upper grips in a planter.
    """

    def __init__(self, planner: "Planner", actions: Actions, planter_id: artifacts.PlanterID):
        super().__init__("DropInPlanter action", planner, actions)
        self.planter = self.game_context.planters[planter_id]
        self.blocking_extra_move = 20
        self.front_excess = 85
        self.approach_delta = 50
        self.half_robot_length = self.game_context.properties.robot_length / 2

        if self.game_context.camp.color == Camp.Colors.yellow:
            match planter_id:
                case artifacts.PlanterID.Top:
                    drop_x = self.planter.x + self.front_excess + self.blocking_extra_move - self.half_robot_length
                    approach_x = self.planter.x - self.approach_delta - self.half_robot_length
                    approach_y = drop_y = self.planter.y
                case artifacts.PlanterID.LocalSide | artifacts.PlanterID.Test:
                    approach_x = drop_x = self.planter.x
                    drop_y = self.planter.y - self.front_excess - self.blocking_extra_move + self.half_robot_length
                    approach_y = self.planter.y + self.approach_delta + self.half_robot_length
                case artifacts.PlanterID.OppositeSide:
                    approach_x = drop_x = self.planter.x
                    drop_y = self.planter.y + self.front_excess + self.blocking_extra_move - self.half_robot_length
                    approach_y = self.planter.y - self.approach_delta - self.half_robot_length
        else:
            match planter_id:
                case artifacts.PlanterID.Top:
                    drop_x = self.planter.x + self.front_excess + self.blocking_extra_move - self.half_robot_length
                    approach_x = self.planter.x - self.approach_delta - self.half_robot_length
                    approach_y = drop_y = self.planter.y
                case artifacts.PlanterID.LocalSide | artifacts.PlanterID.Test:
                    approach_x = drop_x = self.planter.x
                    drop_y = self.planter.y + self.front_excess + self.blocking_extra_move - self.half_robot_length
                    approach_y = self.planter.y - self.approach_delta - self.half_robot_length
                case artifacts.PlanterID.OppositeSide:
                    approach_x = drop_x = self.planter.x
                    drop_y = self.planter.y - self.front_excess - self.blocking_extra_move + self.half_robot_length
                    approach_y = self.planter.y + self.approach_delta + self.half_robot_length

        self.poses.append(
            Pose(
                x=approach_x,
                y=approach_y,
                O=self.planter.O,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=False,
                before_pose_func=self.before_pose1,
            )
        )

        self.poses.append(
            Pose(
                x=drop_x,
                y=drop_y,
                O=self.planter.O,
                max_speed_linear=5,
                max_speed_angular=5,
                allow_reverse=False,
                bypass_anti_blocking=True,
                timeout_ms=0,  # TODO
                before_pose_func=self.before_pose2,
                after_pose_func=self.after_pose2,
            )
        )

        self.poses.append(
            Pose(
                x=approach_x,
                y=approach_y,
                O=self.planter.O,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=True,
            )
        )

    def set_avoidance(self, new_strategy: AvoidanceStrategy):
        self.game_context.avoidance_strategy = new_strategy
        self.planner.shared_properties["avoidance_strategy"] = new_strategy

    async def before_pose1(self):
        self.start_pose = self.planner.pose_current.model_copy()
        self.start_avoidance = self.game_context.avoidance_strategy
        match self.planter.id:
            case artifacts.PlanterID.LocalSide:
                self.game_context.pot_supplies[artifacts.PotSupplyID.LocalTop].enabled = False
            case artifacts.PlanterID.Test:
                self.game_context.pot_supplies[artifacts.PotSupplyID.LocalMiddle].enabled = False
            case artifacts.PlanterID.OppositeSide:
                self.game_context.pot_supplies[artifacts.PotSupplyID.OppositeMiddle].enabled = False

    async def before_pose2(self):
        self.set_avoidance(AvoidanceStrategy.Disabled)
        await actuators.bottom_grip_mid_close(self.planner)

    async def after_pose2(self):
        self.set_avoidance(self.start_avoidance)

        if self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_LEFT].state:
            self.game_context.score += 3  # Plant valid
            self.game_context.score += 1  # Plant in planter
        if self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_RIGHT].state:
            self.game_context.score += 3  # Plant valid
            self.game_context.score += 1  # Plant in planter

        await actuators.top_lift_mid(self.planner)
        await asyncio.sleep(0.7)
        await actuators.top_grip_open(self.planner)
        await asyncio.sleep(0.5)
        await actuators.bottom_grip_mid_open(self.planner)
        await asyncio.sleep(0.1)
        await actuators.bottom_grip_open(self.planner)

        if self.planner.virtual:
            current_pose = self.planner.pose_current.model_copy()
            if self.game_context.camp.color == Camp.Colors.yellow:
                match self.planter.id:
                    case artifacts.PlanterID.Top:
                        current_pose.x = self.planter.x - self.half_robot_length
                    case artifacts.PlanterID.LocalSide | artifacts.PlanterID.Test:
                        current_pose.y = self.planter.y + self.half_robot_length
                    case artifacts.PlanterID.OppositeSide:
                        current_pose.y = self.planter.y - self.half_robot_length
            else:
                match self.planter.id:
                    case artifacts.PlanterID.Top:
                        current_pose.x = self.planter.x - self.half_robot_length
                    case artifacts.PlanterID.LocalSide | artifacts.PlanterID.Test:
                        current_pose.y = self.planter.y - self.half_robot_length
                    case artifacts.PlanterID.OppositeSide:
                        current_pose.y = self.planter.y + self.half_robot_length

            current_pose.O = self.planter.O
            await self.planner.sio_ns.emit("pose_start", current_pose.model_dump())

        if BoolSensorEnum.TOP_GRIP_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_LEFT].state = False
        if BoolSensorEnum.TOP_GRIP_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_RIGHT].state = False

    def weight(self) -> float:
        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state
        ):
            return 0
        if not (
            self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_LEFT].state
            and self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_RIGHT].state
        ):
            return 0
        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state
        ):
            return 0
        match self.planter.id:
            case artifacts.PlanterID.LocalSide:
                if self.game_context.pot_supplies[artifacts.PotSupplyID.LocalTop].count > 0:
                    return 0
            case artifacts.PlanterID.OppositeSide:
                if self.game_context.pot_supplies[artifacts.PotSupplyID.OppositeMiddle].count > 0:
                    return 0

        return 1000000.0

GripAction #

Bases: Action

Action used to grip plants.

Source code in cogip/tools/planner/actions/base_actions.py
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
class GripAction(Action):
    """
    Action used to grip plants.
    """

    def __init__(self, planner: "Planner", actions: Actions, plant_supply_id: artifacts.PlantSupplyID):
        super().__init__("Grip action", planner, actions)
        self.before_action_func = self.before_action
        self.plant_supply = self.game_context.plant_supplies[plant_supply_id]
        self.stop_before_center_1 = 180

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

    async def before_action(self):
        # Compute first pose to get plants using bottom grips
        self.plant_supply.enabled = False
        self.start_pose = self.planner.pose_current.model_copy()
        dist_x = self.plant_supply.x - self.planner.pose_current.x
        dist_y = self.plant_supply.y - self.planner.pose_current.y
        dist = math.hypot(dist_x, dist_y)
        pose = Pose(
            x=self.plant_supply.x - dist_x / dist * self.stop_before_center_1,
            y=self.plant_supply.y - dist_y / dist * self.stop_before_center_1,
            O=0,
            max_speed_linear=30,
            max_speed_angular=30,
            allow_reverse=False,
            bypass_final_orientation=True,
            before_pose_func=self.before_pose1,
            intermediate_pose_func=self.intermediate_pose1,
            after_pose_func=self.after_pose1,
        )
        self.poses.append(pose)

    async def before_pose1(self):
        await actuators.arm_panel_close(self.planner)
        await actuators.bottom_lift_down(self.planner)
        await asyncio.sleep(0.5)
        await actuators.top_lift_down(self.planner)
        await asyncio.sleep(0.5)
        await asyncio.gather(
            actuators.bottom_grip_open(self.planner),
            actuators.top_grip_open(self.planner),
        )

    async def intermediate_pose1(self):
        # Update first pose to take avoidance into account
        dist_x = self.plant_supply.x - self.planner.pose_current.x
        dist_y = self.plant_supply.y - self.planner.pose_current.y
        dist = math.hypot(dist_x, dist_y)
        self.planner.pose_order.x = self.plant_supply.x - dist_x / dist * self.stop_before_center_1
        self.planner.pose_order.y = self.plant_supply.y - dist_y / dist * self.stop_before_center_1
        self.planner.shared_properties["pose_order"] = self.planner.pose_order.path_pose.model_dump(exclude_unset=True)

    async def after_pose1(self):
        await actuators.top_grip_mid_open(self.planner)
        await asyncio.sleep(0.1)
        await actuators.top_grip_mid(self.planner)
        await asyncio.sleep(0.1)
        await actuators.top_grip_mid_close(self.planner)
        await asyncio.sleep(0.1)
        await actuators.top_grip_close(self.planner)
        await asyncio.sleep(0.1)

        if BoolSensorEnum.TOP_GRIP_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_LEFT].state = True
        if BoolSensorEnum.TOP_GRIP_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_RIGHT].state = True

        # Step back
        back_dist = 100
        diff_x = back_dist * math.cos(math.radians(self.planner.pose_current.O))
        diff_y = back_dist * math.sin(math.radians(self.planner.pose_current.O))

        pose = Pose(
            x=self.planner.pose_current.x - diff_x,
            y=self.planner.pose_current.y - diff_y,
            O=0,
            max_speed_linear=66,
            max_speed_angular=66,
            allow_reverse=True,
            bypass_final_orientation=True,
            after_pose_func=self.after_pose2,
        )
        self.poses.append(pose)

    async def after_pose2(self):
        await actuators.top_lift_up(self.planner)
        await asyncio.sleep(0.5)

        # Compute pose to get plants using bottom grips
        forward_dist = 220
        diff_x = forward_dist * math.cos(math.radians(self.planner.pose_current.O))
        diff_y = forward_dist * math.sin(math.radians(self.planner.pose_current.O))

        pose = Pose(
            x=self.planner.pose_current.x + diff_x,
            y=self.planner.pose_current.y + diff_y,
            O=0,
            max_speed_linear=20,
            max_speed_angular=20,
            allow_reverse=False,
            bypass_final_orientation=True,
            after_pose_func=self.after_pose3,
        )
        self.poses.append(pose)

    async def after_pose3(self):
        await actuators.bottom_grip_mid_open(self.planner)
        await asyncio.sleep(0.1)
        await actuators.bottom_grip_mid(self.planner)
        await asyncio.sleep(0.1)
        await actuators.bottom_grip_mid_close(self.planner)
        await asyncio.sleep(0.1)
        await actuators.bottom_grip_close(self.planner)
        await asyncio.sleep(0.1)
        await actuators.bottom_lift_up(self.planner)
        if BoolSensorEnum.BOTTOM_GRIP_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state = True
        if BoolSensorEnum.BOTTOM_GRIP_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state = True

        # Step back
        back_dist = 250
        diff_x = back_dist * math.cos(math.radians(self.planner.pose_current.O))
        diff_y = back_dist * math.sin(math.radians(self.planner.pose_current.O))

        pose = Pose(
            x=self.planner.pose_current.x - diff_x,
            y=self.planner.pose_current.y - diff_y,
            O=0,
            max_speed_linear=66,
            max_speed_angular=66,
            allow_reverse=True,
            bypass_final_orientation=True,
            after_pose_func=self.after_pose4,
        )
        self.poses.append(pose)

    async def after_pose4(self):
        self.plant_supply.enabled = True

    def weight(self) -> float:
        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.TOP_GRIP_RIGHT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state
        ):
            return 0
        return 1000000.0

PotCaptureAction #

Bases: Action

Action used to capture pots using magnets.

Source code in cogip/tools/planner/actions/base_actions.py
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
class PotCaptureAction(Action):
    """
    Action used to capture pots using magnets.
    """

    def __init__(self, planner: "Planner", actions: Actions, pot_supply_id: artifacts.PotSupplyID):
        super().__init__("PotCapture action", planner, actions)
        self.before_action_func = self.before_action
        self.pot_supply = self.game_context.pot_supplies[pot_supply_id]
        self.shift_approach = 335
        self.shift_forward = 160

        match self.pot_supply.angle:
            case -90:
                self.approach_x = self.pot_supply.x
                self.approach_y = -1500 + self.shift_approach
                self.capture_x = self.approach_x
                self.capture_y = self.approach_y - self.shift_forward
            case 90:
                self.approach_x = self.pot_supply.x
                self.approach_y = 1500 - self.shift_approach
                self.capture_x = self.approach_x
                self.capture_y = self.approach_y + self.shift_forward
            case 180:
                self.approach_x = -1000 + self.shift_approach
                self.approach_y = self.pot_supply.y
                self.capture_x = self.approach_x - self.shift_forward
                self.capture_y = self.approach_y

    async def recycle(self):
        await actuators.cart_magnet_off(self.planner)
        await actuators.cart_in(self.planner)
        self.pot_supply.enabled = True
        self.recycled = True

    async def before_action(self):
        self.start_pose = self.planner.pose_current.model_copy()

        pose = Pose(
            x=self.approach_x,
            y=self.approach_y,
            O=self.pot_supply.angle,
            max_speed_linear=66,
            max_speed_angular=66,
            allow_reverse=False,
            before_pose_func=self.before_pose1,
            after_pose_func=self.after_pose1,
        )
        self.poses.append(pose)

        # Capture
        pose = Pose(
            x=self.capture_x,
            y=self.capture_y,
            O=self.pot_supply.angle,
            max_speed_linear=5,
            max_speed_angular=5,
            allow_reverse=False,
            bypass_anti_blocking=True,
            timeout_ms=3000,
            after_pose_func=self.after_pose2,
        )
        self.poses.append(pose)

        # Step-back
        pose = Pose(
            x=self.approach_x,
            y=self.approach_y,
            O=self.pot_supply.angle,
            max_speed_linear=10,
            max_speed_angular=10,
            allow_reverse=True,
            after_pose_func=self.after_pose3,
        )
        self.poses.append(pose)

    async def before_pose1(self):
        await asyncio.gather(
            actuators.bottom_grip_close(self.planner),
            actuators.top_grip_close(self.planner),
        )
        await asyncio.gather(
            actuators.top_lift_up(self.planner),
            actuators.bottom_lift_up(self.planner),
        )

    async def after_pose1(self):
        await actuators.cart_out(self.planner)
        await actuators.cart_magnet_on(self.planner)

        self.pot_supply.enabled = False

    async def after_pose2(self):
        await asyncio.sleep(1)

    async def after_pose3(self):
        self.pot_supply.enabled = True
        self.pot_supply.count -= 2
        if BoolSensorEnum.MAGNET_LEFT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state = True
        if BoolSensorEnum.MAGNET_RIGHT in self.game_context.emulated_actuator_states:
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state = True

    def weight(self) -> float:
        if self.pot_supply.count < 5:
            return 0
        if not (
            self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_LEFT].state
            and self.game_context.bool_sensor_states[BoolSensorEnum.BOTTOM_GRIP_RIGHT].state
        ):
            return 0
        if (
            self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_LEFT].state
            or self.game_context.bool_sensor_states[BoolSensorEnum.MAGNET_RIGHT].state
        ):
            return 0

        return 2000000.0

PushPotAction #

Bases: Action

push pot in front of a planter.

Source code in cogip/tools/planner/actions/base_actions.py
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
class PushPotAction(Action):
    """
    push pot in front of a planter.
    """

    def __init__(self, planner: "Planner", actions: Actions, pot_supply_id: artifacts.PotSupplyID):
        super().__init__("PushPot action", planner, actions)
        self.pot_supply = self.game_context.pot_supplies[pot_supply_id]
        self.after_action_func = self.after_action
        self.half_robot_length = self.game_context.properties.robot_length / 2
        margin_x = 160
        margin_y = 50

        match pot_supply_id:
            case artifacts.PotSupplyID.LocalMiddle:
                approach_x = self.pot_supply.x - margin_x - self.half_robot_length
                approach_y = push_y = -1500 + margin_y + self.half_robot_length
                approach_angle = push_angle = 180
                push_x = approach_x + margin_x * 2 + 30
            case artifacts.PotSupplyID.LocalTop:
                approach_x = self.pot_supply.x + margin_x + self.half_robot_length
                approach_y = push_y = -1500 + margin_y + self.half_robot_length
                approach_angle = push_angle = 0
                push_x = approach_x - margin_x * 2 - 30
            case _:
                logger.warning(f"PushPot not available for pot supply {pot_supply_id}")
                return

        self.poses.append(
            AdaptedPose(
                x=approach_x,
                y=approach_y,
                O=approach_angle,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=True,
            )
        )

        self.poses.append(
            AdaptedPose(
                x=push_x,
                y=push_y,
                O=push_angle,
                max_speed_linear=10,
                max_speed_angular=10,
                allow_reverse=True,
                bypass_final_orientation=True,
                before_pose_func=self.before_pose2,
            )
        )

    async def before_pose2(self):
        self.pot_supply.enabled = False

    async def after_action(self):
        self.pot_supply.count = 0

    async def recycle(self):
        if self.game_context.countdown > 15:
            self.pot_supply.enabled = True
        self.recycled = True

    def weight(self) -> float:
        return 9000000.0

SolarPanelsAction #

Bases: Action

Activate a solar panel group.

Source code in cogip/tools/planner/actions/base_actions.py
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
class SolarPanelsAction(Action):
    """
    Activate a solar panel group.
    """

    def __init__(self, planner: "Planner", actions: Actions, solar_panels_id: artifacts.SolarPanelsID):
        super().__init__("SolarPanels action", planner, actions)
        self.solar_panels = self.game_context.solar_panels[solar_panels_id]
        self.before_action_func = self.before_action
        self.shift_x = -215
        self.shift_y = 285

    async def recycle(self):
        await actuators.arm_panel_close(self.planner)
        self.recycled = True

    async def before_action(self):
        self.start_pose = self.planner.pose_current.model_copy()
        if self.game_context.camp.color == Camp.Colors.blue:
            self.shift_y = -self.shift_y

        if self.solar_panels.id == artifacts.SolarPanelsID.Shared:
            self.game_context.pot_supplies[artifacts.PotSupplyID.LocalBottom].enabled = False
            self.game_context.pot_supplies[artifacts.PotSupplyID.LocalBottom].count = 0
            await asyncio.sleep(0.5)

        # Start pose
        self.pose1 = Pose(
            x=self.solar_panels.x - self.shift_x,
            y=self.solar_panels.y - self.shift_y,
            O=90,
            max_speed_linear=66,
            max_speed_angular=66,
            allow_reverse=True,
            after_pose_func=self.after_pose1,
        )

        self.poses.append(self.pose1)

        # End pose
        self.poses.append(
            Pose(
                x=self.solar_panels.x - self.shift_x,
                y=self.solar_panels.y + self.shift_y,
                O=90,
                max_speed_linear=66,
                max_speed_angular=66,
                allow_reverse=True,
                after_pose_func=self.after_pose2,
            )
        )

        if self.solar_panels.id == artifacts.SolarPanelsID.Shared:
            # Go back to pose1
            self.poses.append(
                Pose(
                    x=self.pose1.x,
                    y=self.pose1.y,
                    O=self.pose1.O,
                    max_speed_linear=66,
                    max_speed_angular=66,
                    allow_reverse=True,
                )
            )

    async def after_pose1(self):
        await actuators.arm_panel_open(self.planner)
        await asyncio.sleep(1)

    async def after_pose2(self):
        await actuators.arm_panel_close(self.planner)
        await asyncio.sleep(0.5)
        self.game_context.score += 15

    def weight(self) -> float:
        return 2000000.0