-
Notifications
You must be signed in to change notification settings - Fork 1
/
manual_drive.py
109 lines (87 loc) · 3.01 KB
/
manual_drive.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import com_module
from pynput import keyboard
'''
This file contains a function used to transfer keyboard inputs
(up down left and right) to the com module to be sent to R2.
This will be used in case a manual mode is required to control the robot
'''
def drive_controller_on(key, ser):
'''
This functions sends serial commands to the motor driver depending
on what keyboard keys are pressed
Parameters
----------
Key : float
Float value corresponding to the key pressed to control drive controller
ser : serial information for when the com_module is called in order
to send info to the arduino
Returns
-------
None
'''
# this listens to see if keys are pressed
try:
# if w key is pressed
if key.vk == 119:
print("drive forward")
com_module.sendData(ser, [111, 111], 3)
# if a key is pressed
if key.vk == 97:
print("drive left")
com_module.sendData(ser, [222, 222], 3)
# if s key is pressed
if key.vk == 115:
print("drive backward")
com_module.sendData(ser, [444, 444], 3)
# if d key is pressed
if key.vk == 100:
print("drive right")
com_module.sendData(ser, [333, 333], 3)
# if special key is pressed such as ctrl or alt
except AttributeError:
print('special key {0} pressed'.format(key))
# This is the command to send data via the serial terminal
# 'ser' needs to set to provide connection info
# 'data' is the information you are trying to send via serial
# 'digits is transfer length
# com_module.sendData(ser, data, digits)
return 0
def drive_controller_off(key, ser):
'''
This functions sends serial commands to the motor driver to stop
R2 anytime that a key is released. This prevents continuous driving
Parameters
----------
key : listens to the key being pressed and passes along that key
ser : serial information for when the com_module is called in order
to send info to the arduino
Returns
-------
None
'''
print("released")
# sends 000, 000 to com module, then to ardunio, stopping R2
com_module.sendData(ser, [000, 000], 3)
return 0
def main(ser):
'''
This functions starts the keyboard listender and passes keys to
drive_controller_on and drive_controller_off
Parameters
----------
ser : serial information for when the com_module is called in order
to send info to the arduino
Returns
-------
None
'''
with keyboard.Listener(on_press=lambda event: drive_controller_on
(event, ser),
on_release=lambda event:
drive_controller_off(event, ser)) as listener:
listener.join()
# This statement starts the main function if this script is run from
# the terminal
if __name__ == "__main__":
ser = com_module.initSerialConnection("/dev/ttyACM2", 2400)
main(ser)