<commit_before>#!/usr/bin/env python3

# Drive MIP in open-loop as a car

import math
import time
import warnings
import numpy as np
import sys, tty, termios
import threading

import pyctrl

def brief_warning(message, category, filename, lineno, line=None):
    return "*{}\n".format(message)

warnings.formatwarning = brief_warning

# read key stuff

ARROW_UP    = "\033[A"
ARROW_DOWN  = "\033[B"
ARROW_RIGHT = "\033[C"
ARROW_LEFT  = "\033[D"
DEL         = "."
END         = "/"
SPACE       = " "

def read_key():

    key = sys.stdin.read(1)
    if ord(key) == 27:
        key = key + sys.stdin.read(2)
    elif ord(key) == 3:
        raise KeyboardInterrupt    

    return key

def get_arrows(mip, fd):

    pwm = 0
    steer_reference = 0.5
    
    tty.setcbreak(fd)
    while mip.get_state() != pyctrl.EXITING:
        
        print('\rmotor = {:+5.0f} %'
              '  steering = {:+5.2f} %'
              .format(pwm,
                      100*(steer_reference-0.5)),
              end='')
        
        key = read_key()
        if key == ARROW_LEFT:
            steer_reference = max(steer_reference - 20/360, 0)
            mip.set_signal('steer_reference', steer_reference)
        elif key == ARROW_RIGHT:
            steer_reference = min(steer_reference + 20/360, 1)
            mip.set_signal('steer_reference', steer_reference)
        elif key == ARROW_UP:
            pwm = pwm + 10
            mip.set_signal('pwm', - pwm)
        elif key == ARROW_DOWN:
            pwm = pwm - 10
            mip.set_signal('pwm', - pwm)
        elif key == SPACE:
            pwm = 0
            mip.set_signal('pwm', pwm)
            steer_reference = 0.5
            mip.set_signal('steer_reference', steer_reference)
        elif key == DEL:
            steer_reference = 0.5
            mip.set_signal('steer_reference', steer_reference)
        elif key == END:            
            pwm = 0
            mip.set_signal('pwm', pwm)

def main():

    # import blocks and controller
    from pyctrl.rc.mip import Controller
    from pyctrl.block.container import Container, Input, Output
    from pyctrl.block.system import System, Subtract, Differentiator, Sum, Gain
    from pyctrl.block.nl import ControlledCombination, Product<commit_msg>rc_mip_drive<commit_after>#!/usr/bin/env python3

# Drive MIP in open-loop as a car

import math
import time
import warnings
import numpy as np
import sys, tty, termios
import threading

import pyctrl

def brief_warning(message, category, filename, lineno, line=None):
    return "*{}\n".format(message)

warnings.formatwarning = brief_warning

# read key stuff

ARROW_UP    = "\033[A"
ARROW_DOWN  = "\033[B"
ARROW_RIGHT = "\033[C"
ARROW_LEFT  = "\033[D"
DEL         = "."
END         = "/"
SPACE       = " "

def read_key():

    key = sys.stdin.read(1)
    if ord(key) == 27:
        key = key + sys.stdin.read(2)
    elif ord(key) == 3:
        raise KeyboardInterrupt    

    return key

def get_arrows(mip, fd):

    pwm = 0
    steer_reference = 0.5
    
    tty.setcbreak(fd)
    while mip.get_state() != pyctrl.EXITING:
        
        print('\rmotor = {:+5.0f} %'
              '  steering = {:+5.2f} %'
              .format(- pwm,
                      100*(steer_reference-0.5)),
              end='')
        
        key = read_key()
        if key == ARROW_LEFT:
            steer_reference = max(steer_reference - 20/360, 0)
            mip.set_signal('steer_reference', steer_reference)
        elif key == ARROW_RIGHT:
            steer_reference = min(steer_reference + 20/360, 1)
            mip.set_signal('steer_reference', steer_reference)
        elif key == ARROW_UP:
            pwm = pwm - 10
            mip.set_signal('pwm', - pwm)
        elif key == ARROW_DOWN:
            pwm = pwm + 10
            mip.set_signal('pwm', - pwm)
        elif key == SPACE:
            pwm = 0
            mip.set_signal('pwm', pwm)
            steer_reference = 0.5
            mip.set_signal('steer_reference', steer_reference)
        elif key == DEL:
            steer_reference = 0.5
            mip.set_signal('steer_reference', steer_reference)
        elif key == END:            
            pwm = 0
            mip.set_signal('pwm', pwm)

def main():

    # import blocks and controller
    from pyctrl.rc.mip import Controller
    from pyctrl.block.container import Container, Input, Output
    from pyctrl.block.system import System, Subtract, Differentiator, Sum, Gain
    from pyctrl.block.nl import ControlledCombination, Product
{'fraction_non_alphanumeric': 0.11219296304492145, 'fraction_numerical': 0.023013941137419783, 'mean_word_length': 1.979564930784443, 'pattern_counts': {'":': 0, '<': 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': '2098272', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1479, 'n_words': 352}