frankaemika / libfranka

C++ library for Franka research robots
https://frankaemika.github.io
Apache License 2.0
235 stars 156 forks source link

libfranka:Move command abord motion aborted by reflex ["communication_constraints_violation"] #163

Open git183nuozhe opened 6 days ago

git183nuozhe commented 6 days ago

hi, nice see you. when i use libfranka 0.9.1 "communication_test"only, it is ok. but when run the py script as follows at the same time, the error: terminate called after throwing an instance of 'franka::ControlException' what(): libfranka:Move command aborted:motion aborted by reflex!["communication_constraints_violation"] is comming up. it is not cpu hz problem. what should i do?

the hardware situationa: master computer 192.168.1.10, a franka arm192.168.1.6, a realsense slaver computer192.168.1.13,which all in switch network.

py script the py script is running at realsense slaver computer.the "communication_test"is running at master computer . the py script is used to read letters from a txt file and determine whether to save the realsense image data in the ROS topic as a video based on the letters

!/usr/bin/env python3

import rospy import cv2, re, glob from sensor_msgs.msg import Image

from sensor_msgs.msg import JointState

from cv_bridge import CvBridge

import os

from pathlib import Path

import time

bridge = CvBridge() frame_count = 0 video_writer = None video_file = None is_recording = False

save_dir = None video_savefile = None

flag_num_q = 1

def increment_path(path, exist_ok=False, sep='', mkdir=False):

Increment file or directory path, i.e. save_dir/RGB_video/video, video2, video3, ... etc.

path = Path(path)  # os-agnostic
n2 = ''
if path.exists() and not exist_ok:
    suffix = path.suffix
    path = path.with_suffix('')
    dirs = glob.glob(f"{path}{sep}*")  # similar paths
    matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs]
    i = [int(m.groups()[0]) for m in matches if m]  # indices
    n2 = max(i) + 1 if i else 2  # increment number
    path = Path(f"{path}{sep}{n2}{suffix}")  # update path
dir = path if path.suffix == '' else path.parent  # directory
if not dir.exists() and mkdir:
    dir.mkdir(parents=True, exist_ok=True)  # make directory
if len(str(n2)) == 0:
    n2 = 1
return path, str(n2)

def image_callback(msg): global frame_count, video_writer, video_file, is_recording, video_savefile, flag_num_q

try:
    cv_image = bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")
except Exception as e:
    rospy.logerr(f"Image conversion failed: {e}")
    return

ros_time = msg.header.stamp
time_text = f"ROS Time: {ros_time.secs}.{ros_time.nsecs:09d}"

font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
font_color = (255, 255, 255)  
thickness = 2
position = (10, 30)  
cv_image = cv2.putText(cv_image, time_text, position, font, font_scale, font_color, thickness, cv2.LINE_AA)

# cv2.imshow('Frame', cv_image)
#key = cv2.waitKey(1) & 0xFF  
key_file = Path("key.txt")
if key_file.exists():
    with open(key_file,'r') as f:
        key = f.read().strip()
else:
    #print(key)
    rospy.logerr("Error:No key data")
print(key)
#if key != 's':
    #if key != 'q':
        #if key != 'p':
            #if key != 'a':
                #print(key)
                #rospy.loginfo("Alert:key value is not right")
if key == 's':
    #rospy.loginfo(f"Now time111: {time.time()}\n")
    if not is_recording:
        is_recording = True
        rospy.loginfo('Recording started...')

        video_dirname, order_name = 'save_dir/RGB_video', '' 
        exist_ok = False
        save_dir, order_num = increment_path(Path(video_dirname) / order_name, exist_ok=exist_ok)
        save_dir = str(save_dir)
        print(order_num)

        order_num = int(order_num) if order_num.isnumeric() else ''
        Path(save_dir).mkdir(parents=True, exist_ok=True)

        for char in save_dir:
            if char in str(order_num):
                save_dir=save_dir.replace(char,'')
        #print(save_dir)

        video_savefile = Path(save_dir) / f'video_{order_num}.mp4c'
        #joint_rosdata_file = Path(save_dir) / f'joint_rosdata_{order_num}.txt'

        height, width, _ = cv_image.shape
        print(height)
        print(width)
        rospy.loginfo(f"Image dimensions: {width}x{height}")
        fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
        #fourcc = cv2.VideoWriter_fourcc(*'mp4v')  
        video_writer = cv2.VideoWriter(str(video_savefile), fourcc, 30, (width, height))
    else:
        rospy.loginfo('Already Recording...')
if key == 'q':
    if flag_num_q == 0:
        flag_num_q = 1
        print('...video data logging done...')
        print(f',if save video,the data in:{str(video_savefile)}')
        frame_count = 0

        is_recording = False

if key == 'p':
    if is_recording and video_writer:
        video_writer.release()
    rospy.loginfo('Recording stopped and exiting...')
    cv2.destroyAllWindows()
    rospy.signal_shutdown('Video saved and exiting...')

if is_recording:
    #rospy.loginfo(f"Now time222: {time.time()}\n")
    if video_savefile is not None:

        video_writer.write(cv_image)
        # xieru joint data
        #record_ros_data(str(joint_rosdata_file))

        frame_count += 1
        rospy.loginfo(f"Frame {frame_count} with timestamp saved to {video_savefile}")
        flag_num_q = 0
    else:
        rospy.loginfo("Recording was not started. video_savefile is not None.")

def main(): rospy.init_node('realsense_image_saver_with_timestamp', anonymous=True) rospy.Subscriber("/camera/color/image_raw", Image, image_callback, queue_size=1)

try:
    rospy.spin()
except KeyboardInterrupt:
    rospy.loginfo("Shutting down...")
finally:
    if video_writer:
        video_writer.release()
        rospy.loginfo("Video writer released.")
    cv2.destroyAllWindows()

if name == 'main': main()

AndreasKuhner commented 5 days ago

Hi @git183nuozhe , it sounds a bit like that your image processing is blocking the communication with the robot. One iteration for the FCI cycle can only take around 500 us (aka receiving data via libfranka, processing it and sending a new command). If something else blocks or delays that, you will see the communcation violation error.

Maybe you can make sure that the image processing is not blocking/delaying or using the same resource than your communication with the robot via libfranka :smiley:

Cheers, Andreas