stage3-ag6

  • Home
    • Site Map
    • reveal
    • blog
  • About
  • Collaborative flowchart
  • stage1
    • stage1-ag7
    • stage1-ag14
    • stage1-ag13
  • stage2
    • 階段2-ag7
    • stage2-ag12
  • stage3
    • 任務1
      • 40823125-1
      • 40823152-1
    • 任務 2
      • 40823149
      • 40823125-2
      • 40823152-2
    • w16-exam
      • 40823125
      • 40823122
      • 40823152
  • W18
    • 1.Basketball machine senser
    • 2.Basketball machine Slider
    • 3.Basketball machine by (MTB_Robot)
40823125-2 << Previous Next >> w16-exam

40823152-2

stage2-ag12 python keyboard

使用python keyboard 來操控投籃機


程式碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sim as vrep
import sys
import keyboard
# child threaded script:
#simExtRemoteApiStart(19999)
  
vrep.simxFinish(-1)
  
clientID = vrep.simxStart('127.0.0.1', 19999, True, True, 5000, 5)
errorCode,left_motor_handle=vrep.simxGetObjectHandle(clientID,'Revolute_joint',vrep.simx_opmode_oneshot_wait)
 
if clientID!= -1:
    print("Connected to remote server")
    while True:
        try:
            if keyboard.is_pressed('up'):
                  vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,-16, vrep.simx_opmode_oneshot_wait)
                  print('up ')
            if keyboard.is_pressed('down'):    
                  vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,1, vrep.simx_opmode_oneshot_wait)
                  print('down')
            if keyboard.is_pressed('space'):
                  vrep.simxSetJointTargetVelocity(clientID,left_motor_handle,0, vrep.simx_opmode_oneshot_wait)
                  print('space')
                   
            else:
                pass
        except:
            break
else:
    print('Connection not successful')
    sys.exit('Could not connect')
  
  
   
  
if errorCode == -1: 
    print('Can not find left or right motor')
    sys.exit()

參考網址

1
2
#需要:
pip3 install keyboard
1
2
3
4
5
6
7
8
9
10
import keyboard  # using module keyboard
while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('q'):  # if key 'q' is pressed
            print('You Pressed A Key!')
            break  # finishing the loop
        else:
            pass
    except:
        break  # if user pressed a key other than the given key the loop will break

40823125-2 << Previous Next >> w16-exam

Copyright © All rights reserved | This template is made with by Colorlib