-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaptop_Serial_control.py
79 lines (56 loc) · 2.06 KB
/
Laptop_Serial_control.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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 1 20:21:44 2021
@author: matth
"""
import serial
import time
import keyboard
baud = 115200
port='/dev/ttyUSB0'
dt = 0.01
set_left=0
set_right=0
motor_speed=40
steering_ratio=1
if __name__=='__main__':
s =serial.Serial(port,baud,timeout=0.005)
print(s.name)
def ardu_msg(left,right):
command = 'l'+str(int(set_left))+'r'+str(int(set_right))+'\n'
s.write(bytes(command,'utf-8'))
#reply = s.read(1000)
#print (reply)
last_t = time.time()
while True:
## check keyboard and send messages accordingly
if time.time()-last_t >dt:
if keyboard.is_pressed('q'):
break
if keyboard.is_pressed('w') and motor_speed<=40:
motor_speed+=1
print('motor_speed = ',motor_speed)
if keyboard.is_pressed('s') and motor_speed>0:
motor_speed-=1
print('motor_speed = ',motor_speed)
if keyboard.is_pressed('up'):
motor_left=motor_speed
motor_right=motor_speed
elif keyboard.is_pressed('down'):
motor_left=-motor_speed
motor_right=-motor_speed
else:
motor_left=0
motor_right=0
if keyboard.is_pressed('left'):
motor_left-=motor_speed*steering_ratio
motor_right+=motor_speed*steering_ratio
if keyboard.is_pressed('right'):
motor_left+=motor_speed*steering_ratio
motor_right-=motor_speed*steering_ratio
set_left=motor_left
set_right=motor_right
ardu_msg(motor_left,motor_right)
last_t=time.time()
if s.in_waiting:
print(s.read(100).decode('utf-8'))