Skip to content
Snippets Groups Projects
Commit 2fa567c5 authored by Ventsislav Dokusanski's avatar Ventsislav Dokusanski
Browse files

Clean and comment code of the 1dof

parent 1c166886
Branches
No related tags found
No related merge requests found
...@@ -27,25 +27,25 @@ def generate_launch_description(): ...@@ -27,25 +27,25 @@ def generate_launch_description():
declared_arguments.append( declared_arguments.append(
DeclareLaunchArgument( DeclareLaunchArgument(
'max_velocity', 'max_velocity',
default_value = '0.015', default_value = '0.01725',
) )
) )
declared_arguments.append( declared_arguments.append(
DeclareLaunchArgument( DeclareLaunchArgument(
'acceleration', 'acceleration',
default_value = '0.005', default_value = '0.001327',
) )
) )
declared_arguments.append( declared_arguments.append(
DeclareLaunchArgument( DeclareLaunchArgument(
'decelaration', 'decelaration',
default_value = '0.005', default_value = '0.001327',
) )
) )
declared_arguments.append( declared_arguments.append(
DeclareLaunchArgument( DeclareLaunchArgument(
'velocity_constant', 'velocity_constant',
default_value = '0.015', default_value = '0.014',
) )
) )
declared_arguments.append( declared_arguments.append(
......
...@@ -18,13 +18,13 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -18,13 +18,13 @@ class Forward_Kinematics_Node_1Dof(Node):
def __init__(self): def __init__(self):
super().__init__('forward_kinematics_node_1dof') super().__init__('forward_kinematics_node_1dof')
# Create a subscriber, initialising the transformation
self.subscription = self.create_subscription( self.subscription = self.create_subscription(
Float64, Float64,
'/dof1/reference_angles', '/dof1/reference_angles',
self.listener_callback, self.listener_callback,
10) 10)
# load KUKA urdf
urdf_file_path = os.path.join( urdf_file_path = os.path.join(
get_package_share_directory('iiwa_description'), 'urdf', 'iiwa14.urdf') get_package_share_directory('iiwa_description'), 'urdf', 'iiwa14.urdf')
self.robot_model = loadURDF(urdf_file_path) self.robot_model = loadURDF(urdf_file_path)
...@@ -34,6 +34,7 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -34,6 +34,7 @@ class Forward_Kinematics_Node_1Dof(Node):
self.Glist = self.robot_model["Glist"] self.Glist = self.robot_model["Glist"]
self.Blist = self.robot_model["Blist"] self.Blist = self.robot_model["Blist"]
# load simple robot URDF
urdf_file_path_simple_robot = os.path.join( urdf_file_path_simple_robot = os.path.join(
get_package_share_directory('simple_robot_1dof'), 'urdf', 'simple_robot.urdf') get_package_share_directory('simple_robot_1dof'), 'urdf', 'simple_robot.urdf')
self.simple_robot_model = loadURDF(urdf_file_path_simple_robot) self.simple_robot_model = loadURDF(urdf_file_path_simple_robot)
...@@ -43,15 +44,13 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -43,15 +44,13 @@ class Forward_Kinematics_Node_1Dof(Node):
self.simple_robot_Glist = self.simple_robot_model["Glist"] self.simple_robot_Glist = self.simple_robot_model["Glist"]
self.simple_robot_Blist = self.simple_robot_model["Blist"] self.simple_robot_Blist = self.simple_robot_model["Blist"]
# publishers
# create two publishers for the RViz simulation - one for the whole path and one for the marker
self.path_publisher_ = self.create_publisher(Path, '/simple_robot/visualization_path', 10) self.path_publisher_ = self.create_publisher(Path, '/simple_robot/visualization_path', 10)
self.point_publisher_ = self.create_publisher(Marker, '/simple_robot/visualization_marker', 10) self.point_publisher_ = self.create_publisher(Marker, '/simple_robot/visualization_marker', 10)
# create a publisher for the plotjuggler angles
self.angle_publisher_ = self.create_publisher(Float64, '/simple_robot/angles_1dof/plotjuggler', 10)
# create a publisher for the pose
self.pose_publisher_simple_robot_ = self.create_publisher(Pose, '/simple_robot/target_pose', 10) self.pose_publisher_simple_robot_ = self.create_publisher(Pose, '/simple_robot/target_pose', 10)
self.pose_publisher_kuka_robot_ = self.create_publisher(Pose, '/kuka_iiwa/target_pose', 10) self.pose_publisher_kuka_robot_ = self.create_publisher(Pose, '/kuka_iiwa/target_pose', 10)
self.velocity_pub = self.create_publisher(Float64, '/velocity', 10)
#RViz #RViz
self.path = Path() self.path = Path()
self.path.header.frame_id = "map" self.path.header.frame_id = "map"
...@@ -59,58 +58,58 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -59,58 +58,58 @@ class Forward_Kinematics_Node_1Dof(Node):
self.index = 0 self.index = 0
def listener_callback(self, msg): def listener_callback(self, msg):
# Get the message # Get the message
self.phi = msg.data self.k = msg.data
# call the transformation function # add 1 to index
if self.phi == 0: if self.k == 0:
self.index += 1 self.index += 1
# call the transformation function
self.transformation_simple_robot() self.transformation_simple_robot()
def transformation_simple_robot(self): def transformation_simple_robot(self):
# Define the desired rotation # define the desired rotation
self.theta = - pi / 1.8 self.theta = - pi / 1.8
# Split theta # split theta for interpolation
self.theta_fraction = self.phi * self.theta self.theta_fraction = self.k * self.theta
self.publish_angle(self.theta_fraction)
# Define the matrix
self.intermediate_rotation = np.array([ self.intermediate_rotation = np.array([
[cos(self.theta_fraction), -sin(self.theta_fraction), 0, 0], [cos(self.theta_fraction), -sin(self.theta_fraction), 0, 0],
[sin(self.theta_fraction), cos(self.theta_fraction), 0, 0], [sin(self.theta_fraction), cos(self.theta_fraction), 0, 0],
[0, 0, 1, 0], [0, 0, 1, 0],
[0, 0, 0, 1] [0, 0, 0, 1]
]) ])
# define a list of 0s
self.simple_robot_joint_angles = np.zeros(len(self.simple_robot_Slist[0])) self.simple_robot_joint_angles = np.zeros(len(self.simple_robot_Slist[0]))
# forward kinematics
start_forward_kinematics_simple = FKinBody(self.simple_robot_M, self.simple_robot_Blist, self.simple_robot_joint_angles) start_forward_kinematics_simple = FKinBody(self.simple_robot_M, self.simple_robot_Blist, self.simple_robot_joint_angles)
# apply the transformation
end_forward_kinematics_simple = np.matmul(self.intermediate_rotation, start_forward_kinematics_simple) end_forward_kinematics_simple = np.matmul(self.intermediate_rotation, start_forward_kinematics_simple)
# get the desired position and orientation
end_position_simple = end_forward_kinematics_simple[:4, 3] end_position_simple = end_forward_kinematics_simple[:4, 3]
end_orientation_simple = Rotation.from_matrix(end_forward_kinematics_simple[:3, :3]).as_quat() end_orientation_simple = Rotation.from_matrix(end_forward_kinematics_simple[:3, :3]).as_quat()
# call the funct. publishing the desired pose
self.publish_pose_simple_robot(end_position_simple, end_orientation_simple) self.publish_pose_simple_robot(end_position_simple, end_orientation_simple)
# construct path
self.construct_path(end_position_simple) self.construct_path(end_position_simple)
# move KUKA robot only once, when index equals 2 and let simulation with simple robot continue
if self.index == 2: if self.index == 2:
self.transformation_kuka() self.transformation_kuka()
def transformation_kuka(self): def transformation_kuka(self):
# list of joint states that may satisfy the desired pose
kuka_joint_angles = np.zeros(len(self.Slist[0])) kuka_joint_angles = np.zeros(len(self.Slist[0]))
kuka_joint_angles[0] = np.pi kuka_joint_angles[0] = np.pi
kuka_joint_angles[1] = np.pi/2 kuka_joint_angles[1] = np.pi/2
kuka_joint_angles[3] = np.pi/2 kuka_joint_angles[3] = np.pi/2
kuka_joint_angles[5] = np.pi/2 kuka_joint_angles[5] = np.pi/2
# analog to simple robot
start_forward_kinematics_kuka = FKinBody(self.M, self.Blist, kuka_joint_angles) start_forward_kinematics_kuka = FKinBody(self.M, self.Blist, kuka_joint_angles)
end_forward_kinematics_kuka = np.matmul(self.intermediate_rotation, start_forward_kinematics_kuka) end_forward_kinematics_kuka = np.matmul(self.intermediate_rotation, start_forward_kinematics_kuka)
...@@ -118,7 +117,6 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -118,7 +117,6 @@ class Forward_Kinematics_Node_1Dof(Node):
end_position_kuka = end_forward_kinematics_kuka[:4, 3] end_position_kuka = end_forward_kinematics_kuka[:4, 3]
end_orientation_kuka = Rotation.from_matrix(end_forward_kinematics_kuka[:3, :3]).as_quat() end_orientation_kuka = Rotation.from_matrix(end_forward_kinematics_kuka[:3, :3]).as_quat()
self.publish_pose_kuka_pose(end_position_kuka, end_orientation_kuka) self.publish_pose_kuka_pose(end_position_kuka, end_orientation_kuka)
...@@ -134,15 +132,13 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -134,15 +132,13 @@ class Forward_Kinematics_Node_1Dof(Node):
pose.pose.orientation.w = 1.0 pose.pose.orientation.w = 1.0
# if the path is completed clear it # if the path is completed clear it
if self.phi == 0: if self.k == 0:
self.path.poses.clear() self.path.poses.clear()
else: else:
self.path.poses.append(pose) self.path.poses.append(pose)
# publish the path
self.path_publisher_.publish(self.path) self.path_publisher_.publish(self.path)
# call the function publishing the marker
self.publish_marker(pose.pose) self.publish_marker(pose.pose)
def publish_marker(self, current_pose): def publish_marker(self, current_pose):
...@@ -161,16 +157,9 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -161,16 +157,9 @@ class Forward_Kinematics_Node_1Dof(Node):
marker.color.g = 0.75 marker.color.g = 0.75
marker.color.b = 0.0 marker.color.b = 0.0
# publish the marker
self.point_publisher_.publish(marker) self.point_publisher_.publish(marker)
def publish_angle(self, angle):
angle_msg = Float64()
angle_msg.data = angle * 180 / pi
self.angle_publisher_.publish(angle_msg)
def publish_pose_simple_robot(self, vector, quaternion): def publish_pose_simple_robot(self, vector, quaternion):
pose_msg_sr = Pose() pose_msg_sr = Pose()
pose_msg_sr.position.x = vector[0] pose_msg_sr.position.x = vector[0]
...@@ -183,7 +172,6 @@ class Forward_Kinematics_Node_1Dof(Node): ...@@ -183,7 +172,6 @@ class Forward_Kinematics_Node_1Dof(Node):
self.pose_publisher_simple_robot_.publish(pose_msg_sr) self.pose_publisher_simple_robot_.publish(pose_msg_sr)
def publish_pose_kuka_pose(self, vector, quaternion): def publish_pose_kuka_pose(self, vector, quaternion):
# Create a Pose message for the target pose
pose_msg = Pose() pose_msg = Pose()
pose_msg.position.x = vector[0] pose_msg.position.x = vector[0]
pose_msg.position.y = vector[1] pose_msg.position.y = vector[1]
......
...@@ -10,9 +10,11 @@ class Reference_Node_1DoF(Node): ...@@ -10,9 +10,11 @@ class Reference_Node_1DoF(Node):
def __init__(self): def __init__(self):
super().__init__('reference_node_1dof') super().__init__('reference_node_1dof')
#Create a publisher sending the vector as a message # publishers
self.publisher_ = self.create_publisher(Float64, '/dof1/reference_angles', 10) self.publisher_ = self.create_publisher(Float64, '/dof1/reference_angles', 10)
self.delta_publisher_ = self.create_publisher(Float64, '/delta', 10)
# parameters
self.declare_parameter('max_velocity', 0.0) self.declare_parameter('max_velocity', 0.0)
self.declare_parameter('acceleration', 0.0) self.declare_parameter('acceleration', 0.0)
self.declare_parameter('decelaration', 0.0) self.declare_parameter('decelaration', 0.0)
...@@ -25,12 +27,12 @@ class Reference_Node_1DoF(Node): ...@@ -25,12 +27,12 @@ class Reference_Node_1DoF(Node):
self.velocity_constant = self.get_parameter('velocity_constant').get_parameter_value().double_value self.velocity_constant = self.get_parameter('velocity_constant').get_parameter_value().double_value
self.constant_speed = self.get_parameter('constant_speed').get_parameter_value().bool_value self.constant_speed = self.get_parameter('constant_speed').get_parameter_value().bool_value
self.index = 0
self.phi_start = 0.0 self.k_start = 0.0
self.phi_end = 1.0 self.k_end = 1.0
self.phi = self.phi_start self.k = self.k_start
self.current_velocity = 0.0 self.current_velocity = 0.0
self.faktor = 1.3
self.timer_period = 0.1 # seconds self.timer_period = 0.1 # seconds
self.timer = self.create_timer(self.timer_period, self.ref_point_publisher) self.timer = self.create_timer(self.timer_period, self.ref_point_publisher)
# List the points # List the points
...@@ -41,26 +43,32 @@ class Reference_Node_1DoF(Node): ...@@ -41,26 +43,32 @@ class Reference_Node_1DoF(Node):
msg = Float64() msg = Float64()
# const. speed
if self.constant_speed: if self.constant_speed:
self.phi = self.phi + self.velocity_constant self.k = self.k + self.velocity_constant
# trapez. velocity
else: else:
if self.current_velocity < self.max_velocity and self.phi < 0.5: # acceleration phase
self.phi = self.phi + self.current_velocity if self.current_velocity < self.max_velocity and self.k < 0.5:
self.current_velocity = self.current_velocity + self.timer_period * self.acceleration self.k = self.k + self.current_velocity
self.transition_point = self.phi self.current_velocity = self.current_velocity + self.acceleration
elif self.phi > 1 - self.transition_point: self.transition_point = self.k
self.phi = self.phi + self.current_velocity # constant velocity
self.current_velocity = self.current_velocity - self.timer_period * self.decelaration elif self.k >= self.k_end - self.faktor * self.transition_point:
self.k = self.k + self.current_velocity
self.current_velocity = self.current_velocity - self.decelaration
# decelaration phase
else: else:
self.phi = self.phi + self.current_velocity self.current_velocity = self.max_velocity
self.k = self.k + self.current_velocity
#setting back to 0 #setting back to 0
if self.phi>self.phi_end: if self.k>self.k_end:
self.phi = self.phi_start self.k = self.k_start
self.current_velocity = 0.0 self.current_velocity = 0.0
# self.get_logger().info('Resetting "%f"' % self.phi) self.index += 1
msg.data = self.phi msg.data = self.k
self.publisher_.publish(msg) self.publisher_.publish(msg)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment