Example #1
0
class Exit (BlockRenderer):
    size = (.3,) * 3
    size_arrow = (.9,) * 3
    color = (.5,) * 3
    shape_class = shapes.Disc
    color_on = hex_color_f("34B27D")
    color_arr = hex_color_f("DBDB5780")

    def __init__(self, batch, group, x, y, block):
        super(Exit, self).__init__(batch, group, x, y, block)
        self.arrows = shapes.Arrows(batch, group, (x, y, 0), self.size_arrow, self.color_arr)
        self.alpha_arr = Spring(self.color_arr[3], .2, .01)
        self.color_spr = Spring(Vector3(*self.color), .1, .01)
        self.is_on = block.is_on

    def delete(self):
        super(Exit, self).delete()
        self.arrows.delete()

    def update(self):
        if self.is_on != self.block.is_on:
            self.is_on = self.block.is_on
            if self.is_on:
                self.alpha_arr.next_value = 1
                self.color_spr.next_value = Vector3(*self.color_on)
            else:
                self.alpha_arr.next_value = self.color_arr[3]
                self.color_spr.next_value = Vector3(*self.color)

        self.alpha_arr.tick()
        self.color_spr.tick()

        self.shape.vlist.colors[:] = tuple(self.color_spr.value) * self.shape.vertex_count
        self.arrows.vlist.colors[3::4] = (self.alpha_arr.value,) * self.arrows.vertex_count
 def spring_start(self):
     self.spring = Spring(self.time_counter, self.sound_stopper)
     Pin_height = int(self.currentTrial[3])
     if 1 <= Pin_height and Pin_height <= 3:
         self.spring.set_profile(self.currentTrial[2], 'long')
     elif 4 <= Pin_height and Pin_height <= 6:
         self.spring.set_profile(self.currentTrial[2], 'middle')
     elif 7 <= Pin_height and Pin_height <= 9:
         self.spring.set_profile(self.currentTrial[2], 'short')
     self.spring.start()
Example #3
0
    def __init__(self, eye, center, up):
        self.eye = Spring(eye, self.SPEED, self.CLIP)
        self.vec = Spring(center - eye, self.SPEED, self.CLIP)
        self.up = Spring(up, self.SPEED, self.CLIP)
        self.ofs = Spring(0, 0.1, 0.01)

        self.modelview = (GLdouble * 16)()
        self.projection = (GLdouble * 16)()
        self.viewport = (GLint * 4)()
        self.unproj = [GLdouble(), GLdouble(), GLdouble()]
Example #4
0
class Mirror (BlockRenderer):
    size = (.8, .1, .9)
    color = (.1, .9, .9)

    def __init__(self, batch, group, x, y, block):
        group = RotateGroup(x, y, block.slope, parent=group)
        super(Mirror, self).__init__(batch, group, 0, 0, block)

        self.group = group
        self.slope = Spring(block.slope, 0.2, 0.1)

    def update(self):
        self.slope.next_value = self.block.slope
        self.slope.tick()
        self.group.angle = 45 * self.slope.value
Example #5
0
 def init_window(self):
     self.parent.title("SupMold: SpringCalc")
     self.grid(column=0, row=0, sticky="nwse")
     mainFrame = Frame(self, relief='raised', borderwidth=1)
     mainFrame.grid(column=0, row=0, sticky='nsew')
     closeButton = Button(mainFrame, text='Cerrar', command=self.appQuit)
     closeButton.grid(column=0, row=3, padx=5, pady=5)
     nb = Notebook(mainFrame)
     tab1 = Frame(nb)
     tab2 = Frame(nb)
     tab3 = Frame(nb)
     nb.add(tab1, text="Compresión")
     nb.add(tab2, text="Extensión")
     nb.add(tab3, text="Torsión")
     nb.grid(column=0, row=1, sticky='nsew', padx=5, pady=5)
     msg = consoleFrame(mainFrame, 'Mensages')
     dataB = resource_path('wires.db')
     if not os.path.isfile(dataB):
         self.centerWindow()
         messagebox.showerror(
             "ERROR", "Base de datos {} inaccesible. "
             "Favor verificar permisos de lectura o "
             "ubicación de la misma".format(dataB))
         return
     self.sub1 = subWindow(tab1, Spring(database=dataB))
     self.sub1.init_widgets()
     self.sub2 = subWindow2(tab2, eSpring(database=dataB))
     self.sub2.init_widgets()
     self.sub3 = subWindow3(tab3, tSpring(database=dataB))
     self.sub3.init_widgets()
     self.centerWindow()
     nb.bind("<<NotebookTabChanged>>", self.tabChangedEvent)
Example #6
0
    def __init__(self,
                 windowX: int,
                 windowY: int,
                 width=120,
                 height=30,
                 isNew=False,
                 isMoving=False,
                 imagePath='images/floor.png'):
        super().__init__(imagePath, windowX, windowY, width, height)

        self.__isMoving = isMoving  # 地板是否左右移动

        currHeight = Floor.lastHeight + randint(70, 90)
        self._rect.x = randint(0, windowX - width)
        self._rect.y = 50 if isNew else windowY - currHeight
        Floor.lastHeight = currHeight

        # 板子上面的东西:火箭 弹簧 怪物 等...
        self.__itemOn = None
        if randint(0, 100) < 20:
            self.__itemOn = Spring(windowX, windowY)
            self.__itemOn.attachTo(self)
        if randint(0, 100) < 20:
            self.__itemOn = Rocket(windowX, windowY, self.getLeft(),
                                   self.getTop())
            self.__itemOn.attachTo(self)
Example #7
0
class Launcher (BlockRenderer):
    size = (.4, .4, .8)
    color = hex_color_f("4D77CB")
    rotate = (math.pi / 2, 0, 0)
    shape_class = shapes.Pyramid

    def __init__(self, batch, group, x, y, block):
        group = RotateGroup(x, y, block.all_states_idx, parent=group)
        super(Launcher, self).__init__(batch, group, 0, 0, block)

        self.group = group
        self.state = Spring(block.all_states_idx, 0.2, 0.1)

    def update(self):
        self.state.next_value = self.block.all_states_idx
        self.state.tick()
        self.group.angle = 90 * self.state.value
Example #8
0
 def _ChooseNextNodeLocation(self):
     node_list = self.FindAttachmentNodes()
     for node in node_list:
         self.AddObject(Spring(node, self.next_node))
     self.AddObject(self.next_node)
     self.GenerateNextNode()
     sound = random.choice(self.placement_sound_list)
     sound.play()
Example #9
0
def get_func_deriv(k):
    """
    Get the energy value and its derivative for given k value
    """
    f = np.pi
    spr = Spring(k)
    u = spr.solve(0, f)
    E = spr.F(u)
    dRdk = spr.dRdk(u)
    dFdk = spr.dFdk(u)
    dRdq = spr.dRdq(u)
    dFdq = spr.dFdq(u)
    lam = spr.adjoint_solve(u)
    DFDx = dFdk + lam * dRdk
    return E, DFDx
Example #10
0
 def __init__(self, start, board):
     width = 26
     obstacles = []
     obstacles.append(Spring(start + 3, 6, board))
     coin_row(Point(8, start + 6), Dimension(10, 1), board)
     enemy = []
     enemy.append(SlaveEnemy(FLOOR - 2, start, board))
     enemy.append(SlaveEnemy(FLOOR - 2, start + 13, board))
     Hurdle.__init__(self, start, width, obstacles, enemy, board)
     self.load()
Example #11
0
class Popup (object):
    SPEED = 0.2
    SNAP = 0.001
    SLEEP = 60
    POS_Y = 0.3

    def __init__(self, text, color):
        font = PopupFont()
        self.label = pyglet.text.Label(
            text,
            anchor_x = 'center',
            font_name = font.name,
            font_size = font.size,
            anchor_y = 'baseline',
            color = color
        )
        self.offset = Spring(Vector2(1.5, self.POS_Y), self.SPEED, self.SNAP)
        self.offset.next_value = Vector2(.5, self.POS_Y)
        self.sleep = self.SLEEP
        self.done = False

    def tick(self):
        self.offset.tick()
        if self.offset.static:
            if self.done:
                self.label.delete()
                return DONE

            if self.sleep > 0:
                self.sleep -= 1
            else:
                self.offset.next_value = Vector2(-.5, self.POS_Y)
                self.done = True

    def draw(self, window):
        x, y = self.offset.value.xy
        glPushMatrix()
        glTranslatef(x * window.width, y * window.height, 0)
        self.label.draw()
        glPopMatrix()
Example #12
0
    def __init__(self, x, y, length, sections, s_const):
        sec_length = length / sections
        self.masses = [PointMass(x, y, 1, True)]
        self.springs = []
        cur_y = y
        for mass in range(sections):
            cur_y += sec_length
            self.masses.append(PointMass(x, cur_y, 1))

        for spring in range(sections):
            self.springs.append(
                Spring(self.masses[spring], self.masses[spring + 1], s_const,
                       sec_length))
Example #13
0
 def __init__(self, text, color):
     font = PopupFont()
     self.label = pyglet.text.Label(
         text,
         anchor_x = 'center',
         font_name = font.name,
         font_size = font.size,
         anchor_y = 'baseline',
         color = color
     )
     self.offset = Spring(Vector2(1.5, self.POS_Y), self.SPEED, self.SNAP)
     self.offset.next_value = Vector2(.5, self.POS_Y)
     self.sleep = self.SLEEP
     self.done = False
Example #14
0
    def createBackground(self):
        '''generate ground'''
        self.createBase()
        '''generate clouds'''
        x = 60
        y = 6
        while x < self.width:
            self.createCloud(4 + y, x)
            x = x + 50
            y = y + 3
            y = y % 6
        '''generate mountains'''
        self.createMountain(28, 6, 30, 1)
        self.createMountain(28, 6, 405, 1)
        self.createMountain(28, 6, 490, 1)
        self.createMountain(28, 6, 550, 1)
        self.createMountain(28, 6, 650, 1)
        self.createMountain(28, 6, 960, 1)
        self.createMountain(28, 6, 1055, 1)
        '''generate bricks'''
        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 130, 25)
        self.bricks.append([c, 130, 25])

        self.createBricks(self.green + '#' + self.reset, 150, 25)
        self.bricks.append([self.green + '#' + self.reset, 150, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 155, 25)
        self.bricks.append([c, 155, 25])

        self.createBricks(self.green + '#' + self.reset, 160, 25)
        self.bricks.append([self.green + '#' + self.reset, 160, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 165, 25)
        self.bricks.append([c, 165, 25])

        self.createBricks(self.green + '#' + self.reset, 170, 25)
        self.bricks.append([self.green + '#' + self.reset, 170, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 160, 10)
        self.bricks.append([c, 160, 10])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 520, 24)
        self.bricks.append([c, 520, 24])

        self.createBricks(self.green + '#' + self.reset, 525, 24)
        self.bricks.append([self.green + '#' + self.reset, 525, 24])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 530, 24)
        self.bricks.append([c, 530, 24])

        for i in range(500, 560, 5):
            self.createBricks(self.green + '#' + self.reset, i, 10)
            self.bricks.append([self.green + '#' + self.reset, i, 10])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 660, 25)
        self.bricks.append([c, 660, 25])

        self.createBricks(self.green + '#' + self.reset, 705, 25)
        self.bricks.append([self.green + '#' + self.reset, 705, 25])

        self.createBricks(self.green + '#' + self.reset, 710, 25)
        self.bricks.append([self.green + '#' + self.reset, 710, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 760, 25)
        self.bricks.append([c, 760, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 770, 25)
        self.bricks.append([c, 770, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 780, 25)
        self.bricks.append([c, 780, 25])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 770, 10)
        self.bricks.append([c, 770, 10])

        self.createBricks(self.green + '#' + self.reset, 810, 25)
        self.bricks.append([self.green + '#' + self.reset, 810, 25])

        self.createBricks(self.green + '#' + self.reset, 825, 10)
        self.bricks.append([self.green + '#' + self.reset, 825, 10])

        self.createBricks(self.green + '#' + self.reset, 830, 10)
        self.bricks.append([self.green + '#' + self.reset, 830, 10])

        self.createBricks(self.green + '#' + self.reset, 835, 10)
        self.bricks.append([self.green + '#' + self.reset, 835, 10])

        self.createBricks(self.green + '#' + self.reset, 855, 10)
        self.bricks.append([self.green + '#' + self.reset, 855, 10])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 860, 10)
        self.bricks.append([c, 860, 10])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 865, 10)
        self.bricks.append([c, 865, 10])

        self.createBricks(self.green + '#' + self.reset, 870, 10)
        self.bricks.append([self.green + '#' + self.reset, 870, 10])

        self.createBricks(self.green + '#' + self.reset, 860, 24)
        self.bricks.append([self.green + '#' + self.reset, 860, 24])

        self.createBricks(self.green + '#' + self.reset, 865, 24)
        self.bricks.append([self.green + '#' + self.reset, 865, 24])

        self.createBricks(self.green + '#' + self.reset, 1100, 24)
        self.bricks.append([self.green + '#' + self.reset, 1100, 24])

        self.createBricks(self.green + '#' + self.reset, 1105, 24)
        self.bricks.append([self.green + '#' + self.reset, 1105, 24])

        self.createBricks(self.green + '#' + self.reset, 1110, 24)
        self.bricks.append([self.green + '#' + self.reset, 1110, 24])

        c = random.choice([
            self.red + '1' + self.reset, self.red + '2' + self.reset,
            self.red + '3' + self.reset
        ])
        self.createBricks(c, 1115, 24)
        self.bricks.append([c, 1115, 24])

        self.createBricks(self.green + '#' + self.reset, 1120, 24)
        self.bricks.append([self.green + '#' + self.reset, 1120, 24])
        '''generate pipes'''
        self.createPipes(185, 13, 6)
        self.createPipes(250, 13, 8)
        self.createPipes(310, 13, 12)
        self.createPipes(370, 13, 12)
        self.createPipes(1070, 13, 6)
        self.createPipes(1200, 13, 6)
        '''generate pits'''
        self.createPit(440, 40)
        self.createPit(580, 30)
        self.createPit(1005, 18)
        '''generate stairs'''
        self.createStairs(890, 33, 4, 4, 1)
        self.createStairs(920, 33, 4, 4, 0)
        self.createStairs(980, 33, 5, 5, 1)
        self.createStairs(1023, 33, 4, 4, 0)
        self.createStairs(1230, 33, 7, 7, 1)
        '''generate enemies'''
        self.enemies.append(Enemy('(E)', 180, 30, time.time()))
        self.enemies.append(Enemy('(E)', 263, 25, time.time()))
        self.enemies.append(Enemy('(E)', 330, 26, time.time()))
        self.enemies.append(Enemy('(E)', 365, 23, time.time()))
        self.enemies.append(Enemy('(E)', 530, 33, time.time()))
        self.smartEnemies.append(SmartEnemy('(E)', 694, 33, time.time()))
        self.smartEnemies.append(SmartEnemy('(E)', 780, 30, time.time()))
        self.enemies.append(Enemy('(E)', 850, 30, time.time()))
        self.enemies.append(Enemy('(E)', 975, 30, time.time()))
        self.enemies.append(Enemy('(E)', 950, 33, time.time()))
        self.enemies.append(Enemy('(E)', 1096, 33, time.time()))
        self.smartEnemies.append(SmartEnemy('(E)', 100, 20, time.time()))
        '''generate coins'''
        self.createCoins(90, 33)
        self.coins.append([90, 33])

        self.createCoins(190, 27)
        self.coins.append([190, 27])

        for i in range(280, 300, 5):
            self.createCoins(i, 33)
            self.coins.append([i, 33])

        self.createCoins(420, 33)
        self.coins.append([420, 33])

        self.createCoins(525, 20)
        self.coins.append([525, 20])

        self.createCoins(570, 20)
        self.coins.append([570, 20])

        for i in range(730, 750, 5):
            self.createCoins(i, 20)
            self.coins.append([i, 20])

        for i in range(1160, 1180, 5):
            self.createCoins(i, 25)
            self.coins.append([i, 25])
        '''generate bridge'''
        self.createBridge()
        '''generate boundary'''
        for i in range(self.height):
            self.grid[i][self.width - 1] = '#'
        ''' generate spring '''
        self.springs.append(Spring(5, 430, 31))
        for i in self.springs:
            self.createSpring(i)
        ''' draw castle '''
        self.createCastle()
