# Co-Processor - Bilgisayar

## Bilgisayarı Co-processor olarak kullanmak

### &#x20;Renk İşlemek

Eğer Raspberry Pi kullanmadan doğrudan bilgisayar aracılığı ile görüntü işlemek için bilgisayarınıza Python ve belirli kütüphaneleri kurmanız gerekmektedir. Gerekli kütüphaneleri şu şekilde kurabilirsiniz:

`pip install pillow`

`pip install opencv-python`

`pip install pynetworktables`

`pip install imutils`

```python
import numpy as np
from PIL import ImageGrab
from collections import deque
from networktables import NetworkTables
import cv2
import time
import imutils
import argparse
import cv2 as CV

x = 0 #programın ileride hata vermemesi için x 0 olarak tanımlıyorum
y = 0 # programın ileride hata vermemesi için y 0 olarak tanımlıyorum

NetworkTables.initialize(server='roborio-6025-frc.local') # Roborio ile iletişim kuruyoruz

table = NetworkTables.getTable("Vision") # table oluşturuyoruz

#sari rengin algilanmasi
colorLower = (24, 100, 100)
colorUpper = (44, 255, 255)
#converter.py ile convert ettiğiniz rengi buraya giriniz

def screen_record():
    x = 0
    y = 0
    r = 0
    last_time = time.time()
    while(True):
        # 800x600 windowed mode
        printscreen =  np.array(ImageGrab.grab(bbox=(0,40,1024,768)))
        print('Tekrarlanma süresi : {} saniye'.format(time.time()-last_time))
        last_time = time.time()

        frame = printscreen
        frame = imutils.resize(frame, width=600)
        frame = imutils.rotate(frame, angle=0)
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, colorLower, colorUpper)
        mask = cv2.erode(mask, None, iterations=2)
        mask = cv2.dilate(mask, None, iterations=2)
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)[-2]
        center = None
        if len(cnts) > 0:
            c = max(cnts, key=cv2.contourArea)
            ((x, y), radius) = cv2.minEnclosingCircle(c)
            M = cv2.moments(c)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
            if radius > 10:
                cv2.circle(frame, (int(x), int(y)), int(radius),
                (0, 255, 255), 2)
                cv2.circle(frame, center, 5, (0, 0, 255), -1)
        else:
            x = 0
            y = 0

        print("x : ")
        print(x)
        print("y : ")
        print(y)
        table.putNumber("X", x) # roborioya değeri göndermek
        table.putNumber("Y", y) # roborioya değeri göndermek

        cv2.imshow('frame', frame)
        cv2.waitKey(1)
screen_record()
```

Kodunuzu yazıp kaydettikten sonra çalıştırmak için konsoldan şu komutu girmeniz gerekmektedir:

`python yazilimadi.py`

### Javadan Değerleri Çekin!

Kütüphaneleri import edin

```java
import edu.wpi.first.wpilibj.networktables.NetworkTable; // Networktables kütüphanesi
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; // smartdashboardan verileri görmek için
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
```

Class'ınızın altına networktables'i tanımlayın

```java
	public static NetworkTable table1 = NetworkTable.getTable("Vision"); // vision adında table çekiliyor
```

Değerleri okumak için 2 tane void oluşturalım.

```java
public static double konumX()
	{
		return table1.getNumber("X", 0.0); //raspberry pi den gelen x kordinatları
	}
	public static double konumY() 
	{
		return table1.getNumber("Y", 0.0); //raspberry pi den gelen y kordinatları
	}
```

Değerleri SmartDashboard'a yazdıralım.

```java
	public void teleopPeriodic() {
		SmartDashboard.putNumber("Nesnenin X konumu: ", konumX()); // smartdashboarda x konumu yazdır
		SmartDashboard.putNumber("Nesnenin Y konumu: ", konumY()); // smartdashboarda y konumunu yazdır

	}
```

Motorlarımız Değerlere göre haraket ettirelim!

```java
public void autonomousPeriodic() {
	    if(konumX() == 0)
	{
	sagmotor1.set(0);
	sagmotor2.set(0);
	solmotor1.set(0);
	solmotor2.set(0);
	}
		else if(konumX() < 285) // degerler 285'ten kucukse saga don
		{
		sagmotor1.set(0.5); // sag motorları calistir
		sagmotor2.set(0.5);
		}
		else if (konumX() > 295) // degerler 295'ten buyukse sola don
		{
		solmotor1.set(0.5); //sol motorlari calistir
		solmotor2.set(0.5);
		}

	}
```

Kodların tamamına buradan ulaşabilirsiniz :&#x20;

{% embed url="<https://github.com/enisgetmez/FRC-Vision-Processing/blob/master/Java/robot.java>" %}

Artık robotunuzu enable ettiğinizde , yazılımımızı çalıştırdığımızda değerler otomatik olarak SmartDashboard'a düşmeye başlayacaktır.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.frcturkey.org/frc-yazilim/vision-processing-goeruentue-isleme/co-processor-bilgisayar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
