<commit_before>"""Logic for line following."""

import sys
from time import time
from time import sleep
import numpy as np

import lib.lib as lib
import hardware.ir_hub as ir_hub_mod
import driver.mec_driver as mec_driver_mod
import pid as pid_mod
import hardware.color_sensor as color_sensor_mod


class Follower(object):

    """Follows a line, detects intersections and stop conditions."""

    #Class variables
    #Array_Conditions
    Large_Object = 17  #the array sees a large object
    No_Line = 16 #the array see no line
    Noise = 19 #white values not next to each other
    
    #Variables for read_binary calls
    White_Black = True #False  #True= white line, False= black line

    def __init__(self):
        # Build logger
        self.logger = lib.get_logger()

        # Build subsystems
        self.ir_hub = ir_hub_mod.IRHub()
        self.ir_hub.thrsh = 100
        self.driver = mec_driver_mod.MecDriver()
        self.color = color_sensor_mod.ColorSensor()

        # Build PIDs
        self.strafe = pid_mod.PID()
        self.strafe_error = 0.0
        self.rotate_pid = pid_mod.PID()
        self.rotate_error= 0.0
        self.error = "NONE"
        
        # motor variables
        self.translate_speed =  60
        self.prev_rate = 0

        #state variables
        self.heading = None #must be initialize by callee
        self.front_state = Follower.No_Line
        self.back_state = Follower.No_Line
        self.left_state = Follower.No_Line
        self.right_state = Follower.No_Line

        # post error vars
        self.intersection = False
        self.lost_line = False
        self.timeLastUpdated = -1.0
        self.on_x = False

    @lib.api_call
    def get_translate_speed(self):
        return self.translate_speed

    @lib.api_call
    def set_translate_speed(self,speed):
        self.translate_speed = speed

    @lib.api_call
    def update(self):
        """Read IR values, compute aggregates."""
        ir_readings = self.ir_hub.read_binary(Follower.White_Black)
        for name, reading in ir_readings.iteritems():
            reading_arr = np.int_(reading)  # convert readings to numpy array
            reading_sum = np.sum(np_reading)  # = no. of units lit
            if reading_sum > 0:
                self.ir_agg[name] = (np.sum(self.ir_pos * reading_arr)
                                    / reading_sum)
            else:
                self.ir_agg[name] = None
        self.timeLastUpdated = time.time()
   
    @lib.api_call
    def get_strafe_error(self):
        return self.strafe_error

    @lib.api_call
    def get_rotate_error(self):
        return self.rotate_error

    
    @lib.api_call
    def wait_for_start(self):
        """Poll color senor unti green start signal lights up."""
        return self.color.watch_for_color("green") 

    @lib.api_call
    def is_on_line(self):
        return (not self.lost_line)  # TODO: Use IR sensors to perform check

    @lib.api_call
    def is_on_x(self):
        return self.intersection  # TODO: Use IR sensors to perform check

    @lib.api_call
    def is_on_blue(self):
        return True  # TODO: Use color sensor

    @lib.api_call
    def is_on_red(self):
        return True  # TODO: Use color sensor

    def reset_errors(self):
        self.error = "NONE"
        #state variables
        #self.front_state = Follower.No_Line
        #self.back_state = Follower.No_Line
        #self.left_state = Follower.No_Line
        #self.right_state = Follower.No_Line

        # post error vars
        self.intersection = False
        self.lost_line = False
        self.timeLastUpdated = -1.0
        self.strafe_error = 0.0
        self.rotate_error = 0.0
        
        #reset pids
        self.strafe.clear_error()
        self.rotate_pid.clear_error()
        return 
 
    @lib.api_call
    def follow(self, heading, on_x=False):
        """Follow line along given heading"""
        #reset errors
        self.reset_errors()
        self.on_x = on_x
        # Get the initial conditioni
        self.heading = heading
        previous_time = time()
        # Init front_PID
        self.strafe.set_k_values(8, 0, .1)
        # Inti rotate_PID
        self.rotate_pid.set_k_values(6, 0, 0)
        # Get current heading
        self.heading = heading
        # Continue until an error condition
        count_object=0 

        while True:
            # Assign the current states to the correct heading
            self.assign_states()
            # Check for error conditions
            if self.error != "NONE":
                #require two succesive large_object readings to exit
#                if self.error=="LARGE_OBJECT" and count_object<1:
#                    count_object += 1
#                    continue
                self.update_exit_state()
                self.logger.info("Error: {}".format( self.error ))
                self.logger.info("FS: {}, BS: {}, lS: {}, RS: {}".format( <commit_msg>Fixed set speed for new voltage level?<commit_after>"""Logic for line following."""

import sys
from time import time
from time import sleep
import numpy as np

import lib.lib as lib
import hardware.ir_hub as ir_hub_mod
import driver.mec_driver as mec_driver_mod
import pid as pid_mod
import hardware.color_sensor as color_sensor_mod


class Follower(object):

    """Follows a line, detects intersections and stop conditions."""

    #Class variables
    #Array_Conditions
    Large_Object = 17  #the array sees a large object
    No_Line = 16 #the array see no line
    Noise = 19 #white values not next to each other
    
    #Variables for read_binary calls
    White_Black = True #False  #True= white line, False= black line

    def __init__(self):
        # Build logger
        self.logger = lib.get_logger()

        # Build subsystems
        self.ir_hub = ir_hub_mod.IRHub()
        self.ir_hub.thrsh = 100
        self.driver = mec_driver_mod.MecDriver()
        self.color = color_sensor_mod.ColorSensor()

        # Build PIDs
        self.strafe = pid_mod.PID()
        self.strafe_error = 0.0
        self.rotate_pid = pid_mod.PID()
        self.rotate_error= 0.0
        self.error = "NONE"
        
        # motor variables
        self.translate_speed =  75  
        self.prev_rate = 0

        #state variables
        self.heading = None #must be initialize by callee
        self.front_state = Follower.No_Line
        self.back_state = Follower.No_Line
        self.left_state = Follower.No_Line
        self.right_state = Follower.No_Line

        # post error vars
        self.intersection = False
        self.lost_line = False
        self.timeLastUpdated = -1.0
        self.on_x = False

    @lib.api_call
    def get_translate_speed(self):
        return self.translate_speed

    @lib.api_call
    def set_translate_speed(self,speed):
        self.translate_speed = speed

    @lib.api_call
    def update(self):
        """Read IR values, compute aggregates."""
        ir_readings = self.ir_hub.read_binary(Follower.White_Black)
        for name, reading in ir_readings.iteritems():
            reading_arr = np.int_(reading)  # convert readings to numpy array
            reading_sum = np.sum(np_reading)  # = no. of units lit
            if reading_sum > 0:
                self.ir_agg[name] = (np.sum(self.ir_pos * reading_arr)
                                    / reading_sum)
            else:
                self.ir_agg[name] = None
        self.timeLastUpdated = time.time()
   
    @lib.api_call
    def get_strafe_error(self):
        return self.strafe_error

    @lib.api_call
    def get_rotate_error(self):
        return self.rotate_error

    
    @lib.api_call
    def wait_for_start(self):
        """Poll color senor unti green start signal lights up."""
        return self.color.watch_for_color("green") 

    @lib.api_call
    def is_on_line(self):
        return (not self.lost_line)  # TODO: Use IR sensors to perform check

    @lib.api_call
    def is_on_x(self):
        return self.intersection  # TODO: Use IR sensors to perform check

    @lib.api_call
    def is_on_blue(self):
        return True  # TODO: Use color sensor

    @lib.api_call
    def is_on_red(self):
        return True  # TODO: Use color sensor

    def reset_errors(self):
        self.error = "NONE"
        #state variables
        #self.front_state = Follower.No_Line
        #self.back_state = Follower.No_Line
        #self.left_state = Follower.No_Line
        #self.right_state = Follower.No_Line

        # post error vars
        self.intersection = False
        self.lost_line = False
        self.timeLastUpdated = -1.0
        self.strafe_error = 0.0
        self.rotate_error = 0.0
        
        #reset pids
        self.strafe.clear_error()
        self.rotate_pid.clear_error()
        return 
 
    @lib.api_call
    def follow(self, heading, on_x=False):
        """Follow line along given heading"""
        #reset errors
        self.reset_errors()
        self.on_x = on_x
        # Get the initial conditioni
        self.heading = heading
        previous_time = time()
        # Init front_PID
        self.strafe.set_k_values(8, 0, .1)
        # Inti rotate_PID
        self.rotate_pid.set_k_values(6, 0, .10)
        # Get current heading
        self.heading = heading
        # Continue until an error condition
        count_object=0 

        while True:
            # Assign the current states to the correct heading
            self.assign_states()
            # Check for error conditions
            if self.error != "NONE":
                #require two succesive large_object readings to exit
                if self.error=="LARGE_OBJECT" and count_object<1:
                    count_object += 1
                    continue
                self.update_exit_state()
                self.logger.info("Error: {}".format( self.error ))
                self.logger.info("FS: {}, BS: {}, lS: {}, RS: {}".format( 
{'fraction_non_alphanumeric': 0.08318245825789579, 'fraction_numerical': 0.006940253470126735, 'mean_word_length': 2.0185185185185186, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1507714', 'n_tokens_mistral': 3090, 'n_tokens_neox': 2927, 'n_words': 774}