Modèles de code

Les structure de code étant toujours similaires, nous en avons écrit quelques uns pour vous permettre de commencer à coder dans le bon format

Les modèles courts

Simple, basique

from pyniryo2 import *

# Connect to robot & calibrate
robot = NiryoRobot(<robot_ip_address>)
robot.arm.calibrate_auto()

# --- --------- --- #
# --- YOUR CODE --- #
# --- --------- --- #
../../_images/code_template_basic.gif

Modèle Avancé

Ce modèle permet à l’utilisateur de définir son propre processus mais gère la connexion, la calibration, l’équipement d’outil, et met le robot en veille à la fin

from pyniryo2 import *

local_mode = False # Or True
tool_used = ToolID.GRIPPER_1
# Set robot address
robot_ip_address_rpi = "x.x.x.x"
robot_ip_address_local = "127.0.0.1"

robot_ip_address = robot_ip_address_local if local_mode else robot_ip_address_rpi


def process(niryo_edu):
    # --- --------- --- #
    # --- YOUR CODE --- #
    # --- --------- --- #

if __name__ == '__main__':
    # Connect to robot
    robot = NiryoRobot(robot_ip_address)
    # Calibrate robot if robot needs calibration
    robot.arm.calibrate_auto()
    # Equip tool
    robot.tool.update_tool()
    # Launching main process
    process(client)
    # Ending
    robot.arm.go_to_sleep()

Modèle avancé pour convoyeur

Comme le Modèle Avancé mais avec un convoyeur

from pyniryo2 import *

# Set robot address
robot_ip_address = "x.x.x.x"


def process(robot, conveyor_id):
    robot.conveyor.run_conveyor(conveyor_id)

    # --- --------- --- #
    # --- YOUR CODE --- #
    # --- --------- --- #

    robot.conveyor.stop_conveyor()


if __name__ == '__main__':
    # Connect to robot
    robot = NiryoRobot(robot_ip_address)
    # Calibrate robot if robot needs calibration
    robot.arm.calibrate_auto()
    # Equip tool
    robot.tool.update_tool()
    # Activating connexion with conveyor
    conveyor_id = robot.conveyor.set_conveyor()
    # Launching main process
    process(robot, conveyor_id)
    # Ending
    robot.arm.go_to_sleep()
    # Deactivating connexion with conveyor
    robot.conveyor.unset_conveyor(conveyor_id)

Modèle avancé pour la vision

Modèle conséquent pour les utilisateurs de vision !

from pyniryo2 import *

local_mode = False # Or True
workspace_name = "workspace_1"  # Robot's Workspace Name
# Set robot address
robot_ip_address_rpi = "x.x.x.x"
robot_ip_address_local = "127.0.0.1"

robot_ip_address = robot_ip_address_local if local_mode else robot_ip_address_rpi

# The pose from where the image processing happens
observation_pose = PoseObject(
    x=0.18, y=0.0, z=0.35,
    roll=0.0, pitch=1.57, yaw=-0.2,
)

# Center of the conditioning area
place_pose = PoseObject(
    x=0.0, y=-0.23, z=0.12,
    roll=0.0, pitch=1.57, yaw=-1.57
)

def process(robot):
    robot.arm.move_pose(observation_pose)
    catch_count = 0
    while catch_count < 3:
        ret = robot.vision.get_target_pose_from_cam(workspace_name,
                                                    height_offset=0.0,
                                                    shape=ObjectShape.ANY,
                                                    color=ObjectColor.ANY)
        obj_found, obj_pose, shape, color = ret
        if not obj_found:
            continue
        catch_count += 1
        # --- --------- --- #
        # --- YOUR CODE --- #
        # --- --------- --- #
        robot.pick_place.place_from_pose(place_pose)

if __name__ == '__main__':
    # Connect to robot
    robot = NiryoRobot(robot_ip_address)
    # Calibrate robot if robot needs calibration
    robot.arm.calibrate_auto()
    # Equip tool
    robot.tool.update_tool()
    # Launching main process
    process(client)
    # Ending
    robot.arm.go_to_sleep()

Modèle de callback

Modèle d’intégration d’évènements!

# Imports
from pyniryo2 import *
from threading import Event

robot_ip = "xxx.xxx.xxx.xxx"
robot_ip_address_local = "127.0.0.1"

# Events
update_tool_event = Event()
update_tool_event.clear()

calibrated_event = Event()
calibrated_event.clear()

# Poses
pose_1 = PoseObject()

pose_2 = PoseObject()

# Callbacks
def update_tool_success_callback(result):
    update_tool_event.set()
    print 'Update Tool: ', result['message']

def update_tool_error_callback(result):
    print 'Update Tool: ', result['message']

def calibrate_success_callback(result):
    calibrated_event.set()
    print 'Calibrate Callback: ', result["message"]

def calibrate_error_callback(result):
    print 'Calibrate Callback: ', result["message"]

    """
    Add Callbacks Here
    """

def action_function(robot):

    """
    Don't put niryo_robot in parameter, it will take the pyniryo2 package
    add your function here
    """


if __name__ == "__main__":

    # Connect to robot
    robot = NiryoRobot(robot_ip)

    # Calibrate robot if robot needs calibration
    robot.arm.calibrate_auto(callback=calibrate_success_callback, errback=calibrate_error_callback)
    calibrated_event.wait(20)
    if not calibrated_event.is_set():
        quit

    robot.tool.update_tool(callback=update_tool_success_callback, errback=update_tool_error_callback)
    update_tool_event.wait()

    action_function(robot)

    robot.arm.go_to_sleep()