<commit_before>from MiniBotFramework.Sensing.Sensor import Sensor
from MiniBotFramework.Lib.TCS34725 import TCS34725 as CSensor
import smbus, math, time

def distance(p1, p2):
    """ Returns distance between two 3-tuples. 
    Used for evaluating color """
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2)

def normalize(vector):
    """ Returns a 3-element vector as a unit vector. """
    sum = vector[0] + vector[1] + vector[2]
    return (vector[0]/(sum+0.0), vector[1]/(sum+0.0), vector[2]/(sum+0.0))

class ColorSensor(Sensor):
    """ Pre-determined set of colors and corresponding RGB 3-tuples
    that are the basis of color inputs. Sensor inputs will be compared
    with values in this dict to see what "color" it is closest to. """

    def __init__(self, bot, name, pin_number):
        Sensor.__init__(self, bot, name)
        self.pin_number = pin_number
        self.color_sensor = CSensor()
        # self.bus = smbus.SMBus(1)
        # self.bus.write_byte(0x29,0x80|0x12)
        # ver = self.bus.read_byte(0x29)
        # self.bus.write_byte(0x29, 0x80|0x00) # 0x00 = ENABLE register
        # self.bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
        # self.bus.write_byte(0x29, 0x80|0x14) # Reading results start register 14, LSB then MSB

        self.colors = {
            # TRIAL 1
            # "RED":(7885,2631,3034),
            # "GREEN":(4794,10432,8395),
            # "BLUE":(14162,7582,4268),
            # "ORANGE":(14162,7582,4268),
            # "VIOLET":(8263,7538,9303),
            # "YELLOW":(13772,12879,5783),
            # "PINK":(11483,7839,8267)

            # TRIAL 2 - rugged side of color mat, stationary bot, 100 avg
            # "RED":(151,49,58),
            # "GREEN":(160,357,286),
            # "BLUE":(76,158,198),
            # "ORANGE":(250,134,76),
            # "VIOLET":(137,128,156),
            # "YELLOW":(245,229,103),
            # "PINK":(236,165,172)

            # TRIAL 3 - flat side of color mat, moving bot, 100 avg
            # "RED":(155,68,70),
            # "GREEN":(81,170,139),
            # "BLUE":(73,139,167),
            #"ORANGE":(231,138,83),
            #"VIOLET":(140,138,158),
            # "YELLOW":(241,234,113)#,
            #"PINK":(187,150,150)

            # TRIAL 4 - calibration based
            # "RED":-1,
            # "BLUE":-1,
            # "YELLOW":-1,
            # "GREEN":-1

            # TRIAL 5 - non-calibration
            "RED":(171,76,78),
            "BLUE":(86,161,194),
            "GREEN":(98,210,173),
            "YELLOW":(210,203,100),
            "VIOLET":(142,140,160)
        }
        
        # for color in self.colors:
        #     self.colors_normalized[color] = normalize(self.colors[color])

    def calibrate(self):
        """ Calibrates colors. Takes 100 inputs per color, stores normalized average. """
#         print "================== COLOR CALIBRATION =================="
#         print """Before color-sensing, we must calibrate the colors. Please place
# the corresponding color under the color sensor at recommended distance<commit_msg>Added colors to ColorSensor file<commit_after>from MiniBotFramework.Sensing.Sensor import Sensor
from MiniBotFramework.Lib.TCS34725 import TCS34725 as CSensor
import smbus, math, time

def distance(p1, p2):
    """ Returns distance between two 3-tuples. 
    Used for evaluating color """
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2)

def normalize(vector):
    """ Returns a 3-element vector as a unit vector. """
    sum = vector[0] + vector[1] + vector[2]
    return (vector[0]/(sum+0.0), vector[1]/(sum+0.0), vector[2]/(sum+0.0))

class ColorSensor(Sensor):
    """ Pre-determined set of colors and corresponding RGB 3-tuples
    that are the basis of color inputs. Sensor inputs will be compared
    with values in this dict to see what "color" it is closest to. """

    def __init__(self, bot, name, pin_number):
        Sensor.__init__(self, bot, name)
        self.pin_number = pin_number
        self.color_sensor = CSensor()
        # self.bus = smbus.SMBus(1)
        # self.bus.write_byte(0x29,0x80|0x12)
        # ver = self.bus.read_byte(0x29)
        # self.bus.write_byte(0x29, 0x80|0x00) # 0x00 = ENABLE register
        # self.bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
        # self.bus.write_byte(0x29, 0x80|0x14) # Reading results start register 14, LSB then MSB

        self.colors = {
            # TRIAL 1
            # "RED":(7885,2631,3034),
            # "GREEN":(4794,10432,8395),
            # "BLUE":(14162,7582,4268),
            # "ORANGE":(14162,7582,4268),
            # "VIOLET":(8263,7538,9303),
            # "YELLOW":(13772,12879,5783),
            # "PINK":(11483,7839,8267)

            # TRIAL 2 - rugged side of color mat, stationary bot, 100 avg
            # "RED":(151,49,58),
            # "GREEN":(160,357,286),
            # "BLUE":(76,158,198),
            # "ORANGE":(250,134,76),
            # "VIOLET":(137,128,156),
            # "YELLOW":(245,229,103),
            # "PINK":(236,165,172)

            # TRIAL 3 - flat side of color mat, moving bot, 100 avg
            # "RED":(155,68,70),
            # "GREEN":(81,170,139),
            # "BLUE":(73,139,167),
            #"ORANGE":(231,138,83),
            #"VIOLET":(140,138,158),
            # "YELLOW":(241,234,113)#,
            #"PINK":(187,150,150)

            # TRIAL 4 - calibration based
            # "RED":-1,
            # "BLUE":-1,
            # "YELLOW":-1,
            # "GREEN":-1

            # TRIAL 5 - non-calibration
            "RED":(171,76,78),
            "BLUE":(86,161,194),
            "GREEN":(98,210,173),
            "YELLOW":(210,203,100),
            "VIOLET":(142,140,160),
            "PINK":(195,157,156),
            "ORANGE":(140,83,51)
        }
        
        # for color in self.colors:
        #     self.colors_normalized[color] = normalize(self.colors[color])

    def calibrate(self):
        """ Calibrates colors. Takes 100 inputs per color, stores normalized average. """
#         print "================== COLOR CALIBRATION =================="
#         print """Before color-sensing, we must calibrate the colors. Please place
# the corresponding color under the color sensor at recommended distance
{'fraction_non_alphanumeric': 0.1645470246506516, 'fraction_numerical': 0.11524572146333804, 'mean_word_length': 2.2450331125827816, 'pattern_counts': {'":': 62, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '530204', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2186, 'n_words': 459}