• No results found

Chess Playing Robot

N/A
N/A
Protected

Academic year: 2021

Share "Chess Playing Robot"

Copied!
67
0
0

Loading.... (view fulltext now)

Full text

(1)

,

STOCKHOLM SWEDEN 2020

Chess Playing Robot

Robotic arm capable of playing chess

FREDRIK BALDHAGEN

(2)

Chess Playing Robot

Robotic arm capable of playing chess

FREDRIK BALDHAGEN ANTON HEDSTR ¨OM

Bachelor’s Thesis at ITM Supervisor: Nihad Subasic

(3)
(4)

Abstract

The purpose of this thesis was to create a robot that through the use of visual recognition and robotics could play chess.The idea for this project came from the fact that there is an in-creasing demand for smart robots that can make their own decisions in a changing environment, and the fact that chess has recently seen a surge of new players.

The optimal design of the arm making the moves was de-cided to be of SCARA type, which is a common robotic arm that excels in pick-and-place operations. The movement of the arm was driven by two stepper motors connected to a Raspberry Pi and an external power supply. Movement in the Z-direction was achieved through the use of a servo motor driving a gear rack vertically.

A camera was placed above the chessboard, and through the use of numerous programs and functions, images were converted to chess notation which was then sent to a chess engine running on the Raspberry Pi.

The visual recognition worked optimally when the chess-board was well and evenly lit. When lighting was poor, values that defined colors could be changed, allowing for proper evaluation of the colors, however when the illumi-nance dropped below 15 lux the blue pieces became indis-tinguishable from the black squares and therefore the visual recognition stopped working.

(5)

Schackspelande robot

Syftet med det h¨ar examensarbetet var att skapa en ro-bot som genom anv¨andning av bildigenk¨anning och robo-tik kunde spela schack. Id´en till detta projekt kom fr˚an det faktum att det finns ett ¨okande behov av smarta robotar som kan fatta sina egna beslut i en f¨or¨andring milj¨o och det faktum att schack nyligen har sett en ¨okning av nya spelare. Den optimala utformningen av armen som flyt-tar pj¨aserna beslutades vara av SCARA-typ, som ¨ar en vanlig robotarm som utm¨arker sig i ’pick-and-place’ ope-rationer. Armens r¨orelse drivs av tv˚a stegmotorer anslutna till en Raspberry Pi och en extern str¨omk¨alla. R¨orelse i Z-riktningen uppn˚addes genom anv¨andning av en servomo-tor som drev en kuggst˚ang vertikalt. En kamera placerades ovanf¨or schackbr¨adet, och genom anv¨andning av flera pro-gram och funktioner konverterades bilder till schacknota-tion som sedan skickades till en schackmotor som k¨ors p˚a Raspberry Pi.

Bildigenk¨anningen fungerade optimalt n¨ar schackbr¨adet var v¨al och j¨amnt upplyst. N¨ar belysningen var d˚alig kunde v¨arden som definierade f¨arger ¨andras f¨or att m¨ojligg¨ora korrekta utv¨arderingar av f¨argen, men n¨ar belysningsniv˚an sj¨onk under 15 lux blev de bl˚a pj¨aserna oskiljbara fr˚an de svarta rutorna och programmet slutade d¨arf¨or att fungera.

(6)

Acknowledgements

We would like to thank our supervisor and examiner Nihad Subasic for providing guidance and lectures for the duration of this thesis. We would also like to thank all the students who attended seminars and provided feedback, allowing this project to evolve.

(7)

1 Introduction 1 1.1 Background . . . 1 1.2 Purpose . . . 1 1.3 Scope . . . 1 1.4 Method . . . 2 2 Theory 3 2.1 Hardware . . . 3 2.1.1 Raspberry Pi 4 . . . 3 2.1.2 Stepper Motors . . . 3

2.1.3 Stepper Motor Drivers . . . 4

2.1.4 Servomotor . . . 4 2.1.5 Electromagnet . . . 4 2.1.6 NPN Transistor . . . 4 2.2 Software . . . 5 2.2.1 Stockfish . . . 5 2.3 Visual recognition . . . 5 2.3.1 Edge detection . . . 5 2.3.2 Perspective Warp . . . 5 2.3.3 RGB-detection . . . 6 2.4 SCARA . . . 7

2.4.1 Movement in the XY-plane . . . 7

2.4.2 Rack and pinion . . . 9

3 The Construction 11 3.1 SCARA . . . 11 3.1.1 Main Axis . . . 11 3.1.2 Inner Arm . . . 12 3.1.3 Outer Arm . . . 12 3.1.4 Z-movement . . . 12 3.2 Electrical Components . . . 13 3.2.1 Electrical Motors . . . 13 3.2.2 Power Supply . . . 13

(8)

3.2.3 Electromagnet . . . 14

3.2.4 Camera . . . 14

3.3 Software . . . 15

3.3.1 Main Program . . . 15

3.3.2 SCARA Movement . . . 15

3.3.3 Picking and placing pieces . . . 15

3.3.4 Inputs to the robot . . . 15

4 Testing and Results 17 5 Discussion and conclusions 19 5.1 Consistency of the robot . . . 19

5.2 Inputs to the robot . . . 19

5.3 Visual Recognition . . . 20 5.4 Conclusions . . . 20 Bibliography 21 Appendices 23 A Circuit Diagram 23 B Code 25

(9)
(10)

Nomenclature

List of Abbreviations CAD - Computer-aided Design

GPIO - General-Purpose input/output PC - Personal Computer

PSU - Power Supply Unit RGB - Red-Green-Blue

SCARA - Selective Compliance Articulated Robot Arm SSH - Secure Shell

List of Variables

α Angle between the X-axis and the inner arm

β Angle between the extrapolation of the inner arm and the outer arm φ Angle between the inner arm and the hypotenuse of a certain position D Pitch diameter

d Distance traveled l1 Length of inner arm

l2 Length of outer arm

(11)

2.1 Cross-section of a stepper motor[6] . . . 4

2.2 A representation of the RGB-Color model mapped to a cube. The origin, which is hidden from view, is black[14] . . . 6

2.3 An overview of the arm in the XY-plane [Created in MS Paint] . . . 7

2.4 Overview in the XY-plane for inverse kinematics[15] . . . 8

3.1 3D-model of the robotic arm [Created in Solid Edge] . . . 11

3.2 Photo of the lifting mechanism, 2020 . . . 12

(12)

List of Tables

4.1 Attempts at picking up and placing a piece when only moving in the

Z-direction . . . 17

4.2 Attempts at placing the arm directly over a desired square . . . 17

4.3 Attempt at identifying the chessboard at different lightings . . . 18

4.4 Attempt at identifying pieces at different lightings . . . 18

(13)
(14)

Chapter 1

Introduction

1.1

Background

Chess is a board game originated in the sixth century[1]. Chess games have looked almost the same for the past 1500 years, where two players sit opposite each other and play over a chessboard. In the last 30 years there has been change in how people play and practice chess, where playing against a chess computer has become a common way to play the game. With this project we want to combine the old and new ways of playing chess, where the game would be played on a physical chessboard versus a chess computer that could make its own moves.

1.2

Purpose

The purpose of this project was to create a robot that, through the use of visual recognition and a robotic arm, could play chess.

The following research questions were answered:

• How should the robotic arm be constructed to consistently be able to place the chess pieces in the correct location?

• How does lighting affect the ability of the visual recognition?

1.3

Scope

The final chessboard was slightly scaled down in size ,32x32 cm compared to the ones used in competition which range from 40x40 cm to 50x50 cm. This was due to limitations in 3D-printing space and concerns regarding stability. The robotic arm is a Selective Compliance Articulated Robot Arm (SCARA) , which has three degrees of freedom[2]. The SCARA needed to be able to lift the piece that weighed

(15)

contrast colors for easier visual recognition through Red-Green-Blue (RGB) Color detection. There were also steel screws inserted into the pieces to be able to use an electromagnet to move them. The chess pieces were also made the same height.

1.4

Method

Initially, a prototype of the robot was created using computer-aided design (CAD) and this prototype was continuously improved until it was satisfactory in both design and functionality. With these CAD-models created, 3D-printing of the plastic parts started using an ANET A8 3D-printer. When all parts were manufactured, the robot was assembled and stepper motors were integrated in the robot to enable movement in the XY-plane. Furthermore, a continuous servo motor was attached to a gear rack at the end of the outer arm to allow movement in the Z-direction.

A chessboard was then drawn onto a plywood sheet on which the robot and a camera stand were attached. A camera module for the Raspberry Pi was attached to the stand and all electronics were connected to the Pi. At this stage a number of programs were created in order to read the image correctly, send a position to the chess engine, control the movement of the arm and more.

(16)

Chapter 2

Theory

2.1

Hardware

2.1.1 Raspberry Pi 4

The Raspberry Pi is a single-board computer running the Linux based operating system Raspbian. Therefore the Raspberry Pi can run programs developed for Linux such as the chess engine Stockfish, and use a high level programming language such as Python, which would not be possible on a microcontroller. The Raspberry Pi 4 is powered by a 3A USB-C power supply[3].

2.1.2 Stepper Motors

