Play against artificial intelligence! myCobot 280 Open Source Six-Axis Robotic Arm Connect 4 Connect 4 Game Part 2

foreword

In the last article, we discussed how to create a game brain capable of Connect4. It briefly introduces several game algorithms , such as minimax algorithm, Alpha-Beta pruning algorithm, etc. The most important ones are the most popular neural network algorithms and deep learning . The neural network algorithm allows the computer to have a brain that can think like a human being, and set up a unique scene to learn to play chess. In this article, we will further explore how to make the robotic arm realize the chess-playing action and realize the idea. (In other words, AI robotic arm plays chess)

If you are interested, please watch the previous article.

Introduction

The following content is mainly divided into four parts for introduction.

● 获取信息:用摄像头获取到棋盘上的信息,进行对弈


            ● 处理信息:处理获取到的信息识别出棋子的位置,通过对弈算法,计算出下一步棋子应该在哪里下


            ● 机械臂的轨迹:设计机械臂如何抓取棋子,设计放置棋子的路径


            ● 功能的整合:将上面三个功能结合在一起,实现AI机械臂下棋。

copy

Let's take a look~

project

getting information

Environment: python , the latest version of opencv, numpy

First of all, it is necessary to obtain the information of the chessboard, which includes the chessboard and the pieces on the chessboard. We use cv2.aruco.ArucoDetector(dictionary, para ram ete rs ) in OpenCV

This method is used to detect the Aruco QR code, mark the position of Aruco from the picture, and calculate the position and attitude information of the mark. In this way, the position of the entire chessboard can be determined, and the positions of the four corners can be determined.

Code: The code uses the cv2.aruco.ArucoDetector(dictionary, parameters) method to determine the position of our board.

dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
        parameters = cv2.aruco.DetectorParameters()
        detector = cv2.aruco.ArucoDetector(dictionary, parameters)

        corners, ids, rejectedCandidates = detector.detectMarkers(bgr_data)
        rvec, tvec, _ = cv2.aruco.estimatePoseSingleMarkers(corners, 0.05, self.mtx, self.dist)

        if rvec is None or len(corners) != 4:
            return None

# debug
        if DEBUG:
            debug_img = bgr_data.copy()
            for i in range(rvec.shape[0]):
                cv2.drawFrameAxes(debug_img, self.mtx, self.dist, rvec[i, :, :, ], tvec[i, :, :, ],
                                  0.03)
# Draw a square around the marker.
                cv2.aruco.drawDetectedMarkers(debug_img, corners)
            cv2.imshow("debug1", debug_img)

# Sort the detected QR code corner points in the following order: top left, top right, bottom left, bottom right.
        corners = np.mean(corners, axis=2)
        corners = (np.ceil(corners)).astype(int)
        corners = corners.reshape((4, 2))
        cx, cy = (np.mean(corners[:, 0]), np.mean(corners[:, 1]))
        res: list = [None for _ in range(4)]
        for x, y in corners:
            if x < cx and y < cy:
                res[0] = (x, y)
            elif x > cx and y < cy:
                res[1] = (x, y)
            elif x < cx and y > cy:
                res[2] = (x, y)
            else:
                res[3] = (x, y)
        res = np.array(res)

## debug code
        if DEBUG:
            debug_img = bgr_data.copy()
            for p in res:
                cv2.circle(debug_img, p, 3, BGR_GREEN, -1)
            cv2.imshow("aruco", debug_img)

        return res

copy

After determining the chessboard, we use different colors as chess pieces. Here we use two colors with relatively high discrimination, red and yellow, and mark them out.

Set up a logic, when there is one more chess piece on the board, the data of the current chessboard will be returned to the game algorithm to judge how the next move should be.

processing information

Next, we need to process the information of the chessboard.

From the above, we can see that we have obtained the data of the chessboard, and then we need to pass the data to the game algorithm, so that the game algorithm can predict the position of the next chess piece.

Here is the pseudocode for the processing:

