forked from Sven-J-Steinert/Rocket-Hopper-DDPG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeensy_Communication.py
163 lines (121 loc) · 4.85 KB
/
Teensy_Communication.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# importing libraries
import serial # Library for serial communication with python - install command: pip install pyserial
import time # time tracking library
import serial.tools.list_ports
from TD3 import *
from datetime import datetime
from tqdm import tqdm
import random
# create logging file
now = datetime.now()
timestamp = now.strftime("%Y_%m_%d_-_%H_%M_%S")
filename = f"logs_{timestamp}.txt"
with open(filename, 'w') as file:
file.write(f"counter,loop_timer,x_target,teensy_time,acceleration,position,pressure\n")
# Function to log data to a text file
def log_data(counter,loop_timer, x_target, teensy_time, acceleration, position, pressure):
timestamp = time.time() # Use current time as a timestamp
with open(filename, 'a') as file:
file.write(f"{counter},{loop_timer},{x_target},{teensy_time},{acceleration},{position},{pressure}\n")
# teensy loop time
loop_cycle_time = 1/33 # 60 measurement aquistions per second; can be adjusted to max of 66Hz
control_duration = 11 # in [s] --> your anticipated flight duration / control event duration
### establishing serial connection
# you need to figure out the serial port ID of your laptop; it might be different from the predefined
hardware_id = '1A86:7523' # lookup once for specific device
while True:
connected = False
while not connected:
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
if hardware_id in hwid:
dev_port = port
connected = True
time.sleep(0.1)
ser = serial.Serial(dev_port, baudrate=57600)
break
# setup/start commands for the hopper
# resetting error status
ser.write(b'0')
# activating valve
ser.write(b'1')
# setting the reply mode --> sending the input back yes or no
# r = reply
# n = no reply
ser.write(b'n') # don't change this
# setting the failure mode
# F = no failure
# f = failure
ser.write(b'F') # don't change this
# init agent
agent = TD3(state_dim=4, action_dim=1)
agent.load('142') # in same folder as this script
# print(agent)
# define target height
x_target = 2
# define step counter
counter = 0
# -------------------------------------------------------------------------
### main loop
main_timer = time.time() # control timer --> duration of entire control event / flight duration
# logging
#loop_timer, teensy_time, acceleration, position, pressure
action = 0
# control input which is going to be sent to teensy
action = f'<1:{action}>'
while True:
START = time.time()
# send control input / action to teensy
ser.write(action.encode())
loop_timer = time.time() # loop timer --> the time it takes for one control loop is either equivalnt to the loop_cycle_time or slower
# signal coming from teensy
raw_data = ser.readline().decode().strip() # reading the data comming from the teensy, decoding the data and removing white spaces
ser.flush()
raw_data = raw_data.split(':') # spliting the message message
print(raw_data)
teensy_time = int(raw_data[0][1:]) # time in ms since teensy power on
acceleration = float(raw_data[1]) - 9.80665 # float in [m/s^2] --> when Hopper at rest, it shows + 9.80665 m/s^2
position = float(raw_data[2]) # float in [m]
pressure = int(raw_data[3]) # int as 12 bit signal --> 0 corresponds to 0barg; 4095 correponds to 10barg
'''
INSERT YOUR CONTROL LAW HERE
'''
# # compensate 11cm offset on ground
# position = max(0, position-0.11)
# # gradually decrease the x_target to zero
# if counter >= 300:
# x_target = max(0,x_target - (2/300))
# state = np.array([x_target,position,acceleration,pressure]) # [x_target, x, a, p_actual]
# action = agent.select_action(state)
# action = map(action, -1, 1, 0, 4095)
# action = int(action)
action = 0
log_data(counter,loop_timer,x_target,teensy_time,acceleration,position,pressure)
'''
INSERT YOUR CONTROL LAW HERE
'''
# control input which is going to be sent to teensy
action = f'<1:{action}>'
# wait until loop time has passed
while time.time() - loop_timer < loop_cycle_time:
counter += 1
continue
# send control input / action to teensy
ser.write(action.encode())
END = time.time()
total_time= END - START
print(total_time)
# stop the control event once, control duration is reached
if time.time() - main_timer > control_duration:
break
# ser.write('<1:1000>'.encode()) # reduce thrust to decrease altitude
# time.sleep(1)
ser.write('<1:0>'.encode()) # shut off valve
time.sleep(0.1)
ser.write('<1:0>'.encode()) # shut off valve
time.sleep(0.1)
ser.write('<1:0>'.encode()) # shut off valve
# resetting error status
ser.write(b'0')
# close serial connection
ser.close()