A stepper motor is a motor powered by direct current that moves in discrete steps. The motor has multiple coils organized in phases, seen in figure 2.1, and by acti-vating each phase sequentially the motor rotates one step. By actiacti-vating multiple phases simultaneously, fractions of whole steps can be achieved, for example, what is known as ”half-steps”. This increases the number of steps per rotation greatly. With a computer controlled stepping, a very high precision can be achieved regard-ing both speed and positionregard-ing [4][5].

(17)

Figure 2.1: Cross-section of a stepper motor[6]

2.1.3 Stepper Motor Drivers

To control the stepper motors with the Raspberry Pi, a stepper motor driver is needed. The stepper motor driver gets input signals from the Raspberry Pi, which it converts into electrical pulses to drive the stepper motor. To achieve a higher resolution or smoother motion a driver with micro stepping can be used[5].

2.1.4 Servomotor

The servomotor is a motor with all the components needed for precision control integrated into the same box. These components are a DC-motor, a potentiometer and a control circuit. The DC-motor is used to drive the shaft of the servomotor. The purpose of the potentiometer is to send a signal about the position of the servo-motor. When the control circuit gets a signal to move the servomotor it supplies the DC-motor with power until the correct position is reached according to the signal from the potentiometer[7][8].

2.1.5 Electromagnet

An electromagnet is a type of magnet where the magnetic field is generated by an electric current. Electromagnets are used wherever controllable magnets are required, as the magnetic field can be varied, reversed or switched on and off by altering the current.[9]

2.1.6 NPN Transistor

A transistor consists of three channels: Collector, Emitter and Base. In a NPN transistor the current flows in through the collector and out through the emitter if a signal is applied to the base pin of the transistor[10].

(18)

2.2. SOFTWARE

2.2

Software

2.2.1 Stockfish

Stockfish is an open-source chess engine, which first builds a tree of all legal moves for the depth chosen, then it performs depth-first search and compares the resulting boards to determine the best move. The ”skill” of Stockfish depends on which depth the engine is using for the searches. However a deeper search will also increase the computing time since the number of branches of the tree increases rapidly with each step down the tree. Given enough computing time Stockfish is virtually unbeatable for a human player. Therefore the limit of Stockfish lies in the computing time and not in the capability of the engine itself[11].

2.3

Visual recognition

2.3.1 Edge detection

To read and evaluate the chessboard the visual recognition has to be able to recog-nise where on the picture the chessboard is located. When the chessboard has been located on the image it can be cropped in a way which results in an image only con-taining the chessboard. To be able to crop the image in this way some sort of edge detection is needed. The OpenCV library for computer vision has a function for detecting the inner corners of a chessboard and returning their positions. Although this function is intended for camera calibration, with minor modifications and the use of linear extrapolation the outer corners could be located[12].

2.3.2 Perspective Warp

The camera module can’t be located the same distance from all squares of the chessboard, and this means that different squares on the board will appear to be differently sized from the cameras perspective. To combat this problem, a trans-formation matrix for the chessboard had to be calculated, and the matrix is then applied to each picture during the game to get an accurate reading of the chess-board. Since all the squares of the chessboard are now of the same size, the image can easily be navigated to analyse each square.

(19)

2.3.3 RGB-detection

To read the chessboard the visual recognition needs to be able to determine if a square is empty or if it contains a black or white piece. In order to do this, RGB-detection was used.

In RGB-color space any color can be represented as a combination of three primary colors: red, green and blue, with varying intensities[13]. The intensities most often range from zero to 255, which is the range that a single 8-bit byte can offer. Therefore, any color can be represented with the equation:

C = R(r) + G(g) + B(b) (1)

where r, g and b are the intensities of respective color. To detect if a square is vacant or not, and if not vacant what color the piece is, pixels near the center of the square were analyzed in regards to its red, green and blue values.

Figure 2.2: A representation of the RGB-Color model mapped to a cube. The origin, which is hidden from view, is black[14]

(20)

2.4. SCARA

2.4

SCARA

The SCARA acronym stands for Selective Compliance Articulated Robot Arm. A SCARA has a jointed two-link arm layout that is similar to our human arms, and this results in free movement in the XY-plane in an area that is decided by the length of the arms, l1 and l2.

Movement in the Z-direction can be achieved in different ways, most commonly with a gripper or rod that moves at the end of the arm, but also by altering the height of the base of the arm[2].

2.4.1 Movement in the XY-plane

Forward Kinematics

If the angles of the joints are known but not the X and Y coordinates, the X and Y coordinates can then be found using forward kinematics. The position of the gripper in the XY-plane can be solved using trigonometry. The angles needed for these calculations can be seen in figure 2.3.

The equation for the position in X-direction will be:

l1· Cos(α) + l2· Cos(α + β) (2) The equation for the position in Y-direction will be:

l1· Sin(α) + l2· Sin(α + β) (3)

(21)

Inverse Kinematics

If the X and Y coordinates in the plane are known but not the angle of the joints the angles can be found using inverse kinematics. The variables needed for these calculations can be found in figure 2.4.

Figure 2.4: Overview in the XY-plane for inverse kinematics[15] .

β= arcos(X

2+ Y2− l2 1− l22

2 · l1· l2 ) (4)

When X > 0, α + φ can be found with equation 5. When X < 0, α + φ is found in equation 6. The case X = 0 is not considered since X will not be equal to zero for any square on the board.

α+ φ = arctan(Y X) (5) α+ φ = arctan(Y X) + π (6) φ= arccos(X 2+ Y2+ l2 1− l22 2 · l1X2+ Y2 ) (7) α= (α + φ) − φ (8)

(22)

2.4. SCARA

By selecting the positive arc-cosine solutions in the equations above the right-armed case is derived. The right-hand case means that the inner arm (l1) will never cross the Y-axis[15].

2.4.2 Rack and pinion

Rack and pinion is a gear drive which consists of a pinion and a gear rack. The pinion is mounted on a motor so that when the motor is rotating a linear motion is achieved. The linear motion can be achieved for either the motor or the gear rack. If the motor is prohibited from moving and not the gear rack, the gear rack will move and vice versa. The linear distance traveled for the rack or pinion can be found in equation 9. This equation is derived from the fact that the circumference of a circle is equal to pi times the diameter[16].

(23)
(24)

Chapter 3

The Construction

3.1

SCARA

Figure 3.1: 3D-model of the robotic arm [Created in Solid Edge]

3.1.1 Main Axis

The main axis of the robot consists of three levels as seen in figure 3.1. The main purpose of the base level is to give stability to the robot arm. This is accomplished by keeping the threaded rods in place and by attaching the base to a plywood sheet on which the chessboard is placed.

The two upper levels were created to be able to mount the stepper motors and to set the height of the arms. The stepper motor mount is slotted to get the ability to

(25)

3.1.2 Inner Arm

The inner arm is mounted on the main axis resting on bearings to minimize friction between the arm and the threaded rod. On this end of the arm, there is also a gear designed for a timing belt to be able to drive the rotation of the arm using the upper stepper motor. The other end of the arm has a cutout for an M8 bolt to attach the outer arm.

3.1.3 Outer Arm

The outer arm is attached to the M8 bolt inserted into the inner arm, this arm also rests on bearings to minimize friction. This end of the arm also has a gear designed for a timing belt built into it. The end furthest from the main axis on this arm has premade holes to give the servomotor a place to be mounted. To drive the outer arm, an additional gear is placed on the main axis. This is done to keep the timing belt tensioned at all positions of the inner arm.

3.1.4 Z-movement

The movement in the Z-direction is accomplished by using a pinion mounted on a servomotor and a gear rack on which the electromagnet is placed, shown in figure 3.2. The pinion has a module of 2 and the pitch diameter is 3cm. The gear rack is 15cm tall.

(26)

3.2. ELECTRICAL COMPONENTS

3.2

Electrical Components

A circuit diagram can be seen in Appendix A.

3.2.1 Electrical Motors

Two stepper motors were used in this construction to drive the movement of the arms with the help of timing belts and gears. To drive the movement of the elec-tromagnetic gripper, a servomotor was used.

Servomotor

The servomotor used is a Hi-Tec MSR-3.2-36 which is a continuous rotation servo-motor and has rated voltage of 4.8 V which means that the Raspberry Pi 4 is able to drive the motor.

Stepper Motors

The stepper motors used are bipolar and of Nema 17 type. The rated voltage is 3 V and the rated current is 1.7 A. Since the Raspberry Pi 4 3.3 V rail only can supply about 50 mA, an external Power Supply Unit (PSU) needed to be used to drive the stepper motors. The angle of every step is 0.9° which means that for every rotation of the motor there is 400 steps.

Stepper Motor Drivers

The stepper motor drivers used are Texas Instruments DRV8825, which are able to drive the stepper motors used.

3.2.2 Power Supply

To power the stepper motors and the electromagnet, a more powerful PSU was required since the Raspberry Pi 4 couldn’t provide the power needed. The PSU is an old Personal Computer (PC) PSU rated at 400 watts and can provide well above the power needed.

To use the PSU outside a PC, a small modification had to be made; the ”Power On” cable of the PSU had to be connected to a ground wire which was done by soldering them together.

