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