Example #15
0
    def __init__(
        self, roomnumber, floorNumber
    ):  #FIXME: les couleurs dans design niveaux ne sont pas homogenes.
        self.floorNumber = floorNumber
        if floorNumber == 1:  #python doesn't have switch statements smh
            floordesignPath = 'design niveaux/lvl1.png'
        elif floorNumber == 2:
            floordesignPath = 'design niveaux/lvl2.png'
        elif floorNumber == 3:
            floordesignPath = 'design niveaux/lvl3.png'
        elif floorNumber == 4:
            floordesignPath = 'design niveaux/lvl4.png'
        elif floorNumber == 5:
            floordesignPath = 'design niveaux/lvl5.png'
        elif floorNumber == 6:
            floordesignPath = 'design niveaux/salle du boss.png'

        floordesign = pygame.image.load(floordesignPath)
        self.roomnumber = roomnumber
        self.roomBlocks = []
        for y in range(9):
            for x in range(16):
                color = floordesign.get_at((x + (roomnumber * 16), y))
                #                print("x = " + str(x+ (roomnumber*16)) + "; y = " + str(y) + "; color = " + str(color))
                if color == (0, 0, 0, 255):  # wall
                    self.roomBlocks.append(Wall((x, y)))
                elif color == (88, 88, 88, 255):  # spike
                    if floordesign.get_at(
                        (x + (roomnumber * 16), y + 1)) == (0, 0, 0, 255):
                        self.roomBlocks.append(Spike((x, y)))
                    elif floordesign.get_at(
                        (x + (roomnumber * 16) + 1, y)) == (0, 0, 0, 255):
                        self.roomBlocks.append(Spike((x, y), 1))
                    elif floordesign.get_at(
                        (x + (roomnumber * 16) - 1, y)) == (0, 0, 0, 255):
                        self.roomBlocks.append(Spike((x, y), 3))
                    elif floordesign.get_at(
                        (x + (roomnumber * 16), y - 1)) == (0, 0, 0, 255):
                        self.roomBlocks.append(Spike((x, y), 2))
                    else:
                        self.roomBlocks.append(Spike((x, y)))
                elif color == (18, 189, 99, 255):
                    self.roomBlocks.append(Coin((x, y)))
                elif color == (189, 18, 18, 255):
                    #enemy
                    if floordesign.get_at(
                        (x + (roomnumber * 16), y + 1)) == (0, 0, 0, 255):
                        if floorNumber == 1:
                            self.roomBlocks.append(Slime((x, y)))
                        elif floorNumber == 2:
                            self.roomBlocks.append(Slime2((x, y)))
                        elif floorNumber == 3:
                            self.roomBlocks.append(Slime3((x, y)))
                        elif floorNumber == 4:
                            self.roomBlocks.append(Slime4((x, y)))
                        elif floorNumber == 5:
                            self.roomBlocks.append(Slime5((x, y)))
                    else:
                        # bat
                        if floorNumber == 1:
                            self.roomBlocks.append(Bat((x, y)))
                        elif floorNumber == 2:
                            self.roomBlocks.append(Bat2((x, y)))
                        elif floorNumber == 3:
                            self.roomBlocks.append(Bat3((x, y)))
                        elif floorNumber == 4:
                            self.roomBlocks.append(Bat4((x, y)))
                        elif floorNumber == 5:
                            self.roomBlocks.append(Bat5((x, y)))
                elif color == (23, 18, 189, 255):
                    #spring
                    self.roomBlocks.append(Spring((x, y)))
                elif color == (215, 223, 1, 255):
                    self.roomBlocks.append(SpawnPoint((x, y)))
                elif color == (230, 89, 1, 255):
                    self.roomBlocks.append(Button((x, y)))
                elif color == (168, 7, 125, 255):
                    self.roomBlocks.append(PNJ((x, y)))
                elif color == (9, 199, 254):
                    self.roomBlocks.append(Table((x, y)))
                elif color == (124, 48, 201):
                    self.roomBlocks.append(Boss((x, y)))
Example #16
0
    DFDx = dFdk + lam * dRdk
    return E, DFDx


# Create random parameters
pfactory = ParameterFactory()
K = pfactory.createNormalParameter(
    'K', dict(mu=np.pi / 2., sigma=0.1 * (np.pi / 2.)), 7)

# Add random parameters into a container and initialize
pc = ParameterContainer()
pc.addParameter(K)
pc.initialize()

# Create deterministic and stochastic spring elements
dspr = Spring(k=np.pi / 2.)
sspr = StochasticSpring(dspr, pc)

#######################################################################
# Test the system
#######################################################################

# Assemble stochastic force vector from deterministic vector
f = np.pi
U = sspr.solve(0, f)
# E = dspr.F(uhat)
# print("force   :", f)
# print("disp    :", U)

# Decompose f(y) and dfdx(y) in stochastic basis
E = np.zeros((sspr.nsdof))
Example #17
0
    def __init__(self, batch, group, x, y, block):
        group = RotateGroup(x, y, block.slope, parent=group)
        super(Mirror, self).__init__(batch, group, 0, 0, block)

        self.group = group
        self.slope = Spring(block.slope, 0.2, 0.1)
