-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrobot.py
79 lines (63 loc) · 3.15 KB
/
robot.py
1
2
3
4
5
6
7
8
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright (c) 2017-2018 FIRST. All Rights Reserved.
# Open Source Software - may be modified and shared by FRC teams. The code
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
import wpilib
from rev import ColorSensorV3, ColorMatch
class MyRobot(wpilib.TimedRobot):
"""
This is a simple example to show the values that can be read from the REV
Color Sensor V3
"""
def robotInit(self):
self.colorSensor = ColorSensorV3(wpilib.I2C.Port.kOnboard)
# A Rev Color Match object is used to register and detect known colors. This can
# be calibrated ahead of time or during operation.
#
# This object uses a simple euclidian distance to estimate the closest match
# with given confidence range.
self.colorMatcher = ColorMatch()
# Note: Any example colors should be calibrated as the user needs, these
# are here as a basic example.
self.blueTarget = wpilib.Color(0.143, 0.427, 0.429)
self.greenTarget = wpilib.Color(0.197, 0.561, 0.240)
self.redTarget = wpilib.Color(0.561, 0.232, 0.114)
self.yellowTarget = wpilib.Color(0.361, 0.524, 0.113)
self.colorMatcher.addColorMatch(self.blueTarget)
self.colorMatcher.addColorMatch(self.greenTarget)
self.colorMatcher.addColorMatch(self.redTarget)
self.colorMatcher.addColorMatch(self.yellowTarget)
def robotPeriodic(self):
# The method GetColor() returns a normalized color value from the sensor and can be
# useful if outputting the color to an RGB LED or similar. To
# read the raw color, use GetRawColor().
# The color sensor works best when within a few inches from an object in
# well lit conditions (the built in LED is a big help here!). The farther
# an object is the more light from the surroundings will bleed into the
# measurements and make it difficult to accurately determine its color.
detectedColor = self.colorSensor.getColor()
# Run the color match algorithm on our detected color. The confidence
# specifies how close the detectedColor has to be to one of your colors
# for it to match.
match, confidence = self.colorMatcher.matchClosestColor(detectedColor)
colorString = ""
if match == self.blueTarget:
colorString = "Blue"
elif match == self.redTarget:
colorString = "Red"
elif match == self.greenTarget:
colorString = "Green"
elif match == self.yellowTarget:
colorString = "Yellow"
else: # match is black
colorString = "Unknown"
# Open Smart Dashboard or Shuffleboard to see the color detected by the
# sensor.
wpilib.SmartDashboard.putNumber("Red", detectedColor.red)
wpilib.SmartDashboard.putNumber("Green", detectedColor.green)
wpilib.SmartDashboard.putNumber("Blue", detectedColor.blue)
wpilib.SmartDashboard.putNumber("Confidence", confidence)
wpilib.SmartDashboard.putString("Detected Color", colorString)
if __name__ == "__main__":
wpilib.run(MyRobot)