(27)

3.2.3 Electromagnet

The electromagnet is controlled by a NPN transistor. The electromagnet is con-nected to the emitter of the transistor and ground of the PSU. The collector on the transistor is connected to a 5 V output on the PSU and the base of the transistor is connected to a General-Purpose input/output (GPIO) pin on the Raspberry Pi. Electromagnet

The electromagnet used to grip the pieces has a rated voltage of 5 V and draws 220 mA of current. It has a holding force of 2.5 kg. The current drawn by the magnet exceeds what the Raspberry Pi 4 can output which made it necessary to power it with an external PSU.

NPN Transistor

The transistor used is a NPN 2N2222A transistor.

3.2.4 Camera

The camera used in this construction was a Raspberry Pi camera module V2. Which is an eight megapixel camera developed for use with the Raspberry Pi.

(28)

3.3. SOFTWARE

3.3

Software

The software was written in the programming language Python using the text ed-itor Atom on PC and in the Thonny integrated development environment on the Raspberry Pi 4.

3.3.1 Main Program

The movement of the SCARA was not predetermined since the movement would be decided by the Stockfish engine and change depending on which moves was played by the player, and therefore large segments of the code were generalized and iterated over for the duration of a game. To achieve this, a number of functions were created: identifying colors and pieces from an image, creating a virtual chessboard and creating an FEN, to name a few. These functions were then integrated in a main program which not only resulted in an organized structure, but also gave the player the opportunity to, amongst many things, chose which color to play as without the need to modify the input to more than one function. Figure 3.3 shows a flowchart describing how the main program works.

3.3.2 SCARA Movement

The movement of the arm was programmed in a number of steps. First the squares involved in the move are converted into x and y coordinates. Then a calculation of how to reach these coordinates with the arm lengths l1 and l2 is done using the inverse kinematic equations given in section 2.4.1, resulting in the two angles α and β. From these angles the number of steps to move on each stepper motor is calculated. For this robot only the right-hand case was considered for the SCARA movement since there was a camera stand placed on the left side of the robot, which the robot arm could crash into if the left-hand case was used.

3.3.3 Picking and placing pieces

To pick up a piece, a function was written which lowers the magnet down to the piece, turns on the electromagnet and then moves the magnet up to the starting position. To place a piece, the same motion is made but instead of turning on the magnet turns off when the piece is lowered.

The magnet is turned on by setting the GPIO pin output connected to the base of the transistor to HIGH and turned of by setting it to LOW again.

3.3.4 Inputs to the robot

To start a game of chess versus the robot, the main program had to be opened on the Raspberry Pi which was done by Secure Shell (SSH) on a PC. A player playing against the robot has to press enter in the command line to indicate that a move is made.

(29)
(30)

Chapter 4

Testing and Results

Three larger tests were conducted before attempting to play an entire chess game. These tests concerned motion in the Z-direction, motion in the XY-plane and the visual recognition. The motion in the Z-direction was tested by measuring the accuracy with which a piece was successfully lifted and returned to its place using the servo driving the gear rack with the electromagnet. The result of this test is shown in Table 4.1.

Attempts Successful

10 10

Table 4.1: Attempts at picking up and placing a piece when only moving in the Z-direction

The motion in the Z-direction worked perfectly and had a success rate of 100 %. The motion in the XY-plane was tested by measuring how well the SCARA managed to place itself directly above the desired position after several movements. The results of this test is shown in Table 4.2.

Attempts Successful

10 10

Table 4.2: Attempts at placing the arm directly over a desired square The motion in the XY-plane also had a success rate of 100 %.

The tests regarding the visual recognition concerned how its ability was affected when the lighting changed. This test was applied to both the RGB-detection and the ability to identify a chessboard in an image. The light intensity values were gathered using the application ”Google Science Journals” on a Samsung Galaxy

(31)

Mean Light intensity [lx] Detected Chessboard

1000 YES

374 YES

15 YES

5 YES

Table 4.3: Attempt at identifying the chessboard at different lightings Mean Light intensity [lx] Detect Blue Pieces

Correctly Detect Red PiecesCorrectly

1000 YES YES

374 YES YES

15 No YES

5 No No

Table 4.4: Attempt at identifying pieces at different lightings

At 1,000 lux(indirect sunlight), and 374 lux(office lighting), the program man-aged to detect all pieces flawlessly. At 15 lux the program still manman-aged to identify the red pieces, although with a very small margin. It was almost completely unable to distinguish the blue pieces, only successfully doing so with an average of four out of 16 pieces using the same threshold for the colors. At this illuminance lowering the threshold for the colors was not possible, without black squares being classified as blue, as the black color used was not a ’perfect’ black, and the blue used was quite dark.

When the testing above was completed lighting was optimized and attempts at playing entire chess games against the robot started. The results from these tests are shown in Table 4.5.

Angular Velocity [rad/s] Attempts Unsuccessful

Attempts Mean number ofmoves before failure

0.8 10 8 9

0.4 10 2 12

(32)

Chapter 5

Discussion and conclusions

5.1

Consistency of the robot

The failures of the robot during a game of chess were mostly caused by the magnet dropping the chess piece. This always happened during short movements in the XY-plane which made the arm shake a lot. This problem has several solutions; one is lowering the speed of the stepper motors. This was done during the second attempt of playing entire games of chess, and this improved the performance of the robot significantly although at the cost of much slower gameplay. To solve this problem, the movement could be slowed down for only these short movements but due to time constraints tests with this solution were not done at the publishing of this thesis. Other solutions could be investing in a stronger electromagnet, using another method for grabbing the pieces or using a stiffer material to construct the arms.

Another problem which made the robot fail during some moves was that the inner arm on the SCARA was bending somewhat under the weight of the outer arm. This caused problems with the motion in Z-direction, since the magnet got placed lower on some squares and higher on others depending on how far out from the base the arm is. Most times the moves still worked but a few times the pieces would slip or shoot away. This problem could also be solved by using stiffer building materials, or it could be solved by adding a second inner arm to lower the stress on the already existing inner arm.

5.2

Inputs to the robot

The plan from the start was to not have any inputs to the robot from a PC but instead turning it on with a switch and indicating that a move is made with either a button that would also act as a chess clock or by capturing pictures of the board between moves to see when a move is made. But due to time constraints neither of

(33)

5.3

Visual Recognition

With adequate lighting the visual recognition reliably could detect the chessboard and the chess pieces. With a dimmer lighting the blue chess pieces would not get recognised all the time. This was likely because the blue plastic which the pieces were printed from was too dark and would be interpreted as black. However, the red pieces were detected by the program at lower levels of illumination than the blue pieces.

There is one big disadvantage with only using RGB-detection to recognize the pieces. When the human player promotes a pawn, there is no way for the vi-sual recognition to recognise what piece the pawn has been promoted to, since the pieces can’t be differentiated with only RGB-detection. With this setup hard input is needed. A solution to this problem could be visual recognition with machine learning.

5.4

Conclusions

In this section we will summarize our answers to the research questions.

• How should the robotic arm be constructed to consistently be able to place the

chess pieces in the correct location?

Constructing the robot arm as a SCARA proved suitable for this application, since it excels in pick-and-place operations. Using a rack and pinion driven by a servo also proved to be a great way to achieve vertical motion.

• How does lighting affect the ability of the visual recognition?

The RGB-detection worked fine until the illuminance was about 15 lux, which is roughly the same as an illuminated city street at night. At this lighting the red pieces could still be detected but most of the blue pieces were not.

The detection of the chessboard didn’t change at the different light levels tested and would likely work fine until the illuminance is so low that the white squares are indistinguishable from the black squares.

(34)

Bibliography

[1] P.Dillion ’Artefacts, found objects and early games. A cultural ecological

per-spective on proto-chess pieces’

https://www.doi.org/10.1080/1751696X.2020.1718312

[2] P.Bhatia, J.Thirunarayanan N.Dave ’An expert system-based design of SCARA

robot’

https://doi.org/10.1016/S0957-4174(98)00015-3 [3] Raspberry Pi 4 product info. (Last Accessed: 2020-02-16)

https://www.raspberrypi.org/products/raspberry-pi-4-model-b/ [4] J.De Viaene, S.Derammelaere, K.Stockman ’Load angle estimation for dynamic

stepping motor motion applications’

https://doi.org/10.1016/j.mechatronics.2018.06.017

[5] Adafruit Industries, all about steppermotors. (Last Accessed: 2020-02-16) https://cdn-learn.adafruit.com/downloads/pdf/

all-about-stepper-motors.pdf

[6] ’Structure of a stepper motor’ (Last Accessed:2020-03-29)

https://www.orientalmotor.com/stepper-motors/technology/ stepper-motor-overview.html

[7] S.Michal et al. ’DC motors and servo-motors controlled by Raspberry Pi 2B’.

eng. In: MATEC Web of Conferences 125 (2017).

https://doi.org/10.1051/matecconf/201712502025

[8] F.Nickols, Y.Lin Creating Precision Robots A Project-Based Approach to the Study of Mechatronics and Robotics eng. Elsevier, 2019, pp.135-154.

