Guide on Using Python

Modified on Tue, 17 Aug 2021 at 11:24 AM

This guide will be used to explain how to install the python dependent package to be able to communicate with the COBOTTA for program execution. This guide will also explain the procedure for how to call a python file (.py) into the terminal for the program to be executed and to have the COBOTTA preform the program's called in actions. The guide will lastly explain the lines of code used to get the COBOTTA to "move-it" using the provided packing_pose.py file. It is highly recommended to create a folder dedicated to all the Python files that will be created for using the COBOTTA. If the user wants to interact with the COBOTTA in a different way, go back to the What's Next document. 


Note: The packing_pose.py file is located in the following directory, catkin_ws>src>denso_cobotta_ros>scripts folder.


The following link contains the GitHub repository of python files that can be used with the COBOTTA: COBOTTA OSS MOVEIT Samples  

  

 

  

  

Installing the Python Dependent Package Into the COBOTTA:


This section of the guide will be used to install the Python dependent package using the terminal.


The following link contains the guide and code used to install the python dependent package: Installing the Python Dependent Package 


Note: 
The code for installing the dependent package can be easy to miss when using the link. It is located underneath Quickstart as step 3 (Run the sample script).



Step 1:

Open a new terminal and enter the following:
$ source ~/catkin_ws/devel/setup.bash


Note: 
If the catkin_ws folder is located in another directory, then the code above needs to be ran through that directory. Enter the following into the terminal before running the 'source' command line:


$ cd <directory>




Step 2:

Enter the following into the terminal to install the python dependent package:



$ sudo apt install ros-$(rosversion -d)-
moveit-python





The next section will demonstrate how to Launch a Python File in the Terminal for it to run.


Finished

 

 

 

 

Launching a Python File in the Terminal:


This section of the guide will be used to show how to call a Python file to be executed in the terminal. The python file that will be launched in this section is the provided packing_pose.py file that is provided when installing the dependent package.


Step 1:

Open a new terminal and enter the following to launch the RVIZ software (this document will be based on using the Electric Gripper):
$ source ~/catkin_ws/devel/setup.bash
$ roslaunch denso_cobotta_bringup 
denso_cobotta_bringup.launch


The Electric Gripper can also be launched using the following:

$ source ~/catkin_ws/devel/setup.bash
$ roslaunch denso_cobotta_bringup
denso_cobotta_bringup.launch
gripper_type:=parallel


The Vacuum Gripper can be launched using the following:

$ source ~/catkin_ws/devel/setup.bash
$ roslaunch denso_cobotta_bringup
denso_cobotta_bringup.launch
gripper_type:=vacuum




Step 2:

Open a new terminal to call in the 
packing_pose.py file:


Note: 
Make sure the software has been loaded in before calling in the file.

 

Step 3:

Enter the following in the terminal to call in the 
packing_pose.py file:


$ python ~/catkin_ws/src/denso_cobotta_ros/
scripts/packing_pose.py


Note: 
The calling of the python file based on the directory it is located in.

 

Step 4:

Wait for the file to load in the terminal. This 
packing_pose.py file requires user input based off using integer values. Enter the following into the terminal to get specific positions for the COBOTTA to perform:


(0) To move to the old packing position
(1) To move to the new packing position
(2) To move to the home position
Enter any other integer to exit the program


The next section will be used to aid in Understanding the Python Code using the packing_pose.py file.


Finished

 

 

 

 

Understanding the Python Code:


This section of the guide will explain the coding logic used to interact with the COBOTTA using python.


The following link contains the GitHub for the full packing_pose.py code: packing_pose.py (GitHub) 


The following link contains how to interact with a robot using python: Move Group Python Interface 


Note: 
The explanation of each section will be in the order from top to bottom of the packing_pose.py file.


Note: 
A row of dashed lines (-----) in the code section implies that there is more code that was not inserted in the document. The full code can be obtained from the GitHub link.


Description:

This section of the code imports all the necessary libraries and some libraries only call in specific functions, such as the denso_cobotta_gripper only imports sections that will allow the Electric Gripper to move. The moveit_commander is one of the most important libraries since it contains the commands use to make the COBOTTA carry out actions using move_group (moveit_commander Reference), manipulate the planning scene using PlanningSceneInterface, and get the status of the robot using the RobotCommander.

Code:

----------------------------------------------------------------------------------
import rospy
import actionlib
----------------------------------------------------------------------------------
import moveit_commander
import rosservice
from geometry_msgs.msg import Pose, Point, Quaternion
from denso_cobotta_gripper import GripperMoveAction, GripperMoveGoal
from denso_cobotta_driver.srv import GetMotorState


Description:

