-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_simple_ctrl.py
93 lines (76 loc) · 2.66 KB
/
serial_simple_ctrl.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
import serial
import argparse
import threading
import json
import time
def read_serial():
while True:
data = ser.readline().decode('utf-8')
if data:
print(f"Received: {data}", end='')
def load_test():
while True:
for i in range(-500, 500):
data = {"T":133,"X":i/100,"Y":i/100,"SPD":0,"ACC":0}
ser.write((json.dumps(data)+'\n').encode("utf-8"))
for i in range(500, -500, -1):
data = {"T":133,"X":i/100,"Y":i/100,"SPD":0,"ACC":0}
ser.write((json.dumps(data)+'\n').encode("utf-8"))
# for i in range(-200, 200):
# data = {"T":1,"L":i/1000,"R":i/1000}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
# for i in range(200, -200, -1):
# data = {"T":1,"L":i/1000,"R":i/1000}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
def main():
global ser
# parser = argparse.ArgumentParser(description='Serial JSON Communication')
# parser.add_argument('port', type=str, help='Serial port name (e.g., COM1 or /dev/ttyUSB0)')
# args = parser.parse_args()
ser = serial.Serial('/dev/ttyACM0', baudrate=115200, dsrdtr=None)
ser.setRTS(False)
ser.setDTR(False)
# serial_recv_thread = threading.Thread(target=read_serial)
# serial_recv_thread.daemon = True
# serial_recv_thread.start()
while True:
read_serial()
# try:
# while True:
# command = input("")
# ser.write(command.encode() + b'\n')
# except KeyboardInterrupt:
# pass
# finally:
# ser.close()
# serial_recv_thread = threading.Thread(target=load_test)
# serial_recv_thread.daemon = True
# serial_recv_thread.start()
# serial_recv_thread_1 = threading.Thread(target=load_test)
# serial_recv_thread_1.daemon = True
# serial_recv_thread_1.start()
# data = {"T":1,"L":0.5,"R":0.5}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
# print(data)
# time.sleep(0.00001)
# try:
# while True:
# for i in range(-500, 500):
# data = {"T":1,"L":i/1000,"R":i/1000}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
# for i in range(500, -500, -1):
# data = {"T":1,"L":i/1000,"R":i/1000}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
# except KeyboardInterrupt:
# pass
# finally:
# ser.close()
# try:
# data = {"T":600}
# ser.write((json.dumps(data)+'\n').encode("utf-8"))
# except KeyboardInterrupt:
# pass
# finally:
# ser.close()
if __name__ == "__main__":
main()