-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver10.py
167 lines (128 loc) · 3.75 KB
/
driver10.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
163
164
165
166
167
import numpy as np
import math as m
from kivy.clock import Clock
from simple_pid import PID
import simEngine
from TimeHelper import TimeHelper
from kivy.properties import NumericProperty,ObjectProperty
from kivy.uix.widget import Widget
try:
from PCPlot import PCPlot
except:
pass
# pid?
class driver10:
fps = 30.0
work = False
lastValue = None
def __init__(self, simEng):
self.sim = simEng
self.gui = self.sim.gui
self.th = TimeHelper()
self.actionHistory = [0]
self.plt = None
#self.Kp = 0.023
self.pidValues = Widget()
self.pidP = 0.023
self.pidI = 0.009
self.pidD = 0.0018
self.p = PID( self.pidP, self.pidI, self.pidD)#, setpoint=0,auto_mode=True )
self.p.proportional_on_measurement = True
self.resp = 0.0002
#self.p.output_limits = (-1,1)
def plotIt(self):
self.gui.plt = PCPlot(self.gui)
def updateGuiPIDVals(self):
self.gui.rl.ids.ti_d10_p.text = str(self.p.Kp)
self.gui.rl.ids.ti_d10_i.text = str(self.p.Ki)
self.gui.rl.ids.ti_d10_d.text = str(self.p.Kd)
self.gui.rl.ids.ti_d10_response.text = str(self.resp)
def run(self,render = True):
self.sim.reset()
self.lc = 0
self.work = True
self.render = render
d = 0.5
self.updateGuiPIDVals()
#self.p.proportional_on_measurement = True
#self.p.set_auto_mode(True, last_output=8.0)
if self.gui.platform == 'pc':
self.plt = self.gui.plt
self.mainLoop()
def setPidPars(self):
self.p.tunings = (
float(self.gui.rl.ids.ti_d10_p.text),
float(self.gui.rl.ids.ti_d10_i.text),
float(self.gui.rl.ids.ti_d10_d.text)
)
self.resp = float(self.gui.rl.ids.ti_d10_response.text)
self.updateGuiPIDVals()
def setPar(self, par, obj):
print("setPar",par,"->",obj)
val = obj.value
if par == 0:
self.p.Kp = float(self.gui.rl.ids.ti_d10_p.text)
if par == 1:
self.p.Ki = float(self.gui.rl.ids.ti_d10_i.text)
if par == 2:
self.p.Kd = float(self.gui.rl.ids.ti_d10_d.text)
print(self.p.Kp, self.p.Ki, self.p.Kd)
def mainLoop(self,*a):
s = self.sim
b = s.boat
#control = int(self.p(b['cogError'])*500)/500.000
control = self.p(b['cogError'])
action = 0
if self.gui.platform == 'pc' and self.plt != None:
t = self.th.getTimestamp(True)
self.plt.simPID[0].append(t)
self.plt.simPID[1].append((control*10.0)%360.00)
if 1:
try:
print("target from phone ",self.gui.sen.comCal.hdg)
self.sim.targetCog = int(self.gui.sen.comCal.hdg)
except:
print("EE - no phone data no hdg in comCal")
if self.lastValue == None:
self.lastValue = control
#print(
# "cogError",round(b['cogError'],6),
# " pid",round(control,6),
# " last",round(self.lastValue,6),
# " diff",round((control-self.lastValue),6))
#print("control %s for cogError %s"%(control, b['cogError']))
if 1:
print("resp",self.resp,' fab',m.fabs( control-self.lastValue ))
if m.fabs( control-self.lastValue ) >= self.resp:
if control > self.lastValue:
action = -1
print("-")
elif control < self.lastValue:
action = 1
print("+")
elif 0:
if -control < -0.2:
action = -1
elif -control > 0.2:
action = 1
else:
gRot = 0.5
if -control < -0.2 and b['gRot']>-gRot:
action = -1
elif -control > 0.2 and b['gRot']<gRot:
action = 1
if control != 0 or ( control !=0 and self.actionHistory[-1] != 0 ):
self.lastValue = control
#print(" pid return {} {} {}".format(action,control, b['cogError']))
self.sim.iter(action)
self.actionHistory.append( action )
if len(self.actionHistory) > 500:
self.actionHistory.pop(0)
if self.render:
self.sim.renderFrame()
self.lc+=1
if self.work:
if self.render:
Clock.schedule_once( self.mainLoop, 1.00 / self.fps )
else:
Clock.schedule_once( self.mainLoop, 0.0 )