xArm-Developer / xarm_ros

ROS packages for robotic products from UFACTORY
https://www.ufactory.cc/
BSD 3-Clause "New" or "Revised" License
206 stars 153 forks source link

Getting Maxium speed out of Servo_Cartesian (streamed Cartesian trajectory) #213

Closed Akumar201 closed 6 months ago

Akumar201 commented 6 months ago

Hi, I am trying to see the maximum speed out from the example code to see how fast the robotic arm can move using Servo_Cartesian (streamed Cartesian trajectory) I made some changes in the code and launch file

#!/usr/bin/env python

import sys
import time
import rospy
from xarm_msgs.srv import *

def servo_cartesian_motion(start_pose, freq, time_secs):
    servo_cart = rospy.ServiceProxy('/xarm/move_servo_cart', Move)
    req = MoveRequest() 
    req.pose = start_pose
    req.mvvelo = 2.0
    req.mvacc = 2.0
    req.mvtime = 0 
    loop_num = time_secs * float(freq)
    sleep_sec = 0.1 / float(freq)
    ret = 0
    step = 0.4
    start_time = time.time()  # Get the start time
    # print(loop_num)

    try:
        for i in range(int(loop_num)):
            # req.pose[2] += step
            # req.pose[1] += step
            req.pose[0] += step

            res = servo_cart(req)
            if res.ret:
                print("Something wrong happened calling servo_cart service, index is %d, ret = %d" % (i, res.ret))
                ret = -1
                break
            # time.sleep(sleep_sec)
        end_time = time.time()  # Get the end time after loop completes
        total_time = end_time - start_time  # Calculate total time taken
        frequency = int(loop_num / total_time)  # Calculate frequency
        print("Loop ran at approximately %d Hz" % frequency)
        # return ret

    except rospy.ServiceException as e:
        print("servo_cartesian Service call failed: %s" % e)
    #     return -1

    try:
        for i in range(int(loop_num)):
                # req.pose[2] -= step
                # req.pose[1] -= step
                req.pose[0] -= step

                res = servo_cart(req)
                if res.ret:
                    print("Something wrong happened calling servo_cart service, index is %d, ret = %d" % (i, res.ret))
                    ret = -1
                    break
                # time.sleep(sleep_sec)
        return ret

    except rospy.ServiceException as e:
        print("servo_cartesian Service call failed: %s" % e)
        return -1

if __name__ == "__main__":

    rospy.wait_for_service('/xarm/move_servo_cart')
    rospy.set_param('/xarm/wait_for_finish', True) # return after motion service finish

    motion_en = rospy.ServiceProxy('/xarm/motion_ctrl', SetAxis)
    set_mode = rospy.ServiceProxy('/xarm/set_mode', SetInt16)
    set_state = rospy.ServiceProxy('/xarm/set_state', SetInt16)
    get_position = rospy.ServiceProxy('/xarm/get_position_rpy', GetFloat32List)

    try:
        motion_en(8, 1)
        set_mode(0)
        set_state(0)
    except rospy.ServiceException as e:
        print("Before servo_cartesian, service call failed: %s" % e)
        exit(-1)

    # configurations for servo_cartesian
    set_mode(1)
    set_state(0)

    # Get the current position of the arm
    current_position = get_position().datas
    start_pose = [current_position[0], current_position[1], current_position[2], current_position[3], current_position[4], current_position[5]]

    # publish frequency in Hz:
    freq = 100 
    # publish time in seconds:
    time_secs = 2.0

    # time.sleep(2.0)

    while True:

        if servo_cartesian_motion(start_pose, freq, time_secs) == 0:
            print("Execution finished successfully!")

In the service file I changes <arg name="report_type" default="dev" /> will turning on will help ? , I would like to know what other factors can help me increasing the speed so that I can see how fast the arm can go. If there are other better options avaialbe that can move the arm faster , please let me know as that would be very helpful. Is Online Target Update better option than Servo_Cartesian(streamed Cartesian trajectory) or Servo_Joint (streamed joint-space trajectory).

penglongxiang commented 6 months ago

It is not recommended to control the arm in a very high speed in servo_cartesian, since it requires the user to do the trajectory planning as well as velocity control, as well as a demand of a real-time capable of user PC to ensure a low-latency and stable communication. We also had a linear speed monitoring logic which will pop up an error when it detects a high speed in user command to ensure safety.

Indeed, I think maybe OTG is relatively user friendly and demand less on the user side. It has no strict requirement on the communication latency and has internal speed control. You may update your firmware to the latest and try this function.

Akumar201 commented 6 months ago

I am closing this issue as it has been solved. If the problem persists, please comment and the issue will be reopened if appropriate