ISBN:978-0-12-815758-9 https://doi.org/10.1016/C2017-0-03244-5

https://www.britannica.com/science/electromagnet

[9] ’Electromagnet’, Brittanica (Last Accessed: 2020-05-29) https://www. britannica.com/science/electromagnet

[10] Electronics-tutorials Bipolar Transistor (Last accessed 2020-05-10)

(35)

[11] Stockfish - Open Source Chess Engine (Last Accessed: 2020-02-16) https://stockfishchess.org/

[12] OpenCV documentation, cv2.findChessboardCorners. (Last Accessed:

2020-02-16)

https://docs.opencv.org/3.0-last-rst/modules/calib3d/doc/camera_ calibration_and_3d_reconstruction.html#cv2.findChessboardCorners [13] K.Jack Video Demystified A Handbook for the Digital Engineer eng. Elsevier,

2007, pp.15-36. ISBN:978-0-7506-8395-1

https://doi.org/10.1016/B978-0-7506-8395-1.X5000-7 [14] ’RGB color solid cube’ (Last Accessed: 2020-03-29)

https://commons.wikimedia.org/w/index.php?curid=3375025 [15] Deltatau, ’SCARA Robot Kinematics’ (Last Accessed: 2020-05-13)

http://www.deltatau.com/Common/technotes/SCARA%20Robot% 20Kinematics.pdf

[16] ’Rack and Pinion’ Brittanica (Last Accessed:2020-05-28) https://www.britannica.com/technology/rack-and-pinion

(36)

Appendix A

(37)
(38)

Appendix B

Code

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 def m a i n () : 12 C h e s s B o a r d = I n i t i a t e () # R u n n i n g i n i t i a t i o n , s e t t i n g up f i r s t b o a r d 13 P O S I T I O N S = P o s i t i o n () # G a t h e r i n g all p o s i t i o n s 14 i n p u t(’ C l e a r C h e s s B o a r d ’) # W a i t i n g for i n p u t to m a k e s u r e b o a r d is e m p t y 15 pts , c a m e r a = C a m e r a I n i t () 16 i n p u t(’ S t a r t i n g P o s i t i o n ’) # W a i t i n g for i n p u t to g i v e t i m e to set up b o a r d a f t e r i n i t i a t i o n . 17 C o l o r = W h a t C o l o r () # C h o o s e c o l o r 18 C h e c k m a t e = F a l s e # S t a r t v a l u e to get in l o o p 19 if C o l o r == ’ b ’: 20 w h i l e not C h e c k m a t e : 21 Fen = C r e a t e _ F E N ( C h e s s B o a r d . C , C o l o r = ’ w ’) # C r e a t i n g FEN 22 try:

23 From , To , M o v e = S t o c k f i s h ( Fen ) # S e n d FEN to s t o c k f i s h , get s q u a r e to m o v e # to / f r o m and the m o v e in a l g e b r a i c n o t a t i o n 24 e x c e p t V a l u e E r r o r : # if s t o c k f i s h is u n a b l e to r e t u r n a move , it is in c h e c k m a t e . 25 r e t u r n p r i n t(’ C h e c k m a t e ! You Won ! ’) 26 b r e a k 27 p r i n t(’ C a l c u l a t e d m o v e : ’ + M o v e ) 28 C h e s s B o a r d = M a k e C o m p M o v e ( C h e s s B o a r d , From , To ) # U p d a t i n g the v i r t u a l b o a r d .

(39)

30 C h e s s B o a r d = P r o m o t e ( To , Move , C h e s s B o a r d ) # U p d a t e the p a w n to the p r o m o t e d p i e c e 31 if ’ x ’ in M o v e : # If a p i e c e is taking , we r e m o v e the t a k e n p i e c e f r o m the b o a r d f i r s t . 32 Q = G o T o P o s ( Pos [ To ] , Q ) 33 s l e e p (1) 34 P i c k U p ( To ) 35 Q = G o T o P o s ([0 ,0] , Q ) 36 s l e e p (1) 37 D r o p P i e c e () 38 Q = G o T o P o s ( Pos [ F r o m ]) 39 s l e e p (3) 40 P i c k U p ( F r o m ) 41 s l e e p (1) 42 Q = G o T o P o s ( Pos [ To ] , Q ) 43 s l e e p (3) 44 P l a c e P i e c e ( To ) 45 s l e e p (1) 46 Q = G o T o P o s ([0 ,0] , Q ) 47 e l s e: # M a k i n g the m o v e 48 Q = G o T o P o s ( Pos [ F r o m ]) 49 P i c k U p ( F r o m ) 50 s l e e p (1) 51 Q = G o T o P o s ( Pos [ To ] , Q ) 52 s l e e p (3) 53 P l a c e P i e c e ( To ) 54 s l e e p (1) 55 Q = G o T o P o s ([0 ,0] , Q ) 56 if ’ # ’ in M o v e : # If m o v e l e a d s to c h e c k m a t e 57 C h e c k m a t e = T r u e 58 p r i n t(’ C h e c k m a t e ! You l o s t ! ’) 59 b r e a k 60 for q in C h e s s B o a r d . C : # P r i n t i n g the b o a r d s p o s i t i o n 61 p r i n t( q ) 62 i n p u t(’ M a k e m o v e ’) 63 N e w I m g = N e w I m a g e ( pts , c a m e r a ) # T a k e new i m a g e and s c a l e + t r a n s f o r m it . 64 C h e s s B o a r d = C o m p a r e B o a r d s ( C h e s s B o a r d , N e w I m g ) # C o m p a r e new i m a g e to the v i r t u a l b o a r d and u p d a t e .

65 for q in C h e s s B o a r d . C : # P r i n t i n g the b o a r d s p o s i t i o n 66 p r i n t( q ) 67 e l s e: 68 w h i l e not C h e c k m a t e : 69 i n p u t(’ M a k e m o v e ’) 70 N e w I m g = N e w I m a g e ( pts , c a m e r a ) # T a k e new i m a g e and s c a l e + t r a n s f o r m it . 71 C h e s s B o a r d = C o m p a r e B o a r d s ( C h e s s B o a r d , N e w I m g ) # C o m p a r e new i m a g e to the v i r t u a l b o a r d and u p d a t e .

72 for q in C h e s s B o a r d . C : 73 p r i n t( q ) 74 Fen = C r e a t e _ F E N ( C h e s s B o a r d . C , C o l o r = ’ b ’) 75 try: 76 From , To , M o v e = S t o c k f i s h ( Fen ) 77 e x c e p t V a l u e E r r o r :

(40)

78 r e t u r n p r i n t(’ C h e c k m a t e ! You Won ! ’) 79 p r i n t(’ C a l c u l a t e d m o v e : ’ + M o v e ) 80 C h e s s B o a r d = M a k e C o m p M o v e ( C h e s s B o a r d , From , To ) 81 if ’ = ’ in M o v e : # If a p a w n is p r o m o t i n g 82 C h e s s B o a r d = P r o m o t e ( To , Move , C h e s s B o a r d ) # U p d a t e the p a w n to the p r o m o t e d p i e c e 83 if ’ x ’ in M o v e : # If a p i e c e is taking , we r e m o v e the t a k e n p i e c e f r o m the b o a r d f i r s t . 84 Q = G o T o P o s ( Pos [ To ] , Q ) 85 s l e e p (1) 86 P i c k U p ( To ) 87 Q = G o T o P o s ([0 ,0] , Q ) 88 s l e e p (1) 89 D r o p P i e c e () 90 Q = G o T o P o s ( Pos [ F r o m ]) 91 s l e e p (3) 92 P i c k U p ( F r o m ) 93 s l e e p (1) 94 Q = G o T o P o s ( Pos [ To ] , Q ) 95 s l e e p (3) 96 P l a c e P i e c e ( To ) 97 s l e e p (1) 98 Q = G o T o P o s ([0 ,0] , Q ) 99 e l s e: # M a k i n g the m o v e 100 Q = G o T o P o s ( Pos [ F r o m ]) 101 P i c k U p ( F r o m ) 102 s l e e p (1) 103 Q = G o T o P o s ( Pos [ To ] , Q ) 104 s l e e p (3) 105 P l a c e P i e c e ( To ) 106 s l e e p (1) 107 Q = G o T o P o s ([0 ,0] , Q ) 108 if ’ # ’ in M o v e : 109 C h e c k m a t e = T r u e 110 p r i n t(’ C h e c k m a t e ! You l o s t ! ’) 111 for q in C h e s s B o a r d . C : 112 p r i n t( q )

(41)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10

11 # C r e a t e the c l a s s Board , w h i c h has two a t t r i b u t e s : a c l a s s i f i e d b o a r d t h a t ’ r e m e m b e r s ’

12 # the t y p e s of p i e c e s t h a t a s q u a r e h o l d / held , and one u n c l a s s i f i e d b o a r d t h a t o n l y

13 # m a k e s d i f f e r e n c e b e t w e e n the c o l o r s of the p i e c e s .

14

15 # The c l a s s has t h r e e f u n c t i o n s t h a t u p d a t e s the d i f f e r e n t b o a r d s in d i f f e r e n t r e g a r d s .

