Materials:
- (1) Robotic arm with gripper.
- (1) Web cam.
- (1) Arduino One.
- (1) Board of 3 x 3 squares.
- Red and white pieces.
The web cam is for the image capture of the board. The image is analyzed by a visual basic program and using the minimax algorithm decides where the computer must place it´s piece. The program sends commands to the arduino one and this moves the robotic arm to the chosen position by the algorithm.
1- THE ROBOTIC ARM
I bought the robotic arm here but you can build your own robot.
This robotic arm contains of 3 servos S3003, 1 for the base, 1 for the shoulder and 1 for the wrist. Also uses a servo sg90 for the gripper.
It is controlled by the Arduino One which is commanded by a visual basic program.
2 - THE DIAGRAM
As shown in the first step, 4 servos are connected to the pins 5,6,9 and 10 of the arduino one.
3 - THE IMAGE CAPTURE
The web cam captures the image of the board and through a procedure in the visual basic program (you have to install the web cam library) , the image is divided into nine squares and each square is traveled for determining if contains red pixels. If so, the pixel is marked with red and later if the amount of points red of the square is greater than 60, then there is a red piece in that square and is interpreted with a "X", which is the movement of the human player.
4 - THE LOGIC OF THE GAME The visual basic program determines the position of the computer thanks to an algorithm called miniMax, which is an algorithm used in games of two players and helps to decide the best movement for the computer turn. Click here for downloading the full code in visual basic of the project. Note: the source code of the minimax algorithm used in the program was written by Ralph Main, you can see the article here.
5 - THE MOVEMENT OF THE ARM When the Artificial Intelligence of the minimax algorithm decides in which position the piece must be placed, it sends a string with the number of that position to the arduino through the serial port. The arduino receives the data and according to the position calls to the function that contains the movements in angles for moving the robotic arm. Example of code for moving the arm to the second square:
if (dato=='2')
{ segundaPosicion() } ...... //Move the base for(i=180;i>=88;i--)
{ base.write(i); delay(20); }
delay(200);
//Moves the wrist to 90
for(i=114;i>=90;i--)
{
codo.write(i);
delay(20);
}
delay(200);
//Moves the shoulder to 160
for(i=140;i<=170;i++)
{
hombro.write(i);
delay(20);
}
delay(200);
//Moves the wrist for(i=90;i<=102;i++)
{ codo.write(i); delay(20);
}
delay(200);
//Open the gripper for(i=110;i>=90;i--)
{
pinza.write(i);
delay(20);
}
delay(200);
//Lifts the shoulder
for(i=170;i>=140;i--)
{
hombro.write(i);
delay(20);
}
delay(200);
posicionInicial(); //Moves to its initial position }
Download the full Arduino code here. Thanks for watching, comments, suggestions, please let me know it.
Comments