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
 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
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", strategy: Strategy):
        super().__init__("CameraCalibration action", planner, strategy)
        self.camera_positions: list[CameraExtrinsicParameters] = []
        self.before_action_func = self.before_action
        self.after_action_func = self.after_action
        self.linear_speed = 20
        self.angular_speed = 20

        self.poses.append(
            Pose(
                x=-220,
                y=-1220,
                O=120,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-240,
                y=-570,
                O=-120,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-240,
                y=-440,
                O=-120,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-500,
                y=-420,
                O=-80,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-610,
                y=-510,
                O=-70,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-750,
                y=-910,
                O=0,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-610,
                y=-1250,
                O=60,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

        self.poses.append(
            Pose(
                x=-400,
                y=-1250,
                O=90,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
                after_pose_func=self.calibrate_camera,
            )
        )

    async def before_action(self):
        self.disable_fixed_obstacles_backup = self.planner.shared_properties.disable_fixed_obstacles
        self.planner.shared_properties.disable_fixed_obstacles = True
        await actuators.front_arms_open(self.planner)
        await actuators.front_lift_mid(self.planner)
        await actuators.back_arms_open(self.planner)
        await actuators.back_lift_mid(self.planner)
        start_position = self.planner.start_positions.get()
        self.poses.append(
            Pose(
                x=start_position.x,
                y=start_position.y,
                O=start_position.O,
                max_speed_linear=self.linear_speed,
                max_speed_angular=self.angular_speed,
            )
        )

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

    async def after_action(self):
        self.planner.shared_properties.disable_fixed_obstacles = self.disable_fixed_obstacles_backup
        await actuators.front_arms_close(self.planner)
        await actuators.front_lift_down(self.planner)
        await actuators.back_arms_close(self.planner)
        await actuators.back_lift_down(self.planner)

        x = 0
        y = 0
        z = 0
        roll = 0
        pitch = 0
        yaw = 0
        for i, p in enumerate(self.camera_positions):
            self.logger.info(
                f"Camera position {i: 2d}: X={p.x:.0f} Y={p.y:.0f} Z={p.z:.0f}"
                f" Roll={p.roll:.0f} Pitch={p.pitch:.0f} Yaw={p.yaw:.0f}"
            )
            x += p.x
            y += p.y
            z += p.z
            roll += p.roll
            pitch += p.pitch
            yaw += p.yaw

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

    def weight(self) -> float:
        return 1000000.0