Skip to content

camera_calibration

CameraCalibrationAction #

Bases: Action

This action moves around the front right table marker, and take pictures to compute camera extrinsic parameters (ie, the position of the camera relative to the robot center).

Source code in cogip/tools/planner/actions/camera_calibration.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
class CameraCalibrationAction(Action):
    """
    This action moves around the front right table marker, and take pictures to compute
    camera extrinsic parameters (ie, the position of the camera relative to the robot center).
    """

    def __init__(self, planner: "Planner", actions: Actions):
        super().__init__("CameraCalibration action", planner, actions)
        self.camera_positions: list[Vertex] = []
        self.after_action_func = self.print_camera_positions

        self.poses.append(
            Pose(
                x=-220,
                y=-(1500 - 450 + self.game_context.properties.robot_width / 2),
                O=90,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-220,
                y=-800,
                O=160,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-220,
                y=-540,
                O=-160,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-260,
                y=-320,
                O=-130,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-500,
                y=-320,
                O=-90,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-710,
                y=-460,
                O=-70,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-810,
                y=-760,
                O=0,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-(1000 - 450 + self.game_context.properties.robot_width / 2),
                y=-(1500 - 450 + self.game_context.properties.robot_width / 2),
                O=90,
                max_speed_linear=66,
                max_speed_angular=66,
                after_pose_func=self.calibrate_camera,
            )
        )

    async def calibrate_camera(self):
        await asyncio.sleep(1)
        if pose := await calibrate_camera(self.planner):
            self.camera_positions.append(pose)
        await asyncio.sleep(0.5)

    async def print_camera_positions(self):
        x = 0
        y = 0
        z = 0
        for i, p in enumerate(self.camera_positions):
            logger.info(f"Camera position {i: 2d}: X={p.x:.0f} Y={p.y:.0f} Z={p.z:.0f}")
            x += p.x
            y += p.y
            z += p.z

        if n := len(self.camera_positions):
            p = Vertex(x=x / n, y=y / n, z=z / n)
            logger.info(f"=> Camera position mean: X={p.x:.0f} Y={p.y:.0f} Z={p.z:.0f}")
        else:
            logger.warning("No camera position found")

    def weight(self) -> float:
        return 1000000.0