sr_sinusoid_joint_example.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2019 Shadow Robot Company Ltd.
3 #
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 # more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16 # Example where two joints are specified and move with a sinusoidal trajectory, with a pi/4 phase difference
17 
18 import rospy
19 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
20 from sr_robot_commander.sr_hand_commander import SrHandCommander
21 from sr_utilities.hand_finder import HandFinder
22 from numpy import sin, cos, pi, arange
23 
24 rospy.init_node("joint_sine_example", anonymous=True)
25 
26 # handfinder is used to access the hand parameters
27 hand_finder = HandFinder()
28 hand_parameters = hand_finder.get_hand_parameters()
29 prefix = hand_parameters.mapping.values()
30 hand_serial = hand_parameters.mapping.keys()[0]
31 
32 hand_commander = SrHandCommander(hand_parameters=hand_parameters, hand_serial=hand_serial)
33 
34 # cycles per second of sine wave
35 f = 1
36 # angular frequency, rads/s
37 w = 2 * pi * f
38 # time for motion to complete
39 ts = 20
40 
41 # specify 2 joints to move
42 joint_names = [prefix[0] + '_FFJ3', prefix[0] + '_RFJ3']
43 
44 # set max and min joint positions
45 min_pos_J3 = 0.0
46 max_pos_J3 = pi / 2
47 
48 rospy.sleep(rospy.Duration(2))
49 
50 hand_joints_goal = {joint_names[0]: 0.0, joint_names[1]: 0.0}
51 
52 rospy.loginfo("Running joints trajectory")
53 
54 # initialising the joint trajectory message
55 joint_trajectory = JointTrajectory()
56 joint_trajectory.header.stamp = rospy.Time.now()
57 joint_trajectory.joint_names = list(hand_joints_goal.keys())
58 joint_trajectory.points = []
59 
60 # generate sinusoidal list of data points, two joints moving out of phase
61 for t in arange(0.002, ts, 0.02):
62  trajectory_point = JointTrajectoryPoint()
63  trajectory_point.time_from_start = rospy.Duration.from_sec(float(t))
64  trajectory_point.positions = []
65  trajectory_point.velocities = []
66  trajectory_point.accelerations = []
67  trajectory_point.effort = []
68 
69  for key in joint_trajectory.joint_names:
70  if key in joint_names[0]: # generate joint positions for first joint
71  joint_position = sin(w * t) * (max_pos_J3 - min_pos_J3) / 2 + (max_pos_J3 - min_pos_J3) / 2 + min_pos_J3
72  trajectory_point.positions.append(joint_position)
73  elif key in joint_names[1]: # generate joint positions for second joint
74  joint_position = cos(w * t) * (max_pos_J3 - min_pos_J3) / 2 + (max_pos_J3 - min_pos_J3) / 2 + min_pos_J3
75  trajectory_point.positions.append(joint_position)
76  else:
77  trajectory_point.positions.append(hand_joints_goal[key])
78 
79  trajectory_point.velocities.append(0.0)
80  trajectory_point.accelerations.append(0.0)
81  trajectory_point.effort.append(0.0)
82 
83  joint_trajectory.points.append(trajectory_point)
84 
85 # Send trajectory to hand_commander
86 hand_commander.run_joint_trajectory_unsafe(joint_trajectory)
87 
88 rospy.sleep(rospy.Duration(15))


sr_example
Author(s): Ugo Cupcic
autogenerated on Wed Oct 14 2020 04:05:12