class Experiment_Session:
    def __init__(self):
        # Set the basic params of windows
        self.root = Tkinter.Tk()
        self.root.title("Haptic Experiment")
        w = self.root.winfo_screenwidth()
        h = self.root.winfo_screenheight()
        self.width = w
        self.height = h
        self.root.geometry("%dx%d" % (w, h))
        self.root.minsize(300, 240)

        self.user_num = -1
        self.user_name = ""
        self.user_gender = ""
        self.user_age = 0
        self.haptic_feel_lookup = {
            1: 'empty',
            2: 'low',
            3: 'high',
            4: 'medium',
            5: 'click',
            6: 'drop',
            7: 'None'
        }

        self.cmd = Produce_Read_Order_List.Produce_Read_Order_List(
        )  # Initiate list of command

        self.User_feel_FP = ""  # record the user's actual choice of haptic feeling
        self.global_times_counter = 0  # record the user's repeated times
        self.current_time = 0
        self.correct_times = 0
        self.correct_RL_times = 0
        self.RL_total = 0
        self.start = 0  # Start timestamp for haptic sensing
        self.end = 0  # End timestamp for haptic sensing
        self.deltatime = 0  # The time duration for haptic sensing
        self.startTrialNum = 0  # Start number specified for program crash
        self.outputfile = None  # Output file for trial recording
        self.outputfile_timeStamp = None  # Output file for timestamp
        self.currentTrial = None  # Current trial information
        self.play_electronic_element = None  # Define the instance of electronic element sound play
        self.SpacePressTime = 0  # Times of press [Space]
        self.EnterPressTime = 0  # Times of press [Enter]
        self.PressSpaceTwice = False  # Space pressed for more than twice without [Enter]
        self.user_choice = -1  # User choice of last i'th electronic element
        self.ask_last_num = -1  # the last i'th elements hear of under the Recongnition load
        self.write_info = ""  # Information written to output file
        self.show_info = ""  # Information showed on the panel
        self.pin_height = ""  # Information of the pin height
        self.last_num = []  # list for last ask number contains [2(27), 1(27)]

        self.space_sound_start = ""  # Timestamp of Space and music start time
        self.force_profile_start = ""  # Timestamp of Force profile start
        self.sound_stop = ""  # Timestamp of sound stop
        self.space_curTrial_stop = ""  # Timestamp of current trial stop

        # Switch between show debug and not show
        self.show_debug = False

        self.TrialInfo = Tkinter.StringVar(value='')  # Trial information
        self.Answer = Tkinter.StringVar(value='')  # User answer
        self.Question_text = Tkinter.StringVar(value='')  # Text of Question
        self.Position_Info = Tkinter.StringVar(
            value='')  # Position Information about the Spring
        self.Debug_Info = Tkinter.StringVar(
            value='')  # Debug Information about the Spring

        # Bind the Space Key press to continue
        self.root.bind(
            "<KeyPress>",
            self.SpaceContinue)  # Bind the [Space] press and its function
        self.root.focus_set()
        self.root.bind('<Return>',
                       self.EnterPress)  # Bind the [Enter] Key press

        self.varNum = Tkinter.StringVar(value='')
        self.varName = Tkinter.StringVar(value='')
        self.varGender = Tkinter.StringVar(value='')
        self.varAge = Tkinter.StringVar(value='')

        # Top level Panel for user information enter, show at program boot, hide when information entered and confirmed
        top_entry_y = h / 80
        top_col_1 = w / 15
        top_col_2 = top_col_1 + w / 12 + w / 18
        top_col_3 = top_col_2 + w / 12 + w / 18
        top_col_4 = top_col_3 + w / 12 + w / 18
        top_col_5 = top_col_4 + w / 12 + w / 15
        top_col_6 = top_col_5 + w / 12 + w / 12

        self.labelNum = Tkinter.Label(self.root, text='User Num:')
        self.labelNum.place(x=top_col_1,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.labelNum.config(font=("Courier", 15, "bold"))
        self.entryNum = Tkinter.Entry(self.root, textvariable=self.varNum)
        self.entryNum.place(x=top_col_1 + w / 12,
                            y=top_entry_y,
                            width=w / 18,
                            height=h / 20)

        self.LabelName = Tkinter.Label(self.root, text='Name:')
        self.LabelName.place(x=top_col_2,
                             y=top_entry_y,
                             width=w / 12,
                             height=h / 20)
        self.LabelName.config(font=("Courier", 15, "bold"))
        self.entryName = Tkinter.Entry(self.root, textvariable=self.varName)
        self.entryName.place(x=top_col_2 + w / 12,
                             y=top_entry_y,
                             width=w / 20,
                             height=h / 20)

        self.LabelGender = Tkinter.Label(self.root, text='Gender:')
        self.LabelGender.place(x=top_col_3,
                               y=top_entry_y,
                               width=w / 12,
                               height=h / 20)
        self.LabelGender.config(font=("Courier", 15, "bold"))
        self.entryGender = Tkinter.Entry(self.root,
                                         width=80,
                                         textvariable=self.varGender)
        self.entryGender.place(x=top_col_3 + w / 12,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)

        self.LabelAge = Tkinter.Label(self.root, text='Age:')
        self.LabelAge.place(x=top_col_4,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.LabelAge.config(font=("Courier", 15, "bold"))
        self.entryAge = Tkinter.Entry(self.root,
                                      width=80,
                                      textvariable=self.varAge)
        self.entryAge.place(x=top_col_4 + w / 12,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)

        self.LabelStartFromTrial = Tkinter.Label(self.root, text='Start Num:')
        self.LabelStartFromTrial.place(x=top_col_5,
                                       y=top_entry_y,
                                       width=w / 12,
                                       height=h / 20)
        self.LabelStartFromTrial.config(font=("Courier", 15, "bold"))
        self.entryStartFromTrial = Tkinter.Entry(
            self.root, width=80, textvariable=self.startTrialNum)
        self.entryStartFromTrial.place(x=top_col_5 + w / 12,
                                       y=top_entry_y,
                                       width=w / 20,
                                       height=h / 20)

        self.buttonOk = Tkinter.Button(self.root,
                                       text='Confirm',
                                       command=self.login)
        self.buttonOk.place(x=top_col_6,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)
        self.buttonOk.config(font=("Courier", 12, "bold"))
        self.buttonCancel = Tkinter.Button(self.root,
                                           text='Cancel',
                                           command=self.cancel)
        self.buttonCancel.place(x=top_col_6 + w / 18,
                                y=top_entry_y,
                                width=w / 20,
                                height=h / 20)
        self.buttonCancel.config(font=("Courier", 12, "bold"))

        self.buttonRetry = Tkinter.Button(self.root,
                                          text='Retry',
                                          command=self.retry)
        self.buttonRetry.place(x=top_col_6 + 2 * w / 18,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)
        self.buttonRetry.config(font=("Courier", 12, "bold"))

        self.Info_Header = Label(self.root,
                                 text="Trial\tRecongition Load\tComponent",
                                 anchor=W)
        self.Info_Header.config(font=("Courier", 12, "bold"))

        self.Info = Label(self.root, textvariable=self.TrialInfo, anchor=W)
        self.Info.config(font=("Courier", 12, "bold"))

        img_dx = w / 5
        img_dy = h / 5

        # Set up and place the images at the second level
        img_1 = Image.open('img/F01.jpg')
        img_1 = img_1.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_1 = ImageTk.PhotoImage(img_1)

        img_2 = Image.open('img/F02.jpg')
        img_2 = img_2.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_2 = ImageTk.PhotoImage(img_2)

        img_3 = Image.open('img/F03.jpg')
        img_3 = img_3.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_3 = ImageTk.PhotoImage(img_3)

        img_4 = Image.open('img/F04.jpg')
        img_4 = img_4.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_4 = ImageTk.PhotoImage(img_4)

        img_5 = Image.open('img/F05.jpg')
        img_5 = img_5.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_5 = ImageTk.PhotoImage(img_5)

        img_6 = Image.open('img/F06.jpg')
        img_6 = img_6.resize((img_dx, img_dy), Image.ANTIALIAS)
        img_6 = ImageTk.PhotoImage(img_6)

        col_x_1 = 2 * w / 20
        col_x_2 = 4 * w / 20 + img_dx
        col_x_3 = 6 * w / 20 + 2 * img_dx

        label_level_1_y = top_entry_y + 2 * h / 20
        label_level_2_y = top_entry_y + 4 * h / 20 + img_dy

        img_level_1_y = top_entry_y + 3 * h / 20
        img_level_2_y = top_entry_y + 5 * h / 20 + img_dy

        self.FP1 = Label(self.root, text="1. empty", fg='blue')
        self.FP1.place(x=col_x_1, y=label_level_1_y)
        self.FP1.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_1).place(x=col_x_1,
                                                        y=img_level_1_y)

        self.FP2 = Label(self.root, text="2. low", fg='blue')
        self.FP2.place(x=col_x_2, y=label_level_1_y)
        self.FP2.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_2).place(x=col_x_2,
                                                        y=img_level_1_y)

        self.FP3 = Label(self.root, text="3. high", fg='blue')
        self.FP3.place(x=col_x_3, y=label_level_1_y)
        self.FP3.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_3).place(x=col_x_3,
                                                        y=img_level_1_y)

        self.FP4 = Label(self.root, text="4. medium", fg='blue')
        self.FP4.place(x=col_x_1, y=label_level_2_y)
        self.FP4.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_4).place(x=col_x_1,
                                                        y=img_level_2_y)

        self.FP5 = Label(self.root, text="5. click", fg='blue')
        self.FP5.place(x=col_x_2, y=label_level_2_y)
        self.FP5.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_5).place(x=col_x_2,
                                                        y=img_level_2_y)

        self.FP6 = Label(self.root, text="6. drop", fg='blue')
        self.FP6.place(x=col_x_3, y=label_level_2_y)
        self.FP6.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_6).place(x=col_x_3,
                                                        y=img_level_2_y)

        # User haptic feel part and Quize part
        self.Question = Label(self.root, textvariable=self.Question_text)

        # Show the current position information on right side
        self.CurrPosition = Label(self.root,
                                  text="Position Info",
                                  textvariable=self.Position_Info)
        self.CurrPosition.place(x=12 * self.width / 16, y=7 * self.height / 8)
        self.CurrPosition.config(font=("Courier", 15, "bold"), fg="red")

        # Show the Force Profile on left side
        self.CurrDebug = Label(self.root,
                               text="Debug Info",
                               textvariable=self.Debug_Info)
        if self.show_debug:
            self.CurrDebug.place(x=2 * self.width / 16, y=3 * self.height / 4)
            self.CurrDebug.config(font=("Courier", 20, "bold"), fg="red")

        self.User_Answer = Tkinter.Label(self.root, text='Answer: ', fg="blue")
        self.User_Answer.place(x=w / 2 - w / 8, y=7 * h / 8)
        self.User_Answer.config(font=("Courier", 20, "bold"))
        self.entry_Answer = Tkinter.Entry(self.root,
                                          width=80,
                                          textvariable=self.Answer)
        self.entry_Answer.place(x=w / 2 - w / 25,
                                y=7 * h / 8,
                                width=w / 12,
                                height=h / 25)

        self.read_first_100 = []

        f_first = open("First_100.txt", 'r')

        self.word_pron = []
        for i in f_first:
            self.word_pron.append(i)

        self.root.mainloop()

    def login(self):

        if self.entryNum.get() == "":
            tkMessageBox.showinfo(title='Warning',
                                  message='Please complete number')
            return

        self.user_num = int(self.entryNum.get())

        if 0 < self.user_num < 13:

            self.user_name = self.entryName.get()
            self.user_age = self.entryAge.get()
            self.user_gender = self.entryGender.get()

            if self.user_name == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete name')
                return
            if self.user_age == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete age')
                return
            if self.user_gender == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete gender')
                return
            if self.entryStartFromTrial.get() != "":
                self.startTrialNum = int(self.entryStartFromTrial.get())

            # check the existence of program crash
            if self.startTrialNum != 0:
                existFilename = "User_Records/Participant" + str(
                    self.user_num) + "/order_list.txt"
                self.cmd.read_command(
                    existFilename
                )  # Read commands => commands in existence file
                self.correct_RL_times, self.RL_total = Recover_from_Record(
                    "User_Records/Participant" + str(self.user_num) +
                    "/user_record.txt")
            else:
                dir = os.getcwd() + "/User_Records/Participant" + str(
                    self.user_num)
                if not os.path.exists(dir):
                    os.makedirs(dir)  # make dirs
                self.cmd.start_up(int(
                    self.user_num))  # Produce commands by user number
                self.cmd.read_command()  # Read commands => commands
                # write the head information to the first line in the file
                self.outputfile = open(
                    "User_Records/Participant" + str(self.user_num) +
                    "/user_record.txt", "w")
                self.outputfile_timeStamp = open(
                    "User_Records/Participant" + str(self.user_num) +
                    "/user_timeStamp.txt", "w")
                self.outputfile_timeStamp.write(
                    "space_sound_start,force_profile_start,sound_stop,space_curTrial_stop\n"
                )
                self.outputfile.write(
                    "User_num,User_name,User_age,User_gender,Trials,Pin_height,Recognition_Load,Force_Profile,Component_Num,Duration_Time,User_Choice,Haptic_Choice_Correctness, ask_last_num,Recognition_Load_Correctness,space_sound_start,force_profile_start,sound_stop,space_curTrial_stop\n"
                )
                self.outputfile_timeStamp.close()
                self.outputfile.close()

            # Re-open the output file again for later record useage
            self.outputfile = open(
                "User_Records/Participant" + str(self.user_num) +
                "/user_record.txt", "a")
            self.outputfile_timeStamp = open(
                "User_Records/Participant" + str(self.user_num) +
                "/user_timeStamp.txt", "a")
        else:
            tkMessageBox.showinfo('Error',
                                  message='Please enter an valid number[0-12]')
            return

        # Hide the label and entry from the panel
        self.labelNum.place_forget()
        self.entryNum.place_forget()
        self.LabelName.place_forget()
        self.entryName.place_forget()
        self.LabelGender.place_forget()
        self.entryGender.place_forget()
        self.LabelAge.place_forget()
        self.entryAge.place_forget()
        self.LabelStartFromTrial.place_forget()
        self.entryStartFromTrial.place_forget()
        self.buttonOk.place_forget()
        self.buttonCancel.place_forget()

        # Show the trial information after confirmation
        self.Info_Header.place(x=30 * self.width / 80,
                               y=self.height / 80,
                               width=17 * self.width / 64,
                               height=self.height / 25)
        self.Info_Header.config(bg="red", fg="white")
        self.Info_Header.config(font=("Courier", 15, "bold"))

        self.Info.place(x=30 * self.width / 80,
                        y=self.height / 80 + self.height / 22,
                        width=17 * self.width / 64,
                        height=self.height / 25)
        self.Info.config(font=("Courier", 15, "bold"))
        self.Info.config(bg="blue", fg="white")

    def cancel(self):
        self.varNum.set('')
        self.varAge.set('')
        self.varGender.set('')
        self.varName.set('')

    # Start and End a haptic trial by [Space] keypress
    def SpaceContinue(self, event):

        if event.keysym == "space":
            # Press [Enter] for 1,3,5,7,9
            if self.EnterPressTime == 0:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Press [Enter] to start a trial before proceed')
                return

            # Press Space more than twice without press [Enter]
            if self.PressSpaceTwice is True:
                tkMessageBox.showinfo(
                    'Warning', message='Answer the question before proceed')
                return

            print "Space Entered"
            if self.SpacePressTime % 2 == 0:
                self.Question_text.set("Sound START")
                self.Question.place(x=7 * self.width / 16,
                                    y=3 * self.height / 4)
                self.Question.config(fg="red", font=("Courier", 23, "bold"))
                # Start to play sounds in recognition load
                if self.currentTrial[1] == '1':
                    self.play_electronic_element = play_electronic_element()
                    self.play_electronic_element.start()
                    self.space_sound_start = datetime.datetime.now().strftime(
                        '%Y-%m-%d %H:%M:%S.%f').split()[1]
                else:
                    self.space_sound_start = "-1"
            else:
                # Stop the movement of Haptic Spring
                self.spring.terminate()
                self.deltatime = int(round(time.time() * 1000)) - self.start
                self.Question_text.set("Haptic Test END, Select a Haptic Feel")
                self.space_curTrial_stop = datetime.datetime.now().strftime(
                    '%Y-%m-%d %H:%M:%S.%f').split()[1]
                self.PressSpaceTwice = True
            self.SpacePressTime += 1

    # [Enter] Press for 1. Show a Trial Information 2. Show a electronic elements
    def EnterPress(self, event):

        # Incomplete Personal Information
        if self.EnterPressTime == 0:
            if self.user_num == -1 or self.user_name == "" or self.user_gender == "" or self.user_age == "":
                tkMessageBox.showinfo(
                    'Warning', message='Complete Personal Info before proceed')
                return

        # the first time press [Enter]: show a Trial Information
        if self.EnterPressTime == 0:

            start_num = self.startTrialNum
            if start_num == 0:
                self.currentTrial = self.cmd.read_command_by_line()
            while start_num != 0:
                self.currentTrial = self.cmd.read_command_by_line()
                start_num -= 1

            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                self.current_time = self.global_times_counter + 1
            else:
                self.current_time = self.global_times_counter + self.startTrialNum

            self.show_info += "  " + str(self.current_time) + "\t   "
            self.write_info += str(self.current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","
                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "  True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "  False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                if i == 3:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.TrialInfo.set(self.show_info)
            self.global_times_counter += 1
            self.show_info = ""
            self.spring_start()

        elif self.EnterPressTime % 2 == 1:

            # Incomplete Haptic Test Start--End
            if self.PressSpaceTwice is False:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Finish Haptic Test before proceed Press [Space]')
                return

            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning', message='Enter your haptic feel before proceed')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 8:
                    self.User_feel_FP = self.haptic_feel_lookup[int(
                        self.entry_Answer.get())]
                    result = tkMessageBox.askyesno(
                        title='Notice',
                        message='Your Haptic Choice: ' + self.User_feel_FP +
                        '\nCan you confirm ?')
                    if result is True:
                        pass
                    else:
                        return
                else:
                    tkMessageBox.showinfo(
                        title='Warning',
                        message='Your Choice MUST BE Integer in [1-7]')
                    return

                self.Answer.set("")

                # Show the recognition load question
                if self.currentTrial[1] == '1':
                    self.ask_last_num = int(self.currentTrial[-1])
                    self.Question_text.set("Select the Last " +
                                           str(self.ask_last_num) +
                                           " Chinese word\n")
                    print "word_Pronounce: " + self.word_pron[
                        self.play_electronic_element.last_i_th(
                            self.ask_last_num)].decode("gbk")
                else:
                    self.ask_last_num = -1
                    self.Question_text.set(
                        "No Question just Press [Enter] to Proceed")

        # Write the current user trial data information to output file
        # and choice and show a trial Information
        elif self.EnterPressTime % 2 == 0:
            # Recognition Load question answer
            if self.currentTrial[1] == '1':
                if len(self.entry_Answer.get()) == 0:
                    tkMessageBox.showinfo(
                        'Warning',
                        message=
                        'Enter the correctness of recognition Load before proceed'
                    )
                    return
                else:
                    if self.entry_Answer.get().strip().isdigit():
                        self.user_choice = int(self.entry_Answer.get())
                    else:
                        tkMessageBox.showinfo(
                            title='Notice',
                            message='Your Choice MUST BE Integer in [1-9]')
                        return

                result = tkMessageBox.askyesno(
                    'Notice',
                    message='Your Recognition Load Correctness: ' +
                    str(self.user_choice) + '\nCan you confirm ?')
                if result is True:
                    pass
                else:
                    return
            else:
                self.user_choice = -1

            self.Question_text.set("")
            self.Answer.set("")

            self.write_info += str(self.deltatime) + ","

            if self.User_feel_FP != 'None':
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
                if self.User_feel_FP == self.currentTrial[2]:
                    self.write_info += str(1) + ","
                    self.correct_times += 1
                else:
                    self.write_info += str(0) + ","
            else:
                self.write_info += str(7) + ','

            print "Actual FP: " + self.currentTrial[
                2] + "----User FP: " + self.User_feel_FP
            print "***" + str(self.correct_times) + "/" + str(
                self.global_times_counter)

            self.write_info += str(self.ask_last_num) + ","
            if self.currentTrial[1] == '1':
                self.write_info += str(self.user_choice) + "\n"
                if self.user_choice == 1:
                    self.correct_RL_times += 1
                self.RL_total += 1
            else:
                self.write_info += str(-1) + ","
                self.write_info += str(self.user_choice) + "\n"

            timeStamp = self.space_sound_start + "," + self.force_profile_start + "," + self.sound_stop + "," + self.space_curTrial_stop + "\n"
            self.outputfile_timeStamp.write(timeStamp)
            self.outputfile.write(self.write_info)
            self.outputfile_timeStamp.flush()
            self.outputfile.flush()
            self.write_info = ""

            # When the trial times reach the end
            if self.current_time == len(self.cmd.Commands):
                tkMessageBox.showinfo('Notice',
                                      message='Haptic Test End, Thank you!!')
                return

            # Begin the next trial
            self.currentTrial = self.cmd.read_command_by_line()
            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                self.current_time = self.global_times_counter + 1
            else:
                self.current_time = self.global_times_counter + self.startTrialNum

            self.show_info += str(self.current_time) + "\t\t"
            self.write_info += str(self.current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","
                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    # self.show_info += self.currentTrial[i] + "\t\t"
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                if i == 3:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.PressSpaceTwice = False
            self.TrialInfo.set(self.show_info)
            if self.RL_total != 0:
                precision = float(self.correct_RL_times) / self.RL_total
                self.Position_Info.set(
                    str(self.correct_RL_times) + "/" + str(self.RL_total) +
                    "=" + str(round(precision * 100)) + "%")
            self.global_times_counter += 1
            self.show_info = ""
            self.spring_start()

        self.EnterPressTime += 1

    def retry(self):
        # Recognition Load question answer
        if self.currentTrial[1] == '1':
            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Select the Electronic Element before retry')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 10:
                    self.user_choice = int(self.entry_Answer.get())
                else:
                    tkMessageBox.showinfo(
                        title='Notice',
                        message='Your Choice MUST BE Integer in [1-9]')
                    return
            result = tkMessageBox.askyesno(
                'Notice',
                message='Your Recognition Load Choice: ' +
                str(self.user_choice) + '\nCan you confirm ?')
            if result is True:
                pass
            else:
                return
        else:
            self.user_choice = -1

        self.Question_text.set("")
        self.Answer.set("")

        self.write_info += str(self.deltatime) + ","

        if self.User_feel_FP != 'None':
            self.write_info += str(
                self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
            if self.User_feel_FP == self.currentTrial[2]:
                self.write_info += str(1) + ","
                self.correct_times += 1
                print str(self.correct_times) + "/" + str(
                    self.global_times_counter)
            else:
                self.write_info += str(0) + ","
        else:
            self.write_info += str(7) + ','

        self.write_info += str(self.ask_last_num) + ","
        if self.currentTrial[1] == '1':
            self.write_info += str(
                self.play_electronic_element.last_i_th(
                    self.ask_last_num)) + ","
            self.write_info += str(self.user_choice) + "\n"
        else:
            self.write_info += str(-1) + ","
            self.write_info += str(self.user_choice) + "\n"

        timeStamp = self.space_sound_start + "," + self.force_profile_start + "," + self.sound_stop + "," + self.space_curTrial_stop + "\n"
        self.outputfile_timeStamp.write(timeStamp)
        self.outputfile.write(self.write_info)
        self.outputfile_timeStamp.flush()
        self.outputfile.flush()
        self.write_info = ""

        # Begin the next trial
        print self.currentTrial
        self.global_times_counter -= 1

        # Current/Total times
        self.write_info += str(
            self.user_num) + "," + self.user_name + "," + str(
                self.user_age) + "," + self.user_gender + ","

        if self.startTrialNum == 0:
            self.current_time = self.global_times_counter + 1
        else:
            self.current_time = self.global_times_counter + self.startTrialNum

        self.show_info += str(self.current_time) + "\t\t"
        self.write_info += str(self.current_time) + ","

        for i in range(len(self.currentTrial)):
            if i == 0:
                self.pin_height = self.currentTrial[i]
                self.write_info += str(
                    self.cmd.condition_lookup_table.index(
                        self.pin_height)) + ","
            if i == 1:
                if self.currentTrial[i] == '1':
                    self.show_info += "True\t\t"
                    self.write_info += "1,"
                else:
                    self.show_info += "False\t\t"
                    self.write_info += "0,"
            if i == 2:
                self.Debug_Info.set("FP: " + self.currentTrial[i])
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.currentTrial[i])) + ","
            if i == 3:
                self.show_info += self.cmd.Electronic_Element_Component_lookup[
                    self.currentTrial[i]]
                self.write_info += self.currentTrial[i] + ","

        self.PressSpaceTwice = False
        self.TrialInfo.set(self.show_info)
        self.Position_Info.set(
            str(self.correct_RL_times) + "/" + str(self.RL_total) + "=" +
            str(self.correct_RL_times / self.RL_total * 1.0))
        self.show_info = ""
        self.global_times_counter += 1
        self.EnterPressTime += 1

        self.spring_start()

    def spring_start(self):
        self.spring = Spring(self.time_counter, self.sound_stopper)
        Pin_height = int(self.currentTrial[3])
        if 1 <= Pin_height and Pin_height <= 3:
            self.spring.set_profile(self.currentTrial[2], 'long')
        elif 4 <= Pin_height and Pin_height <= 6:
            self.spring.set_profile(self.currentTrial[2], 'middle')
        elif 7 <= Pin_height and Pin_height <= 9:
            self.spring.set_profile(self.currentTrial[2], 'short')
        self.spring.start()

    def time_counter(self):

        if self.SpacePressTime % 2 != 1:
            tkMessageBox.showinfo(
                'Warning', message='Press [Space] before Force Profile Sense')
            return

        self.start = int(round(time.time() * 1000))
        self.Question_text.set("Haptic Test START")
        self.force_profile_start = datetime.datetime.now().strftime(
            '%Y-%m-%d %H:%M:%S.%f').split()[1]
        self.Question.place(x=6 * self.width / 16, y=3 * self.height / 4)
        self.Question.config(font=("Courier", 23, "bold"))
        self.Question.config(fg="green")

    def sound_stopper(self):
        # Stop play the sound of electronic element
        self.Question_text.set("Sound END")
        self.Question.place(x=7 * self.width / 16, y=3 * self.height / 4)
        self.Question.config(fg="red", font=("Courier", 23, "bold"))
        if self.currentTrial[1] == '1':
            self.play_electronic_element.terminate()
            self.sound_stop = datetime.datetime.now().strftime(
                '%Y-%m-%d %H:%M:%S.%f').split()[1]
        else:
            self.sound_stop = "-1"
Example #19
0
class Camera(object):
    SPEED = 0.1
    CLIP = 0.01

    def __init__(self, eye, center, up):
        self.eye = Spring(eye, self.SPEED, self.CLIP)
        self.vec = Spring(center - eye, self.SPEED, self.CLIP)
        self.up = Spring(up, self.SPEED, self.CLIP)
        self.ofs = Spring(0, 0.1, 0.01)

        self.modelview = (GLdouble * 16)()
        self.projection = (GLdouble * 16)()
        self.viewport = (GLint * 4)()
        self.unproj = [GLdouble(), GLdouble(), GLdouble()]

    def resize(self, x, y, w, h):
        glViewport(x, y, w, h)
        self.viewport[:] = (x, y, w, h)
        glMatrixMode(gl.GL_PROJECTION)
        glLoadIdentity()
        glu.gluPerspective(45.0, w / h, 0.1, 50)
        glMatrixMode(gl.GL_MODELVIEW)

    def move(self, eye, center, up):
        self.eye.next_value = eye
        self.vec.next_value = center - eye
        self.up.next_value = up

    def shake(self, amount):
        self.ofs.value = amount

    def tick(self):
        self.eye.tick()
        self.vec.tick()
        self.up.tick()
        self.ofs.tick()

    def setup(self):
        eye = self.eye.value + Vector3(uniform(0, 1), uniform(0, 1), uniform(0, 1)) * self.ofs.value
        center = eye + self.vec.value
        up = self.up.value
        gluLookAt(eye.x, eye.y, eye.z, center.x, center.y, center.z, up.x, up.y, up.z)
        glGetDoublev(GL_MODELVIEW_MATRIX, self.modelview)
        glGetDoublev(GL_PROJECTION_MATRIX, self.projection)

    def _unproject(self, x, y, z):
        gluUnProject(
            x, y, z, self.modelview, self.projection, self.viewport, self.unproj[0], self.unproj[1], self.unproj[2]
        )
        return Vector3(*[v.value for v in self.unproj])

    def unproject(self, (x, y)):
        # http://stackoverflow.com/questions/9406269/object-picking-with-ray-casting
        l0 = self._unproject(x, y, 0.1)
        l1 = self._unproject(x, y, 0.9)
        ld = l1 - l0

        # http://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection
        # assuming that p0 = (0, 0, 0), and n = (0, 0, -1)
        d = -l0.z / ld.z
        p = l0 + ld * d

        return p
Example #20
0
    def retry(self):
        # Recognition Load question answer
        if self.currentTrial[1] == '1':
            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Select the Electronic Element before retry')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 10:
                    self.user_choice = int(self.entry_Answer.get())
                else:
                    tkMessageBox.showinfo(
                        title='Notice',
                        message='Your Choice MUST BE Integer in [1-9]')
                    return
            result = tkMessageBox.askyesno(
                'Notice',
                message='Your Recognition Load Choice: ' +
                str(self.user_choice) + '\nCan you confirm ?')
            if result is True:
                pass
            else:
                return
        else:
            self.user_choice = -1

        self.Question_text.set("")
        self.Question_choice_row_1.set("")
        self.Question_choice_row_2.set("")
        self.Question_choice_row_3.set("")
        self.Answer.set("")

        self.write_info += str(self.deltatime) + ","

        if self.User_feel_FP != 'None':
            self.write_info += str(
                self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
            if self.User_feel_FP == self.currentTrial[3]:
                self.write_info += str(1) + ","
                self.correct_times += 1
                print str(self.correct_times) + "/" + str(
                    self.global_times_counter)
            else:
                self.write_info += str(0) + ","
        else:
            self.write_info += str(8) + ','

        self.write_info += str(self.ask_last_num) + ","
        if self.currentTrial[1] == '1':
            self.write_info += str(
                self.play_electronic_element.last_i_th(
                    self.ask_last_num)) + ","
            self.write_info += str(self.user_choice) + ","
            if int(self.user_choice) == int(
                    self.play_electronic_element.last_i_th(self.ask_last_num)):
                self.write_info += str(1) + "\n"
            else:
                self.write_info += str(0) + "\n"
        else:
            self.write_info += str(-1) + ","
            self.write_info += str(self.user_choice) + ","
            self.write_info += str(-1) + "\n"

        self.outputfile.write(self.write_info)
        self.outputfile.flush()
        self.write_info = ""

        # Begin the next trial
        print self.currentTrial
        self.global_times_counter -= 1

        # Current/Total times
        self.write_info += str(
            self.user_num) + "," + self.user_name + "," + str(
                self.user_age) + "," + self.user_gender + ","

        if self.startTrialNum == 0:
            current_time = self.global_times_counter + 1
        else:
            current_time = self.global_times_counter + self.startTrialNum

        self.show_info += str(current_time) + "\t\t"
        self.write_info += str(current_time) + ","

        for i in range(len(self.currentTrial)):
            if i == 0:
                self.pin_height = self.currentTrial[i]
                self.write_info += str(
                    self.cmd.condition_lookup_table.index(
                        self.pin_height)) + ","
            if i == 1:
                if self.currentTrial[i] == '1':
                    self.show_info += "True\t\t"
                    self.write_info += "1,"
                else:
                    self.show_info += "False\t\t"
                    self.write_info += "0,"
            if i == 2:
                if self.currentTrial[i] == '1':
                    self.show_info += "Dominant\t  "
                    self.write_info += "1,"
                else:
                    self.show_info += "Non-Dominant\t  "
                    self.write_info += "0,"
            if i == 3:
                self.Debug_Info.set("FP: " + self.currentTrial[i])
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.currentTrial[i])) + ","
            if i == 4:
                self.show_info += self.cmd.Electronic_Element_Component_lookup[
                    self.currentTrial[i]]
                self.write_info += self.currentTrial[i] + ","

        self.PressSpaceTwice = False
        self.TrialInfo.set(self.show_info)
        self.Position_Info.set(
            str(self.correct_RL_times) + "/" + str(self.RL_total))
        self.show_info = ""
        self.global_times_counter += 1
        self.EnterPressTime += 1

        # Create thread for handling haptic Spring
        #self.spring = Spring(self.Position_Info.set)
        self.spring = Spring()
        Pin_height = int(self.currentTrial[4])
        if 1 <= Pin_height and Pin_height <= 3:
            self.spring.set_profile(self.currentTrial[3], 'long')
        elif 4 <= Pin_height and Pin_height <= 6:
            self.spring.set_profile(self.currentTrial[3], 'middle')
        elif 7 <= Pin_height and Pin_height <= 9:
            self.spring.set_profile(self.currentTrial[3], 'short')
        self.spring.start()
Example #21
0
    def EnterPress(self, event):

        # Incomplete Personal Information
        if self.EnterPressTime == 0:
            if self.user_num == -1 or self.user_name == "" or self.user_gender == "" or self.user_age == "":
                tkMessageBox.showinfo(
                    'Warning', message='Complete Personal Info before proceed')
                return

        # the first time press [Enter]: show a Trial Information
        if self.EnterPressTime == 0:

            start_num = self.startTrialNum

            if start_num == 0:
                self.currentTrial = self.cmd.read_command_by_line()

            while start_num != 0:
                self.currentTrial = self.cmd.read_command_by_line()
                start_num -= 1

            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                current_time = self.global_times_counter + 1
            else:
                current_time = self.global_times_counter + self.startTrialNum

            self.show_info += "  " + str(current_time) + "\t   "
            self.write_info += str(current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","

                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "  True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "  False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    if self.currentTrial[i] == '1':
                        self.show_info += "Dominant\t  "
                        self.write_info += "1,"
                    else:
                        self.show_info += "Non-Dominant\t  "
                        self.write_info += "0,"
                if i == 3:
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                if i == 4:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.TrialInfo.set(self.show_info)
            self.global_times_counter += 1
            self.show_info = ""

            # Create thread for handling haptic Spring
            #self.spring = Spring(self.Position_Info.set)
            self.spring = Spring()
            Pin_height = int(self.currentTrial[4])
            if 1 <= Pin_height and Pin_height <= 3:
                self.spring.set_profile(self.currentTrial[3], 'long')
            elif 4 <= Pin_height and Pin_height <= 6:
                self.spring.set_profile(self.currentTrial[3], 'middle')
            elif 7 <= Pin_height and Pin_height <= 9:
                self.spring.set_profile(self.currentTrial[3], 'short')
            self.spring.start()

        elif self.EnterPressTime % 2 == 1:

            # Incomplete Haptic Test Start--End
            if self.PressSpaceTwice is False:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Finish Haptic Test before proceed Press [Space]')
                return

            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning', message='Enter your haptic feel before proceed')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 10:
                    self.User_feel_FP = self.haptic_feel_lookup[int(
                        self.entry_Answer.get())]
                    result = tkMessageBox.askyesno(
                        title='Notice',
                        message='Your Haptic Choice: ' + self.User_feel_FP +
                        '\nCan you confirm ?')
                    if result is True:
                        pass
                    else:
                        return
                else:
                    tkMessageBox.showinfo(
                        title='Warning',
                        message='Your Choice MUST BE Integer in [1-9]')
                    return

                self.Answer.set("")

                # Show the recognition load question
                if self.currentTrial[1] == '1':
                    self.ask_last_num = random.randrange(
                        1, 4)  # random number for quiz of last element
                    if self.ask_last_num > self.trackLength:
                        self.ask_last_num = self.trackLength

                    self.Question_text.set("Select the Last " +
                                           str(self.ask_last_num) +
                                           " Electronic Element\n")
                    self.Question_choice_row_1.set("1. 光敏电阻\t2. 发光二极管\t3. 电阻")
                    self.Question_choice_row_2.set("4. 场效应管\t5. 三极管\t6. 电容")
                    self.Question_choice_row_3.set("7. 芯片\t\t8. 电位器\t9. 跳线")

                    base_question_x = 19 * self.width / 64
                    base_question_y = 11 * self.height / 16
                    base_question_y_row_1 = base_question_y + 30
                    base_question_y_row_2 = base_question_y_row_1 + 30
                    base_question_y_row_3 = base_question_y_row_2 + 30

                    self.Question.place(x=base_question_x,
                                        y=base_question_y,
                                        anchor=W)
                    self.Choice_row_1.place(x=base_question_x,
                                            y=base_question_y_row_1,
                                            anchor=W)
                    self.Choice_row_2.place(x=base_question_x,
                                            y=base_question_y_row_2,
                                            anchor=W)
                    self.Choice_row_3.place(x=base_question_x,
                                            y=base_question_y_row_3,
                                            anchor=W)
                    self.Question.config(font=("Courier", 20, "bold"),
                                         fg="red")
                    self.Choice_row_1.config(font=("Courier", 20, "bold"),
                                             fg="black")
                    self.Choice_row_2.config(font=("Courier", 20, "bold"),
                                             fg="black")
                    self.Choice_row_3.config(font=("Courier", 20, "bold"),
                                             fg="black")
                else:
                    self.ask_last_num = -1
                    self.Question_text.set(
                        "No Question just Press [Enter] to Proceed")

        # Write the current user trial data information to output file
        # and choice and show a trial Information
        elif self.EnterPressTime % 2 == 0:
            # Recognition Load question answer
            if self.currentTrial[1] == '1':
                if len(self.entry_Answer.get()) == 0:
                    tkMessageBox.showinfo(
                        'Warning',
                        message='Select the Electronic Element before proceed')
                    return
                else:
                    if self.entry_Answer.get().strip().isdigit() and 0 < int(
                            self.entry_Answer.get().strip()) < 10:
                        self.user_choice = int(self.entry_Answer.get())
                    else:
                        tkMessageBox.showinfo(
                            title='Notice',
                            message='Your Choice MUST BE Integer in [1-9]')
                        return

                result = tkMessageBox.askyesno(
                    'Notice',
                    message='Your Recognition Load Choice: ' +
                    str(self.user_choice) + '\nCan you confirm ?')
                if result is True:
                    pass
                else:
                    return
            else:
                self.user_choice = -1

            self.Question_text.set("")
            self.Question_choice_row_1.set("")
            self.Question_choice_row_2.set("")
            self.Question_choice_row_3.set("")
            self.Answer.set("")

            self.write_info += str(self.deltatime) + ","

            if self.User_feel_FP != 'None':
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
                if self.User_feel_FP == self.currentTrial[3]:
                    self.write_info += str(1) + ","
                    self.correct_times += 1
                else:
                    self.write_info += str(0) + ","
            else:
                self.write_info += str(8) + ','
            print "Actual FP: " + self.currentTrial[
                3] + " User FP: " + self.User_feel_FP
            print "***" + str(self.correct_times) + "/" + str(
                self.global_times_counter)

            self.write_info += str(self.ask_last_num) + ","
            if self.currentTrial[1] == '1':
                self.write_info += str(
                    self.play_electronic_element.last_i_th(
                        self.ask_last_num)) + ","
                self.write_info += str(self.user_choice) + ","
                if int(self.user_choice) == int(
                        self.play_electronic_element.last_i_th(
                            self.ask_last_num)):
                    self.write_info += str(1) + "\n"
                    self.correct_RL_times += 1
                else:
                    self.write_info += str(0) + "\n"
                self.RL_total += 1
            else:
                self.write_info += str(-1) + ","
                self.write_info += str(self.user_choice) + ","
                self.write_info += str(-1) + "\n"

            self.outputfile.write(self.write_info)
            self.outputfile.flush()
            self.write_info = ""

            # When the trial times reach the end
            if self.global_times_counter == len(self.cmd.Commands):
                tkMessageBox.showinfo('Notice',
                                      message='Haptic Test End, Thank you!!')
                return

            # Begin the next trial
            self.currentTrial = self.cmd.read_command_by_line()
            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                current_time = self.global_times_counter + 1
            else:
                current_time = self.global_times_counter + self.startTrialNum

            self.show_info += str(current_time) + "\t\t"
            self.write_info += str(current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","
                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    if self.currentTrial[i] == '1':
                        self.show_info += "Dominant\t  "
                        self.write_info += "1,"
                    else:
                        self.show_info += "Non-Dominant\t  "
                        self.write_info += "0,"
                if i == 3:
                    # self.show_info += self.currentTrial[i] + "\t\t"
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                if i == 4:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.PressSpaceTwice = False
            self.TrialInfo.set(self.show_info)
            self.Position_Info.set(
                str(self.correct_RL_times) + "/" + str(self.RL_total))
            self.global_times_counter += 1
            self.show_info = ""

            # Create thread for handling haptic Spring
            #self.spring = Spring(self.Position_Info.set)
            self.spring = Spring()
            Pin_height = int(self.currentTrial[4])
            if 1 <= Pin_height and Pin_height <= 3:
                self.spring.set_profile(self.currentTrial[3], 'long')
            elif 4 <= Pin_height and Pin_height <= 6:
                self.spring.set_profile(self.currentTrial[3], 'middle')
            elif 7 <= Pin_height and Pin_height <= 9:
                self.spring.set_profile(self.currentTrial[3], 'short')
            self.spring.start()

        self.EnterPressTime += 1
Example #22
0
    def __init__(self, batch, group, x, y, block):
        group = RotateGroup(x, y, block.all_states_idx, parent=group)
        super(Launcher, self).__init__(batch, group, 0, 0, block)

        self.group = group
        self.state = Spring(block.all_states_idx, 0.2, 0.1)
Example #23
0
class Experiment_Session:
    def __init__(self):
        # Set the basic params of windows
        self.root = Tkinter.Tk()
        self.root.title("Haptic Experiment")
        w = 1920  #self.root.winfo_screenwidth()
        h = 1080  #self.root.winfo_screenheight()
        self.width = w
        self.height = h
        self.root.geometry("%dx%d" % (w, h))
        self.root.minsize(300, 240)

        self.user_num = -1
        self.user_name = ""
        self.user_gender = ""
        self.user_age = 0
        self.haptic_feel_lookup = {
            1: 'empty',
            2: 'low',
            3: 'high',
            4: 'medium',
            5: 'increasing',
            6: 'decreasing',
            7: 'click',
            8: 'drop',
            9: 'None',
        }

        self.cmd = Produce_Read_Order_List.Produce_Read_Order_List(
        )  # Initiate list of command

        self.User_feel_FP = ""  # record the user's actual choice of haptic feeling
        self.global_times_counter = 0  # record the user's repeated times
        self.correct_times = 0
        self.correct_RL_times = 0
        self.RL_total = 0
        self.start = 0  # Start timestamp for haptic sensing
        self.end = 0  # End timestamp for haptic sensing
        self.deltatime = 0  # The time duration for haptic sensing
        self.startTrialNum = 0  # Start number specified for program crash
        self.outputfile = None  # Output file for trial recording
        self.currentTrial = None  # Current trial information
        self.play_electronic_element = None  # Define the instance of electronic element sound play
        self.SpacePressTime = 0  # Times of press [Space]
        self.EnterPressTime = 0  # Times of press [Enter]
        self.PressSpaceTwice = False  # Space pressed for more than twice without [Enter]
        self.user_choice = -1  # User choice of last i'th electronic element
        self.ask_last_num = -1  # the last i'th elements hear of under the Recongnition load
        self.trackLength = 0  # track the length of the sound playlist
        self.write_info = ""  # Information written to output file
        self.show_info = ""  # Information showed on the panel
        self.pin_height = ""  # Information of the pin height

        # Switch between show debug and not show
        self.show_debug = False

        self.TrialInfo = Tkinter.StringVar(value='')  # Trial information
        self.Answer = Tkinter.StringVar(value='')  # User answer
        self.Question_text = Tkinter.StringVar(value='')  # Text of Question
        self.Question_choice_row_1 = Tkinter.StringVar(
            value='')  # First row of choice of question
        self.Question_choice_row_2 = Tkinter.StringVar(
            value='')  # Second row of choice of question
        self.Question_choice_row_3 = Tkinter.StringVar(
            value='')  # Third row of choice of question
        self.Position_Info = Tkinter.StringVar(
            value='')  # Position Information about the Spring
        self.Debug_Info = Tkinter.StringVar(
            value='')  # Debug Information about the Spring

        # Bind the Space Key press to continue
        self.root.bind(
            "<KeyPress>",
            self.SpaceContinue)  # Bind the [Space] press and its function
        self.root.focus_set()
        self.root.bind('<Return>',
                       self.EnterPress)  # Bind the [Enter] Key press
        self.spring = Spring()

        self.varNum = Tkinter.StringVar(value='')
        self.varName = Tkinter.StringVar(value='')
        self.varGender = Tkinter.StringVar(value='')
        self.varAge = Tkinter.StringVar(value='')

        # Top level Panel for user information enter, show at program boot, hide when information entered and confirmed
        top_entry_y = h / 80
        top_col_1 = w / 15
        top_col_2 = top_col_1 + w / 12 + w / 18
        top_col_3 = top_col_2 + w / 12 + w / 18
        top_col_4 = top_col_3 + w / 12 + w / 18
        top_col_5 = top_col_4 + w / 12 + w / 15
        top_col_6 = top_col_5 + w / 12 + w / 12

        self.labelNum = Tkinter.Label(self.root, text='User Num:')
        self.labelNum.place(x=top_col_1,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.labelNum.config(font=("Courier", 15, "bold"))
        self.entryNum = Tkinter.Entry(self.root, textvariable=self.varNum)
        self.entryNum.place(x=top_col_1 + w / 12,
                            y=top_entry_y,
                            width=w / 18,
                            height=h / 20)

        self.LabelName = Tkinter.Label(self.root, text='Name:')
        self.LabelName.place(x=top_col_2,
                             y=top_entry_y,
                             width=w / 12,
                             height=h / 20)
        self.LabelName.config(font=("Courier", 15, "bold"))
        self.entryName = Tkinter.Entry(self.root, textvariable=self.varName)
        self.entryName.place(x=top_col_2 + w / 12,
                             y=top_entry_y,
                             width=w / 20,
                             height=h / 20)

        self.LabelGender = Tkinter.Label(self.root, text='Gender:')
        self.LabelGender.place(x=top_col_3,
                               y=top_entry_y,
                               width=w / 12,
                               height=h / 20)
        self.LabelGender.config(font=("Courier", 15, "bold"))
        self.entryGender = Tkinter.Entry(self.root,
                                         width=80,
                                         textvariable=self.varGender)
        self.entryGender.place(x=top_col_3 + w / 12,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)

        self.LabelAge = Tkinter.Label(self.root, text='Age:')
        self.LabelAge.place(x=top_col_4,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.LabelAge.config(font=("Courier", 15, "bold"))
        self.entryAge = Tkinter.Entry(self.root,
                                      width=80,
                                      textvariable=self.varAge)
        self.entryAge.place(x=top_col_4 + w / 12,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)

        self.LabelStartFromTrial = Tkinter.Label(self.root, text='Start Num:')
        self.LabelStartFromTrial.place(x=top_col_5,
                                       y=top_entry_y,
                                       width=w / 12,
                                       height=h / 20)
        self.LabelStartFromTrial.config(font=("Courier", 15, "bold"))
        self.entryStartFromTrial = Tkinter.Entry(
            self.root, width=80, textvariable=self.startTrialNum)
        self.entryStartFromTrial.place(x=top_col_5 + w / 12,
                                       y=top_entry_y,
                                       width=w / 20,
                                       height=h / 20)

        self.buttonOk = Tkinter.Button(self.root,
                                       text='Confirm',
                                       command=self.login)
        self.buttonOk.place(x=top_col_6,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)
        self.buttonOk.config(font=("Courier", 12, "bold"))
        self.buttonCancel = Tkinter.Button(self.root,
                                           text='Cancel',
                                           command=self.cancel)
        self.buttonCancel.place(x=top_col_6 + w / 18,
                                y=top_entry_y,
                                width=w / 20,
                                height=h / 20)
        self.buttonCancel.config(font=("Courier", 12, "bold"))

        self.buttonRetry = Tkinter.Button(self.root,
                                          text='Retry',
                                          command=self.retry)
        self.buttonRetry.place(x=top_col_6 + 2 * w / 18,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)
        self.buttonRetry.config(font=("Courier", 12, "bold"))

        self.Info_Header = Label(
            self.root,
            text="Trial\tRecongition Load\tHandness\tComponent",
            anchor=W)
        self.Info_Header.config(font=("Courier", 12, "bold"))

        self.Info = Label(self.root, textvariable=self.TrialInfo, anchor=W)
        self.Info.config(font=("Courier", 12, "bold"))

        # Set up and place the images at the second level
        img_1 = Image.open('img/F01.jpg')
        img_1 = img_1.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_1 = ImageTk.PhotoImage(img_1)

        img_2 = Image.open('img/F02.jpg')
        img_2 = img_2.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_2 = ImageTk.PhotoImage(img_2)

        img_3 = Image.open('img/F03.jpg')
        img_3 = img_3.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_3 = ImageTk.PhotoImage(img_3)

        img_4 = Image.open('img/F04.jpg')
        img_4 = img_4.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_4 = ImageTk.PhotoImage(img_4)

        img_5 = Image.open('img/F05.jpg')
        img_5 = img_5.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_5 = ImageTk.PhotoImage(img_5)

        img_6 = Image.open('img/F06.jpg')
        img_6 = img_6.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_6 = ImageTk.PhotoImage(img_6)

        img_7 = Image.open('img/F07.jpg')
        img_7 = img_7.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_7 = ImageTk.PhotoImage(img_7)

        img_8 = Image.open('img/F08.jpg')
        img_8 = img_8.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_8 = ImageTk.PhotoImage(img_8)

        col_x_1 = w / 15
        col_x_2 = 2 * w / 15 + w / 6
        col_x_3 = 3 * w / 15 + 2 * w / 6
        col_x_4 = 4 * w / 15 + 3 * w / 6

        label_level_1_y = top_entry_y + 2 * h / 20
        label_level_2_y = top_entry_y + 4 * h / 20 + h / 6

        img_level_1_y = top_entry_y + 3 * h / 20
        img_level_2_y = top_entry_y + 5 * h / 20 + h / 6

        self.FP1 = Label(self.root, text="1. empty", fg='blue')
        self.FP1.place(x=col_x_1, y=label_level_1_y)
        self.FP1.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_1).place(x=col_x_1,
                                                        y=img_level_1_y)

        self.FP2 = Label(self.root, text="2. low", fg='blue')
        self.FP2.place(x=col_x_2, y=label_level_1_y)
        self.FP2.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_2).place(x=col_x_2,
                                                        y=img_level_1_y)

        self.FP3 = Label(self.root, text="3. high", fg='blue')
        self.FP3.place(x=col_x_3, y=label_level_1_y)
        self.FP3.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_3).place(x=col_x_3,
                                                        y=img_level_1_y)

        self.FP4 = Label(self.root, text="4. medium", fg='blue')
        self.FP4.place(x=col_x_4, y=label_level_1_y)
        self.FP4.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_4).place(x=col_x_4,
                                                        y=img_level_1_y)

        self.FP5 = Label(self.root, text="5. increasing", fg='blue')
        self.FP5.place(x=col_x_1, y=label_level_2_y)
        self.FP5.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_5).place(x=col_x_1,
                                                        y=img_level_2_y)

        self.FP6 = Label(self.root, text="6. decreasing", fg='blue')
        self.FP6.place(x=col_x_2, y=label_level_2_y)
        self.FP6.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_6).place(x=col_x_2,
                                                        y=img_level_2_y)

        self.FP7 = Label(self.root, text="7. click", fg='blue')
        self.FP7.place(x=col_x_3, y=label_level_2_y)
        self.FP7.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_7).place(x=col_x_3,
                                                        y=img_level_2_y)

        self.FP8 = Label(self.root, text="8. drop", fg='blue')
        self.FP8.place(x=col_x_4, y=label_level_2_y)
        self.FP8.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_8).place(x=col_x_4,
                                                        y=img_level_2_y)

        # User haptic feel part and Quize part
        self.Question = Label(self.root, textvariable=self.Question_text)

        # Show the current position information on right side
        self.CurrPosition = Label(self.root,
                                  text="Position Info",
                                  textvariable=self.Position_Info)
        self.CurrPosition.place(x=12 * self.width / 16, y=7 * self.height / 8)
        self.CurrPosition.config(font=("Courier", 15, "bold"), fg="red")

        # Show the Force Profile on left side
        self.CurrDebug = Label(self.root,
                               text="Debug Info",
                               textvariable=self.Debug_Info)
        if self.show_debug:
            self.CurrDebug.place(x=2 * self.width / 16, y=3 * self.height / 4)
            self.CurrDebug.config(font=("Courier", 20, "bold"), fg="red")

        self.Choice_row_1 = Label(self.root,
                                  textvariable=self.Question_choice_row_1)
        self.Choice_row_2 = Label(self.root,
                                  textvariable=self.Question_choice_row_2)
        self.Choice_row_3 = Label(self.root,
                                  textvariable=self.Question_choice_row_3)

        self.User_Answer = Tkinter.Label(self.root, text='Answer: ', fg="blue")
        self.User_Answer.place(x=w / 2 - w / 8, y=7 * h / 8)
        self.User_Answer.config(font=("Courier", 20, "bold"))
        self.entry_Answer = Tkinter.Entry(self.root,
                                          width=80,
                                          textvariable=self.Answer)
        self.entry_Answer.place(x=w / 2 - w / 25,
                                y=7 * h / 8,
                                width=w / 12,
                                height=h / 25)

        self.root.mainloop()

    def login(self):

        if self.entryNum.get() == "":
            tkMessageBox.showinfo(title='Warning',
                                  message='Please complete number')
            return

        self.user_num = int(self.entryNum.get())

        if 0 < self.user_num < 13:

            self.user_name = self.entryName.get()
            self.user_age = self.entryAge.get()
            self.user_gender = self.entryGender.get()

            if self.user_name == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete name')
                return
            if self.user_age == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete age')
                return
            if self.user_gender == "":
                tkMessageBox.showinfo(title='Warning',
                                      message='Please complete gender')
                return
            if self.entryStartFromTrial.get() != "":
                self.startTrialNum = int(self.entryStartFromTrial.get())

            # check the existence of program crash
            if self.startTrialNum != 0:
                existFilename = "Order_List/User_" + str(
                    self.user_num) + "_order_list.txt"
                self.cmd.read_command(
                    existFilename
                )  # Read commands => commands in existence file
            else:
                self.cmd.start_up(int(
                    self.user_num))  # Produce commands by user number
                self.cmd.read_command()  # Read commands => commands
                # write the head information to the first line in the file
                self.outputfile = open(
                    "Records/User_" + str(self.user_num) + "_record.txt", "w")
                self.outputfile.write(
                    "User_num,User_name,User_age,User_gender,Trials,Pin_height,Recognition_Load,Handness,Force_Profile,Component_Num,Duration_Time,User_Choice,Haptic_Choice_Correctness, ask_last_num,actual_electronic_element,user_RL_Choice,Recognition_Load_Correctness\n"
                )
                self.outputfile.close()

            # Re-open the output file again for later record useage
            self.outputfile = open(
                "Records/User_" + str(self.user_num) + "_record.txt", "a")
        else:
            tkMessageBox.showinfo('Error',
                                  message='Please enter an valid number[0-12]')
            return

        # Hide the label and entry from the panel
        self.labelNum.place_forget()
        self.entryNum.place_forget()
        self.LabelName.place_forget()
        self.entryName.place_forget()
        self.LabelGender.place_forget()
        self.entryGender.place_forget()
        self.LabelAge.place_forget()
        self.entryAge.place_forget()
        self.LabelStartFromTrial.place_forget()
        self.entryStartFromTrial.place_forget()
        self.buttonOk.place_forget()
        self.buttonCancel.place_forget()

        # Show the trial information after confirmation
        self.Info_Header.place(x=20 * self.width / 80,
                               y=self.height / 80,
                               width=34 * self.width / 64,
                               height=self.height / 25)
        self.Info_Header.config(bg="red", fg="white")
        self.Info_Header.config(font=("Courier", 15, "bold"))

        self.Info.place(x=20 * self.width / 80,
                        y=self.height / 80 + self.height / 22,
                        width=34 * self.width / 64,
                        height=self.height / 25)
        self.Info.config(font=("Courier", 15, "bold"))
        self.Info.config(bg="blue", fg="white")

    def cancel(self):
        self.varNum.set('')
        self.varAge.set('')
        self.varGender.set('')
        self.varName.set('')

    # Start and End a haptic trial by [Space] keypress
    def SpaceContinue(self, event):
        if event.keysym == "Right":
            # Stop play the sound of electronic element
            if self.currentTrial[1] == '1':
                self.play_electronic_element.terminate()
                self.trackLength = len(self.play_electronic_element.traceList)

        if event.keysym == "space":
            # Press [Enter] for 1,3,5,7,9
            if self.EnterPressTime == 0:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Press [Enter] to start a trial before proceed')
                return

            # Press Space more than twice without press [Enter]
            if self.PressSpaceTwice is True:
                tkMessageBox.showinfo(
                    'Warning', message='Answer the question before proceed')
                return

            print "Space Entered"
            if self.SpacePressTime % 2 == 0:
                self.start = int(round(time.time() * 1000))
                self.Question_text.set("Haptic Test START")
                self.Question.place(x=6 * self.width / 16,
                                    y=3 * self.height / 4)
                self.Question.config(font=("Courier", 23, "bold"))
                self.Question.config(fg="green")

                # Start to play the electronic element
                if self.currentTrial[1] == '1':
                    self.play_electronic_element = play_electronic_element()
                    self.play_electronic_element.start()
            else:
                # Stop the movement of Haptic Spring
                self.spring.terminate()
                self.deltatime = int(round(time.time() * 1000)) - self.start
                self.Question_text.set("Haptic Test END")
                self.Question.place(x=7 * self.width / 16,
                                    y=3 * self.height / 4)
                self.Question.config(fg="red", font=("Courier", 23, "bold"))
                self.Question.after(500, lambda: self.Question_text.set(""))

                self.Question.after(
                    500,
                    lambda: self.Question_text.set("Select a Haptic Feel"))
                self.Question.place(x=5 * self.width / 16,
                                    y=3 * self.height / 4)
                self.Question.config(font=("Courier", 23, "bold"), fg="blue")
                self.PressSpaceTwice = True
            self.SpacePressTime += 1

    # [Enter] Press for 1. Show a Trial Information 2. Show a electronic elements
    def EnterPress(self, event):

        # Incomplete Personal Information
        if self.EnterPressTime == 0:
            if self.user_num == -1 or self.user_name == "" or self.user_gender == "" or self.user_age == "":
                tkMessageBox.showinfo(
                    'Warning', message='Complete Personal Info before proceed')
                return

        # the first time press [Enter]: show a Trial Information
        if self.EnterPressTime == 0:

            start_num = self.startTrialNum

            if start_num == 0:
                self.currentTrial = self.cmd.read_command_by_line()

            while start_num != 0:
                self.currentTrial = self.cmd.read_command_by_line()
                start_num -= 1

            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                current_time = self.global_times_counter + 1
            else:
                current_time = self.global_times_counter + self.startTrialNum

            self.show_info += "  " + str(current_time) + "\t   "
            self.write_info += str(current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","

                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "  True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "  False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    if self.currentTrial[i] == '1':
                        self.show_info += "Dominant\t  "
                        self.write_info += "1,"
                    else:
                        self.show_info += "Non-Dominant\t  "
                        self.write_info += "0,"
                if i == 3:
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                if i == 4:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.TrialInfo.set(self.show_info)
            self.global_times_counter += 1
            self.show_info = ""

            # Create thread for handling haptic Spring
            #self.spring = Spring(self.Position_Info.set)
            self.spring = Spring()
            Pin_height = int(self.currentTrial[4])
            if 1 <= Pin_height and Pin_height <= 3:
                self.spring.set_profile(self.currentTrial[3], 'long')
            elif 4 <= Pin_height and Pin_height <= 6:
                self.spring.set_profile(self.currentTrial[3], 'middle')
            elif 7 <= Pin_height and Pin_height <= 9:
                self.spring.set_profile(self.currentTrial[3], 'short')
            self.spring.start()

        elif self.EnterPressTime % 2 == 1:

            # Incomplete Haptic Test Start--End
            if self.PressSpaceTwice is False:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Finish Haptic Test before proceed Press [Space]')
                return

            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning', message='Enter your haptic feel before proceed')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 10:
                    self.User_feel_FP = self.haptic_feel_lookup[int(
                        self.entry_Answer.get())]
                    result = tkMessageBox.askyesno(
                        title='Notice',
                        message='Your Haptic Choice: ' + self.User_feel_FP +
                        '\nCan you confirm ?')
                    if result is True:
                        pass
                    else:
                        return
                else:
                    tkMessageBox.showinfo(
                        title='Warning',
                        message='Your Choice MUST BE Integer in [1-9]')
                    return

                self.Answer.set("")

                # Show the recognition load question
                if self.currentTrial[1] == '1':
                    self.ask_last_num = random.randrange(
                        1, 4)  # random number for quiz of last element
                    if self.ask_last_num > self.trackLength:
                        self.ask_last_num = self.trackLength

                    self.Question_text.set("Select the Last " +
                                           str(self.ask_last_num) +
                                           " Electronic Element\n")
                    self.Question_choice_row_1.set("1. 光敏电阻\t2. 发光二极管\t3. 电阻")
                    self.Question_choice_row_2.set("4. 场效应管\t5. 三极管\t6. 电容")
                    self.Question_choice_row_3.set("7. 芯片\t\t8. 电位器\t9. 跳线")

                    base_question_x = 19 * self.width / 64
                    base_question_y = 11 * self.height / 16
                    base_question_y_row_1 = base_question_y + 30
                    base_question_y_row_2 = base_question_y_row_1 + 30
                    base_question_y_row_3 = base_question_y_row_2 + 30

                    self.Question.place(x=base_question_x,
                                        y=base_question_y,
                                        anchor=W)
                    self.Choice_row_1.place(x=base_question_x,
                                            y=base_question_y_row_1,
                                            anchor=W)
                    self.Choice_row_2.place(x=base_question_x,
                                            y=base_question_y_row_2,
                                            anchor=W)
                    self.Choice_row_3.place(x=base_question_x,
                                            y=base_question_y_row_3,
                                            anchor=W)
                    self.Question.config(font=("Courier", 20, "bold"),
                                         fg="red")
                    self.Choice_row_1.config(font=("Courier", 20, "bold"),
                                             fg="black")
                    self.Choice_row_2.config(font=("Courier", 20, "bold"),
                                             fg="black")
                    self.Choice_row_3.config(font=("Courier", 20, "bold"),
                                             fg="black")
                else:
                    self.ask_last_num = -1
                    self.Question_text.set(
                        "No Question just Press [Enter] to Proceed")

        # Write the current user trial data information to output file
        # and choice and show a trial Information
        elif self.EnterPressTime % 2 == 0:
            # Recognition Load question answer
            if self.currentTrial[1] == '1':
                if len(self.entry_Answer.get()) == 0:
                    tkMessageBox.showinfo(
                        'Warning',
                        message='Select the Electronic Element before proceed')
                    return
                else:
                    if self.entry_Answer.get().strip().isdigit() and 0 < int(
                            self.entry_Answer.get().strip()) < 10:
                        self.user_choice = int(self.entry_Answer.get())
                    else:
                        tkMessageBox.showinfo(
                            title='Notice',
                            message='Your Choice MUST BE Integer in [1-9]')
                        return

                result = tkMessageBox.askyesno(
                    'Notice',
                    message='Your Recognition Load Choice: ' +
                    str(self.user_choice) + '\nCan you confirm ?')
                if result is True:
                    pass
                else:
                    return
            else:
                self.user_choice = -1

            self.Question_text.set("")
            self.Question_choice_row_1.set("")
            self.Question_choice_row_2.set("")
            self.Question_choice_row_3.set("")
            self.Answer.set("")

            self.write_info += str(self.deltatime) + ","

            if self.User_feel_FP != 'None':
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
                if self.User_feel_FP == self.currentTrial[3]:
                    self.write_info += str(1) + ","
                    self.correct_times += 1
                else:
                    self.write_info += str(0) + ","
            else:
                self.write_info += str(8) + ','
            print "Actual FP: " + self.currentTrial[
                3] + " User FP: " + self.User_feel_FP
            print "***" + str(self.correct_times) + "/" + str(
                self.global_times_counter)

            self.write_info += str(self.ask_last_num) + ","
            if self.currentTrial[1] == '1':
                self.write_info += str(
                    self.play_electronic_element.last_i_th(
                        self.ask_last_num)) + ","
                self.write_info += str(self.user_choice) + ","
                if int(self.user_choice) == int(
                        self.play_electronic_element.last_i_th(
                            self.ask_last_num)):
                    self.write_info += str(1) + "\n"
                    self.correct_RL_times += 1
                else:
                    self.write_info += str(0) + "\n"
                self.RL_total += 1
            else:
                self.write_info += str(-1) + ","
                self.write_info += str(self.user_choice) + ","
                self.write_info += str(-1) + "\n"

            self.outputfile.write(self.write_info)
            self.outputfile.flush()
            self.write_info = ""

            # When the trial times reach the end
            if self.global_times_counter == len(self.cmd.Commands):
                tkMessageBox.showinfo('Notice',
                                      message='Haptic Test End, Thank you!!')
                return

            # Begin the next trial
            self.currentTrial = self.cmd.read_command_by_line()
            print self.currentTrial

            # Current/Total times
            self.write_info += str(
                self.user_num) + "," + self.user_name + "," + str(
                    self.user_age) + "," + self.user_gender + ","

            if self.startTrialNum == 0:
                current_time = self.global_times_counter + 1
            else:
                current_time = self.global_times_counter + self.startTrialNum

            self.show_info += str(current_time) + "\t\t"
            self.write_info += str(current_time) + ","

            for i in range(len(self.currentTrial)):
                if i == 0:
                    self.pin_height = self.currentTrial[i]
                    self.write_info += str(
                        self.cmd.condition_lookup_table.index(
                            self.pin_height)) + ","
                if i == 1:
                    if self.currentTrial[i] == '1':
                        self.show_info += "True\t\t"
                        self.write_info += "1,"
                    else:
                        self.show_info += "False\t\t"
                        self.write_info += "0,"
                if i == 2:
                    if self.currentTrial[i] == '1':
                        self.show_info += "Dominant\t  "
                        self.write_info += "1,"
                    else:
                        self.show_info += "Non-Dominant\t  "
                        self.write_info += "0,"
                if i == 3:
                    # self.show_info += self.currentTrial[i] + "\t\t"
                    self.Debug_Info.set("FP: " + self.currentTrial[i])
                    self.write_info += str(
                        self.cmd.Force_Profile.index(
                            self.currentTrial[i])) + ","
                if i == 4:
                    self.show_info += self.cmd.Electronic_Element_Component_lookup[
                        self.currentTrial[i]]
                    self.write_info += self.currentTrial[i] + ","

            self.PressSpaceTwice = False
            self.TrialInfo.set(self.show_info)
            self.Position_Info.set(
                str(self.correct_RL_times) + "/" + str(self.RL_total))
            self.global_times_counter += 1
            self.show_info = ""

            # Create thread for handling haptic Spring
            #self.spring = Spring(self.Position_Info.set)
            self.spring = Spring()
            Pin_height = int(self.currentTrial[4])
            if 1 <= Pin_height and Pin_height <= 3:
                self.spring.set_profile(self.currentTrial[3], 'long')
            elif 4 <= Pin_height and Pin_height <= 6:
                self.spring.set_profile(self.currentTrial[3], 'middle')
            elif 7 <= Pin_height and Pin_height <= 9:
                self.spring.set_profile(self.currentTrial[3], 'short')
            self.spring.start()

        self.EnterPressTime += 1

    def retry(self):
        # Recognition Load question answer
        if self.currentTrial[1] == '1':
            if len(self.entry_Answer.get()) == 0:
                tkMessageBox.showinfo(
                    'Warning',
                    message='Select the Electronic Element before retry')
                return
            else:
                if self.entry_Answer.get().strip().isdigit() and 0 < int(
                        self.entry_Answer.get().strip()) < 10:
                    self.user_choice = int(self.entry_Answer.get())
                else:
                    tkMessageBox.showinfo(
                        title='Notice',
                        message='Your Choice MUST BE Integer in [1-9]')
                    return
            result = tkMessageBox.askyesno(
                'Notice',
                message='Your Recognition Load Choice: ' +
                str(self.user_choice) + '\nCan you confirm ?')
            if result is True:
                pass
            else:
                return
        else:
            self.user_choice = -1

        self.Question_text.set("")
        self.Question_choice_row_1.set("")
        self.Question_choice_row_2.set("")
        self.Question_choice_row_3.set("")
        self.Answer.set("")

        self.write_info += str(self.deltatime) + ","

        if self.User_feel_FP != 'None':
            self.write_info += str(
                self.cmd.Force_Profile.index(self.User_feel_FP)) + ","
            if self.User_feel_FP == self.currentTrial[3]:
                self.write_info += str(1) + ","
                self.correct_times += 1
                print str(self.correct_times) + "/" + str(
                    self.global_times_counter)
            else:
                self.write_info += str(0) + ","
        else:
            self.write_info += str(8) + ','

        self.write_info += str(self.ask_last_num) + ","
        if self.currentTrial[1] == '1':
            self.write_info += str(
                self.play_electronic_element.last_i_th(
                    self.ask_last_num)) + ","
            self.write_info += str(self.user_choice) + ","
            if int(self.user_choice) == int(
                    self.play_electronic_element.last_i_th(self.ask_last_num)):
                self.write_info += str(1) + "\n"
            else:
                self.write_info += str(0) + "\n"
        else:
            self.write_info += str(-1) + ","
            self.write_info += str(self.user_choice) + ","
            self.write_info += str(-1) + "\n"

        self.outputfile.write(self.write_info)
        self.outputfile.flush()
        self.write_info = ""

        # Begin the next trial
        print self.currentTrial
        self.global_times_counter -= 1

        # Current/Total times
        self.write_info += str(
            self.user_num) + "," + self.user_name + "," + str(
                self.user_age) + "," + self.user_gender + ","

        if self.startTrialNum == 0:
            current_time = self.global_times_counter + 1
        else:
            current_time = self.global_times_counter + self.startTrialNum

        self.show_info += str(current_time) + "\t\t"
        self.write_info += str(current_time) + ","

        for i in range(len(self.currentTrial)):
            if i == 0:
                self.pin_height = self.currentTrial[i]
                self.write_info += str(
                    self.cmd.condition_lookup_table.index(
                        self.pin_height)) + ","
            if i == 1:
                if self.currentTrial[i] == '1':
                    self.show_info += "True\t\t"
                    self.write_info += "1,"
                else:
                    self.show_info += "False\t\t"
                    self.write_info += "0,"
            if i == 2:
                if self.currentTrial[i] == '1':
                    self.show_info += "Dominant\t  "
                    self.write_info += "1,"
                else:
                    self.show_info += "Non-Dominant\t  "
                    self.write_info += "0,"
            if i == 3:
                self.Debug_Info.set("FP: " + self.currentTrial[i])
                self.write_info += str(
                    self.cmd.Force_Profile.index(self.currentTrial[i])) + ","
            if i == 4:
                self.show_info += self.cmd.Electronic_Element_Component_lookup[
                    self.currentTrial[i]]
                self.write_info += self.currentTrial[i] + ","

        self.PressSpaceTwice = False
        self.TrialInfo.set(self.show_info)
        self.Position_Info.set(
            str(self.correct_RL_times) + "/" + str(self.RL_total))
        self.show_info = ""
        self.global_times_counter += 1
        self.EnterPressTime += 1

        # Create thread for handling haptic Spring
        #self.spring = Spring(self.Position_Info.set)
        self.spring = Spring()
        Pin_height = int(self.currentTrial[4])
        if 1 <= Pin_height and Pin_height <= 3:
            self.spring.set_profile(self.currentTrial[3], 'long')
        elif 4 <= Pin_height and Pin_height <= 6:
            self.spring.set_profile(self.currentTrial[3], 'middle')
        elif 7 <= Pin_height and Pin_height <= 9:
            self.spring.set_profile(self.currentTrial[3], 'short')
        self.spring.start()
    def __init__(self):
        '''Initialization'''
        self.over = False
        self.height = 30
        self.width = 90
        self.score = 0
        self.coins = 0
        self.layout = []
        self.enemyarr = []

        self.pipearr = [
            Pipe(18, 315), Pipe(18, 60),
            Pipe(18, 120), Pipe(18, 165)
        ]

        self.springarr = [
            Spring(19, 337), Spring(19, 390)
        ]

        self.brickarr = [
            Brick("unbreakable", 18, 37), Brick(
                "breakable", 18, 40),
            Brick("unbreakable", 18, 43), Brick(
                "breakable", 14, 40),
            Brick("breakable", 18, 113), Brick(
                "unbreakable", 14, 121),
            Brick("unbreakable", 10, 129), Brick(
                "unbreakable", 18, 235),
            Brick("unbreakable", 18, 225), Brick(
                "unbreakable", 18, 245),
            Brick("unbreakable", 14, 230), Brick(
                "unbreakable", 14, 240),
            Brick("breakable", 10, 235), Brick(
                "breakable", 10, 135),
            Brick("unbreakable", 14, 143), Brick(
                "unbreakable", 18, 151),
            Brick("unbreakable", 21, 340), Brick(
                "unbreakable", 13, 340),
            Brick("unbreakable", 20, 340), Brick(
                "unbreakable", 19, 340),
            Brick("unbreakable", 18, 340), Brick(
                "unbreakable", 17, 340),
            Brick("unbreakable", 16, 340), Brick(
                "unbreakable", 15, 340),
            Brick("unbreakable", 14, 340)
        ]

        self.coinarr = [
            Coin(21, 39), Coin(21, 41), Coin(21, 43), Coin(13, 40),
            Coin(9, 129), Coin(9, 235), Coin(13, 230), Coin(13, 240),
            Coin(17, 235)
        ]

        for _ in range(self.height):
            self.layout.append([])
        for i in range(self.height):
            for _ in range(self.width):
                self.layout[i].append(' ')

        # Fixed Score, lives and coins in the 1st row
        self.layout[0][0:6] = 'Score:'
        self.layout[0][40:46] = 'Lives:'
        self.layout[0][60:66] = 'Coins:'
def object_generator():
    return Spring(1, 1)
Example #26
0
    def __init__(self):
        # Set the basic params of windows
        self.root = Tkinter.Tk()
        self.root.title("Haptic Experiment")
        w = 1920  #self.root.winfo_screenwidth()
        h = 1080  #self.root.winfo_screenheight()
        self.width = w
        self.height = h
        self.root.geometry("%dx%d" % (w, h))
        self.root.minsize(300, 240)

        self.user_num = -1
        self.user_name = ""
        self.user_gender = ""
        self.user_age = 0
        self.haptic_feel_lookup = {
            1: 'empty',
            2: 'low',
            3: 'high',
            4: 'medium',
            5: 'increasing',
            6: 'decreasing',
            7: 'click',
            8: 'drop',
            9: 'None',
        }

        self.cmd = Produce_Read_Order_List.Produce_Read_Order_List(
        )  # Initiate list of command

        self.User_feel_FP = ""  # record the user's actual choice of haptic feeling
        self.global_times_counter = 0  # record the user's repeated times
        self.correct_times = 0
        self.correct_RL_times = 0
        self.RL_total = 0
        self.start = 0  # Start timestamp for haptic sensing
        self.end = 0  # End timestamp for haptic sensing
        self.deltatime = 0  # The time duration for haptic sensing
        self.startTrialNum = 0  # Start number specified for program crash
        self.outputfile = None  # Output file for trial recording
        self.currentTrial = None  # Current trial information
        self.play_electronic_element = None  # Define the instance of electronic element sound play
        self.SpacePressTime = 0  # Times of press [Space]
        self.EnterPressTime = 0  # Times of press [Enter]
        self.PressSpaceTwice = False  # Space pressed for more than twice without [Enter]
        self.user_choice = -1  # User choice of last i'th electronic element
        self.ask_last_num = -1  # the last i'th elements hear of under the Recongnition load
        self.trackLength = 0  # track the length of the sound playlist
        self.write_info = ""  # Information written to output file
        self.show_info = ""  # Information showed on the panel
        self.pin_height = ""  # Information of the pin height

        # Switch between show debug and not show
        self.show_debug = False

        self.TrialInfo = Tkinter.StringVar(value='')  # Trial information
        self.Answer = Tkinter.StringVar(value='')  # User answer
        self.Question_text = Tkinter.StringVar(value='')  # Text of Question
        self.Question_choice_row_1 = Tkinter.StringVar(
            value='')  # First row of choice of question
        self.Question_choice_row_2 = Tkinter.StringVar(
            value='')  # Second row of choice of question
        self.Question_choice_row_3 = Tkinter.StringVar(
            value='')  # Third row of choice of question
        self.Position_Info = Tkinter.StringVar(
            value='')  # Position Information about the Spring
        self.Debug_Info = Tkinter.StringVar(
            value='')  # Debug Information about the Spring

        # Bind the Space Key press to continue
        self.root.bind(
            "<KeyPress>",
            self.SpaceContinue)  # Bind the [Space] press and its function
        self.root.focus_set()
        self.root.bind('<Return>',
                       self.EnterPress)  # Bind the [Enter] Key press
        self.spring = Spring()

        self.varNum = Tkinter.StringVar(value='')
        self.varName = Tkinter.StringVar(value='')
        self.varGender = Tkinter.StringVar(value='')
        self.varAge = Tkinter.StringVar(value='')

        # Top level Panel for user information enter, show at program boot, hide when information entered and confirmed
        top_entry_y = h / 80
        top_col_1 = w / 15
        top_col_2 = top_col_1 + w / 12 + w / 18
        top_col_3 = top_col_2 + w / 12 + w / 18
        top_col_4 = top_col_3 + w / 12 + w / 18
        top_col_5 = top_col_4 + w / 12 + w / 15
        top_col_6 = top_col_5 + w / 12 + w / 12

        self.labelNum = Tkinter.Label(self.root, text='User Num:')
        self.labelNum.place(x=top_col_1,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.labelNum.config(font=("Courier", 15, "bold"))
        self.entryNum = Tkinter.Entry(self.root, textvariable=self.varNum)
        self.entryNum.place(x=top_col_1 + w / 12,
                            y=top_entry_y,
                            width=w / 18,
                            height=h / 20)

        self.LabelName = Tkinter.Label(self.root, text='Name:')
        self.LabelName.place(x=top_col_2,
                             y=top_entry_y,
                             width=w / 12,
                             height=h / 20)
        self.LabelName.config(font=("Courier", 15, "bold"))
        self.entryName = Tkinter.Entry(self.root, textvariable=self.varName)
        self.entryName.place(x=top_col_2 + w / 12,
                             y=top_entry_y,
                             width=w / 20,
                             height=h / 20)

        self.LabelGender = Tkinter.Label(self.root, text='Gender:')
        self.LabelGender.place(x=top_col_3,
                               y=top_entry_y,
                               width=w / 12,
                               height=h / 20)
        self.LabelGender.config(font=("Courier", 15, "bold"))
        self.entryGender = Tkinter.Entry(self.root,
                                         width=80,
                                         textvariable=self.varGender)
        self.entryGender.place(x=top_col_3 + w / 12,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)

        self.LabelAge = Tkinter.Label(self.root, text='Age:')
        self.LabelAge.place(x=top_col_4,
                            y=top_entry_y,
                            width=w / 12,
                            height=h / 20)
        self.LabelAge.config(font=("Courier", 15, "bold"))
        self.entryAge = Tkinter.Entry(self.root,
                                      width=80,
                                      textvariable=self.varAge)
        self.entryAge.place(x=top_col_4 + w / 12,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)

        self.LabelStartFromTrial = Tkinter.Label(self.root, text='Start Num:')
        self.LabelStartFromTrial.place(x=top_col_5,
                                       y=top_entry_y,
                                       width=w / 12,
                                       height=h / 20)
        self.LabelStartFromTrial.config(font=("Courier", 15, "bold"))
        self.entryStartFromTrial = Tkinter.Entry(
            self.root, width=80, textvariable=self.startTrialNum)
        self.entryStartFromTrial.place(x=top_col_5 + w / 12,
                                       y=top_entry_y,
                                       width=w / 20,
                                       height=h / 20)

        self.buttonOk = Tkinter.Button(self.root,
                                       text='Confirm',
                                       command=self.login)
        self.buttonOk.place(x=top_col_6,
                            y=top_entry_y,
                            width=w / 20,
                            height=h / 20)
        self.buttonOk.config(font=("Courier", 12, "bold"))
        self.buttonCancel = Tkinter.Button(self.root,
                                           text='Cancel',
                                           command=self.cancel)
        self.buttonCancel.place(x=top_col_6 + w / 18,
                                y=top_entry_y,
                                width=w / 20,
                                height=h / 20)
        self.buttonCancel.config(font=("Courier", 12, "bold"))

        self.buttonRetry = Tkinter.Button(self.root,
                                          text='Retry',
                                          command=self.retry)
        self.buttonRetry.place(x=top_col_6 + 2 * w / 18,
                               y=top_entry_y,
                               width=w / 20,
                               height=h / 20)
        self.buttonRetry.config(font=("Courier", 12, "bold"))

        self.Info_Header = Label(
            self.root,
            text="Trial\tRecongition Load\tHandness\tComponent",
            anchor=W)
        self.Info_Header.config(font=("Courier", 12, "bold"))

        self.Info = Label(self.root, textvariable=self.TrialInfo, anchor=W)
        self.Info.config(font=("Courier", 12, "bold"))

        # Set up and place the images at the second level
        img_1 = Image.open('img/F01.jpg')
        img_1 = img_1.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_1 = ImageTk.PhotoImage(img_1)

        img_2 = Image.open('img/F02.jpg')
        img_2 = img_2.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_2 = ImageTk.PhotoImage(img_2)

        img_3 = Image.open('img/F03.jpg')
        img_3 = img_3.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_3 = ImageTk.PhotoImage(img_3)

        img_4 = Image.open('img/F04.jpg')
        img_4 = img_4.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_4 = ImageTk.PhotoImage(img_4)

        img_5 = Image.open('img/F05.jpg')
        img_5 = img_5.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_5 = ImageTk.PhotoImage(img_5)

        img_6 = Image.open('img/F06.jpg')
        img_6 = img_6.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_6 = ImageTk.PhotoImage(img_6)

        img_7 = Image.open('img/F07.jpg')
        img_7 = img_7.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_7 = ImageTk.PhotoImage(img_7)

        img_8 = Image.open('img/F08.jpg')
        img_8 = img_8.resize((w / 6, h / 6), Image.ANTIALIAS)
        img_8 = ImageTk.PhotoImage(img_8)

        col_x_1 = w / 15
        col_x_2 = 2 * w / 15 + w / 6
        col_x_3 = 3 * w / 15 + 2 * w / 6
        col_x_4 = 4 * w / 15 + 3 * w / 6

        label_level_1_y = top_entry_y + 2 * h / 20
        label_level_2_y = top_entry_y + 4 * h / 20 + h / 6

        img_level_1_y = top_entry_y + 3 * h / 20
        img_level_2_y = top_entry_y + 5 * h / 20 + h / 6

        self.FP1 = Label(self.root, text="1. empty", fg='blue')
        self.FP1.place(x=col_x_1, y=label_level_1_y)
        self.FP1.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_1).place(x=col_x_1,
                                                        y=img_level_1_y)

        self.FP2 = Label(self.root, text="2. low", fg='blue')
        self.FP2.place(x=col_x_2, y=label_level_1_y)
        self.FP2.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_2).place(x=col_x_2,
                                                        y=img_level_1_y)

        self.FP3 = Label(self.root, text="3. high", fg='blue')
        self.FP3.place(x=col_x_3, y=label_level_1_y)
        self.FP3.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_3).place(x=col_x_3,
                                                        y=img_level_1_y)

        self.FP4 = Label(self.root, text="4. medium", fg='blue')
        self.FP4.place(x=col_x_4, y=label_level_1_y)
        self.FP4.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_4).place(x=col_x_4,
                                                        y=img_level_1_y)

        self.FP5 = Label(self.root, text="5. increasing", fg='blue')
        self.FP5.place(x=col_x_1, y=label_level_2_y)
        self.FP5.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_5).place(x=col_x_1,
                                                        y=img_level_2_y)

        self.FP6 = Label(self.root, text="6. decreasing", fg='blue')
        self.FP6.place(x=col_x_2, y=label_level_2_y)
        self.FP6.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_6).place(x=col_x_2,
                                                        y=img_level_2_y)

        self.FP7 = Label(self.root, text="7. click", fg='blue')
        self.FP7.place(x=col_x_3, y=label_level_2_y)
        self.FP7.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_7).place(x=col_x_3,
                                                        y=img_level_2_y)

        self.FP8 = Label(self.root, text="8. drop", fg='blue')
        self.FP8.place(x=col_x_4, y=label_level_2_y)
        self.FP8.config(font=("Courier", 15, "bold"))
        Label(self.root, text="abc", image=img_8).place(x=col_x_4,
                                                        y=img_level_2_y)

        # User haptic feel part and Quize part
        self.Question = Label(self.root, textvariable=self.Question_text)

        # Show the current position information on right side
        self.CurrPosition = Label(self.root,
                                  text="Position Info",
                                  textvariable=self.Position_Info)
        self.CurrPosition.place(x=12 * self.width / 16, y=7 * self.height / 8)
        self.CurrPosition.config(font=("Courier", 15, "bold"), fg="red")

        # Show the Force Profile on left side
        self.CurrDebug = Label(self.root,
                               text="Debug Info",
                               textvariable=self.Debug_Info)
        if self.show_debug:
            self.CurrDebug.place(x=2 * self.width / 16, y=3 * self.height / 4)
            self.CurrDebug.config(font=("Courier", 20, "bold"), fg="red")

        self.Choice_row_1 = Label(self.root,
                                  textvariable=self.Question_choice_row_1)
        self.Choice_row_2 = Label(self.root,
                                  textvariable=self.Question_choice_row_2)
        self.Choice_row_3 = Label(self.root,
                                  textvariable=self.Question_choice_row_3)

        self.User_Answer = Tkinter.Label(self.root, text='Answer: ', fg="blue")
        self.User_Answer.place(x=w / 2 - w / 8, y=7 * h / 8)
        self.User_Answer.config(font=("Courier", 20, "bold"))
        self.entry_Answer = Tkinter.Entry(self.root,
                                          width=80,
                                          textvariable=self.Answer)
        self.entry_Answer.place(x=w / 2 - w / 25,
                                y=7 * h / 8,
                                width=w / 12,
                                height=h / 25)

        self.root.mainloop()
Example #27
0
 def __init__(self, batch, group, x, y, block):
     super(Exit, self).__init__(batch, group, x, y, block)
     self.arrows = shapes.Arrows(batch, group, (x, y, 0), self.size_arrow, self.color_arr)
     self.alpha_arr = Spring(self.color_arr[3], .2, .01)
     self.color_spr = Spring(Vector3(*self.color), .1, .01)
     self.is_on = block.is_on