functionmodel_predict(state, available_actions):
# 将available_actions转换为numpy数组
    available_actions = np.array(available_actions)
# 对state进行扩展,以适应ONNX模型的输入要求
    state = np.expand_dims(np.expand_dims(np.array(state, dtype=np.float32), axis=0), axis=0)
# 构建ONNX模型的输入
    ort_inputs ={self.policy_net.get_inputs()[0].name: state}
# 进行模型预测,获取每个可用位置的预测值
    r_actions =self.policy_net.run(None, ort_inputs)[0][0,:]
# 根据预测值选择最优的落子位置
    state_action_values = np.array(
[r_actions[action]for action in available_actions])
    argmax_action = np.argmax(state_action_values)
    greedy_action = available_actions[argmax_action]
return greedy_action

copy

The main logic in this method is to use the ONNX model for model prediction, and select the optimal placement position according to the prediction results. First, the available position available_actions is converted to a numpy array, and the current game state state is expanded to fit the input requirements of the ONNX model. Then, pass the expanded state to the ONNX model for prediction, and save the prediction result in the r_actions variable. Next, calculate the predicted value of each available position according to the prediction result and the available position, select the largest corresponding position as the optimal position, and return it.

Trajectory of the robotic arm

The brain (playing algorithm) and eyes (recognition algorithm) are all there, and now only one hand is needed to perform the action. We use the python library pymycobot to control the robotic arm. Because of the chessboard, the chess pieces can only be dropped from the top of the chessboard. We set a coordinate point for each sequence of chess pieces to complete the path planning of the robotic arm. Because the chess surface is relatively clean and has no obstructions, there is no need to consider too many path factors.

The following is the pseudocode of the position of the robot arm’s trajectory:

# 初始化定义几个坐标点
# 设定一个长度为7的列表
self.chess_table = [None for _ in range(7)]
self.chess_table[0]-[6] = [J1,J2,J3,J4,J5,J6] # 七个棋格的位置

self.angle_table = {
    "recovery": [0, 0, 0, 0, 0, 0], #初始位置
    "observe": [-2.54, 135.0, -122.95, -32.34, 2.46, -90.35],#观察位置
    "stack-hover-1": [-47.19, -37.96, -58.53, 1.05, -1.93, -1.84],#吸取棋子的位置
        }

copy

Next, introduce some methods of controlling the robotic arm in pymycobot:

#Sending the angle to the robotic arm.
self.send_angles(self.chess_table[n], ARM_SPEED)

#Sending the Cartesian coordinates to the robotic arm.
self.send_coords(coord_list,ARM_SPEED)

copy

Integration of functions

Before integrating the function points, we have to sort out the logic between them.

With the flow chart of playing chess, the next step is to combine the function points together.

This is the structure file of the program.

├── Agent.py The logic of the robotic arm's gameplay.

├── ArmCamera.pyCamera control.

├── ArmInterface.py Robotics Arm control

├── Board.py Data structure of the chessboard and related judgments.

├── CameraDemo.py Small program for testing camera performance.

├── config.pyUtilized to define specific configuration details.

├── Detection.py Machine vision recognition.

├── dqn.ptNeural network model file, used to implement gameplay logic.

├── main.pymain program.

├── StateMachine.pyA state machine.

Summarize

In theory, almost no one can win. Because the machine's algorithm can predict how many moves or more will be played in the future, and for ordinary people, generally only two or three moves can be predicted. But judging from the video , the AI ​​only won one game, and the one it lost was because of structural factors, the position of the chessboard that should have been played was forced to change.

Do you find this project interesting? We will improve the Connect4 suit in the future and put it on our website. Friends who are interested can pay attention to us, and we will update it later.

Will you try to use the robotic arm to achieve other chess skills? For example, chess, Chinese chess, etc., the algorithms used by different chess skills will be very different, welcome everyone to leave a message with us in the underground to share your ideas.

Guess you like

Origin blog.csdn.net/m0_71627844/article/details/131511954