Code templates

As code structures are always the same, we wrote down few templates for you to start your code file with a good form.

The short template

Very simple, straightforward:

 1from pyniryo import NiryoRobot
 2
 3# Connect to robot & calibrate
 4robot = NiryoRobot('<robot_ip_address>')
 5robot.calibrate_auto()
 6
 7...
 8
 9# Releasing connection
10robot.close_connection()

Advanced template

This template let the user define his own process but it handles connection, calibration, tool equipping, and makes the robot go to sleep at the end:

 1from pyniryo import NiryoRobot, ToolID
 2
 3local_mode = False  # Or True
 4tool_used = ToolID.GRIPPER_1
 5# Set robot address
 6robot_ip_address = '<robot_ip_address>'
 7robot_ip_address_local = "127.0.0.1"
 8
 9robot_ip_address = robot_ip_address_local if local_mode else robot_ip_address
10
11
12def process(robot):
13    ...
14
15
16if __name__ == '__main__':
17    # Connect to robot
18    robot = NiryoRobot(robot_ip_address)
19    # Calibrate robot if robot needs calibration
20    robot.calibrate_auto()
21    # Equip tool
22    robot.update_tool()
23    # Launching main process
24    process(robot)
25    # Ending
26    robot.go_to_sleep()
27    # Releasing connection
28    robot.close_connection()

Advanced template for Conveyor Belt

Same as Advanced template but with a Conveyor Belt

 1from pyniryo import NiryoRobot
 2
 3# Set robot address
 4robot_ip_address = '<robot_ip_address>'
 5
 6
 7def process(robot, conveyor_id):
 8    robot.run_conveyor(conveyor_id)
 9
10    ...
11
12    robot.stop_conveyor()
13
14
15if __name__ == '__main__':
16    # Connect to robot
17    robot = NiryoRobot(robot_ip_address)
18    # Calibrate robot if robot needs calibration
19    robot.calibrate_auto()
20    # Equip tool
21    robot.update_tool()
22    # Activating connexion with conveyor
23    conveyor_id = robot.set_conveyor()
24    # Launching main process
25    process(robot, conveyor_id)
26    # Ending
27    robot.go_to_sleep()
28    # Deactivating connexion with conveyor
29    robot.unset_conveyor(conveyor_id)
30    # Releasing connection
31    robot.close_connection()

Advanced template for Vision

Huge template for Vision users!

 1from pyniryo import NiryoRobot, PoseObject, ObjectShape, ObjectColor
 2
 3local_mode = False  # Or True
 4workspace_name = "workspace_1"  # Robot's Workspace Name
 5# Set robot address
 6robot_ip_address = '<robot_ip_address>'
 7robot_ip_address_local = "127.0.0.1"
 8
 9robot_ip_address = robot_ip_address_local if local_mode else robot_ip_address
10
11# The pose from where the image processing happens
12observation_pose = PoseObject(0.18, 0.0, 0.34, 3.14, 0.01, -0.2)
13
14# Center of the conditioning area
15place_pose = PoseObject(0.0, -0.23, 0.13, 3.14, -0.02, -1.56)
16
17
18def process(robot: NiryoRobot):
19    robot.move(observation_pose)
20    catch_count = 0
21    while catch_count < 3:
22        ret = robot.get_target_pose_from_cam(workspace_name,
23                                             height_offset=0.0,
24                                             shape=ObjectShape.ANY,
25                                             color=ObjectColor.ANY)
26        obj_found, obj_pose, shape, color = ret
27        if not obj_found:
28            continue
29        catch_count += 1
30
31        ...
32
33        robot.place(place_pose)
34
35
36if __name__ == '__main__':
37    # Connect to robot
38    robot = NiryoRobot(robot_ip_address)
39    # Calibrate robot if robot needs calibration
40    robot.calibrate_auto()
41    # Equip tool
42    robot.update_tool()
43    # Launching main process
44    process(robot)
45    # Ending
46    robot.go_to_sleep()
47    # Releasing connection
48    robot.close_connection()