forked from ShunFang-cart/ESP
Fix: update relay logic and improve comments for clarity
This commit is contained in:
59
main.py
59
main.py
@ -32,8 +32,10 @@ DIN_PINS = (25, 26, 27, 32)
|
|||||||
DOUT_PINS = (18, 19)
|
DOUT_PINS = (18, 19)
|
||||||
|
|
||||||
digital_inputs = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in DIN_PINS]
|
digital_inputs = [Pin(pin, Pin.IN, Pin.PULL_UP) for pin in DIN_PINS]
|
||||||
digital_outputs = [Pin(pin, Pin.OUT, value=0) for pin in DOUT_PINS]
|
# Low-level trigger relay:
|
||||||
|
# GPIO HIGH = relay OFF
|
||||||
|
# GPIO LOW = relay ON
|
||||||
|
digital_outputs = [Pin(pin, Pin.OUT, value=1) for pin in DOUT_PINS]
|
||||||
|
|
||||||
# ---------------- SERIAL CONFIG ----------------
|
# ---------------- SERIAL CONFIG ----------------
|
||||||
|
|
||||||
@ -102,15 +104,18 @@ poller.register(sys.stdin, select.POLLIN)
|
|||||||
# ============================================================
|
# ============================================================
|
||||||
# DOUT / gripper
|
# DOUT / gripper
|
||||||
# ============================================================
|
# ============================================================
|
||||||
|
|
||||||
def clear_douts():
|
def clear_douts():
|
||||||
global dout_shadow
|
global dout_shadow
|
||||||
|
|
||||||
|
# Logical DOUT state reported to PC.
|
||||||
|
# 0 means no gripper command active.
|
||||||
dout_shadow = 0
|
dout_shadow = 0
|
||||||
|
|
||||||
|
# Low-level trigger relay:
|
||||||
|
# HIGH = relay OFF
|
||||||
|
# LOW = relay ON
|
||||||
for pin in digital_outputs:
|
for pin in digital_outputs:
|
||||||
pin.value(0)
|
pin.value(1)
|
||||||
|
|
||||||
|
|
||||||
def set_gripper(mode):
|
def set_gripper(mode):
|
||||||
global dout_shadow
|
global dout_shadow
|
||||||
@ -120,39 +125,47 @@ def set_gripper(mode):
|
|||||||
# 1 = OPEN
|
# 1 = OPEN
|
||||||
# 2 = CLOSE
|
# 2 = CLOSE
|
||||||
#
|
#
|
||||||
# DOUT[0] = GPIO18
|
# DOUT[0] = GPIO18 -> relay IN1
|
||||||
# DOUT[1] = GPIO19
|
# DOUT[1] = GPIO19 -> relay IN2
|
||||||
|
#
|
||||||
|
# Low-level trigger relay:
|
||||||
|
# GPIO HIGH = relay OFF
|
||||||
|
# GPIO LOW = relay ON
|
||||||
|
#
|
||||||
|
# Tested hardware behavior:
|
||||||
|
# IN1 = LOW, IN2 = HIGH -> CLOSE
|
||||||
|
# IN1 = HIGH, IN2 = LOW -> OPEN
|
||||||
|
|
||||||
if mode == 1:
|
if mode == 1:
|
||||||
# OPEN -> DOUT=[0, 1]
|
# OPEN -> logical DOUT=[0, 1]
|
||||||
dout_shadow = 0b10
|
dout_shadow = 0b10
|
||||||
|
|
||||||
elif mode == 2:
|
elif mode == 2:
|
||||||
# CLOSE -> DOUT=[1, 0]
|
# CLOSE -> logical DOUT=[1, 0]
|
||||||
dout_shadow = 0b01
|
dout_shadow = 0b01
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# STOP -> DOUT=[0, 0]
|
# STOP -> logical DOUT=[0, 0]
|
||||||
dout_shadow = 0b00
|
dout_shadow = 0b00
|
||||||
|
|
||||||
# Shadow test mode: do not touch real GPIO pins.
|
# Shadow test mode: do not touch real GPIO pins.
|
||||||
if not USE_REAL_DOUT:
|
if not USE_REAL_DOUT:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Real output mode.
|
# First turn both relays OFF.
|
||||||
digital_outputs[0].value(0)
|
digital_outputs[0].value(1) # GPIO18 / IN1 OFF
|
||||||
digital_outputs[1].value(0)
|
digital_outputs[1].value(1) # GPIO19 / IN2 OFF
|
||||||
time.sleep_ms(20)
|
time.sleep_ms(20)
|
||||||
|
|
||||||
if mode == 1:
|
if mode == 1:
|
||||||
# OPEN
|
# OPEN -> trigger IN2 / GPIO19
|
||||||
digital_outputs[1].value(1)
|
digital_outputs[1].value(0)
|
||||||
|
|
||||||
elif mode == 2:
|
elif mode == 2:
|
||||||
# CLOSE
|
# CLOSE -> trigger IN1 / GPIO18
|
||||||
digital_outputs[0].value(1)
|
digital_outputs[0].value(0)
|
||||||
|
|
||||||
# mode 0 keeps both LOW
|
# mode 0 keeps both HIGH/OFF
|
||||||
|
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@ -256,11 +269,11 @@ def make_packet():
|
|||||||
for index, pin in enumerate(digital_inputs):
|
for index, pin in enumerate(digital_inputs):
|
||||||
din_mask |= pin.value() << index
|
din_mask |= pin.value() << index
|
||||||
|
|
||||||
if USE_REAL_DOUT:
|
# if USE_REAL_DOUT:
|
||||||
dout_mask = 0
|
# dout_mask = 0
|
||||||
for index, pin in enumerate(digital_outputs):
|
# for index, pin in enumerate(digital_outputs):
|
||||||
dout_mask |= pin.value() << index
|
# dout_mask |= pin.value() << index
|
||||||
else:
|
# else:
|
||||||
dout_mask = dout_shadow
|
dout_mask = dout_shadow
|
||||||
|
|
||||||
# Packet layout:
|
# Packet layout:
|
||||||
|
|||||||
@ -119,3 +119,4 @@ def main():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
# python3 /home/shun/esp/pc_reader.py /dev/ttyUSB0 --baud 115200
|
||||||
Reference in New Issue
Block a user