Skip to content

← Back to Home

Hardware Control

Robot control systems for movement, positioning, and piece manipulation.

Related Components: Game Engine | Vision System | Communication | UI

Overview

The Hardware Control component is responsible for:

  • Pathfinding & Trajectory Planning - Calculate optimal robot paths from as grid coordinates
  • Servo Control - Manage piece picking and placement

Key Features

  • Intelligent Pathfinding - Efficient path calculation avoiding collisions using A* algorithm
  • Servo Management - Automated piece manipulation with magnet control

Architecture

Hardware Control
├── Pathfinding Engine
│   ├── Start Position
│   ├── Destination Position
│   └── Path Generation
└── Movement Commands
    ├── Grid Positions
    └── Servo Operations

Coordinate Systems

Chess Board Coordinates

  • Columns: A-H (Left to Right)
  • Rows: 1-8 (Bottom to Top)
  • Example: e4 (King's Pawn opening)

Grid Coordinates

  • Grid double the size of chess board (16x16), so the pieces can be moved in between the squares. (0,0) represents the bottom left corner
  • Calibration required to the bottom left corner for accurate mapping

Usage Example

from Control import Control
import chess

# Initialize control system
control = Control()

# Home the robot
control.go_home()

# Get path for a move
move = chess.Move.from_uci('e2e4')
path = control.get_path(move, board_state)

Module Documentation

Control Module

The core control system for robot movement and pathfinding.

Command

Represents a movement command with position and magnet state.

Source code in python/Control.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Command:
    """Represents a movement command with position and magnet state."""
    position: Position
    magnet_state: bool

    def __init__(self, position: Position = Position(0.0, 0.0), magnet_state: bool = False):
        """Initialize command with position and magnet state.

        Args:
            position (Position): Target position for movement.
            magnet_state (bool): Magnet state (True for on, False for off).

        Return:
            None
        """
        self.position = position
        self.magnet_state = magnet_state

__init__(position=Position(0.0, 0.0), magnet_state=False)

Initialize command with position and magnet state.

Parameters:

Name Type Description Default
position Position

Target position for movement.

Position(0.0, 0.0)
magnet_state bool

Magnet state (True for on, False for off).

False
Return

None

Source code in python/Control.py
54
55
56
57
58
59
60
61
62
63
64
65
def __init__(self, position: Position = Position(0.0, 0.0), magnet_state: bool = False):
    """Initialize command with position and magnet state.

    Args:
        position (Position): Target position for movement.
        magnet_state (bool): Magnet state (True for on, False for off).

    Return:
        None
    """
    self.position = position
    self.magnet_state = magnet_state

Control

Physical control system for chess board movement and pathfinding.

Source code in python/Control.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
class Control:
    """Physical control system for chess board movement and pathfinding."""
    grid: Grid

    def __init__(self):
        """Initialize control system with 8x8 grid for pathfinding.

        Args:
            None

        Return:
            None
        """
        self.grid = Grid(8, 8)
        self.grid.initialize_links()

    def update_board_state(self, boardState: str):
        """Update grid obstacles from board state.

        Args:
            boardState (str): FEN notation of board state.

        Return:
            None
        """
        self.grid.update_obstacles(boardState)

    def get_path(self, move: chess.Move, board: chess.Board) -> list[Command]:
        """Generate movement commands for a chess move using pathfinding.

        Args:
            move (chess.Move): Chess move to generate path for.
            board (chess.Board): Current board state.

        Return:
            list[Command]: List of commands with positions and magnet states to execute move.
        """
        start_x = chess.square_file(move.from_square) + 1
        start_y = chess.square_rank(move.from_square) + 1
        end_x = chess.square_file(move.to_square) + 1
        end_y = chess.square_rank(move.to_square) + 1

        # Pathfinding expects 1-based board coordinates to align with 0.5 grid offsets
        start_pos = Position(start_x, start_y)
        end_pos = Position(end_x, end_y)

        path_to_obstacle_removal = []
        if board.is_capture(move):
            if board.is_en_passant(move): # Handle en passant by adding an additional command to remove the captured pawn
                # For en passant, also need to remove obstacle at captured pawn's position
                print("En passant capture detected, planning path to captured pawn removal point.")
                captured_pawn_square = chess.square(chess.square_file(move.to_square), chess.square_rank(move.from_square))
                captured_pawn_pos = Position(chess.square_file(captured_pawn_square) + 1, chess.square_rank(captured_pawn_square) + 1)

                self.grid.remove_obstacle(captured_pawn_pos)
                path_to_obstacle_removal = self.grid.a_star(captured_pawn_pos, self.grid.obstacle_remove_position)
            else:
                print("Obstacle detected at end position, planning path to obstacle removal point.")
                self.grid.remove_obstacle(end_pos)
                path_to_obstacle_removal = self.grid.a_star(end_pos, self.grid.obstacle_remove_position)
        self.grid.remove_obstacle(start_pos)  # Ensure start position is not an obstacle
        path = self.grid.a_star(start_pos, end_pos)
        full_path = path_to_obstacle_removal + path


        # Convert path to commands with magnet states
        commands = []
        for i, pos in enumerate(full_path):
            if i == 0:
                # Turn magnet on at start position
                commands.append(Command(pos, True))
            elif i == len(path_to_obstacle_removal) - 1 or i == len(full_path) - 1:
                # Turn magnet off at end position
                commands.append(Command(pos, False))
            else:
                # Keep magnet on during movement
                commands.append(Command(pos, True))


        # Handle castling by adding additional commands for rook movement
        if board.is_castling(move):
            # Update obstacle before calculating rook path to ensure it accounts for king's new position
            self.grid.add_obstacle(end_pos)  # Temporarily add obstacle back to calculate rook path correctly

            # Handle castling by moving the king first, then the rook
            if board.turn == chess.WHITE:
                if board.is_kingside_castling(move):
                    commands = commands + self.get_path(chess.Move.from_uci("h1f1"), board)
                else:
                    commands = commands + self.get_path(chess.Move.from_uci("a1d1"), board)
            else:
                if board.is_kingside_castling(move):
                    commands = commands + self.get_path(chess.Move.from_uci("h8f8"), board)
                else:
                    commands = commands + self.get_path(chess.Move.from_uci("a8d8"), board)

        output = self.optimize_path(commands)

        return output


    def optimize_path(self, path: list[Command]) -> list[Command]:
        """Remove unnecessary waypoints from path by eliminating collinear points.

        Args:
            path (list[Command]): Input path with all waypoints.

        Return:
            list[Command]: Optimized path with collinear points removed.
        """
        if not path:
            return []

        optimized_path = [path[0]]
        for i in range(1, len(path) - 1):
            prev_cmd = optimized_path[-1]
            curr_cmd = path[i]
            next_cmd = path[i + 1]

            vec1 = (curr_cmd.position.x - prev_cmd.position.x, curr_cmd.position.y - prev_cmd.position.y)
            vec2 = (next_cmd.position.x - curr_cmd.position.x, next_cmd.position.y - curr_cmd.position.y)

            # Check if the direction is the same (collinear)
            if vec1[0] * vec2[1] != vec1[1] * vec2[0] or curr_cmd.magnet_state != prev_cmd.magnet_state:
                optimized_path.append(curr_cmd)

        optimized_path.append(path[-1])  # Always include the last command
        return optimized_path

    def print_path(self, path: list[Command]):
        """Print path waypoints for debugging purposes.

        Args:
            path (list[Command]): Path to print.

        Return:
            None
        """
        for cmd in path:
            pos = cmd.position
            print(f"({pos.x}, {pos.y})", end=" -> ")
        print("END")    

__init__()

Initialize control system with 8x8 grid for pathfinding.

Return

None

Source code in python/Control.py
329
330
331
332
333
334
335
336
337
338
339
def __init__(self):
    """Initialize control system with 8x8 grid for pathfinding.

    Args:
        None

    Return:
        None
    """
    self.grid = Grid(8, 8)
    self.grid.initialize_links()

get_path(move, board)

Generate movement commands for a chess move using pathfinding.

Parameters:

Name Type Description Default
move Move

Chess move to generate path for.

required
board Board

Current board state.

required
Return

list[Command]: List of commands with positions and magnet states to execute move.

Source code in python/Control.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
def get_path(self, move: chess.Move, board: chess.Board) -> list[Command]:
    """Generate movement commands for a chess move using pathfinding.

    Args:
        move (chess.Move): Chess move to generate path for.
        board (chess.Board): Current board state.

    Return:
        list[Command]: List of commands with positions and magnet states to execute move.
    """
    start_x = chess.square_file(move.from_square) + 1
    start_y = chess.square_rank(move.from_square) + 1
    end_x = chess.square_file(move.to_square) + 1
    end_y = chess.square_rank(move.to_square) + 1

    # Pathfinding expects 1-based board coordinates to align with 0.5 grid offsets
    start_pos = Position(start_x, start_y)
    end_pos = Position(end_x, end_y)

    path_to_obstacle_removal = []
    if board.is_capture(move):
        if board.is_en_passant(move): # Handle en passant by adding an additional command to remove the captured pawn
            # For en passant, also need to remove obstacle at captured pawn's position
            print("En passant capture detected, planning path to captured pawn removal point.")
            captured_pawn_square = chess.square(chess.square_file(move.to_square), chess.square_rank(move.from_square))
            captured_pawn_pos = Position(chess.square_file(captured_pawn_square) + 1, chess.square_rank(captured_pawn_square) + 1)

            self.grid.remove_obstacle(captured_pawn_pos)
            path_to_obstacle_removal = self.grid.a_star(captured_pawn_pos, self.grid.obstacle_remove_position)
        else:
            print("Obstacle detected at end position, planning path to obstacle removal point.")
            self.grid.remove_obstacle(end_pos)
            path_to_obstacle_removal = self.grid.a_star(end_pos, self.grid.obstacle_remove_position)
    self.grid.remove_obstacle(start_pos)  # Ensure start position is not an obstacle
    path = self.grid.a_star(start_pos, end_pos)
    full_path = path_to_obstacle_removal + path


    # Convert path to commands with magnet states
    commands = []
    for i, pos in enumerate(full_path):
        if i == 0:
            # Turn magnet on at start position
            commands.append(Command(pos, True))
        elif i == len(path_to_obstacle_removal) - 1 or i == len(full_path) - 1:
            # Turn magnet off at end position
            commands.append(Command(pos, False))
        else:
            # Keep magnet on during movement
            commands.append(Command(pos, True))


    # Handle castling by adding additional commands for rook movement
    if board.is_castling(move):
        # Update obstacle before calculating rook path to ensure it accounts for king's new position
        self.grid.add_obstacle(end_pos)  # Temporarily add obstacle back to calculate rook path correctly

        # Handle castling by moving the king first, then the rook
        if board.turn == chess.WHITE:
            if board.is_kingside_castling(move):
                commands = commands + self.get_path(chess.Move.from_uci("h1f1"), board)
            else:
                commands = commands + self.get_path(chess.Move.from_uci("a1d1"), board)
        else:
            if board.is_kingside_castling(move):
                commands = commands + self.get_path(chess.Move.from_uci("h8f8"), board)
            else:
                commands = commands + self.get_path(chess.Move.from_uci("a8d8"), board)

    output = self.optimize_path(commands)

    return output

optimize_path(path)

Remove unnecessary waypoints from path by eliminating collinear points.

Parameters:

Name Type Description Default
path list[Command]

Input path with all waypoints.

required
Return

list[Command]: Optimized path with collinear points removed.

Source code in python/Control.py
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def optimize_path(self, path: list[Command]) -> list[Command]:
    """Remove unnecessary waypoints from path by eliminating collinear points.

    Args:
        path (list[Command]): Input path with all waypoints.

    Return:
        list[Command]: Optimized path with collinear points removed.
    """
    if not path:
        return []

    optimized_path = [path[0]]
    for i in range(1, len(path) - 1):
        prev_cmd = optimized_path[-1]
        curr_cmd = path[i]
        next_cmd = path[i + 1]

        vec1 = (curr_cmd.position.x - prev_cmd.position.x, curr_cmd.position.y - prev_cmd.position.y)
        vec2 = (next_cmd.position.x - curr_cmd.position.x, next_cmd.position.y - curr_cmd.position.y)

        # Check if the direction is the same (collinear)
        if vec1[0] * vec2[1] != vec1[1] * vec2[0] or curr_cmd.magnet_state != prev_cmd.magnet_state:
            optimized_path.append(curr_cmd)

    optimized_path.append(path[-1])  # Always include the last command
    return optimized_path

print_path(path)

Print path waypoints for debugging purposes.

Parameters:

Name Type Description Default
path list[Command]

Path to print.

required
Return

None

Source code in python/Control.py
454
455
456
457
458
459
460
461
462
463
464
465
466
def print_path(self, path: list[Command]):
    """Print path waypoints for debugging purposes.

    Args:
        path (list[Command]): Path to print.

    Return:
        None
    """
    for cmd in path:
        pos = cmd.position
        print(f"({pos.x}, {pos.y})", end=" -> ")
    print("END")    

update_board_state(boardState)

Update grid obstacles from board state.

Parameters:

Name Type Description Default
boardState str

FEN notation of board state.

required
Return

None

Source code in python/Control.py
341
342
343
344
345
346
347
348
349
350
def update_board_state(self, boardState: str):
    """Update grid obstacles from board state.

    Args:
        boardState (str): FEN notation of board state.

    Return:
        None
    """
    self.grid.update_obstacles(boardState)

Grid

A* pathfinding grid for motion planning on chess board.

Source code in python/Control.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
class Grid:
    """A* pathfinding grid for motion planning on chess board."""
    nodes: list[list[Node]]
    width: int
    height: int
    obstacle_remove_position: Position

    def __init__(self, width: int, height: int):
        """Initialize A* pathfinding grid with nodes.

        Args:
            width (int): Grid width in squares.
            height (int): Grid height in squares.

        Return:
            None
        """
        self.obstacle_remove_position = Position(0.5, 5.5)  # Position to remove obstacle for captured pieces
        self.width = width * 2 + 1
        self.height = height * 2 + 1
        # Use half-step coordinates so intermediate nodes land between board squares
        self.nodes = [[Node(Position((x + 1) / 2, (y + 1) / 2)) for x in range(self.width)] for y in range(self.height)]

    def get_node(self, position: Position) -> Node:
        """Return node at given position, or None if out of bounds.

        Args:
            position (Position): Position to retrieve node from.

        Return:
            Node: Node at position, or None if invalid.
        """
        if position.x < 0.5 or position.x > self.width/2 or position.y < 0.5 or position.y > self.height/2:
            return None
        return self.nodes[int(position.y*2)-1][int(position.x*2)-1]

    def get_neighbors(self, node: Node) -> list[Node]:
        """Get neighboring nodes for pathfinding (4-directional or 8-directional).

        Args:
            node (Node): Node to get neighbors for.

        Return:
            list[Node]: List of valid neighboring nodes.
        """
        neighbors = []
        if (node.position.x % 1 == 0 and node.position.y % 1 != 0) or (node.position.x % 1 != 0 and node.position.y % 1 == 0):
            # direction without diagonals
            directions = [(-0.5, 0), (0.5, 0), (0, -0.5), (0, 0.5)]
        else:
            # direction with diagonals
            directions = [(-0.5, 0), (0.5, 0), (0, -0.5), (0, 0.5),
                        (-0.5, -0.5), (-0.5, 0.5), (0.5, -0.5), (0.5, 0.5)]

        for direction in directions:
            neighbor_x = node.position.x + direction[0]
            neighbor_y = node.position.y + direction[1]
            neighbor_node = self.get_node(Position(neighbor_x, neighbor_y))
            if neighbor_node is not None:
                neighbors.append(neighbor_node)

        return neighbors

    def initialize_links(self):
        """Set up neighbor relationships for all nodes in grid.

        Args:
            None

        Return:
            None
        """
        for i in range(self.height):
            for j in range(self.width):
                node = self.nodes[i][j]
                node.neighbors = self.get_neighbors(node)

    def add_obstacle(self, position: Position):
        """Add obstacle by removing all neighbors from node.

        Args:
            position (Position): Position to add obstacle at.

        Return:
            None
        """
        node = self.get_node(position)
        if node:
            node.neighbors = []  # Remove all neighbors to create an obstacle

    def remove_obstacle(self, position: Position):
        """Remove obstacle by restoring neighbors to node.

        Args:
            position (Position): Position to remove obstacle from.

        Return:
            None
        """
        node = self.get_node(position)
        if node:
            node.neighbors = self.get_neighbors(node)  # Restore neighbors to remove obstacle

    def heuristic(a: Node, b: Node) -> float:
        """Calculate Euclidean heuristic distance between two nodes.

        Args:
            a (Node): First node.
            b (Node): Second node.

        Return:
            float: Heuristic distance between nodes.
        """
        return np.sqrt((a.position.x - b.position.x) ** 2 + (a.position.y - b.position.y) ** 2)

    def a_star(self, start_pos: Position, end_pos: Position) -> list[Position]:
        """Perform A* pathfinding algorithm between start and end positions.

        Args:
            start_pos (Position): Starting position.
            end_pos (Position): Ending position.

        Return:
            list[Position]: List of positions forming the path, or empty list if no path exists.
        """
        start_node = self.get_node(start_pos)
        end_node = self.get_node(end_pos)

        if start_node is None or end_node is None:
            return []

        # Reset all node states to avoid stale data from previous searches
        for row in self.nodes:
            for node in row:
                node.gCost = float('inf')
                node.hCost = 0.0
                node.fCost = float('inf')
                node.parent = None

        open_set = []
        closed_set = set()

        start_node.gCost = 0.0
        start_node.fCost = Grid.heuristic(start_node, end_node)
        open_set.append(start_node)

        while open_set:
            current_node = min(open_set, key=lambda node: node.fCost)

            if current_node == end_node:
                path = []
                node = current_node
                while node is not None:
                    path.append(node.position)
                    node = node.parent
                return path[::-1]  # Return reversed path

            open_set.remove(current_node)
            closed_set.add(current_node)

            for neighbor in current_node.neighbors:
                if neighbor in closed_set:
                    continue

                tentative_gCost = current_node.gCost + Grid.heuristic(current_node, neighbor)

                if neighbor not in open_set:
                    open_set.append(neighbor)
                elif tentative_gCost >= neighbor.gCost:
                    continue

                neighbor.parent = current_node
                neighbor.gCost = tentative_gCost
                neighbor.hCost = Grid.heuristic(neighbor, end_node)
                neighbor.fCost = neighbor.gCost + neighbor.hCost

        return []  # No path found

    def update_obstacles(self, boardState: str):
        """Update obstacles in grid based on current board state.

        Args:
            boardState (str): FEN notation of board state.

        Return:
            None
        """
        board = chess.Board(boardState)
        for i in range(8):
            for j in range(8):
                piece = board.piece_at(chess.square(i, j))
                position = Position(i + 1, j + 1)
                if piece is not None:
                    self.add_obstacle(position)
                else:
                    self.remove_obstacle(position)

    def is_obstacle(self, position: Position) -> bool:
        """Check if position contains an obstacle.

        Args:
            position (Position): Position to check.

        Return:
            bool: True if position is an obstacle, False otherwise.
        """
        node = self.get_node(position)
        if node:
            return len(node.neighbors) == 0
        return False

__init__(width, height)

Initialize A* pathfinding grid with nodes.

Parameters:

Name Type Description Default
width int

Grid width in squares.

required
height int

Grid height in squares.

required
Return

None

Source code in python/Control.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def __init__(self, width: int, height: int):
    """Initialize A* pathfinding grid with nodes.

    Args:
        width (int): Grid width in squares.
        height (int): Grid height in squares.

    Return:
        None
    """
    self.obstacle_remove_position = Position(0.5, 5.5)  # Position to remove obstacle for captured pieces
    self.width = width * 2 + 1
    self.height = height * 2 + 1
    # Use half-step coordinates so intermediate nodes land between board squares
    self.nodes = [[Node(Position((x + 1) / 2, (y + 1) / 2)) for x in range(self.width)] for y in range(self.height)]

a_star(start_pos, end_pos)

Perform A* pathfinding algorithm between start and end positions.

Parameters:

Name Type Description Default
start_pos Position

Starting position.

required
end_pos Position

Ending position.

required
Return

list[Position]: List of positions forming the path, or empty list if no path exists.

Source code in python/Control.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
def a_star(self, start_pos: Position, end_pos: Position) -> list[Position]:
    """Perform A* pathfinding algorithm between start and end positions.

    Args:
        start_pos (Position): Starting position.
        end_pos (Position): Ending position.

    Return:
        list[Position]: List of positions forming the path, or empty list if no path exists.
    """
    start_node = self.get_node(start_pos)
    end_node = self.get_node(end_pos)

    if start_node is None or end_node is None:
        return []

    # Reset all node states to avoid stale data from previous searches
    for row in self.nodes:
        for node in row:
            node.gCost = float('inf')
            node.hCost = 0.0
            node.fCost = float('inf')
            node.parent = None

    open_set = []
    closed_set = set()

    start_node.gCost = 0.0
    start_node.fCost = Grid.heuristic(start_node, end_node)
    open_set.append(start_node)

    while open_set:
        current_node = min(open_set, key=lambda node: node.fCost)

        if current_node == end_node:
            path = []
            node = current_node
            while node is not None:
                path.append(node.position)
                node = node.parent
            return path[::-1]  # Return reversed path

        open_set.remove(current_node)
        closed_set.add(current_node)

        for neighbor in current_node.neighbors:
            if neighbor in closed_set:
                continue

            tentative_gCost = current_node.gCost + Grid.heuristic(current_node, neighbor)

            if neighbor not in open_set:
                open_set.append(neighbor)
            elif tentative_gCost >= neighbor.gCost:
                continue

            neighbor.parent = current_node
            neighbor.gCost = tentative_gCost
            neighbor.hCost = Grid.heuristic(neighbor, end_node)
            neighbor.fCost = neighbor.gCost + neighbor.hCost

    return []  # No path found

add_obstacle(position)

Add obstacle by removing all neighbors from node.

Parameters:

Name Type Description Default
position Position

Position to add obstacle at.

required
Return

None

Source code in python/Control.py
191
192
193
194
195
196
197
198
199
200
201
202
def add_obstacle(self, position: Position):
    """Add obstacle by removing all neighbors from node.

    Args:
        position (Position): Position to add obstacle at.

    Return:
        None
    """
    node = self.get_node(position)
    if node:
        node.neighbors = []  # Remove all neighbors to create an obstacle

get_neighbors(node)

Get neighboring nodes for pathfinding (4-directional or 8-directional).

Parameters:

Name Type Description Default
node Node

Node to get neighbors for.

required
Return

list[Node]: List of valid neighboring nodes.

Source code in python/Control.py
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
def get_neighbors(self, node: Node) -> list[Node]:
    """Get neighboring nodes for pathfinding (4-directional or 8-directional).

    Args:
        node (Node): Node to get neighbors for.

    Return:
        list[Node]: List of valid neighboring nodes.
    """
    neighbors = []
    if (node.position.x % 1 == 0 and node.position.y % 1 != 0) or (node.position.x % 1 != 0 and node.position.y % 1 == 0):
        # direction without diagonals
        directions = [(-0.5, 0), (0.5, 0), (0, -0.5), (0, 0.5)]
    else:
        # direction with diagonals
        directions = [(-0.5, 0), (0.5, 0), (0, -0.5), (0, 0.5),
                    (-0.5, -0.5), (-0.5, 0.5), (0.5, -0.5), (0.5, 0.5)]

    for direction in directions:
        neighbor_x = node.position.x + direction[0]
        neighbor_y = node.position.y + direction[1]
        neighbor_node = self.get_node(Position(neighbor_x, neighbor_y))
        if neighbor_node is not None:
            neighbors.append(neighbor_node)

    return neighbors

get_node(position)

Return node at given position, or None if out of bounds.

Parameters:

Name Type Description Default
position Position

Position to retrieve node from.

required
Return

Node: Node at position, or None if invalid.

Source code in python/Control.py
137
138
139
140
141
142
143
144
145
146
147
148
def get_node(self, position: Position) -> Node:
    """Return node at given position, or None if out of bounds.

    Args:
        position (Position): Position to retrieve node from.

    Return:
        Node: Node at position, or None if invalid.
    """
    if position.x < 0.5 or position.x > self.width/2 or position.y < 0.5 or position.y > self.height/2:
        return None
    return self.nodes[int(position.y*2)-1][int(position.x*2)-1]

heuristic(a, b)

Calculate Euclidean heuristic distance between two nodes.

Parameters:

Name Type Description Default
a Node

First node.

required
b Node

Second node.

required
Return

float: Heuristic distance between nodes.

Source code in python/Control.py
217
218
219
220
221
222
223
224
225
226
227
def heuristic(a: Node, b: Node) -> float:
    """Calculate Euclidean heuristic distance between two nodes.

    Args:
        a (Node): First node.
        b (Node): Second node.

    Return:
        float: Heuristic distance between nodes.
    """
    return np.sqrt((a.position.x - b.position.x) ** 2 + (a.position.y - b.position.y) ** 2)

Set up neighbor relationships for all nodes in grid.

Return

None

Source code in python/Control.py
177
178
179
180
181
182
183
184
185
186
187
188
189
def initialize_links(self):
    """Set up neighbor relationships for all nodes in grid.

    Args:
        None

    Return:
        None
    """
    for i in range(self.height):
        for j in range(self.width):
            node = self.nodes[i][j]
            node.neighbors = self.get_neighbors(node)

is_obstacle(position)

Check if position contains an obstacle.

Parameters:

Name Type Description Default
position Position

Position to check.

required
Return

bool: True if position is an obstacle, False otherwise.

Source code in python/Control.py
311
312
313
314
315
316
317
318
319
320
321
322
323
def is_obstacle(self, position: Position) -> bool:
    """Check if position contains an obstacle.

    Args:
        position (Position): Position to check.

    Return:
        bool: True if position is an obstacle, False otherwise.
    """
    node = self.get_node(position)
    if node:
        return len(node.neighbors) == 0
    return False

remove_obstacle(position)

Remove obstacle by restoring neighbors to node.

Parameters:

Name Type Description Default
position Position

Position to remove obstacle from.

required
Return

None

Source code in python/Control.py
204
205
206
207
208
209
210
211
212
213
214
215
def remove_obstacle(self, position: Position):
    """Remove obstacle by restoring neighbors to node.

    Args:
        position (Position): Position to remove obstacle from.

    Return:
        None
    """
    node = self.get_node(position)
    if node:
        node.neighbors = self.get_neighbors(node)  # Restore neighbors to remove obstacle

update_obstacles(boardState)

Update obstacles in grid based on current board state.

Parameters:

Name Type Description Default
boardState str

FEN notation of board state.

required
Return

None

Source code in python/Control.py
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
def update_obstacles(self, boardState: str):
    """Update obstacles in grid based on current board state.

    Args:
        boardState (str): FEN notation of board state.

    Return:
        None
    """
    board = chess.Board(boardState)
    for i in range(8):
        for j in range(8):
            piece = board.piece_at(chess.square(i, j))
            position = Position(i + 1, j + 1)
            if piece is not None:
                self.add_obstacle(position)
            else:
                self.remove_obstacle(position)

Node

Represents a node in A* pathfinding grid with costs and neighbors.

Source code in python/Control.py
 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
class Node:
    """Represents a node in A* pathfinding grid with costs and neighbors."""
    position: Position
    gCost: float
    hCost: float
    fCost: float
    parent: 'Node'
    neighbors: list['Node']

    def __init__(self, position: Position = Position(0.0, 0.0)):
        """Initialize pathfinding node with position and costs.

        Args:
            position (Position): Node position on grid.

        Return:
            None
        """
        self.position = position
        self.gCost = 0.0
        self.hCost = 0.0
        self.fCost = 0.0
        self.parent = None
        self.neighbors = []

    def __eq__(self, value):
        """Compare nodes by position.

        Args:
            value (Node): Node to compare with.

        Return:
            bool: True if positions are equal, False otherwise.
        """
        return self.position == value.position

    def __hash__(self):
        """Return hash of node by position for set/dict storage.

        Args:
            None

        Return:
            int: Hash value of the node's position.
        """
        return hash(self.position)

__eq__(value)

Compare nodes by position.

Parameters:

Name Type Description Default
value Node

Node to compare with.

required
Return

bool: True if positions are equal, False otherwise.

Source code in python/Control.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def __eq__(self, value):
    """Compare nodes by position.

    Args:
        value (Node): Node to compare with.

    Return:
        bool: True if positions are equal, False otherwise.
    """
    return self.position == value.position

__hash__()

Return hash of node by position for set/dict storage.

Return

int: Hash value of the node's position.

Source code in python/Control.py
103
104
105
106
107
108
109
110
111
112
def __hash__(self):
    """Return hash of node by position for set/dict storage.

    Args:
        None

    Return:
        int: Hash value of the node's position.
    """
    return hash(self.position)

__init__(position=Position(0.0, 0.0))

Initialize pathfinding node with position and costs.

Parameters:

Name Type Description Default
position Position

Node position on grid.

Position(0.0, 0.0)
Return

None

Source code in python/Control.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def __init__(self, position: Position = Position(0.0, 0.0)):
    """Initialize pathfinding node with position and costs.

    Args:
        position (Position): Node position on grid.

    Return:
        None
    """
    self.position = position
    self.gCost = 0.0
    self.hCost = 0.0
    self.fCost = 0.0
    self.parent = None
    self.neighbors = []

Position

Represents a 2D position point with x and y coordinates.

Source code in python/Control.py
 9
10
11
12
13
14
15
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
class Position:
    """Represents a 2D position point with x and y coordinates."""
    x: float
    y: float

    def __init__(self, x: float, y: float):
        """Initialize position with x and y coordinates.

        Args:
            x (float): X coordinate value.
            y (float): Y coordinate value.

        Return:
            None
        """
        self.x = x
        self.y = y

    def __eq__(self, value):
        """Compare two positions for equality.

        Args:
            value (Position): Position to compare with.

        Return:
            bool: True if positions are equal, False otherwise.
        """
        return self.x == value.x and self.y == value.y

    def __hash__(self):
        """Return hash of position for use in sets/dicts during pathfinding.

        Args:
            None

        Return:
            int: Hash value of the position.
        """
        return hash((self.x, self.y))

__eq__(value)

Compare two positions for equality.

Parameters:

Name Type Description Default
value Position

Position to compare with.

required
Return

bool: True if positions are equal, False otherwise.

Source code in python/Control.py
27
28
29
30
31
32
33
34
35
36
def __eq__(self, value):
    """Compare two positions for equality.

    Args:
        value (Position): Position to compare with.

    Return:
        bool: True if positions are equal, False otherwise.
    """
    return self.x == value.x and self.y == value.y

__hash__()

Return hash of position for use in sets/dicts during pathfinding.

Return

int: Hash value of the position.

Source code in python/Control.py
38
39
40
41
42
43
44
45
46
47
def __hash__(self):
    """Return hash of position for use in sets/dicts during pathfinding.

    Args:
        None

    Return:
        int: Hash value of the position.
    """
    return hash((self.x, self.y))

__init__(x, y)

Initialize position with x and y coordinates.

Parameters:

Name Type Description Default
x float

X coordinate value.

required
y float

Y coordinate value.

required
Return

None

Source code in python/Control.py
14
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, x: float, y: float):
    """Initialize position with x and y coordinates.

    Args:
        x (float): X coordinate value.
        y (float): Y coordinate value.

    Return:
        None
    """
    self.x = x
    self.y = y

```