The joints_packing and joints_home lines of code are used to set the value of joints 1 to 6 to specific angles. There are only 6 joints on the COBOTTA, therefore there are 6 different angles being specified for each position.


Note: It is recommended to run RVIZ while changing the joint values to teach the new positions.


The gripper_parallel lines are used to set the Electric gripper to either open all the way (0.015m) or close (0.0m), along with how fast it should open and close (1.0 - 100.0 m/s) and with home much effort it should apply (6.0 - 20.0 Newtons).

Code:

----------------------------------------------------------------------------------
joints_packing_old = [90, -60, 125, 90, -95, 0]
joints_packing_new = [90, -30, 120, -170, -94, 0]
joints_home = [0, 30, 100, 0, 50, 0]
gripper_parallel_open = 0.015
gripper_parallel_close = 0.0
gripper_parallel_speed = 10.0
gripper_parallel_effort = 10.0


Description:

This is a function called arm_move with the two parameters being the move_group (which is used to both move and stop the servo motors) and joint_goal (which is used to help identify the "goal" position). The pose_radian line converts the joint_angles of the COBOTTA from degrees to radians.

Code:

def arm_move(move_group, joint_goal):
    pose_radian = [x / 180.0 * math.pi for x in joint_goal]
    move_group.go(pose_radian, wait=True)
    move_group.stop()


Description:

This is a function called gripper_move which contains four parameters: gripper_clientwidthspeed, and effort

  • gripper_client is used to identify the type of gripper (The Electric Gripper).
  • width identifies how much the gripper should open and close.
  • speed identifies how fast to open and close the gripper.
  • effort identifies how much force the gripper should apply (in Newtons).

goal is used to identify and execute all four parameters. The goal is sent to the main program to be executed.

Code:

def gripper_move(gripper_client, width, speed, effort):
    goal = GripperMoveGoal()
    goal.target_position = width
    goal.speed = speed
    goal.effort = effort
    gripper_client.send_goal(goal)


Description:

This is a function used to see if the motor is running or not. The wait_for_service command blocks the service call until it is available. ServiceProxy creates a reference to a ROS service for the supported calls. If the motor is on, it will return the motor state. If the motor is not on, it will use a print statement to print out a "Service call failed" in the terminal. Here is a link that contains all the functions used from the rospy library.

Code:

def is_motor_running():
    rospy.wait_for_service('/cobotta/get_motor_state', 3.0)
    try:
        get_motor_state = rospy.ServiceProxy('/cobotta/get_motor_state', GetMotorState)
        res = get_motor_state()
        return res.state
    except rospy.ServiceException, e:
        print >> sys.stderr, " Service call failed: %s" % e


Description:

This is a function that checks to see if the get_motor_state is a part of the rosservice list, if it is, it will return a False. The service_list contains a list of services that can be called in and used. To see the full-service list in the terminal, enter the following after the service_list variable:

print(service_list)

Code:

def is_simulation():
    service_list = rosservice.get_service_list()
    if '/cobotta/get_motor_state' in service_list:
        return False
    return True


Description:

This section is the main program, where all functions are called and libraries get defined as well as initialized into the program. Specific actions are called from the libraries, such as defining the COBOTTA's 6 joints as "arm" and the gripper_client being used to call the actionlib to get the functions needed to get the gripper to move. The move_group variable is being set to the function inside the moveit_commander that will be used to move the 6 joints.

The os.path.basename(__file__) is used to tell what python file is being used, in this case it will print out packing_pose.py along with the "set pose goal and moves COBOTTA".

The is_simulation() and is_motor_running() functions are being called to see if both the motor is running and the simulation is running, if not then the terminal will print "Please motor on."

The gripper_move() and arm_move() functions are called to move the COBOTTA's joints and gripper based on the parameters in place. Notice that in arm_move(), the pose_goal has been changed to joints, the variable joints stores the position value in a vector and is then used as the pose_goal.

Code:

if __name__ == '__main__':
    rospy.init_node("packing_pose")
    moveit_commander.roscpp_initialize(sys.argv)
    gripper_client = actionlib.SimpleActionClient('/cobotta/gripper_move', GripperMoveAction)

    print(os.path.basename(__file__) + " sets psoe gola and moves COBOTTA.")
----------------------------------------------------------------------------------
    if not is_simulation() and is_motor_running() is not True:
        print >> sys.stderr, " Please motor on."
        continue
    gripper_move(gripper_client, gripper_width, gripper_parallel_speed, gripper_parallel_effort)
    arm_move(move_group, joints)



Info:
FINISHED.
This concludes the use of Python to interact with the COBOTTA. Go to the What's Next document to see other options for interacting with the COBOTTA.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article