# Color Blob and Line Tracking

## Import KOI 2 Library

```python
from future import *
from koi2 import KOI2
```

## Serial Initialization

```python
koi = KOI2(tx, rx)
```

Initializes the serial connection to KOI.

Parameters:

tx: TX Pin, use P2 for Robotbit EDU

rx: RX Pin, use P12 for Robotbit EDU

## Select Color Blob Tracking Mode

```
koi.setModel(16)
```

Sets the KOI mode to Color Blob Tracking.

## Set Color to Track

```
koi.colorSwitch(color)
```

Parameters:

color: Color to track

<table><thead><tr><th width="286">color</th><th width="249">meaning</th></tr></thead><tbody><tr><td>0</td><td>Red</td></tr><tr><td>1</td><td>Green</td></tr><tr><td>2</td><td>Blue</td></tr><tr><td>3</td><td>Yellow</td></tr><tr><td>9</td><td>Custom(requires calibration)</td></tr></tbody></table>

## Calibrate Custom Color

```
koi.colorCalibration()
```

Calibrates KOI to track a custom color.

## Get Color Blob Data

```
koi.xywh[0]
```

Returns data for the tracked color blob.

Parameters:&#x20;

data: The type of data to return.

<table><thead><tr><th width="324">data</th><th>Meaning</th></tr></thead><tbody><tr><td>0</td><td>X coordinates</td></tr><tr><td>1</td><td>Y coordinates</td></tr><tr><td>2</td><td>Width of blob</td></tr><tr><td>3</td><td>Height of blob</td></tr></tbody></table>

## Sample Program: Color Blob Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(16)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.colorSwitch(0)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.colorSwitch(9)
  screen.fill((0, 0, 0))
  screen.text(koi.xywh[0],5,10,1,(255, 255, 255))
  screen.text(koi.xywh[1],5,30,1,(255, 255, 255))
  screen.text(koi.xywh[2],5,50,1,(255, 255, 255))
  screen.text(koi.xywh[3],5,70,1,(255, 255, 255))
  screen.refresh()

```

{% file src="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2FnjkCwGjvtU5w1j9tVN9C%2FColor%20Blob%20Tracking.py?alt=media&token=9f4f4185-bc06-4139-8a36-b257c16b77d2>" %}

## Select Line Tracking Mode

```
koi.setModel(32)
```

Sets the KOI mode to Color Blob Tracking.

## Set Color to Track

```
koi.lineSwitch(color)
```

Parameters:

color: Color to track

<table><thead><tr><th width="286">color</th><th width="249">meaning</th></tr></thead><tbody><tr><td>0</td><td>Red</td></tr><tr><td>1</td><td>Green</td></tr><tr><td>2</td><td>Blue</td></tr><tr><td>3</td><td>Yellow</td></tr><tr><td>9</td><td>Custom(requires calibration)</td></tr></tbody></table>

## Calibrate Custom Color

```
koi.colorCalibration()
```

Calibrates KOI to track a custom color.

## Get Line Data

```
koi.xy12[data]
```

Returns data for the tracked line.

Parameters:&#x20;

data: The type of data to return.

| data | Meaning        |
| ---- | -------------- |
| 0    | X1 coordinates |
| 1    | Y1 coordinates |
| 2    | X2 coordinates |
| 3    | Y2 coordinates |

## Sample Program: Line Tracking

```
from future import *
from koi2 import KOI2



koi = KOI2('P2', 'P12')
koi.setModel(32)
screen.sync = 0
while True:
  koi.read_from_uart()
  if sensor.btnValue('a'):
    koi.lineSwitch(3)
  if sensor.btnValue('b'):
    koi.colorCalibration()
    koi.lineSwitch(4)
  screen.fill((0, 0, 0))
  screen.text(koi.xy12[0],5,10,1,(255, 255, 255))
  screen.text(koi.xy12[1],5,30,1,(255, 255, 255))
  screen.text(koi.xy12[2],5,50,1,(255, 255, 255))
  screen.text(koi.xy12[3],5,70,1,(255, 255, 255))
  screen.refresh()

```

{% file src="<https://686851495-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7Bv9xBdKh3R9w6Vp7asd%2Fuploads%2FFBAsvNorwJK0BSXfJI9Z%2FLine%20Tracking.py?alt=media&token=8425f5b2-570a-42f8-8891-867b3faecb69>" %}
