|
| 1 | +#! /usr/bin/env python |
| 2 | +import roslib; roslib.load_manifest('atlas_sim_interface_tutorial') |
| 3 | + |
| 4 | +from atlas_msgs.msg import AtlasSimInterfaceCommand, AtlasSimInterfaceState, AtlasState |
| 5 | +from geometry_msgs.msg import Pose, Point |
| 6 | +from std_msgs.msg import String |
| 7 | +from tf.transformations import quaternion_from_euler, euler_from_quaternion |
| 8 | + |
| 9 | +import math |
| 10 | +import rospy |
| 11 | +import sys |
| 12 | + |
| 13 | +class AtlasWalk(): |
| 14 | + |
| 15 | + def walk(self): |
| 16 | + # Initialize atlas mode and atlas_sim_interface_command publishers |
| 17 | + self.mode = rospy.Publisher('/atlas/mode', String, None, False, \ |
| 18 | + True, None) |
| 19 | + self.asi_command = rospy.Publisher('/atlas/atlas_sim_interface_command', AtlasSimInterfaceCommand, None, False, True, None) |
| 20 | + |
| 21 | + # Assume that we are already in BDI Stand mode |
| 22 | + |
| 23 | + # Initialize some variables before starting. |
| 24 | + self.step_index = 0 |
| 25 | + self.is_swaying = False |
| 26 | + |
| 27 | + # Subscribe to atlas_state and atlas_sim_interface_state topics. |
| 28 | + self.asi_state = rospy.Subscriber('/atlas/atlas_sim_interface_state', AtlasSimInterfaceState, self.asi_state_cb) |
| 29 | + self.atlas_state = rospy.Subscriber('/atlas/atlas_state', AtlasState, self.atlas_state_cb) |
| 30 | + |
| 31 | + # Walk in circles until shutdown. |
| 32 | + while not rospy.is_shutdown(): |
| 33 | + rospy.spin() |
| 34 | + print("Shutting down") |
| 35 | + |
| 36 | + # /atlas/atlas_sim_interface_state callback. Before publishing a walk command, we need |
| 37 | + # the current robot position |
| 38 | + def asi_state_cb(self, state): |
| 39 | + try: |
| 40 | + x = self.robot_position.x |
| 41 | + except AttributeError: |
| 42 | + self.robot_position = Point() |
| 43 | + self.robot_position.x = state.pos_est.position.x |
| 44 | + self.robot_position.y = state.pos_est.position.y |
| 45 | + self.robot_position.z = state.pos_est.position.z |
| 46 | + |
| 47 | + if self.is_static: |
| 48 | + self.static(state) |
| 49 | + else: |
| 50 | + self.dynamic(state) |
| 51 | + |
| 52 | + # /atlas/atlas_state callback. This message provides the orientation of the robot from the torso IMU |
| 53 | + # This will be important if you need to transform your step commands from the robot's local frame to world frame |
| 54 | + def atlas_state_cb(self, state): |
| 55 | + # If you don't reset to harnessed, then you need to get the current orientation |
| 56 | + roll, pitch, yaw = euler_from_quaternion([state.orientation.x, state.orientation.y, state.orientation.z, state.orientation.w]) |
| 57 | + |
| 58 | + # An example of commanding a dynamic walk behavior. |
| 59 | + def dynamic(self, state): |
| 60 | + command = AtlasSimInterfaceCommand() |
| 61 | + command.behavior = AtlasSimInterfaceCommand.WALK |
| 62 | + |
| 63 | + # k_effort is all 0s for full BDI controll of all joints. |
| 64 | + command.k_effort = [0] * 28 |
| 65 | + |
| 66 | + # Observe next_step_index_needed to determine when to switch steps. |
| 67 | + self.step_index = state.walk_feedback.next_step_index_needed |
| 68 | + |
| 69 | + # A walk behavior command needs to know three additional steps beyond the current step needed to plan |
| 70 | + # for the best balance |
| 71 | + for i in range(4): |
| 72 | + step_index = self.step_index + i |
| 73 | + is_right_foot = step_index % 2 |
| 74 | + |
| 75 | + command.walk_params.step_queue[i].step_index = step_index |
| 76 | + command.walk_params.step_queue[i].foot_index = is_right_foot |
| 77 | + |
| 78 | + # A duration of 0.63s is a good default value |
| 79 | + command.walk_params.step_queue[i].duration = 0.63 |
| 80 | + |
| 81 | + # As far as I can tell, swing_height has yet to be implemented |
| 82 | + command.walk_params.step_queue[i].swing_height = 0.2 |
| 83 | + |
| 84 | + # Determine pose of the next step based on the step_index |
| 85 | + command.walk_params.step_queue[i].pose = self.calculate_pose(step_index) |
| 86 | + |
| 87 | + # Publish this command every time we have a new state message |
| 88 | + self.asi_command.publish(command) |
| 89 | + # End of dynamic walk behavior |
| 90 | + |
| 91 | + # An example of commanding a static walk/step behavior. |
| 92 | + def static(self, state): |
| 93 | + |
| 94 | + # When the robot status_flags are 1 (SWAYING), you can publish the next step command. |
| 95 | + if (state.step_feedback.status_flags == 1 and not self.is_swaying): |
| 96 | + self.step_index += 1 |
| 97 | + self.is_swaying = True |
| 98 | + print("Step " + str(self.step_index)) |
| 99 | + elif (state.step_feedback.status_flags == 2): |
| 100 | + self.is_swaying = False |
| 101 | + |
| 102 | + is_right_foot = self.step_index % 2 |
| 103 | + |
| 104 | + command = AtlasSimInterfaceCommand() |
| 105 | + command.behavior = AtlasSimInterfaceCommand.STEP |
| 106 | + |
| 107 | + # k_effort is all 0s for full bdi control of all joints |
| 108 | + command.k_effort = [0] * 28 |
| 109 | + |
| 110 | + # step_index should always be one for a step command |
| 111 | + command.step_params.desired_step.step_index = 1 |
| 112 | + command.step_params.desired_step.foot_index = is_right_foot |
| 113 | + |
| 114 | + # duration has far as I can tell is not observed |
| 115 | + command.step_params.desired_step.duration = 0.63 |
| 116 | + |
| 117 | + # swing_height is not observed |
| 118 | + command.step_params.desired_step.swing_height = 0.1 |
| 119 | + |
| 120 | + if self.step_index > 30: |
| 121 | + print(str(self.calculate_pose(self.step_index))) |
| 122 | + # Determine pose of the next step based on the number of steps we have taken |
| 123 | + command.step_params.desired_step.pose = self.calculate_pose(self.step_index) |
| 124 | + |
| 125 | + # Publish a new step command every time a state message is received |
| 126 | + self.asi_command.publish(command) |
| 127 | + # End of static walk behavior |
| 128 | + |
| 129 | + # This method is used to calculate a pose of step based on the step_index |
| 130 | + # The step poses just cause the robot to walk in a circle |
| 131 | + def calculate_pose(self, step_index): |
| 132 | + # Right foot occurs on even steps, left on odd |
| 133 | + is_right_foot = step_index % 2 |
| 134 | + is_left_foot = 1 - is_right_foot |
| 135 | + |
| 136 | + # There will be 60 steps to a circle, and so our position along the circle is current_step |
| 137 | + current_step = step_index % 60 |
| 138 | + |
| 139 | + # yaw angle of robot around circle |
| 140 | + theta = current_step * math.pi / 30 |
| 141 | + |
| 142 | + R = 2 # Radius of turn |
| 143 | + W = 0.3 # Width of stride |
| 144 | + |
| 145 | + # Negative for inside foot, positive for outside foot |
| 146 | + offset_dir = 1 - 2 * is_left_foot |
| 147 | + |
| 148 | + # Radius from center of circle to foot |
| 149 | + R_foot = R + offset_dir * W/2 |
| 150 | + |
| 151 | + # X, Y position of foot |
| 152 | + X = R_foot * math.sin(theta) |
| 153 | + Y = (R - R_foot*math.cos(theta)) |
| 154 | + |
| 155 | + # Calculate orientation quaternion |
| 156 | + Q = quaternion_from_euler(0, 0, theta) |
| 157 | + pose = Pose() |
| 158 | + pose.position.x = self.robot_position.x + X |
| 159 | + pose.position.y = self.robot_position.y + Y |
| 160 | + |
| 161 | + # The z position is observed for static walking, but the foot |
| 162 | + # will be placed onto the ground if the ground is lower than z |
| 163 | + pose.position.z = 0 |
| 164 | + |
| 165 | + pose.orientation.x = Q[0] |
| 166 | + pose.orientation.y = Q[1] |
| 167 | + pose.orientation.z = Q[2] |
| 168 | + pose.orientation.w = Q[3] |
| 169 | + |
| 170 | + return pose |
| 171 | + |
| 172 | +if __name__ == '__main__': |
| 173 | + rospy.init_node('walking_tutorial') |
| 174 | + walk = AtlasWalk() |
| 175 | + if len(sys.argv) > 0: |
| 176 | + walk.is_static = (sys.argv[-1] == "static") |
| 177 | + else: |
| 178 | + walk.is_static = False |
| 179 | + walk.walk() |
0 commit comments