Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
X
XMU-China
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
2023 Competition
Software Tools
XMU-China
Commits
25bb2014
Commit
25bb2014
authored
1 year ago
by
Jiacheng Huang
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
2de89f4d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ESP-32/Controller.py
+707
-0
707 additions, 0 deletions
ESP-32/Controller.py
with
707 additions
and
0 deletions
ESP-32/Controller.py
0 → 100644
+
707
−
0
View file @
25bb2014
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
#import webrepl
#webrepl.start()
import
network
import
utime
as
time
#ssid = "This man belongs to charming Emi"
#password = "cjh961018"
ssid
=
"
Pilab494
"
password
=
"
Pilab494
"
#ssid = 'pilab-494'
#password = 'pilab-494'
station
=
network
.
WLAN
(
network
.
STA_IF
)
station
.
active
(
True
)
time
.
sleep
(
5
)
station
.
connect
(
ssid
,
password
)
print
(
station
.
isconnected
())
while
station
.
isconnected
()
==
False
:
print
(
'
connecting now
'
)
time
.
sleep
(
1
)
print
(
'
connected
'
)
import
urequests
as
requests
databese_web
=
'
http://101.34.0.108:2000/
'
def
get_previes_experiment
():
create_url
=
databese_web
+
'
get_previous_experiment
'
res
=
requests
.
post
(
create_url
)
data
=
[
'
Connection Lost
'
,]
if
res
.
status_code
==
200
:
data
=
dict
(
res
.
json
())[
'
msg
'
]
return
data
def
creat_new_process
(
create_time
):
create_url
=
databese_web
+
'
creat_new_process
'
payload
=
{
"
time
"
:
create_time
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
work
=
0
if
res
.
status_code
==
200
:
if
res
.
json
()[
'
msg
'
]:
work
=
1
return
work
def
update_database
(
create_time
,
index
,
x
,
y
):
create_url
=
databese_web
+
'
update_database
'
payload
=
{
"
time
"
:
create_time
,
'
index
'
:
index
,
'
x
'
:
x
,
'
y
'
:
y
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
work
=
0
if
res
.
status_code
==
200
:
if
res
.
json
()[
'
msg
'
]:
work
=
1
return
work
def
update_index
(
create_time
,
index
,
data
):
create_url
=
databese_web
+
'
update_index
'
payload
=
{
"
time
"
:
create_time
,
'
index
'
:
index
,
'
data
'
:
data
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
work
=
0
if
res
.
status_code
==
200
:
if
res
.
json
()[
'
msg
'
]:
work
=
1
return
work
def
lookup_database
(
create_time
,
index
,
cur
):
create_url
=
databese_web
+
'
lookup_database
'
payload
=
{
"
time
"
:
create_time
,
'
index
'
:
index
,
'
cur
'
:
cur
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
if
res
.
status_code
==
200
:
return
res
.
json
()[
'
x
'
],
res
.
json
()[
'
y
'
]
else
:
return
[],
[]
def
lookup_index
(
create_time
,
index
):
create_url
=
databese_web
+
'
lookup_index
'
payload
=
{
"
time
"
:
create_time
,
'
index
'
:
index
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
if
res
.
status_code
==
200
:
return
res
.
json
()[
'
data
'
]
else
:
return
[]
def
lookup_cab
(
create_time
):
create_url
=
databese_web
+
'
lookup_cab
'
payload
=
{
"
time
"
:
create_time
}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
if
res
.
status_code
==
200
:
return
res
.
json
()[
'
data
'
]
else
:
return
[]
def
lookup_work
():
create_url
=
databese_web
+
'
lookup_work
'
res
=
requests
.
post
(
create_url
)
if
res
.
status_code
==
200
:
return
res
.
json
()[
'
data
'
]
else
:
return
[]
def
F
(
create_time
,
control_data
,
sensor_data
):
create_url
=
databese_web
+
'
F
'
payload
=
{
"
time
"
:
create_time
,
"
control_data
"
:
str
(
control_data
),
"
sensor_data
"
:
str
(
sensor_data
)}
res
=
requests
.
post
(
create_url
,
json
=
payload
)
if
res
.
status_code
==
200
:
return
res
.
json
()[
'
send_control_data
'
]
else
:
return
[]
from
machine
import
Pin
,
ADC
,
PWM
import
onewire
import
ds18x20
pumps
=
[
15
,
13
,
4
,
5
,
18
,
19
,
22
]
# pump1, pump2, pump3, pump4, pump5, pump6, pump7
sensors
=
[
34
,
32
]
# pH, Temp
heat
=
[
2
]
# heat
air
=
[
21
]
# Air
pumps_pin
=
[]
sensors_pin
=
[]
heat_pin
=
[]
air_pin
=
[]
for
i
in
pumps
:
pumps_pin
.
append
(
Pin
(
i
,
Pin
.
OUT
))
pumps_pin
[
-
1
].
off
()
for
i
in
sensors
:
sensors_pin
.
append
(
Pin
(
i
))
ph
=
ADC
(
sensors_pin
[
0
],
atten
=
ADC
.
ATTN_11DB
)
ow
=
onewire
.
OneWire
(
sensors_pin
[
1
])
ds
=
ds18x20
.
DS18X20
(
ow
)
roms
=
ds
.
scan
()
ds
.
convert_temp
()
time
.
sleep
(
1
)
feedback
=
ds
.
read_temp
(
roms
[
0
])
print
(
feedback
)
for
i
in
heat
:
heat_pin
.
append
(
Pin
(
i
,
Pin
.
OUT
))
heat_pin
[
-
1
].
off
()
for
i
in
air
:
air_pin
.
append
(
PWM
(
Pin
(
i
)))
air_pin
[
-
1
].
freq
(
10
)
air_pin
[
-
1
].
duty
(
0
)
class
PID
:
def
__init__
(
self
,
P
=
0.2
,
I
=
0.0
,
D
=
0.0
):
self
.
Kp
=
P
self
.
Ki
=
I
self
.
Kd
=
D
self
.
sample_time
=
0.00
self
.
current_time
=
time
.
time
()
self
.
last_time
=
self
.
current_time
self
.
clear
()
def
clear
(
self
):
self
.
SetPoint
=
0.0
self
.
PTerm
=
0.0
self
.
ITerm
=
0.0
self
.
DTerm
=
0.0
self
.
last_error
=
0.0
self
.
int_error
=
0.0
self
.
windup_guard
=
20.0
self
.
output
=
0.0
def
update
(
self
,
feedback_value
):
error
=
self
.
SetPoint
-
feedback_value
self
.
current_time
=
time
.
time
()
delta_time
=
self
.
current_time
-
self
.
last_time
delta_error
=
error
-
self
.
last_error
if
(
delta_time
>=
self
.
sample_time
):
self
.
PTerm
=
self
.
Kp
*
error
# 比例
self
.
ITerm
+=
error
*
delta_time
# 积分
if
(
self
.
ITerm
<
-
self
.
windup_guard
):
self
.
ITerm
=
-
self
.
windup_guard
elif
(
self
.
ITerm
>
self
.
windup_guard
):
self
.
ITerm
=
self
.
windup_guard
self
.
DTerm
=
0.0
if
delta_time
>
0
:
self
.
DTerm
=
delta_error
/
delta_time
self
.
last_time
=
self
.
current_time
self
.
last_error
=
error
self
.
output
=
self
.
PTerm
+
(
self
.
Ki
*
self
.
ITerm
)
+
(
self
.
Kd
*
self
.
DTerm
)
def
setKp
(
self
,
proportional_gain
):
self
.
Kp
=
proportional_gain
def
setKi
(
self
,
integral_gain
):
self
.
Ki
=
integral_gain
def
setKd
(
self
,
derivative_gain
):
self
.
Kd
=
derivative_gain
def
setWindup
(
self
,
windup
):
self
.
windup_guard
=
windup
def
setSampleTime
(
self
,
sample_time
):
self
.
sample_time
=
sample_time
import
uasyncio
async
def
blink
(
led
,
period_ms
):
led
.
on
()
await
uasyncio
.
sleep_ms
(
int
(
5
*
1000
*
period_ms
/
100
))
led
.
off
()
async
def
temp_read
():
await
uasyncio
.
sleep_ms
(
4_000
)
ds
.
convert_temp
()
async
def
air
(
led
,
period_ms
):
led
.
duty
(
int
(
period_ms
/
100
*
1000
))
async
def
main
(
control_data
):
uasyncio
.
create_task
(
blink
(
pumps_pin
[
0
],
control_data
[
0
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
1
],
control_data
[
1
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
2
],
control_data
[
2
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
3
],
control_data
[
3
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
4
],
control_data
[
4
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
5
],
control_data
[
5
]))
uasyncio
.
create_task
(
blink
(
pumps_pin
[
6
],
control_data
[
6
]))
uasyncio
.
create_task
(
blink
(
heat_pin
[
0
],
control_data
[
7
]))
uasyncio
.
create_task
(
air
(
air_pin
[
0
],
control_data
[
8
]))
uasyncio
.
create_task
(
temp_read
())
await
uasyncio
.
sleep_ms
(
5_000
)
# ssid = "Pilab494"
# password = "Pilab494"
# station = network.WLAN(network.STA_IF)
# station.active(True)
# time.sleep(5)
# station.connect(ssid, password)
# print(station.isconnected())
# while station.isconnected() == False:
# print('connecting now')
# time.sleep(1)
# print('connected')
start_time
=
time
.
time
()
cab
=
False
ph_average
=
[]
while
True
:
# try:
create_time
=
lookup_work
()
print
(
create_time
)
now_time
=
time
.
time
()
if
not
(
cab
):
time
.
sleep
(
1
)
cab_now
=
lookup_cab
(
create_time
)
print
(
cab_now
)
if
cab_now
==
[
1
,
3
,
5
,
5
,
5
,
5
,
5
,
5
,
5
]:
cab
=
True
data
=
lookup_database
(
create_time
,
'
Calibration-pH
'
,
-
1
)
vx
,
py
=
data
[
1
],
data
[
0
]
k_list
=
[]
b_list
=
[]
k_list
.
append
((
py
[
0
]
-
py
[
1
])
/
(
vx
[
0
]
-
vx
[
1
]))
k_list
.
append
((
py
[
1
]
-
py
[
2
])
/
(
vx
[
1
]
-
vx
[
2
]))
k_list
.
append
((
py
[
0
]
-
py
[
2
])
/
(
vx
[
0
]
-
vx
[
2
]))
b_list
.
append
(
py
[
0
]
-
vx
[
0
]
*
k_list
[
0
])
b_list
.
append
(
py
[
1
]
-
vx
[
1
]
*
k_list
[
1
])
b_list
.
append
(
py
[
0
]
-
vx
[
0
]
*
k_list
[
2
])
phk
=
sum
(
k_list
)
/
len
(
k_list
)
phb
=
sum
(
b_list
)
/
len
(
b_list
)
pid1
=
PID
(
0.8
,
0.6
,
0.005
)
pid1
.
SetPoint
=
37.0
pid1
.
setSampleTime
(
0.01
)
pid2
=
PID
(
0.01
,
0.001
,
0.00003
)
pid2
.
SetPoint
=
7.0
pid2
.
setSampleTime
(
0.01
)
control_data
=
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
]
# pump + heat + air
sensor_data
=
[
0
,
0
]
else
:
# Pump
for
i
in
range
(
2
,
9
):
if
cab_now
[
i
]
==
0
:
continue
elif
cab_now
[
i
]
==
1
:
pumps_pin
[
i
-
2
].
on
()
elif
cab_now
[
i
]
==
2
:
pumps_pin
[
i
-
2
].
off
()
elif
cab_now
[
i
]
==
3
:
pumps_pin
[
i
-
2
].
on
()
time
.
sleep
(
60
)
pumps_pin
[
i
-
2
].
off
()
update_index
(
create_time
,
'
c_Pump
'
+
str
(
i
-
1
),
4
)
elif
cab_now
[
i
]
==
4
:
continue
elif
cab_now
[
i
]
==
5
:
continue
# pH
if
not
(
cab_now
[
0
]):
if
cab_now
[
1
]
==
0
:
continue
elif
cab_now
[
1
]
==
1
:
start_ph_time
=
time
.
time
()
while
True
:
now_ph_time
=
time
.
time
()
if
now_ph_time
-
start_ph_time
<
60
:
ph_data
=
ph
.
read
()
update_database
(
create_time
,
'
d_pH
'
,
ph_data
,
ph_data
)
time
.
sleep
(
0.1
)
else
:
break
elif
cab_now
[
1
]
==
2
:
continue
elif
cab_now
[
1
]
==
3
:
continue
else
:
work
=
int
(
lookup_index
(
create_time
,
'
p_Work
'
))
print
(
work
)
if
work
==
1
:
try
:
feedback1
=
ds
.
read_temp
(
roms
[
0
])
pid1
.
update
(
feedback1
)
operation1
=
pid1
.
output
/
10
*
100
print
(
feedback1
)
control_data
[
7
]
=
min
(
max
(
operation1
,
0
),
100
)
sensor_data
[
1
]
=
feedback1
print
(
control_data
[
7
])
feedback2
=
ph
.
read
()
if
len
(
ph_average
)
<
5
:
ph_average
.
append
(
feedback2
)
else
:
ph_average
=
ph_average
[
1
::]
ph_average
.
append
(
feedback2
)
print
(
ph_average
)
feedback2
=
sum
(
ph_average
)
/
len
(
ph_average
)
trans_feedback2
=
feedback2
*
phk
+
phb
print
(
trans_feedback2
)
pid2
.
update
(
trans_feedback2
)
operation2
=
pid2
.
output
/
10
*
100
control_data
[
0
]
=
min
(
max
(
operation2
,
0
),
100
)
sensor_data
[
0
]
=
trans_feedback2
print
(
control_data
[
0
])
a
=
time
.
time
()
uasyncio
.
run
(
main
(
control_data
))
b
=
time
.
time
()
print
(
b
-
a
)
send_control_data
=
F
(
create_time
,
control_data
,
sensor_data
)
control_data
[
1
]
=
send_control_data
[
1
]
control_data
[
2
]
=
send_control_data
[
2
]
control_data
[
3
]
=
send_control_data
[
3
]
control_data
[
4
]
=
send_control_data
[
4
]
control_data
[
5
]
=
send_control_data
[
5
]
control_data
[
6
]
=
send_control_data
[
6
]
control_data
[
7
]
=
send_control_data
[
7
]
pid1
.
SetPoint
=
send_control_data
[
9
]
pid2
.
SetPoint
=
send_control_data
[
8
]
control_data
[
8
]
=
send_control_data
[
10
]
except
:
# a = time.time()
#
# uasyncio.run(main(control_data))
#
# b = time.time()
#
# print('nimabi')
#
# print(b-a)
# send_control_data = F(create_time, control_data, sensor_data)
control_data
[
1
]
=
send_control_data
[
1
]
control_data
[
2
]
=
send_control_data
[
2
]
control_data
[
3
]
=
send_control_data
[
3
]
control_data
[
4
]
=
send_control_data
[
4
]
control_data
[
5
]
=
send_control_data
[
5
]
control_data
[
6
]
=
send_control_data
[
6
]
control_data
[
7
]
=
send_control_data
[
7
]
pid1
.
SetPoint
=
send_control_data
[
9
]
pid2
.
SetPoint
=
send_control_data
[
8
]
control_data
[
8
]
=
send_control_data
[
10
]
else
:
time
.
sleep
(
5
)
print
(
'
Rest
'
)
# except:
#
# print('Fuck')
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment