Color Blob and Line Tracking
Import KOI 2 Library
from future import *
from koi2 import KOI2
Serial Initialization
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
color
meaning
0
Red
1
Green
2
Blue
3
Yellow
9
Custom(requires calibration)
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:
data: The type of data to return.
data
Meaning
0
X coordinates
1
Y coordinates
2
Width of blob
3
Height of blob
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()
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
color
meaning
0
Red
1
Green
2
Blue
3
Yellow
9
Custom(requires calibration)
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:
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()
Last updated