Time has come to fight a worthy opponent, Ned2, at Tic-Tac-Toe!
Introduction
This application uses our PyNiryo API, the Ned2 robot, and the Vision Set. Image processing enables the robot to detect every object on the workspace, used as the game board. The AI makes Ned2 a fearsome opponent, depending on the difficulty level chosen at the beginning of each game.
Click here to download the code
Requirements
- Ned2
- The Vacuum Pump (recommended) or any gripper
- 5 pawns for the player and 5 for the robot, in different colors (red, blue, or green)
- The Vision Set and workspace
- Optional: The slope from the Conveyor Belt
You can also customize your own workspace. (Provide printable template)
Set-Up
Setting up Ned2
When you start tictactoe_gui.py
, you will be able to change different settings.
- First, choose the robot to connect to by specifying its IP address.
- Click "Connect".
In the settings menu, configure the following options:
- Robot Color: The color the robot will play.
- Human Color: The color the human will play.
- AI Difficulty: Integer between 0 and 1000 (0 = random moves, 1000 = unbeatable).
- Turn End Mode:
- Learning Mode: move the robot to trigger its turn.
- Button Press: If using a button connected to the robot's I/O.
- Use Slope: Indicate if the slope is used for pawn storage. If no slope is used , the stored pawns will be placed on the board directly.
- Loop Mode: Determines if the game restarts automatically.
- Workspace Name: Default is
tictactoe_ws
. Calibrate the vision workspace using NiryoStudio. - Setting Important Robot Poses
In the settings menu of the application, you are asked to manually define and save several robot positions. These are crucial for proper and efficient gameplay. Here's what each of them means and how to configure them:
- Set Sleep Joints:
Move the robot to a safe and neutral position where it can rest between games or after completing its actions. This is typically a retracted pose, where no parts of the robot arm interfere with the workspace or other equipment. Once the robot is in the desired position, click the “Set sleep_joints” button to save it. - Set Approach Slope Joints:
If you are using the optional slope, move the robot to a position just before it reaches the slope – not touching it yet. This is usually an intermediate pose from where the robot can safely approach and drop pawns onto the slope. Press the “Set approach_slope_joints” button to record this position. - Set slop_joints :
This is the position where the robot can actually pick the object from the slope. - Set cheat_joints :
The pose in which the robot will go when he think you are cheating. - Set Observation Pose:
The robot enter in learning mode and display the camera feed. This is the key vision position from which the robot uses the Vision Set to scan the workspace and identify pawns and landmarks. You’ll need to manually move the robot to a position where its camera has a clear and centered view of all four landmarks on the board. When satisfied with the view, press any key on your keyboard. This pose ensures accurate detection of game state. - Set Store Robot Pawn Pose:
Move the robot to the place where it will store or pick its own pawns before making a move. This spot must be accessible and distinct from the game board area. After positioning the arm correctly over the robot pawn storage, press the “Set store_robot_pawn_pose” button. - Set Store Human Pawn Pose:
Similar to the robot pawn position, this defines where the human player’s pawns will be stored or placed by the robot (especially if it handles cleaning or repositioning at the end of a game). Move the robot to this location and press the “Set store_human_pawn_pose” button to record it.
- Set Sleep Joints:
Important: Firmly attach the robot, slope, and workspace.
How It Works
The Vision Set captures an image of the workspace. Using image processing, the pawns’ positions are identified and mapped to a 3x3 matrix for the game board. Based on the difficulty setting, the AI selects a move and commands the robot to place a pawn accordingly.
The application consists of several Python files:
tictactoe_gui.py
: Manages the GUI and game loop.game.py
: Implements theTicTacToe
class and game logic.robot_player.py
: Manages robot actions with theRobotPlayer
class.object_detection.py
: Handles object detection and image display.
Default settings:
- 1 game
- Notify robot of your move by manually moving a joint
- AI difficulty set to 500
- Using slope
Other Features
- AI detects cheating and reacts with an animation before playing.
- Adjustable difficulty: from unbeatable to random.
- Automatic workspace cleaning before each game.
- Live display of image processing when analyzing the board.
- Small animations based on game results.
The Game
If Using the Slope
Place 5 robot pawns on the slope and begin the game. The entire board will be used.
If Not Using the Slope
Place 5 robot pawns on the side of the workspace, then launch the game.
The left section holds the robot's pawns, while the top-right 3x3 grid is the main game board.
The GUI will show:
- The robot’s last view of the workspace
- Grids with the detected positions of blue and green pawns
If You Start the Game
- Place a pawn anywhere on the board.
- Indicate your turn is over (shake or button press).
- Ned2 scans and plays its turn.
- Repeat until the game ends.
If Ned2 Starts the Game
- Trigger its move (shake or button press).
- Ned2 scans and plays.
- You respond, and the loop continues until the game ends.