Python Reference Examples
Lua API
Control task execution by sending Lua API commands to TCP port 5180
import socket
client = socket.socket()
client.connect(('127.0.0.1', 5180))
client.send("scene(10087)".encode())
client.close()
SDK
Control task execution by downloading and installing the SDK
lebai_sdk
import lebai_sdk
# # If a RuntimeError related to the event loop occurs, try adding the following 2 lines
# import nest_asyncio
# nest_asyncio.apply()
lebai_sdk.init()
def main():
# print(lebai_sdk.discover_devices(2)) #Discover robots on the same LAN
robot_ip = "192.168.x.x" #Set the robot IP address. It needs to be modified according to the actual robot IP address
lebai = lebai_sdk.connect(robot_ip, False) #Create an instance
lebai.stop_sys() #Stop the arm
print(lebai.get_robot_state()) #Get the arm state. IDLE idle, MOVING running, ESTOP emergency stop. See the official website for details
lebai.start_sys() #Start the arm
print(lebai.get_robot_state())
status_dic = lebai.get_kin_data() #Get the current motion data of the arm, returns a dictionary. See the official website for details. Among them, actual_joint_pose and actual_tcp_pose are the positions expressed in joint angle coordinates and Cartesian coordinates respectively
print('feedback joint position',status_dic['actual_joint_pose']) # Format as follows [-6.282171983178839, -1.4205823273455875, 1.553330269510449, -0.1335442689785058, -4.712171983247641, 0.0]
print('feedback Cartesian position',status_dic['actual_tcp_pose']) # Format as follows {'x' : -0.383, 'y' : -0.121, 'z' : 0.36, 'rz' : -1.57, 'ry' : 0, 'rx' : 1.57}
joint_pose = [0,-1.05,1.05,0,1.57,0] #Target pose joint data
cartesian_pose = {'x' : -0.383, 'y' : -0.121, 'z' : 0.36, 'rz' : -1.57, 'ry' : 0, 'rx' : 1.57}#Target pose Cartesian data
a = 0.5 #Joint acceleration (rad/s2)
v = 0.2 #Joint velocity (rad/s)
t = 0 #Motion time (s). When t > 0, the velocity v and acceleration a parameters are invalid
r = 0 #Blending radius (m). Used to specify the smoothing effect of the path
lebai.movej(joint_pose,a,v,t,r) #Joint motion https://help.lebai.ltd/sdk/motion.html#%E5%85%B3%E8%8A%82%E8%BF%90%E5%8A%A8
a = 0.3 #Spatial acceleration (m/s2)
v = 0.1 #Spatial velocity (m/s)
t = 0 #Motion time (s). When t > 0, the velocity v and acceleration a parameters are invalid
r = 0 #Blending radius (m). Used to specify the smoothing effect of the path
lebai.movel(cartesian_pose,a,v,t,r) #Linear motion https://help.lebai.ltd/sdk/motion.html#%E7%9B%B4%E7%BA%BF%E8%BF%90%E5%8A%A8
lebai.wait_move() #Wait for the motion to complete
cart = lebai.kinematics_forward(joint_pose) #Forward kinematics, convert joint positions to Cartesian positions
joints = lebai.kinematics_inverse(cartesian_pose, joint_pose) # Inverse kinematics, convert Cartesian position and orientation to joint positions. The calculation result is related to the current TCP setting and the current joint position. joints is the joint space reference position, defaults to the current feedback joint position
#When you want to calculate the coordinates of a new position relative to the flange end coordinate system in the base coordinate system. For example, the position 0.1m forward along the z-axis relative to the end flange, solved in the base coordinate system, can use pose_trans
current_tcp_pose = lebai.get_kin_data()['actual_tcp_pose'] #Current flange end coordinate
print('current_tcp_pose',current_tcp_pose)
offset_position = {'x' : 0, 'y' :0, 'z' : 0.1, 'rz' :0, 'ry' : 0, 'rx' : 0} #Position offset relative to the flange end
calculate_location = lebai.pose_trans(current_tcp_pose, offset_position) #Calculated new position
print('calculate_location',calculate_location)
#If you want the current position and orientation to undergo a pose transformation in a certain coordinate system. For example, relative to the base coordinate system, increase by 0.1 along the base z-axis
base = lebai.get_kin_data()['actual_tcp_pose'] #Current end coordinate
delta = {'x' : 0, 'y' :0, 'z' : 0.1, 'rz' :0, 'ry' : 0, 'rx' : 0} #Move 0.1m along the z-axis of the frame coordinate system
frame ={'x' : 0, 'y' :0, 'z' : 0, 'rz' :0, 'ry' : 0, 'rx' : 0} #Base coordinate system. Pose offset direction, only the orientation part is effective
pose = lebai.pose_add(base, delta, frame)
print('pose',pose)
# Relative to a new coordinate system rotated 45° about the y-axis so that the z-axis points diagonally upward, increase by 0.1 along the base z-axis
base = lebai.get_kin_data()['actual_tcp_pose'] #Current end coordinate
delta = {'x' : 0, 'y' :0, 'z' : 0.1, 'rz' :0, 'ry' : 0, 'rx' : 0} #Move 0.1m along the z-axis of the frame coordinate system
frame ={'x' : 0, 'y' :0, 'z' : 0, 'rz' :0, 'ry' :-0.78, 'rx' : 0} #New coordinate system rotated 45° about the y-axis, with the z-axis pointing diagonally upward
pose = lebai.pose_add(base, delta, frame)
print('pose',pose)
#Robot Lua scene call. The purpose of the Lua scene is that you can write some continuous motion control logic in the Lua scene, and then call the Lua scene through the SDK to execute these motion control logic. Moreover, it is more convenient to write motion logic in Lua in the front-end interface
scene_number = "10000" #Scene number to be called
task_id = lebai.start_task(scene_number, [], "", False, 1) #Call the scene and start the task
state = lebai.get_task_state(task_id) #Get the task status
print('state',state)
lebai.cancel_task(task_id) #Stop the specified task
#For interaction between the external system of the industrial computer and the robot scene process, semaphores can be used for interaction. Define the meaning of each signal according to your own needs
value = lebai.get_signal(0) #Get the value corresponding to semaphore 0
lebai.set_signal(0, 1) #Set the value corresponding to semaphore 0 to 1
#Gripper control of the robot
lebai.set_claw(100, 30) #Set the gripper force (0-100) and opening (0-100). 100 represents maximum force, 30 represents 30% of the maximum opening
claw_status = lebai.get_claw() #Get the gripper status, returns a dictionary, {'force': 100.0, 'amplitude': 31.0, 'weight': 0.0, 'hold_on': False}. The weight parameter has no effect. hold_on indicates whether the gripper is stable
print('claw_status',claw_status)
#Robot digital input/output setting and getting. This is mainly to get the status of the sensors connected to the robot, and to set the do output. There are 4 di and 4 do on the control box, and 2 di and 2 do on the flange
robot_di0 = lebai.get_di("ROBOT", 0) # 0 corresponds to getting the first di signal
print('robot_di0',robot_di0)
flan_di0 = lebai.get_di("FLANGE", 0) # 0 corresponds to getting the first di signal
print('flan_di0',flan_di0)
lebai.set_do("ROBOT", 0, 1) #Set the digital output. Set the first do signal to 1, corresponding to 24V voltage output on that do
lebai.set_do("FLANGE", 0, 1)
robot_do0 = lebai.get_do("ROBOT", 0)
flan_do0 = lebai.get_do("FLANGE", 0)
#If the 485/232 interface of the control box is connected to other devices, they can be controlled through the serial port of the control box
device= "/dev/ttyS1" # 485 serial port address
# device = "/dev/ttyS3" #232 serial port address
lebai.set_serial_baud_rate(device, 9600) #Set the serial baud rate
lebai.set_serial_timeout(device, 1000) #Set the timeout to 1000ms
str_data = "123"
str_byte = str_data.encode(encoding='utf-8')
lebai.write_serial(device, str_byte) # Send string data
# lebai.write_serial(device, [0x01, 0x02, 0x03]) #Send HEX data
try:
data = lebai.read_serial(device,3) #If the length of the read data is insufficient, it will report a timeout. Pay attention to the handling when reading data
print(data) #Print the data, displayed as the integer type of each byte. For example, receiving 0xAA 0x56 displays as [170,86]
hex_data = []
for byte in data:
hex_data.append(hex(byte))
print(hex(byte))
print(hex_data) #Print in hexadecimal form
except Exception as e:
print('read_serial error :%s'%e)
main()
lebai_sdk_asyncio
import asyncio
import lebai_sdk
lebai_sdk.init()
async def main():
# print(await lebai_sdk.discover_devices(2)) #Discover robots on the same LAN
robot_ip = "192.168.x.x" #Set the robot IP address. It needs to be modified according to the actual robot IP address
lebai = await lebai_sdk.connect(robot_ip, False) #Create an instance
await lebai.start_sys() #Start the arm
joint_pose = [0,-1.05,1.05,0,1.57,0] #Target pose joint data
cartesian_pose = {'x' : -0.383, 'y' : -0.121, 'z' : 0.36, 'rz' : -1.57, 'ry' : 0, 'rx' : 1.57}#Target pose Cartesian data
a = 0.5 #Joint acceleration (rad/s2)
v = 0.2 #Joint velocity (rad/s)
t = 0 #Motion time (s). When t > 0, the velocity v and acceleration a parameters are invalid
r = 0 #Blending radius (m). Used to specify the smoothing effect of the path
await lebai.movej(joint_pose,a,v,t,r) #Joint motion https://help.lebai.ltd/sdk/motion.html#%E5%85%B3%E8%8A%82%E8%BF%90%E5%8A%A8
a = 0.3 #Spatial acceleration (m/s2)
v = 0.1 #Spatial velocity (m/s)
t = 0 #Motion time (s). When t > 0, the velocity v and acceleration a parameters are invalid
r = 0 #Blending radius (m). Used to specify the smoothing effect of the path
await lebai.movel(cartesian_pose,a,v,t,r) #Linear motion https://help.lebai.ltd/sdk/motion.html#%E7%9B%B4%E7%BA%BF%E8%BF%90%E5%8A%A8
await lebai.wait_move() #Wait for the motion to complete
# scene_number = "10000" #Scene number to be called
# await lebai.start_task(scene_number, [], "", False, 1) #Call the scene
await lebai.stop_sys() #Stop the arm
asyncio.run(main())
