-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.py
168 lines (135 loc) · 4.26 KB
/
motor.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
'''
motor control module
'''
import lgpio
#import picologging as logging
import logging
import pump_util as util
import constant
logger = logging.getLogger(util.MAIN_LOGGER_NAME)
#==============================================================================
# Device Properties
#==============================================================================
M0_OUT = 2 #24v
M1_OUT = 3 #24v
M2_OUT = 4 #24v
RUN_MODE_OUT = 17 #24v
M0_IN = 26 #cur_sw0
M1_IN = 19 #cur_sw1
M2_IN = 13 #cur_sw2
def init_motors(c):
logger.info(f'init_motors({c})')
r = lgpio.gpio_claim_input(c, M0_IN, lFlags=lgpio.SET_PULL_UP)
logger.info(f'gpio_claim_input(M0_IN) return:{r}')
r = lgpio.gpio_claim_input(c, M1_IN, lFlags=lgpio.SET_PULL_UP)
logger.info(f'gpio_claim_input(M1_IN) return:{r}')
r = lgpio.gpio_claim_input(c, M2_IN, lFlags=lgpio.SET_PULL_UP)
logger.info(f'gpio_claim_input(M2_IN) return:{r}')
#set_all_motors(c, (0,0,0)) # 단말 부팅 시 모터 초기화하지 않음
#set_run_mode(c, 0)
def set_run_mode(chip, v):
'''
아래 2개 중 어떤 모드를 출력해야 하나?
(1) 수동/자동 모드
(2) 수위계/AI
'''
lgpio.gpio_write(chip, RUN_MODE_OUT, v)
# 2: PLC 모드, pump1 on
# 3: PLC 모드, pump2 on
# 4: PLC 모드, pump1 + pump2 on
# 5: PLC 모드, pump3 on
# 6: PLC 모드, pump1 + pump2 + pump3 on
def get_plc_motor_state(chip, pv):
'''모터 상태를 (M2,M1,M0)로 리턴'''
ms0 = 0
ms1 = 0
ms2 = 0
if pv.plc_pump_info==2:
ms0 = 1
elif pv.plc_pump_info==3:
ms1 = 1
elif pv.plc_pump_info==4:
ms0 = 1
ms1 = 1
elif pv.plc_pump_info==5:
ms2 = 1
elif pv.plc_pump_info==6:
ms0 = 1
ms1 = 1
ms2 = 1
return (ms0, ms1, ms2)
def get_all_motors(chip, pv):
"""3대의 모터 상태를 (M2,M1,M0)로 리턴
"""
#ms = [0, 0, 0]
#if pv.source==constant.SOURCE_AI:
# ms0 = lgpio.gpio_read(chip, M0_IN)
# ms1 = lgpio.gpio_read(chip, M1_IN)
# ms2 = lgpio.gpio_read(chip, M2_IN)
#else:
# s = pv.pump_state_plc
# ms0 = s & 1
# ms1 = s & 2
# ms2 = s & 4
ms0 = lgpio.gpio_read(chip, M0_OUT)#M0_IN)
ms1 = lgpio.gpio_read(chip, M1_OUT)#M1_IN)
ms2 = lgpio.gpio_read(chip, M2_OUT)#M1_IN)
if pv.pump1_mode == constant.PUMP_MODE_MANUAL and pv.pump2_mode == constant.PUMP_MODE_MANUAL and pv.pump3_mode == constant.PUMP_MODE_MANUAL:
ms0, ms1, ms2 = get_plc_motor_state(chip, pv)
return (ms0, ms1, ms2)
def get_motor_state(chip, m, pv):
'''m=0,1,2'''
#if pv.source==constant.SOURCE_AI:
# if m == 0:
# return lgpio.gpio_read(chip, M0_IN)
# elif m == 1:
# return lgpio.gpio_read(chip, M1_IN)
# elif m == 2:
# return lgpio.gpio_read(chip, M2_IN)
#else:
# s = pv.pump_state_plc
# return s & (2**m)
if pv.pump1_mode == constant.PUMP_MODE_MANUAL and pv.pump2_mode == constant.PUMP_MODE_MANUAL and pv.pump3_mode == constant.PUMP_MODE_MANUAL:
ms0, ms1, ms2 = get_plc_motor_state(chip, pv)
if m == 0:
return ms0
elif m == 1:
return ms1
elif m == 2:
return ms2
if m == 0:
return lgpio.gpio_read(chip, M0_OUT)#IN)
elif m == 1:
return lgpio.gpio_read(chip, M1_OUT)#IN)
elif m == 2:
return lgpio.gpio_read(chip, M2_OUT)#IN)
return None
def is_motor_running(chip, pv):
'''안쓰는 모터는 접점을 열어둬서 모터가 구동 안되는 것으로 인식하도록 해야 함'''
return get_motor_state(chip, 0, pv) or get_motor_state(
chip, 1, pv) or get_motor_state(chip, 2, pv)
def set_motor_state(chip, m, on_off):
if m == 0:
lgpio.gpio_write(chip, M0_OUT, on_off)
elif m == 1:
lgpio.gpio_write(chip, M1_OUT, on_off)
elif m == 2:
lgpio.gpio_write(chip, M2_OUT, on_off)
logger.info("SET MOTOR#{%d}/(0,1,2) = {%d}", m, on_off)
def set_all_motors(chip, m):
'''(M0, M1, M2)'''
a, b, c = m
lgpio.gpio_write(chip, M0_OUT, a)
lgpio.gpio_write(chip, M1_OUT, b)
lgpio.gpio_write(chip, M2_OUT, c)
#CFLOW_PASS = 0
#CFLOW_CPU = 1
#def set_current_flow(chip, cflow):
# if cflow == CFLOW_PASS:
# lgpio.gpio_write(chip, CSW0, 0)
# lgpio.gpio_write(chip, CSW1, 0)
# lgpio.gpio_write(chip, CSW2, 0)
# else:
# lgpio.gpio_write(chip, CSW0, 1)
# lgpio.gpio_write(chip, CSW1, 1)
# lgpio.gpio_write(chip, CSW2, 1)