<commit_before>import time
import math
import numpy
import platform, sys, select

from ctrl.client import Controller
import ctrl.block as block
import ctrl.block.logger as logger

# initialize controller
HOST, PORT = "192.168.10.102", 9999
controller = Controller(host = HOST, port = PORT)
controller.add_sink('logger', 
                    logger.Logger(), 
                    ['clock','encoder1','encoder2'])
controller.add_sink('printer', block.Printer(endln = '\r'), 
                    ['clock','motor1', 'encoder1','motor2', 'encoder2'])
    
print(controller.info('all'))

# define tests

def test(k, args, query_msg, failed_msg, test_function):
    print('\n> TEST #{}'.format(k))
    passed, retval = test_function(args)
    if not passed:
        print("> Failed test #{}: {}".format(k, retval))
        sys.exit(2)
    time.sleep(.1)
    if query_msg:
        answer = input('\n< ' + query_msg + ' [Y/n]').lower()
        if 'n' in answer:
            print("> Failed test #{}: {}".format(k, failed_msg))
            sys.exit(2)
    return retval

def test_motor_forward(args):

    motor, encoder = args[0], args[1]
    with controller:
        position1 = controller.get_signal(encoder)
        controller.set_signal(motor,100)
        time.sleep(2)
        position2 = controller.get_signal(encoder)
    return True, (position1, position2)

def test_encoder(args):

    position1, position2 = args[0], args[1]
    if position2 == position1:
        return False, 'encoder1 not working'
    elif position2 < position1:
        return False, 'encoder1 reading reversed'
    return True, []

def test_reset_clock(args):

    t1 = controller.get_signal('clock')
    controller.set_source('clock', reset = True)
    controller.set_signal('motor1',0)
    with controller:
        time.sleep(.1)
    t2 = controller.get_signal('clock')
    if t2 > t1:
        return False, 'clock did not reset ({} > {})'.format(t2,t1)
    return True, []

def test_motor_backward(args):

    with controller:
        controller.set_source('clock', reset = True)
        position1 = controller.get_signal('encoder1')
        controller.set_signal('motor1',-100)
        time.sleep(2)
        position2 = controller.get_signal('encoder1')
    return True, (position1, position2)

def test_motor_speeds(args):

    with controller:
        controller.set_source('clock', reset = True)
        controller.set_signal('motor1',100)
        time.sleep(1)
        controller.set_signal('motor1',50)
        time.sleep(1)
    return True, []

def test_reset_encoder(args):

    with controller:
        controller.set_signal('motor1',0)
        time.sleep(.1) # sleep to make sure it is stoped
        position1 = controller.get_signal('encoder1')
        controller.set_source('encoder1', reset = True)
        time.sleep(.1) # sleep to make sure it did not move
        position2 = controller.get_signal('encoder1')

    if position2 != 0:
        return False, 'could not reset encoder1 ({} != 0)'.format(position2)
    return True, [position1, position2]

def test_potentiometer(args):
    
    # Calibrate potentiometer
    KMAX = 600 
    TOL = 2
    #controller.get_source('pot1', ['full_scale', 'invert'])
    controller.set_source('pot1', full_scale = 1, invert = False)
    inverted = False
    full_scale = 100
    print("> Set the potentiometer to the minimum position and hit <ENTER>") <commit_msg>Added enabled_pin to motor<commit_after>import time
import math
import numpy
import platform, sys, select

from ctrl.client import Controller
import ctrl.block as block
import ctrl.block.logger as logger

# initialize controller
HOST, PORT = "192.168.10.102", 9999
controller = Controller(host = HOST, port = PORT)
controller.add_sink('logger', 
                    logger.Logger(), 
                    ['clock','encoder1','encoder2'])
controller.add_sink('printer', block.Printer(endln = '\r'), 
                    ['clock','motor1', 'encoder1','motor2', 'encoder2'])
    
print(controller.info('all'))

# define tests

def test(k, args, query_msg, failed_msg, test_function):
    print('\n> TEST #{}'.format(k))
    passed, retval = test_function(args)
    if not passed:
        print("> Failed test #{}: {}".format(k, retval))
        sys.exit(2)
    time.sleep(.1)
    if query_msg:
        answer = input('\n< ' + query_msg + ' [Y/n]').lower()
        if 'n' in answer:
            print("> Failed test #{}: {}".format(k, failed_msg))
            sys.exit(2)
    return retval

def test_motor_forward(args):

    motor, encoder = args[0], args[1]
    with controller:
        position1 = controller.get_signal(encoder)
        controller.set_signal(motor,100)
        time.sleep(2)
        position2 = controller.get_signal(encoder)
        controller.set_signal(motor,0)
    return True, (position1, position2)

def test_encoder(args):

    position1, position2 = args[0], args[1]
    if position2 == position1:
        return False, 'encoder1 not working'
    elif position2 < position1:
        return False, 'encoder1 reading reversed'
    return True, []

def test_reset_clock(args):

    t1 = controller.get_signal('clock')
    controller.set_source('clock', reset = True)
    controller.set_signal('motor1',0)
    with controller:
        time.sleep(.1)
    t2 = controller.get_signal('clock')
    if t2 > t1:
        return False, 'clock did not reset ({} > {})'.format(t2,t1)
    return True, []

def test_motor_backward(args):

    with controller:
        controller.set_source('clock', reset = True)
        position1 = controller.get_signal('encoder1')
        controller.set_signal('motor1',-100)
        time.sleep(2)
        position2 = controller.get_signal('encoder1')
        controller.set_signal(motor,0)
    return True, (position1, position2)

def test_motor_speeds(args):

    with controller:
        controller.set_source('clock', reset = True)
        controller.set_signal('motor1',100)
        time.sleep(1)
        controller.set_signal('motor1',50)
        time.sleep(1)
        controller.set_signal(motor,0)
    return True, []

def test_reset_encoder(args):

    with controller:
        controller.set_signal('motor1',0)
        time.sleep(.1) # sleep to make sure it is stoped
        position1 = controller.get_signal('encoder1')
        controller.set_source('encoder1', reset = True)
        time.sleep(.1) # sleep to make sure it did not move
        position2 = controller.get_signal('encoder1')

    if position2 != 0:
        return False, 'could not reset encoder1 ({} != 0)'.format(position2)
    return True, [position1, position2]

def test_potentiometer(args):
    
    # Calibrate potentiometer
    KMAX = 600 
    TOL = 2
    #controller.get_source('pot1', ['full_scale', 'invert'])
    controller.set_source('pot1', full_scale = 1, invert = False)
    inverted = False
    full_scale = 100
    print("> Set the potentiometer to the minimum position and hit <ENTER>") 
{'fraction_non_alphanumeric': 0.12828662236347876, 'fraction_numerical': 0.02903785033227391, 'mean_word_length': 2.9925028835063436, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7418934', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2108, 'n_words': 518}