16 c l a s s B o a r d :

17 def _ _ i n i t _ _ ( s e l f ) :

18 # The c l a s s i f i e d b o a r d . I n i t i a l v a l u e s are the s t a r t i n g p o s i t i o n s of the pieces ,

19 # and as p i e c e s are m o v e d the s q u a r e s w i l l be u p d a t e d u s i n g the f u n c t i o n s b e l o w . 20 s e l f . C = [ 21 [’ r ’, ’ n ’, ’ b ’, ’ q ’, ’ k ’, ’ b ’, ’ n ’, ’ r ’] , 22 [’ p ’, ’ p ’, ’ p ’, ’ p ’, ’ p ’, ’ p ’, ’ p ’, ’ p ’] , 23 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 24 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 25 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 26 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 27 [’ P ’, ’ P ’, ’ P ’, ’ P ’, ’ P ’, ’ P ’, ’ P ’, ’ P ’] , 28 [’ R ’, ’ N ’, ’ B ’, ’ Q ’, ’ K ’, ’ B ’, ’ N ’, ’ R ’] , 29 ] 30 31

32 # The u n c l a s s i f i e d board , w h i c h w i l l a l w a y s s t a r t off as b e i n g e m p t y ( ’1 ’ = e m p t y s q u a r e ) .

33 # The f u n c t i o n f i r s t U C w i l l be a p p l i e d to the s t a r t i n g board , g i v i n g the c o r r e c t s t a r t i n g v a l u e s .

34 # In the f o l l o w i n g boards , s q u a r e s w i l l be f i l l e d w i t h the c o l o r s of the p i e c e s o c c u p y i n g the s q u a r e s u s i n g

35 # c o m p u t e r v i s i o n and the f u n c t i o n u p d a t e U C . T h i s new b o a r d is t h e n c o m p a r e d

36 # to the old one to see w h i c h p i e c e has m o v e d and w h e r e .

37 s e l f . UC = [ 38 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 39 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 40 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 41 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 42 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 43 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 44 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 45 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] ,

(42)

46 ] 47 48 # T h i s f u n c t i o n is o n l y a p p l i e d once , to g i v e the c o r r e c t s t a r t i n g p o s i t i o n s . 49 def f i r s t U C ( s e l f ) : 50 s e l f . UC = [ 51 [’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’] , 52 [’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’, ’ R ’] , 53 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 54 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 55 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 56 [’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’, ’ 1 ’] , 57 [’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’] , 58 [’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’, ’ B ’] , 59 ] 60 61 # F i l l i n g the s q u a r e s w i t h the c o l o r s o c c u p y i n g t h e m .

62 def u p d a t e U C ( self , NewRank , NewFile , C o l o r ) : 63 s e l f . UC [ N e w R a n k ][ N e w F i l e ] = C o l o r

64 # U p d a t i n g the c l a s s i f i e d b o a r d .

65 def u p d a t e C B o a r d ( self , NewRank , NewFile , OldRank , O l d F i l e ) : 66 s e l f . C [ N e w R a n k ][ N e w F i l e ] = s e l f . C [ O l d R a n k ][ O l d F i l e ] 67 s e l f . C [ O l d R a n k ][ O l d F i l e ] = ’ 1 ’

(43)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t cv2 12 i m p o r t n u m p y as np 13 f r o m F i n d P o i n t s i m p o r t * 14 f r o m t r a n s f o r m i m p o r t f o u r _ p o i n t _ t r a n s f o r m 15 f r o m p i c a m e r a i m p o r t P i C a m e r a 16 f r o m t i m e i m p o r t s l e e p 17 18 19 # F u n c t i o n t h a t c a l i b r a t e s the camera , r e t u r n i n g p o i n t s u s e d in a f o u r p o i n t t r a n s f o r m 20 # w h e r e the p i c t u r e s are t u r n e d i n t o a f l a t s q u a r e i m a g e c o n t a i n i n g o n l y the c h e s s b o a r d .

21 # A l s o r e t u r n s the c a m e r a i n s t a n c e so we do not n e e d to s t a r t a new for e v e r y i m a g e . 22 def C a m e r a I n i t () : 23 c a m e r a = P i C a m e r a () # S e t t i n g up the P i C a m e r a 24 c a m e r a . r e s o l u t i o n = (1080 , 1 0 8 0 ) # C h o o s i n g the r e s o l u t i o n 25 c a m e r a . s t a r t _ p r e v i e w () # S h o w i n g a p r e v i e w of the i m a g e 26 s l e e p (1) # W a i t i n g 5 seconds , g i v i n g the c a m e r a t i m e to f o c u s 27 c a m e r a . c a p t u r e (’ / h o m e / pi / D e s k t o p / V8 .2/ I n i t . jpg ’) # C a p t u r i n g and s a v i n g the i m a g e 28 c a m e r a . s t o p _ p r e v i e w () # S t o p p i n g the p r e v i e w 29 # c a m e r a . c l o s e () # C l o s i n g t h i s c a m e r a i n s t a n c e 30

31 # R e t r i e v i n g the f o u r p o i n t s we n e e d for the t r a n s f o r m f r o m the f u n c t i o n f i n d p o i n t s and r e t u r n i n g t h e m in an a r r a y 32 t o p _ l e f t , t o p _ r i g h t , b o t t o m _ l e f t , b o t t o m _ r i g h t = f i n d p o i n t s (’ I n i t . jpg ’) 33 pts = np . a r r a y (e v a l(" [ " + str( t o p _ l e f t ) + " , " + str( t o p _ r i g h t ) + " , " + str( b o t t o m _ l e f t ) + " , " + str( b o t t o m _ r i g h t ) + " ] ") , d t y p e = " f l o a t 3 2 ") 34 35 r e t u r n pts , c a m e r a

(44)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 f r o m B o a r d i m p o r t * 12 f r o m F i n d C o l o r i m p o r t * 13 i m p o r t cv2 14 i m p o r t n u m p y as np 15 16 # F u n c t i o n t h a t c r e a t e s a new u n c l a s s i f i e d c h e s s b o a r d f r o m an image , c o m p a r e s it

17 # to the p r e v i o u s u n c l a s s i f i e d board , t h e n u p d a t e s the c l a s s i f i e d b o a r d and r e t u r n s 18 # the u p d a t e d b o a r d s . 19 def C o m p a r e B o a r d s ( P r e v B o a r d , P i c t u r e ) : 20 P i c t u r e = cv2 . i m r e a d (’ / h o m e / pi / D e s k t o p / V8 / N e w I m g . jpg ’, 1) # O p e n i n g the p i c t u r e of c h e s s b o a r d . L o c a t i o n is h a r d c o d e d 21 Width , Height , c h a n n e l s = P i c t u r e . s h a p e # R e t r i e v i n g the s i z e of the p i c t u r e 22 F i l e s = [’ a ’, ’ b ’, ’ c ’, ’ d ’, ’ e ’, ’ f ’, ’ g ’, ’ h ’] 23 R a n k s = [’ 1 ’, ’ 2 ’, ’ 3 ’, ’ 4 ’, ’ 5 ’, ’ 6 ’, ’ 7 ’, ’ 8 ’] 24 K o r d = [1/16 , 3/16 , 5/16 , 7/16 , 9/16 , 11/16 , 13/16 , 1 5 / 1 6 ] 25 X K o r d = [ x * W i d t h for x in K o r d ] # X c o o r d i n a t e s for the c e n t e r of e a c h f i l e 26 Y K o r d = [ x * H e i g h t for x in K o r d ] # Y c o o r d i n a t e s for the c e n t e r of e a c h r a n k 27 # L o o p i n g o v e r t h e s e y i e l d s the c o o r d i n a t e s of the c e n t e r of e a c h s q u a r e . 28 29 N e w B o a r d = B o a r d () # C r e a t e new B o a r d 30 C o u n t X = 0 31 # L o o p o v e r the X - and Y - c o o r d i n a t e s , c h e c k w h a t c o l o r is in a c l u s t e r of

32 # p i x e l s n e a r the c e n t e r and u p d a t e the b o a r d w i t h the c o r r e c t c o l o r . 33 for x in X K o r d : 34 C o u n t Y = 0 35 for y in Y K o r d : 36 C o l o r = F i n d C o l o r ( P i c t u r e [int( x ) , int( y ) ]) # C h e c k w h a t c o l o r is in the p i x e l s by r e t r i e v i n g the rgb - v a l u e s .

37 # S i n c e t h e r e is a s c r e w f a s t e n e d at the top of e a c h piece , t h e r e is a c h a n c e t h a t the p i x e l in the c e n t e r of e a c h s q u a r e c o n t a i n s the s c r e w

38 # and t h e r e f o r e is i n a c c u r a t e l y c l a s s e d as an e m p t y square , s i n c e it isn ’ t c l a s s e d as b l u e nor red . T h e r e f o r e two m o r e p i x e l s a l o n g the c e n t e r

(45)

