pymycobot Python API

repository·main·Indexed 42 days ago

https://github.com/elephantrobotics/pymycobot

Official Python API for serial communication and control of ElephantRobotics products, including the MyCobot series (280, 320, Pro450), MechArm 270, MyPalletizer 260, myAGV, myArm_M&C, ultraArm P340, and MyBuddy. Supports Python 2 and Python 3.5+.

Tokens
48.6K
Snippets
104
Records
277
Agent score
52%

What's inside pymycobot

  1. Overview of pymycobot

    main

    pymycobot is the official Python API for ElephantRobotics products. It provides programmatic control for various robotic hardware including the MyCobot series (280, 320, Pro450), MechArm 270, MyPalletizer 260, myAGV, myArm_M&C, ultraArm P340, and MyBuddy.

    Supported Python environments:

    • Python 2
    • Python 3.5 or later
  2. How to handle API return values

    main

    When calling API functions that return data (such as retrieving the current speed), simply calling the function in a script will not display the result. You must wrap the function call in a print() statement to see the returned value in your console.

    If the documentation specifies a function as having no return value (无返回值), you do not need to use print(). If it has a return value, use print(function_name()) to view it.

    # Incorrect: will not show the speed in the console
    get_speed()
    
    # Correct: will print the current speed value
    print(get_speed())
  3. Serial port connection sequence for myArm M&C

    main

    When controlling the myArm M&C system, you must follow a specific sequence to open the serial ports:

    1. First, open the serial port connection for myArmM.
    2. Second, open the serial port connection for myArmC.

    Once both serial ports are successfully opened, you can control the movement of myArmM by manipulating myArmC.

  4. Retrieve return values from MyBuddy API functions

    main

    Functions in the MyBuddy API that return values (such as speed or status) do not print to the console automatically. You must wrap the function call in a print() statement to see the returned data.

    # Incorrect: will not show the value in console
    get_speed()
    
    # Correct: will print the speed value
    print(get_speed())
  5. How to initialize serial ports for myArm M&C control

    main

    When controlling the myArm M&C system, there is a strict sequence requirement for opening serial port connections to ensure proper operation:

    1. First, open the serial port connection for myArmM.
    2. Second, open the serial port connection for myArmC.

    Once both serial ports are successfully opened, you can control the movement of the myArmM unit by manipulating the myArmC unit.

  6. Switch between Synchronous and Asynchronous control modes in Pro630

    main

    The Pro630 supports two control modes via the set_pos_switch method. You should call this method before sending motion commands.

    • Synchronous Mode (set_pos_switch(1)): The default mode. The control function blocks and only returns after the robot has finished moving to the target position.
    • Asynchronous Mode (set_pos_switch(0)): The function returns immediately after the command is sent, without waiting for the robot to reach the target position.
    from pymycobot import Pro630
    
    p = Pro630("/dev/ttyAMA0")
    
    # Set to asynchronous mode
    p.set_pos_switch(0)
    
    # Send motion command (returns immediately in async mode)
    p.send_angle(5, 100, 20)
  7. Configure serial port connection order for myArm M&C

    main

    When using the myArm M&C control demo, the serial ports must be opened in a specific sequence to function correctly:

    1. Open the serial connection for myArmM first.
    2. Open the serial connection for myArmC second.

    Once both serial ports are successfully opened, moving the myArmC unit will control the movement of the myArmM unit.

  8. Quickstart with MyArmM

    main

    To use the MyArm API, import the MyArmM class from pymycobot and initialize it with the appropriate serial port (e.g., /dev/ttyAMA1).

    Basic operations include retrieving joint angles and moving a specific joint to a target angle at a set speed.

    from pymycobot import MyArmM
    
    # Initialize with the serial port
    mam = MyArmM('/dev/ttyAMA1')
    
    # Gets the current angle of all joints
    angles = mam.get_joints_angle()
    print(angles)
    
    # Set 1 joint to move to 40 and speed to 20
    mam.set_joint_angle(1, 40, 20)
  9. Initialize MechArm 270 and perform basic movement

    main

    To use the MechArm 270, import MechArm270 from pymycobot. You must specify the serial port (e.g., 'COM3') during initialization.

    Basic operations include retrieving joint angles with get_angles() and moving a specific joint using send_angle(id, degree, speed).

    from pymycobot import MechArm270
    
    # Initialize with the correct COM port
    mc = MechArm270('COM3')
    
    # Gets the current angle of all joints
    angles = mc.get_angles()
    print(angles)
    
    # Set 1 joint to move to 40 degrees at speed 20
    mc.send_angle(1, 40, 20)
  10. Set up the myAGVPro Composite Kit handle control demo

    main

    To run the myAGVPro Composite Kit handle control demonstration, follow these steps:

    1. Clone the repository:
      git clone https://github.com/elephantrobotics/pymycobot.git
    2. Navigate to the demo directory:
      cd pymycobot/demo/myAGVPro_Composite_Kit
    3. Install dependencies:
      pip install -r requirement.txt
    4. Configure hardware settings (see Configuration Details) before running.
    5. Run the program:
      python main.py

    Important Notes:

    • Ensure the handle is connected to the myAGVPro and powered on before starting the program.
    • The MyArmM750 robotic arm does not support the suction pump; the program will issue a warning if used.
    git clone https://github.com/elephantrobotics/pymycobot.git
    cd pymycobot/demo/myAGVPro_Composite_Kit
    pip install -r requirement.txt
    python main.py