Read IO Status and Control Peripherals

The AGVPlus chassis mainboard provides a set of IO interfaces for peripherals. You can use these interfaces to read input signals or output high/low level signals to control external relays and the chassis fan.

1 API Description

Controlling IO and peripherals mainly involves the following three APIs:

  • get_pin_input(pin): Read the input pin status on the mainboard. The pin range is 1-6. The return value is 0 (low level, hardware default), 1 (high level), and it returns -1 if the reading fails or the pin is invalid.
  • set_pin_output(pin, state=0): Control the level of the output pin on the mainboard. The pin range is 1-6. Pass 0 for state to set it to low level (default), and 1 for high level.
  • set_fan_state(state=1): Set the switch of the chassis cooling fan. Pass 0 for state to turn it off, and 1 to turn it on (hardware default is on).

2 Simple Demo

Create a new Python file named io_test.py and enter the following code:

from pymycobot.myagvplus import MyAGVPlus
import time

# Initialize the MyAGVPlus object
agv = MyAGVPlus('/dev/myagvplus_controller', baudrate=921600, esp32_port='/dev/ttyACM0', esp32_baud=115200, debug=True)

# IO and peripherals are directly controlled by the underlying mainboard, testing requires the chassis power to be on (Note: this will also enable the motors)
agv.power_on()

print("--- Start fan and peripheral output test ---")
print("Turn off the fan for 3 seconds...")
agv.set_fan_state(0)
time.sleep(3)

print("Turn the fan back on...")
agv.set_fan_state(1)
time.sleep(1)

print("Set output pin 1 to high level (1)...")
agv.set_pin_output(1, 1)
time.sleep(2)
print("Restore output pin 1 to low level (0)...")
agv.set_pin_output(1, 0)
time.sleep(1)

print("\n--- Start input pin test ---")
# Test reading the input status of pin 2
pin2_state = agv.get_pin_input(2)
print(f"Current input status of pin 2: {pin2_state}")

# After running, safely turn off the robot power and disable motors
agv.power_off()

3 Run the Example File

Enter the following command in the terminal to run the code:

python3 io_test.py

After execution, you will clearly hear the sound change of the chassis fan turning off and back on, and the terminal will print the real-time read status of the corresponding pin.

← Previous Page | Next class →

results matching ""

    No results matching ""