40 if C o l o r != ’ R ’ and C o l o r != ’ B ’: 41 C o l o r = F i n d C o l o r ( P i c t u r e [int( x ) +int( ( 1 / 3 2 ) * W i d t h ) , int( y ) +int( ( 1 / 3 2 ) * H e i g h t ) ]) 42 if C o l o r != ’ R ’ and C o l o r != ’ B ’: 43 C o l o r = F i n d C o l o r ( P i c t u r e [int( x ) -int( ( 1 / 3 2 ) * W i d t h ) , int( y ) -int( ( 1 / 3 2 ) * H e i g h t ) ]) 44 if C o l o r == ’ R ’ or C o l o r == ’ B ’: 45 N e w B o a r d . u p d a t e U C ( CountX , CountY , C o l o r ) # U p d a t e the b o a r d w i t h the c o r r e c t c o l o r s 46 C o u n t Y += 1 47 C o u n t X += 1 48

49 # P r i n t the u n c l a s s i f i e d b o a r d . G o o d t o o l to see if the c o l o r r e c o g n i t i o n is w o r k i n g as i n t e n d e d . 50 # for q in N e w B o a r d . UC : 51 # p r i n t ( q ) 52 53 for i in r a n g e(0 ,8) : 54 for j in r a n g e(0 ,8) : 55 # If a s q u a r e now h o l d s a p i e c e it d i d n t before , t h i s is w h e r e a p i e c e has m o v e d . 56 if N e w B o a r d . UC [ i ][ j ] == ’ R ’ and P r e v B o a r d . UC [ i ][ j ] != ’ R ’ : 57 N e w R a n k = R a n k s [ i ] 58 N e w F i l e = F i l e s [ j ] 59 # If a s q u a r e now d o e s n ’ t h o l d a p i e c e and it p r e v i o u s l y did , t h i s is w h e r e a p i e c e has m o v e d f r o m . 60 if N e w B o a r d . UC [ i ][ j ] != ’ R ’ and P r e v B o a r d . UC [ i ][ j ] == ’ R ’ : 61 O l d R a n k = R a n k s [ i ] 62 O l d F i l e = F i l e s [ j ]

63 # U p d a t e the b o a r d u s i n g the new and old r a n k s and f i l e s .

64 P r e v B o a r d . u p d a t e C B o a r d ( R a n k s . i n d e x ( N e w R a n k ) , F i l e s . i n d e x ( N e w F i l e ) , R a n k s . i n d e x ( O l d R a n k ) , F i l e s . i n d e x ( O l d F i l e ) ) 65 # U p d a t e the u n c l a s s i f i e d b o a r d . 66 P r e v B o a r d . UC = N e w B o a r d . UC 67 68 r e t u r n P r e v B o a r d

(46)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 # T h i s is a f u n c t i o n t h a t c r e a t e s a s t r i n g in Forsyth - E d w a r d s N o t a t i o n d e s c r i b i n g the c u r r e c t p o s i t i o n .

12 # In Forsyth - E d w a r d s N o t a t i o n the p o s i t i o n is d e s c r i b e d by m e r g i n g the a b b r e v i a t i o n s

13 # of the p i e c e s o c c u y i n g e a c h square , r a n k by rank , w h e r e an e m p t y s q u a r e is m a r k e d w i t h a ’1 ’ , m u l t i p l e e m p t y s q u a r e s

14 # n e x t to e a c h o t h e r are a d d e d t o g e t h e r and r a n k s are s e p a r a t e d by a ’/ ’.

15 # A d d e d to t h i s s t r i n g is t h e n w h a t c o l o r is n e x t to m o v e and c a s t l i n g a v a i l a b i l i t y , s e p a r a t e d by a s p a c e .

16 # Who is n e x t to m o v e is s i m p l y s h o w n by p u t t i n g a ’ b ’ for b l a c k and ’ w ’ for w h i t e . 17 # C a s t l i n g a v a i l a b i l i t y is s h o w n by p u t t i n g K / k for k i n g s i d e and Q / q for q u e e n s i d e for w h i t e r e s p e c t i v e b l a c k . 18 def C r e a t e _ F E N ( Board , C o l o r ) : 19 Num = [’ 1 ’,’ 2 ’,’ 3 ’,’ 4 ’,’ 5 ’,’ 6 ’,’ 7 ’] 20 S e p a r a t o r = ’ ’ 21 FEN = [] 22 for L i n e in B o a r d : 23 L i n e 2 = ( S e p a r a t o r . j o i n ( L i n e ) + ’ / ’) # M e r g i n g one r a n k at a time , and a d d i n g a ’/ ’ at the end .

24 FEN += L i n e 2 # M e r g i n g the r a n k s i n t o one l i n e .

25 for x in r a n g e(len( FEN ) ) : # T h i s l o o p a d d s up the 1 ’ s t h a t are n e x t to e a c h o t h e r

26 if FEN [ x ] in Num and FEN [ x +1] in Num :

27 FEN [ x +1] = str(int( FEN [ x ]) + int( FEN [ x + 1 ] ) ) 28 FEN [ x ] = ’ ’

29 FEN = FEN [: -1] # R e m o v i n g the l a s t ’/ ’

30 FEN = S e p a r a t o r . j o i n ( FEN ) + ’ ’ + C o l o r # Add the c o l o r t h a t is n e x t to m o v e .

31

32 r e t u r n FEN

(47)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t p i g p i o 12 i m p o r t t i m e 13 i m p o r t RPi . G P I O as G P I O 14 f r o m P i c k U p i m p o r t * 15 16 def D r o p P i e c e () : 17 G P I O . s e t m o d e ( G P I O . BCM ) 18 M A G N E T = 10 19 G P I O . s e t u p ( MAGNET , G P I O . OUT ) 20 G P I O . o u t p u t ( MAGNET , G P I O . LOW )

(48)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 # T h i s f u n c t i o n t a k e s an i n p u t pixel , a n a l y s e s it ’ s rgb - v a l u e s and r e t u r n s w h a t c o l o r it is . 12 # The c o n s t r a i n s d e c i d i n g w h a t s h o u l d be c o n s i d e r e d w h a t c o l o r w e r e d e p e n d e n t by the l i g h t i n g and w e r e t e s t e d u n t i l f o u n d 13 # s a t i s f a c t o r y 14 def F i n d C o l o r ( bgr ) :

15 # If the b l u e v a l u e s of the p i x e l w e r e m o r e t h a n 50 a b o v e the red v a l u e s of the pixel , the c o l o r was c o n s i d e r e d b l u e .

16 if bgr [0] > bgr [2] + 50: 17 c o l o r = ’ B ’

18 # If the red v a l u e s of the p i x e l w e r e m o r e t h a n 50 a b o v e the b l u e v a l u e s of the pixel , the c o l o r was c o n s i d e r e d red .

19 e l i f bgr [2] > bgr [0] + 50: 20 c o l o r = ’ R ’

21 # If the c o l o r is not red or blue , it set to ’ - ’.

22 e l s e:

23 c o l o r = ’ - ’

24 r e t u r n c o l o r

(49)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t n u m p y as np 12 i m p o r t cv2 13 14 # T h i s f u n c t i o n u s e s m u l t i p l e cv2 - f u n c t i o n s to f i n d the i n n e r c o r n e r s of the c h e s s b o a r d .

15 # T h r o u g h e x t r a p o l a t i o n we t h e n f i n d the o u t e r c o r n e r s of the board , and r e t u r n t h e s e p o i n t s . 16 def f i n d p o i n t s ( i m a g e ) : 17 # T h i s is a t e r m i n a t i o n c r i t e r i a 18 c r i t e r i a = ( cv2 . T E R M _ C R I T E R I A _ E P S + cv2 . T E R M _ C R I T E R I A _ M A X _ I T E R , 30 , 0 . 0 0 1 ) 19 img = cv2 . i m r e a d ( i m a g e ) # R e a d s the i m a g e 20 g r a y = cv2 . c v t C o l o r ( img , cv2 . C O L O R _ B G R 2 G R A Y ) # C o n v e r t s the i m a g e to g r a y s c a l e . 21 ret = F a l s e 22 # F i n d the c h e s s b o a r d i n n e r c o r n e r s 23 ret , c o r n e r s = cv2 . f i n d C h e s s b o a r d C o r n e r s ( gray , (7 ,7) ) 24

25 # If c o r n e r s are found , o b j e c t and i m a g e p o i n t s are a d d e d .

26 # T h i s s e c t i o n is not n e e d e d but can p r o v i d e f u t h e r u n d e r s t a n d i n g of the p r o g r a m

27 # as it d i s p l a y s the i d e n t i f i e d c h e s s b o a r d c o r n e r s on top of the i m a g e .

28 if ret == T r u e :

29 cv2 . c o r n e r S u b P i x ( gray , corners , (11 ,11) , ( -1 , -1) , c r i t e r i a ) 30 cv2 . d r a w C h e s s b o a r d C o r n e r s ( img , (7 ,7) , corners , ret )

