forked from MTS-AUV-ZHCET/ROVC2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththruster5.py
125 lines (100 loc) · 2.91 KB
/
thruster5.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
import ms5837 # librarty of preessure sensor
import time
import pigpio
import numpy as np
from pysticks import get_controller
con = get_controller()
thruster_1 = 9 # Enter the PIN Number to Which Thrsuter 1 is coonected
thruster_2 = 11 # Enter the PIN Number to Which Thrsuter 2 is coonected
thruster_3 = 25 # Enter the PIN Number to Which Thrsuter 3 is coonected
thruster_4 = 8
thruster_pins = [thruster_1, thruster_2, thruster_3, thruster_4]
thvalue = [1500, 1500, 1500, 1500]
pid_p = 0
pid_i = 0
pid_d = 0
kp = 655
ki = 0.00
kd = 655
global time_
sensor = ms5837.MS5837_02BA()
if not sensor.init():
print("Sensor could not be initialized")
exit(1)
if not sensor.read():
print("Sensor read failed!")
exit(1)
pi = pigpio.pi()
for item in thruster_pins:
pi.set_servo_pulsewidth(item, 1500)
def map_values_int(value,max_val,min_val):
if value < -1 or value > 1:
return None
elif value == 0:
return 1500
else:
scaled_value = (value + 1) / 2 * (max_val - min_val) + min_val
return int(scaled_value)
def map_values(value):
if value < -1 or value > 1:
return None
elif value == 0:
return 1500
else:
return int(1500 + (value * 300))
def sig(value):
if value < -1 or value > 1:
return None
elif value == 0:
return 1500
else:
return int((np.sign(value) * (27*(abs(value)) - 1) / (27*(1) - 1)) * 300 + 1500)
def forward():
con.update()
up=map_values_int(con.getThrottle(),1900,1100)
move=map_values_int(con.getPitch(),1900,1100)
turn=sig(con.getYaw())
pi.set_servo_pulsewidth(thruster_1, move)
pi.set_servo_pulsewidth(thruster_3, move)
print("turn : ", map_values(turn))
print(move)
def map_values_dynamic(value, max_val, min_val):
scaled_value = (value + 1) / 2 * (max_val - min_val) + min_val
return float(scaled_value)
start_time=0
while True:
start_time = time.time()
forward()
con.update()
target_depth = map_values_dynamic(con.getThrottle(), 5, 0)
if sensor.read():
currentDepth = sensor.depth()
h = currentDepth+0.25
time_prev = start_time
run_time = time.time()
elapsedTime=(run_time-time_prev)/1000
print(h)
error = h-target_depth
#pid
previous_error=0
pid_p = kp * error
if -0.05 < error < 0.05:
pid_i = pid_i + (ki * error)
pid_d = kd * ((error - previous_error) / elapsedTime)
PID = pid_p + pid_i + pid_d
if PID < -400:
PID = -400
if PID > 400:
PID = 400
pwmup = 1500 - PID
if pwmup < 1100:
pwmup = 1100
if pwmup > 1900:
pwmup = 1900
print(pwmup)
pi.set_servo_pulsewidth(thruster_1, pwmup)
pi.set_servo_pulsewidth(thruster_2, pwmup)
previous_error = error
else:
print("Sensor read failed!")
exit(1)