*3.2. Ball Trajectory Estimation*

Considering the limitations of the joint speed, the robot arm should be able to move in advance by estimating the trajectory of the ball. To estimate the trajectory of the ball, we used weighted least-squares regression [27]. Each time a new ball position and time is acquired by the vision sensor, the trajectory of the ball is updated using weighted least-squares regression.

#### 3.2.1. Least Square Regression

The *x* and *y* planes of the robot coordinate system are horizontal planes and the *z* direction is perpendicular to the x-y plane. A linear function was used to estimate the horizontal trajectory of the ball, and a quadratic function was used to estimate the vertical trajectory. Because the ball moves in a parabolic trajectory, the *z*-axis trajectory of the ball is fitted as a quadratic function, unlike other axes. The trajectory function for each of the *x*, *y*, and *z* axes of the ball is given by

$$\begin{cases} \quad \begin{cases} \quad x = a\_1 t + b\_1 \\ y = a\_2 t + b\_2 \\ z = a\_3 t^2 + b\_3 t + c \end{cases} \end{cases} \tag{5}$$

Whenever three-dimensional position data are newly measured in the stereo vision sensor, the coefficient of the trajectory function (5) is newly calculated through least square regression [28]. The position data sampling interval of the ball is 20 ms, which is the image acquisition period of the stereo vision sensor. Since the flight time of the ball is 0.5 s on average, approximately 15 to 20 ball position data points are measured. Figure 7 represents the estimated trajectory of the ball according to the number of data points using least squares regression. As the number of data points increases, it can be seen that the estimated trajectory converges to one trajectory.

**Figure 7.** Estimated ball trajectory according to the number of sampled data points. As the number of sampled data points increases, the trajectory estimation becomes more accurate.

#### 3.2.2. Weighted Least Square Regression

Least square regression was applied to the ball trajectory estimation regression described in Section 3.2.1, based on the assumption that the data measured by the stereo vision sensor are accurate. However, the accuracy of the position data measured from the stereo vision sensor is inversely proportional to the distance between the stereo vision sensor and the object. This is because the closer the distance, the greater is the number of pixels representing the object. Figure 8 shows a graph of position accuracy versus distance between Bumblebee2 and the object. Therefore, it is more appropriate to apply the weighted least-squares regression than the least-squares regression considering the accuracy of the measured value according to the distance.

**Figure 8.** Depth error vs. distance. Depth accuracy of stereo vision sensor according to distance.

Least squares regression minimizes the sum of the squares of the residuals, and the weighted least-squares regression minimizes the sums (Q) of the squares of the residuals (*yi* − *bxi*) multiplied by the weight (*wi*); it is given by

$$\mathbf{Q} = \sum\_{i=1}^{n} w\_i (y\_i - bx\_i)^2,\text{ where } w\_i = w\_{i+1}\lambda \tag{6}$$

The weights are calculated as a geometric sequence based on the last weight. In several tests, 0.8 was used as λ of the weighted least square. Figure 9 is a graph showing the RMS errors for the X, Y, and Z components obtained by applying the weighted least square and least square regressions. Although there is almost no difference in the RMS error for X and Y with and without the weighted least square, the RMS error decreases rapidly with weighted least square for the Z axis. This result is expected because the change in the trajectory of the ball on the z axis is larger than that on the x and y axes.

**Figure 9.** RMS error vs. sampling data number about the *x* axis. RMS error of the estimated ball trajectory with and without weighted least squares method. RMS error for each axis is displayed in the order of X axis, Y axis, and Z axis from the left.

### *3.3. Motion Control of the Robot Arm*

The trajectory of the robot arm consists of two stages: before and after batting. The speed of the end-effector of the robot arm increases before batting, and after batting, a trajectory is generated that slows down the increased speed of the end-effector. The robotic arm used in the experiment has six joints, and, therefore, we need to generate the trajectories of the six joints. For convenience, the joint trajectory generation method is described by the notation of one joint, and the same method is applied to the remaining joints. The trajectory generation method before batting is described first, followed by the trajectory generation method after batting.

The cubic spline interpolation function for generating the motion trajectory of a joint is:

$$f(\mathbf{t}) = \sum\_{i=0}^{3} a\_i \mathbf{t}^i,\tag{7}$$

where *ai* is the coefficient of the trajectory function and t is the time. Since coefficients *ai* in Equation (7) are four, four constraints are needed to obtain each coefficient. The constraints of the trajectory function before hitting the ball are as follow:

$$f(0) = \theta\_{0\prime} \, f(t\_h) = \theta\_{h\prime} \, \dot{f}(0) = 0 \, \dot{f}(t\_h) = u\_h \tag{8}$$