31 cv2 . i m s h o w (’ img ’, img ) 32 cv2 . i m w r i t e (’ i m g c o r n e r s . jpg ’, img ) 33 cv2 . w a i t K e y (0) 34 cv2 . d e s t r o y A l l W i n d o w s () 35 36 # E x t r a p o l a t i n g to f i n d the o u t e r c o r n e r s of the c h e s s b o a r d . 37 # T h e s e c o r n e r s are t h e n r e t u r n e d . 38 T o p _ L e f t _ x = 2* c o r n e r s [ 0 ] [ 0 ] [ 0 ] - c o r n e r s [ 8 ] [ 0 ] [ 0 ] 39 T o p _ L e f t _ y = 2* c o r n e r s [ 0 ] [ 0 ] [ 1 ] - c o r n e r s [ 8 ] [ 0 ] [ 1 ] 40 T o p _ R i g h t _ x = 2* c o r n e r s [ 6 ] [ 0 ] [ 0 ] - c o r n e r s [ 1 2 ] [ 0 ] [ 0 ] 41 T o p _ R i g h t _ y = 2* c o r n e r s [ 6 ] [ 0 ] [ 1 ] - c o r n e r s [ 1 2 ] [ 0 ] [ 1 ] 42 B o t t o m _ L e f t _ x = 2* c o r n e r s [ 4 2 ] [ 0 ] [ 0 ] - c o r n e r s [ 3 6 ] [ 0 ] [ 0 ] 43 B o t t o m _ L e f t _ y = 2* c o r n e r s [ 4 2 ] [ 0 ] [ 1 ] - c o r n e r s [ 3 6 ] [ 0 ] [ 1 ] 44 B o t t o m _ R i g h t _ x = 2* c o r n e r s [ 4 8 ] [ 0 ] [ 0 ] - c o r n e r s [ 4 0 ] [ 0 ] [ 0 ] 45 B o t t o m _ R i g h t _ y = 2* c o r n e r s [ 4 8 ] [ 0 ] [ 1 ] - c o r n e r s [ 4 0 ] [ 0 ] [ 1 ] 46 B o t t o m _ L e f t = ( B o t t o m _ L e f t _ x , B o t t o m _ L e f t _ y ) 47 B o t t o m _ R i g h t = ( B o t t o m _ R i g h t _ x , B o t t o m _ R i g h t _ y ) 48 T o p _ L e f t = ( T o p _ L e f t _ x , T o p _ L e f t _ y )

(50)

49 T o p _ R i g h t = ( T o p _ R i g h t _ x , T o p _ R i g h t _ y ) 50

51 r e t u r n T o p _ L e f t , T o p _ R i g h t , B o t t o m _ L e f t , B o t t o m _ R i g h t

(51)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t RPi . G P I O as G P I O 12 f r o m t i m e i m p o r t s l e e p 13 f r o m P o s i t i o n i m p o r t * 14 Pos = P o s i t i o n () 15 16

17 # T h i s f u n c t i o n d e c i d e s the m o v e m e n t of the S C A R A . R e t u r n i n g the p o s i t i o n a f t e r m o v e m e n t . 18 def G o T o P o s ( angles , C u r r e n t = [0 , 0]) : 19 # S e t t i n g up the G P I O p i n s . 20 S T E P 1 = 21 21 D I R 1 = 20 22 S T E P 2 = 19 23 D I R 2 = 26 24 CW = 1 25 CCW = 0 26 M O D E 1 = (23 , 24 , 25) 27 M O D E 2 = (14 , 15 , 18) 28 G P I O . s e t m o d e ( G P I O . BCM ) 29 G P I O . s e t u p ( DIR1 , G P I O . OUT ) 30 G P I O . s e t u p ( DIR2 , G P I O . OUT ) 31 G P I O . s e t u p ( STEP1 , G P I O . OUT ) 32 G P I O . s e t u p ( STEP2 , G P I O . OUT ) 33 G P I O . s e t u p ( MODE1 , G P I O . OUT ) 34 G P I O . s e t u p ( MODE2 , G P I O . OUT ) 35 36 # S e t t i n g s u s e d w h e n micro - s t e p p i n g w i t h the s t e p p e r m o t o r s . 37 R E S O L U T I O N = {’ F U L L ’ : (0 , 0 , 0) , 38 ’ H A L F ’: (1 , 0 , 0) , 39 ’ 1/4 ’ : (0 , 1 , 0) , 40 ’ 1/8 ’ : (1 , 1 , 0) , 41 ’ 1 / 1 6 ’: (0 , 0 , 1) , 42 ’ 1 / 3 2 ’: (1 , 0 , 1) } 43 44 # S e t t i n g the m o d e to 1 / 3 2 s t e p s 45 G P I O . o u t p u t ( MODE1 , R E S O L U T I O N [’ 1 / 3 2 ’]) 46 G P I O . o u t p u t ( MODE2 , R E S O L U T I O N [’ 1 / 3 2 ’]) 47 # The d e l a y b e t w e e n s t e p s . 48 d e l a y = 0 . 0 0 0 0 3 5 49

50 # S e t t i n g how m a n y d e g r e e s are to be m o v e d for e a c h m o t o r .

51 # T h i s is d o n e by s u b t r a c t i n g the c u r r e n t p o s i t i o n f r o m the d e s i r e d p o s i t i o n .

(52)

53 T O D E G 1 = a n g l e s [1] - C u r r e n t [1] + T O D E G 2 54

55 G e a r _ R a t i o = 3.6 # G e a r r a t i o b e t w e e n the m o t o r s and the a r m s

56 DPS = 0.9 # D e g r e e s per s t e p 57 M i c r o S t e p = 32 # C o m p e n s a t i n g for m i c r o s t e p p i n g 58 59 # C a l c u l a t i n g the s t e p c o u n t s for e a c h m o t o r . 60 S t e p _ C o u n t 1 = int(( T O D E G 1 * G e a r _ R a t i o * DPS * M i c r o S t e p ) ) 61 S t e p _ C o u n t 2 = int(( T O D E G 2 * G e a r _ R a t i o * DPS * M i c r o S t e p ) ) 62 63 # D e c i d i n g w h a t arm m o v e s f i r s t . N e g a t i v e d e g r e e s r e s u l t s in a counter - c l o c k w i s e m o v e m e n t . 64 # S e t t i n g the G P I O s i g n a l to H I G H r e s u l t s in a s t e p f r o m the s t e p p e r m o t o r . T h e r e is a d e l a y to p e r f o r m the s t e p . T h e n g o e s to LOW . 65 # T h i s is l o o p e d o v e r the e n t i r e s t e p c o u n t . 66 if T O D E G 2 > 0: 67 G P I O . o u t p u t ( DIR2 , CW ) 68 for x in r a n g e( S t e p _ C o u n t 2 ) : 69 G P I O . o u t p u t ( STEP2 , G P I O . H I G H ) 70 s l e e p ( d e l a y ) 71 G P I O . o u t p u t ( STEP2 , G P I O . LOW ) 72 s l e e p ( d e l a y ) 73 74 if T O D E G 1 > 0: 75 G P I O . o u t p u t ( DIR1 , CW ) 76 for x in r a n g e( S t e p _ C o u n t 1 ) : 77 G P I O . o u t p u t ( STEP1 , G P I O . H I G H ) 78 s l e e p ( d e l a y ) 79 G P I O . o u t p u t ( STEP1 , G P I O . LOW ) 80 s l e e p ( d e l a y ) 81 82 if T O D E G 1 < 0: 83 G P I O . o u t p u t ( DIR1 , CCW ) 84 for x in r a n g e(abs( S t e p _ C o u n t 1 ) ) : 85 G P I O . o u t p u t ( STEP1 , G P I O . H I G H ) 86 s l e e p ( d e l a y ) 87 G P I O . o u t p u t ( STEP1 , G P I O . LOW ) 88 s l e e p ( d e l a y ) 89 90 if T O D E G 2 < 0: 91 G P I O . o u t p u t ( DIR2 , CCW ) 92 for x in r a n g e(abs( S t e p _ C o u n t 2 ) ) : 93 G P I O . o u t p u t ( STEP2 , G P I O . H I G H ) 94 s l e e p ( d e l a y ) 95 G P I O . o u t p u t ( STEP2 , G P I O . LOW ) 96 s l e e p ( d e l a y ) 97

98 # G P I O . c l e a n u p () # C l e a n s up the G P I O setup , r e s e t t i n g the p i n s b a c k to i n p u t .

99 r e t u r n a n g l e s

(53)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 f r o m B o a r d i m p o r t * 12 # S e t t i n g up and r e t u r n i n g the f i r s t C h e s s B o a r d . 13 def I n i t i a t e () : 14 C h e s s B o a r d = B o a r d () 15 C h e s s B o a r d . f i r s t U C () 16 r e t u r n C h e s s B o a r d

(54)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 # F u n c t i o n t h a t u p d a t e s the b o a r d w i t h the m o v e t h a t S t o c k f i s h d e e m s b e s t . 12 # S t o c k f i s h l a b e l s e a c h s q u a r e w i t h a n u m b e r f r o m 0 to 63 , s t a r t i n g w i t h a1 , a2 ,... , h7 . h8 .

13 # I n t e g e r d i v i s i o n w i t h 8 y i e l d s the i n d e x of the rank , and the r e m a i n d e r y i e l d s the i n d e x of the f i l e .

