<commit_before>#!/usr/bin/python

# Copyright (c) 2014, Rethink Robotics
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Rethink Robotics nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import copy
import json
import os

from PIL import (
    Image,
    ImageFont
)

import rospy
import rospkg

from baxter_interface import (
  Navigator,
  RobotEnable,
  CameraController,
  Gripper
)
from sensor_msgs.msg import Image as ImageMsg

from .baxter_procs import (
    kill_python_procs,
    mk_process,
    python_proc_ids,
    RosProcess,
)

from .demo_buttons import BrrButton
from .demo_windows import BrrWindow

from .img_proc import (
    cv_to_msg,
    gen_cv,
    overlay,
)

import demo_functions


'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# UI wrapper class for Baxter Research Robot.
# This class has 2 purposes:
#    1) To handle user interaction with the robot and interpret
#            those interactions in a UI context
#    2) To run robot utility functions and button_press functions
#
#
# Initializiation aguments:
#    windows - a dictionary of BrrWindow objects
#              (all of the windows configured in the UI)
#    btn_context - a dictionary containing metadata for the buttons
#                  (what functions they run and what window they take you to
#    commands - a list of strings to be searched through when killing
#                  running example programs
#    share_path - path to this package's share/ folder
#
# Public Parameters:
#    img - The current image being displayed by the UI, in cv format
#    windows - Dict with the full list of windows in the UI
#    active_window - The BrrWindow object currently selected in the UI
#    xdisp - Publisher for xdisplay topic
#    cameras - Dict of Camera objects from baxter_interface
#    camera_sub - Camera subscriber.
#
# Public Methods:
#    selected(self) - Returns the BrrButton selected in the current active
#                         BrrWindow object.
#    draw(self) - Draws windows recursively, sets self.img, and publishes
#                     to the screen.
#    scroll(self, direction) - Calls the scroll function for the active
#                                  window passing the scroll direction.
#    ok_pressed(self, v, side) - Enacts the function for the selected button
#                                    in the active window,
#    back(self, v) - If in a window with a back button, this will kill all
#                        examples and set the previous window as active.
#    kill_examples(self, v) - Kills all processes matching the criteria in
#                                 self.commands
#    error_screen(self, error) - Will display the selected error screen on
#                                    top of the current display.
#                                Sets the error window's parent to preserve
#                                    "back" functionality
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
class BrrUi(object):
    def __init__(self, share_path, conf_path, commands):
        self.share_path = share_path
        self.conf_path = conf_path
        self.windows = dict()
        self._btn_context = dict()

        self._functions = demo_functions
        self._load_config()

        self.img = Image.new('RGB', (1024, 600), 'white')
        self.active_window = self.windows['demo_1']
        self.xdisp = rospy.Publisher('/robot/xdisplay', ImageMsg, latch=True)

        self._status = RobotEnable()
        self._commands = commands
        self._font = ImageFont.truetype(
                '%s/HelveticaLight.ttf' % share_path, 30
        )
        self._textHeight = self._font.getsize('W')[1]
        self._active_example = False

        self._navigators = {'left': Navigator('left'),
                           'right': Navigator('right')}
        self._connect_listeners()

        self._wheel_ok = True

        self.cameras = dict()
        for cam in ['left_hand', 'right_hand', 'head']:
            try:
                self.cameras[cam] = CameraController('%s_camera' % cam)
            except AttributeError:
                self.windows['cam_submenu'].set_btn_selectable('cam_%s' % cam,
                                                               False)
                sel = self.windows['cam_submenu'].selected_btn()
                bad_cam = self.windows['cam_submenu'].get_btn('cam_%s' % cam)
                if (sel == bad_cam and
                    not self.windows['cam_submenu'].scroll(1)):
                    self.windows['cam_submenu'].selected_btn_index = 0

        self.cam_sub = None

        self._l_grip = {'interface': Gripper('left'), 'type': 'custom'}
        self._r_grip = {'interface': Gripper('right'), 'type': 'custom'}
        rospy.Timer(rospy.Duration(.5), self._check_enable)

        self.error_state = False
        self._enable()
        self.calib_stage = 0<commit_msg>initialized _listeners_connected<commit_after>#!/usr/bin/python

# Copyright (c) 2014, Rethink Robotics
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the Rethink Robotics nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import copy
import json
import os