14 # Our b o a r d s h a v e t h e i r r a n k s i n v e r s e d c o m p a r e d to S t o c k f i s h , t h i s is s o l v e d by s i m p l y u s i n g an l i s t

15 # w i t h i n v e r s e d i n d i c e s .

16 def M a k e C o m p M o v e ( Board , From , To ) :

17 R a n k s = [’ 8 ’, ’ 7 ’, ’ 6 ’, ’ 5 ’, ’ 4 ’, ’ 3 ’, ’ 2 ’, ’ 1 ’] 18 R a n k = R a n k s . i n d e x (str(( To // 8) +1) ) 19 F i l e = To % 8 20 O l d F i l e = F r o m % 8 21 O l d R a n k = R a n k s . i n d e x (str(( F r o m // 8) +1) ) 22 23 # U p d a t i n g the b o a r d . 24 B o a r d . UC [ R a n k ][ F i l e ] = B o a r d . UC [ O l d R a n k ][ O l d F i l e ] 25 B o a r d . C [ R a n k ][ F i l e ] = B o a r d . C [ O l d R a n k ][ O l d F i l e ] 26 B o a r d . UC [ O l d R a n k ][ O l d F i l e ] = ’ 1 ’ 27 B o a r d . C [ O l d R a n k ][ O l d F i l e ] = ’ 1 ’ 28 29 r e t u r n B o a r d

(55)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t cv2 12 f r o m t r a n s f o r m i m p o r t f o u r _ p o i n t _ t r a n s f o r m 13 f r o m p i c a m e r a i m p o r t P i C a m e r a 14 f r o m t i m e i m p o r t s l e e p 15 i m p o r t n u m p y as np 16

17 # F u n c t i o n t h a t c a p t u r e s an image , and u s e s the f u n c t i o n f o u r _ p o i n t _ t r a n s f o r m

18 # to w a r p and s c a l e the i m a g e i n t o a s q u a r e o n l y c o n t a i n i n g the c h e s s b o a r d 19 def N e w I m a g e ( pts , c a m e r a ) : 20 c a m e r a = P i C a m e r a () # S t a r t s c a m e r a 21 c a m e r a . r e s o l u t i o n = (1080 , 1 0 8 0 ) # S e t s the r e s o l u t i o n 22 c a m e r a . s t a r t _ p r e v i e w () # S t a r t s p r e v i e w 23 s l e e p ( 0 . 1 ) # G i v e s t i m e for c a m e r a to f o c u s 24 c a m e r a . c a p t u r e (’ / h o m e / pi / D e s k t o p / V8 .2/ N e w I m g . jpg ’) # C a p t u r e s and s a v e s i m a g e 25 c a m e r a . s t o p _ p r e v i e w () # C l o s e s p r e v i e w 26 # c a m e r a . c l o s e () # C l o s e s c a m e r a i n s t a n c e 27 28 i m g 2 = ’ N e w I m g . jpg ’ 29 img = cv2 . i m r e a d ( i m g 2 ) # R e a d s the i m a g e so t h a t it may be p r o c e s s e d 30 W a r p e d = f o u r _ p o i n t _ t r a n s f o r m ( img , pts ) # U s i n g a f o u r p o i n t t r a n s f o r m to w a r p the i m a g e . 31 32 cv2 . i m w r i t e (’ / h o m e / pi / D e s k t o p / V8 / N e w I m g . jpg ’, W a r p e d ) # S a v i n g the w a r p e d i m a g e 33 34 35 36 r e t u r n ’ N e w I m g . jpg ’ # R e t u r n s the p a t h of the w a r p e d i m a g e .

(56)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t p i g p i o 12 i m p o r t t i m e 13 i m p o r t RPi . G P I O as G P I O 14 15 def P i c k U p ( Pos ) : 16 17 R a n k s = [’ 8 ’, ’ 7 ’, ’ 6 ’, ’ 5 ’, ’ 4 ’, ’ 3 ’, ’ 2 ’, ’ 1 ’] 18 R a n k = R a n k s . i n d e x (str(( Pos // 8) +1) ) 19

20 # The d i s t a n c e to the b o a r d d i f f e r s due to i n c o n s i s t e n s i e s in the m a t e r i a l . 21 # T h e r e f o r e the d u r a t i o n the s e r v o is on a l s o d i f f e r s . 22 D e l a y L i s t = [1 , 1 , 1.1 , 1.25 , 1.25 , 1.25 , 1 . 2 5 , 1 . 2 7 ] 23 D e l a y L i s t = [ x - 0 . 1 5 for x in D e l a y L i s t ] 24 D e l a y = D e l a y L i s t [ R a n k ] 25 26 M A G N E T = 10 27 pi = p i g p i o . pi () # M o d u l e to c o n t r o l the s e r v o 28 G P I O . s e t m o d e ( G P I O . BCM ) 29 G P I O . s e t u p ( MAGNET , G P I O . OUT ) 30 G P I O . o u t p u t ( MAGNET , G P I O . H I G H ) 31 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 1 0 0 0 ) # Max c l o c k w i s e ( d o w n ) 32 t i m e . s l e e p ( D e l a y ) 33 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 0) # S t i l l 34 t i m e . s l e e p ( 1 . 5 ) 35 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 2 0 0 0 ) # Max counter - c l o c k w i s e ( up ) 36 t i m e . s l e e p ( D e l a y * 1 . 1 ) 37 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 0)

(57)

1 # C h e s s P l a y i n g R o b o t 2 # S c h a c k s p e l a n d e R o b o t 3 # D a t u m : 2 0 2 0 - 05 - 24 4 # S k r i v e n av : F r e d r i k B a l d h a g e n och A n t o n H e d s t r o m 5 # E x a m i n a t o r : N i h a d S u b a s i c 6 # TRITA - nr : 2 0 2 0 : 4 1 7 # K u r s k o d : M F 1 3 3 X 8 9 # K a n d i d a t e x a m e n s a r b e t e pa KTH i n o m M e k a t r o n i k 10 11 i m p o r t p i g p i o 12 i m p o r t t i m e 13 i m p o r t RPi . G P I O as G P I O 14 f r o m P i c k U p i m p o r t * 15 16 # F u n c t i o n to p l a c e a p i e c e t h a t has b e e n p i c k e d up . D e l a y d e p e n d s on w h i c h r a n k 17 # due to i n c o n s i s t e n s i e s in the m a t e r i a l . 18 def P l a c e P i e c e ( Pos ) : 19 R a n k s = [’ 8 ’, ’ 7 ’, ’ 6 ’, ’ 5 ’, ’ 4 ’, ’ 3 ’, ’ 2 ’, ’ 1 ’] 20 R a n k = R a n k s . i n d e x (str(( Pos // 8) +1) ) 21 22 D e l a y L i s t = [1 , 1 , 1.1 , 1.15 , 1.15 , 1.25 , 1 . 2 5 , 1 . 2 7 ] 23 D e l a y L i s t = [ x - 0 . 1 5 for x in D e l a y L i s t ] 24 D e l a y = D e l a y L i s t [ R a n k ] 25 26 M A G N E T = 10 27 G P I O . s e t m o d e ( G P I O . BCM ) 28 G P I O . s e t u p ( MAGNET , G P I O . OUT ) 29 G P I O . o u t p u t ( MAGNET , G P I O . H I G H ) 30 pi = p i g p i o . pi () 31 32 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 1 0 0 0 ) 33 t i m e . s l e e p ( D e l a y ) 34 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 0) 35 t i m e . s l e e p ( 0 . 5 ) 36 G P I O . o u t p u t ( MAGNET , G P I O . LOW ) 37 t i m e . s l e e p ( 0 . 5 ) 38 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 2 0 0 0 ) 39 t i m e . s l e e p ( D e l a y * 1 . 1 ) 40 pi . s e t _ s e r v o _ p u l s e w i d t h (13 , 0)

References

Related documents

Generella styrmedel kan ha varit mindre verksamma än man har trott De generella styrmedlen, till skillnad från de specifika styrmedlen, har kommit att användas i större

Närmare 90 procent av de statliga medlen (intäkter och utgifter) för näringslivets klimatomställning går till generella styrmedel, det vill säga styrmedel som påverkar

I dag uppgår denna del av befolkningen till knappt 4 200 personer och år 2030 beräknas det finnas drygt 4 800 personer i Gällivare kommun som är 65 år eller äldre i

Detta projekt utvecklar policymixen för strategin Smart industri (Näringsdepartementet, 2016a). En av anledningarna till en stark avgränsning är att analysen bygger på djupa

DIN representerar Tyskland i ISO och CEN, och har en permanent plats i ISO:s råd. Det ger dem en bra position för att påverka strategiska frågor inom den internationella

Indien, ett land med 1,2 miljarder invånare där 65 procent av befolkningen är under 30 år står inför stora utmaningar vad gäller kvaliteten på, och tillgången till,

Av 2012 års danska handlingsplan för Indien framgår att det finns en ambition att även ingå ett samförståndsavtal avseende högre utbildning vilket skulle främja utbildnings-,

Det är detta som Tyskland så effektivt lyckats med genom högnivåmöten där samarbeten inom forskning och innovation leder till förbättrade möjligheter för tyska företag i