from PIL import (
    Image,
    ImageFont
)

import rospy
import rospkg

from baxter_interface import (
  Navigator,
  RobotEnable,
  CameraController,
  Gripper
)
from sensor_msgs.msg import Image as ImageMsg

from .baxter_procs import (
    kill_python_procs,
    mk_process,
    python_proc_ids,
    RosProcess,
)

from .demo_buttons import BrrButton
from .demo_windows import BrrWindow

from .img_proc import (
    cv_to_msg,
    gen_cv,
    overlay,
)

import demo_functions


'''~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# UI wrapper class for Baxter Research Robot.
# This class has 2 purposes:
#    1) To handle user interaction with the robot and interpret
#            those interactions in a UI context
#    2) To run robot utility functions and button_press functions
#
#
# Initializiation aguments:
#    windows - a dictionary of BrrWindow objects
#              (all of the windows configured in the UI)
#    btn_context - a dictionary containing metadata for the buttons
#                  (what functions they run and what window they take you to
#    commands - a list of strings to be searched through when killing
#                  running example programs
#    share_path - path to this package's share/ folder
#
# Public Parameters:
#    img - The current image being displayed by the UI, in cv format
#    windows - Dict with the full list of windows in the UI
#    active_window - The BrrWindow object currently selected in the UI
#    xdisp - Publisher for xdisplay topic
#    cameras - Dict of Camera objects from baxter_interface
#    camera_sub - Camera subscriber.
#
# Public Methods:
#    selected(self) - Returns the BrrButton selected in the current active
#                         BrrWindow object.
#    draw(self) - Draws windows recursively, sets self.img, and publishes
#                     to the screen.
#    scroll(self, direction) - Calls the scroll function for the active
#                                  window passing the scroll direction.
#    ok_pressed(self, v, side) - Enacts the function for the selected button
#                                    in the active window,
#    back(self, v) - If in a window with a back button, this will kill all
#                        examples and set the previous window as active.
#    kill_examples(self, v) - Kills all processes matching the criteria in
#                                 self.commands
#    error_screen(self, error) - Will display the selected error screen on
#                                    top of the current display.
#                                Sets the error window's parent to preserve
#                                    "back" functionality
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'''
class BrrUi(object):
    def __init__(self, share_path, conf_path, commands):
        self.share_path = share_path
        self.conf_path = conf_path
        self.windows = dict()
        self._btn_context = dict()

        self._functions = demo_functions
        self._load_config()

        self.img = Image.new('RGB', (1024, 600), 'white')
        self.active_window = self.windows['demo_1']
        self.xdisp = rospy.Publisher('/robot/xdisplay', ImageMsg, latch=True)

        self._status = RobotEnable()
        self._commands = commands
        self._font = ImageFont.truetype(
                '%s/HelveticaLight.ttf' % share_path, 30
        )
        self._textHeight = self._font.getsize('W')[1]
        self._active_example = False

        self._navigators = {'left': Navigator('left'),
                           'right': Navigator('right')}

        self._listeners_connected = False
        self._connect_listeners()

        self._wheel_ok = True

        self.cameras = dict()
        for cam in ['left_hand', 'right_hand', 'head']:
            try:
                self.cameras[cam] = CameraController('%s_camera' % cam)
            except AttributeError:
                self.windows['cam_submenu'].set_btn_selectable('cam_%s' % cam,
                                                               False)
                sel = self.windows['cam_submenu'].selected_btn()
                bad_cam = self.windows['cam_submenu'].get_btn('cam_%s' % cam)
                if (sel == bad_cam and
                    not self.windows['cam_submenu'].scroll(1)):
                    self.windows['cam_submenu'].selected_btn_index = 0

        self.cam_sub = None

        self._l_grip = {'interface': Gripper('left'), 'type': 'custom'}
        self._r_grip = {'interface': Gripper('right'), 'type': 'custom'}
        rospy.Timer(rospy.Duration(.5), self._check_enable)

        self.error_state = False
        self._enable()
        self.calib_stage = 0
{'fraction_non_alphanumeric': 0.09529800283063375, 'fraction_numerical': 0.003931435760339676, 'mean_word_length': 2.569744597249509, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5423830', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3208, 'n_words': 1250}