I have been using ROS alongside Python to enable my built robot to walk. I am aware that my code needs some improvement such as classes etc. This is my first Python / ROS program so any help and pointers would be hugely appreciated.#!/usr/bin/env python### launch start_robot_controller.launch before running this.import roslib; roslib.load_manifest('my_dynamixel_tutorial')import rospyimport mathimport operatorimport sysimport select#import msvcrt#import pygameimport termios, sys, osTERMIOS = termiosfrom std_msgs.msg import Float64from dynamixel_msgs.msg import MotorStateListfrom dynamixel_msgs.msg import JointStatefrom geometry_msgs.msg import Twist## define the command publishers for joints, Legs are named as: L - left; R - Right; M - middle; R - rear. each leg has three joints, 1,2,3pub_LF1 = rospy.Publisher(/LF1/command, Float64, latch=True)pub_LF2 = rospy.Publisher(/LF2/command, Float64, latch=True)pub_LF3 = rospy.Publisher(/LF3/command, Float64, latch=True)pub_LM1 = rospy.Publisher(/LM1/command, Float64, latch=True)pub_LM2 = rospy.Publisher(/LM2/command, Float64, latch=True)pub_LM3 = rospy.Publisher(/LM3/command, Float64, latch=True)pub_LR1 = rospy.Publisher(/LR1/command, Float64, latch=True)pub_LR2 = rospy.Publisher(/LR2/command, Float64, latch=True)pub_LR3 = rospy.Publisher(/LR3/command, Float64, latch=True)pub_RF1 = rospy.Publisher(/RF1/command, Float64, latch=True)pub_RF2 = rospy.Publisher(/RF2/command, Float64, latch=True)pub_RF3 = rospy.Publisher(/RF3/command, Float64, latch=True)pub_RM1 = rospy.Publisher(/RM1/command, Float64, latch=True)pub_RM2 = rospy.Publisher(/RM2/command, Float64, latch=True)pub_RM3 = rospy.Publisher(/RM3/command, Float64, latch=True)pub_RR1 = rospy.Publisher(/RR1/command, Float64, latch=True)pub_RR2 = rospy.Publisher(/RR2/command, Float64, latch=True)pub_RR3 = rospy.Publisher(/RR3/command, Float64, latch=True)## leg states. each leg has three states: 1 - move ofrward in the air; 2 - push down to touch ground; 3 - move backward on the ground. ## use only middle legs, LM and RM, to monitor the state. LM and RF and RR are in one tripod, thus the same state. global LM_stateglobal RM_staterad = 180.000001/3.1415926## define the desired positions of each state for each joint, below are only for left legs. The values for right leges should be multiplied by -1, because the motors at two legs are mirrored to each other.AEP = -20 # anterior extreme position of joint 1, in degreePEP = 20  # posterior extreme position of joint 1, in degree# walk with higher body altitude  ## with this, robot sometimes stop walking due to small delta value#P_j1_s1 = AEP/rad  ## state 1#P_j2_s1 = -45/rad#P_j3_s1 = -30/rad#P_j1_s2 = AEP/rad  ## state 2#P_j2_s2 = 0/rad#P_j3_s2 = 0/rad#P_j1_s3 = PEP/rad  ## state 3#P_j2_s3 = 0/rad#P_j3_s3 = 0/rad## OR: walk with lower body altitudeP_j1_s1 = AEP/rad  ## state 1P_j2_s1 = -65/radP_j3_s1 = -30/radP_j1_s2 = AEP/rad  ## state 2P_j2_s2 = -45/radP_j3_s2 = -20/radP_j1_s3 = PEP/rad  ## state 3P_j2_s3 = -45/radP_j3_s3 = -20/rad## define the desired positions of all three joints in each leg# walk with medium body altitude,, With this, robot walking looks better#P_j1_s1 = AEP/rad  ## state 1#P_j2_s1 = -60/rad#P_j3_s1 = -45/rad#P_j1_s2 = AEP/rad  ## state 2#P_j2_s2 = -30/rad#P_j3_s2 = -20/rad#P_j1_s3 = PEP/rad  ## state 3#P_j2_s3 = -30/rad#P_j3_s3 = -20/rad## indicate whether the joints have reached the desired positions in middle legsglobal flag_LM1global flag_LM2global flag_LM3global flag_RM1global flag_RM2global flag_RM3## desired position of each jointglobal    p_LM1 global    p_LM2 global    p_LM3 global    p_LF1 global    p_LF2 global    p_LF3 global    p_LR1 global    p_LR2 global    p_LR3 global    p_RM1 global    p_RM2 global    p_RM3 global    p_RF1 global    p_RF2 global    p_RF3 global    p_RR1 global    p_RR2 global    p_RR3 global robot_movingglobal Ra_L  ## turning ratio for left legglobal Ra_R## turning rate for left and right turningRa_L = 1  ## if being 1, means no turning to leftRa_R = 1robot_moving = 1 # move forwardLM_state = 1RM_state = 3  ## at the beginning, initiate the gait with right middle leg and its tripod.## set the initial desired pos for each joint according to the above LM_state and RM_state values# because LM_state = 1, so:p_LM1 = P_j1_s1p_LM2 = P_j2_s1p_LM3 = P_j3_s1p_RR1 = (-1)*P_j1_s1p_RR2 = (-1)*P_j2_s1p_RR3 = (-1)*P_j3_s1p_RF1 = (-1)*P_j1_s1p_RF2 = (-1)*P_j2_s1p_RF3 = (-1)*P_j3_s1    # because RM_state = 3, so:p_RM1 = (-1)*P_j1_s3p_RM2 = (-1)*P_j2_s3p_RM3 = (-1)*P_j3_s3p_LR1 = P_j1_s3p_LR2 = P_j2_s3p_LR3 = P_j3_s3p_LF1 = P_j1_s3p_LF2 = P_j2_s3p_LF3 = P_j3_s3    global cmdcmd = 2.0global count count = 0count2 = 0## the functions below monitor whether the middle leg joints have reached the desired posdelta = 2.00/rad  ## minimum error allowed, in radius. when below this error, we assume the desired position is reacheddef LM1_callback(msg):    global flag_LM1    global p_LM1    flag_LM1 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_LM1)<delta:        #if msg.is_moving == False:            #print 99            flag_LM1 = 1    #print flag_LM1def LM2_callback(msg):    global flag_LM2    global p_LM2    flag_LM2 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_LM2)<delta:        #if msg.is_moving == False:            flag_LM2 = 1def LM3_callback(msg):    global flag_LM3    global p_LM3    flag_LM3 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_LM3)<delta:        #if msg.is_moving == False:            flag_LM3 = 1def RM1_callback(msg):    global flag_RM1    global RM_1    flag_RM1 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_RM1)<delta:        #if msg.is_moving == False:            flag_RM1 = 1def RM2_callback(msg):    global flag_RM2    global p_RM2    flag_RM2 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_RM2)<delta:        #if msg.is_moving == False:            flag_RM2 = 1def RM3_callback(msg):    global flag_RM3    global p_RM3    flag_RM3 = 0  ## means not reaching the desired pos    if abs(msg.current_pos-p_RM3)<delta:        #if msg.is_moving == False:            flag_RM3 = 1    global flag_LM1    global flag_LM2    global flag_LM3    global flag_RM1    global flag_RM2    global LM_state    global RM_state    ## desired position of each joint    global    p_LM1     global    p_LM2     global    p_LM3     global    p_LF1     global    p_LF2     global    p_LF3     global    p_LR1     global    p_LR2     global    p_LR3     global    p_RM1     global    p_RM2     global    p_RM3     global    p_RF1     global    p_RF2     global    p_RF3     global    p_RR1     global    p_RR2     global    p_RR3    global robot_moving    global Ra_L    global Ra_R     global count     count = count + 1    print     print    print count     print flag_LM1, flag_LM2, flag_LM3, flag_RM1, flag_RM2, flag_RM3    # indicating whenther the middle legs have reached the end of its current state    flag_LM = flag_LM1*flag_LM2*flag_LM3  # left middle    flag_RM = flag_RM1*flag_RM2*flag_RM3  # right middle    ## here are the controls for the states of middle legs    if LM_state==1 and flag_LM==1:        LM_state = 2        flag_LM = 0    if LM_state==2 and flag_LM==1 and RM_state==3 and flag_RM==1:        LM_state = 3        RM_state = 1        flag_LM = 0        flag_RM = 0    if LM_state==3 and flag_LM==1 and RM_state==2 and flag_RM==1:        LM_state = 1        RM_state = 3        flag_LM = 0        flag_RM = 0    if RM_state==1 and flag_RM==1:        RM_state = 2        flag_RM = 0    print LM_state, RM_state     ## calculate the desired position of each joint according to the state of the leg    if LM_state == 1:        p_LM1 = Ra_L*P_j1_s1        p_LM2 = P_j2_s1        p_LM3 = P_j3_s1        p_RR1 = Ra_R*(-1)*P_j1_s1        p_RR2 = (-1)*P_j2_s1        p_RR3 = (-1)*P_j3_s1        p_RF1 = Ra_R*(-1)*P_j1_s1        p_RF2 = (-1)*P_j2_s1        p_RF3 = (-1)*P_j3_s1                    if LM_state == 2:        p_LM1 = Ra_L*P_j1_s2        p_LM2 = P_j2_s2        p_LM3 = P_j3_s2        p_RR1 = Ra_R*(-1)*P_j1_s2        p_RR2 = (-1)*P_j2_s2        p_RR3 = (-1)*P_j3_s2        p_RF1 = Ra_R*(-1)*P_j1_s2        p_RF2 = (-1)*P_j2_s2        p_RF3 = (-1)*P_j3_s2        if LM_state == 3:        p_LM1 = Ra_L*P_j1_s3        p_LM2 = P_j2_s3        p_LM3 = P_j3_s3        p_RR1 = Ra_R*(-1)*P_j1_s3        p_RR2 = (-1)*P_j2_s3        p_RR3 = (-1)*P_j3_s3        p_RF1 = Ra_R*(-1)*P_j1_s3        p_RF2 = (-1)*P_j2_s3        p_RF3 = (-1)*P_j3_s3        if RM_state == 1:        p_RM1 = Ra_R*(-1)*P_j1_s1        p_RM2 = (-1)*P_j2_s1        p_RM3 = (-1)*P_j3_s1        p_LR1 = Ra_L*P_j1_s1        p_LR2 = P_j2_s1        p_LR3 = P_j3_s1        p_LF1 = Ra_L*P_j1_s1        p_LF2 = P_j2_s1        p_LF3 = P_j3_s1        if RM_state == 2:        p_RM1 = Ra_R*(-1)*P_j1_s2        p_RM2 = (-1)*P_j2_s2        p_RM3 = (-1)*P_j3_s2        p_LR1 = Ra_L*P_j1_s2        p_LR2 = P_j2_s2        p_LR3 = P_j3_s2        p_LF1 = Ra_L*P_j1_s2        p_LF2 = P_j2_s2        p_LF3 = P_j3_s2       if RM_state == 3:        p_RM1 = Ra_R*(-1)*P_j1_s3        p_RM2 = (-1)*P_j2_s3        p_RM3 = (-1)*P_j3_s3        p_LR1 = Ra_L*P_j1_s3        p_LR2 = P_j2_s3        p_LR3 = P_j3_s3        p_LF1 = Ra_L*P_j1_s3        p_LF2 = P_j2_s3        p_LF3 = P_j3_s3      ## send command to motors     if robot_moving==1:         pub_LF1.publish(p_LF1)        pub_LF2.publish(p_LF2)        pub_LF3.publish(p_LF3)        pub_LM1.publish(p_LM1)        pub_LM2.publish(p_LM2)        pub_LM3.publish(p_LM3)        pub_LR1.publish(p_LR1)        pub_LR2.publish(p_LR2)        pub_LR3.publish(p_LR3)            pub_RF1.publish(p_RF1)        pub_RF2.publish(p_RF2)        pub_RF3.publish(p_RF3)        pub_RM1.publish(p_RM1)        pub_RM2.publish(p_RM2)        pub_RM3.publish(p_RM3)        pub_RR1.publish(p_RR1)        pub_RR2.publish(p_RR2)        pub_RR3.publish(p_RR3)    def led_control_demo():    rospy.init_node('led_control_demo')    #rospy.Subscriber(/motor_states/pan_tilt_port, MotorStateList, motor_callback)    ### subscribe to the position feedbacks of middle leg joints.    rospy.Subscriber(/LM1/state, JointState, LM1_callback)    rospy.Subscriber(/LM2/state, JointState, LM2_callback)    rospy.Subscriber(/LM3/state, JointState, LM3_callback)    rospy.Subscriber(/RM1/state, JointState, RM1_callback)    rospy.Subscriber(/RM2/state, JointState, RM2_callback)    rospy.Subscriber(/RM3/state, JointState, RM3_callback)       ## run loops continuously    rospy.spin()if __name__ == '__main__':     #rospy.init.node(led_control_demo)     led_control_demo()
18 Motor, 6 Legged robot walking using Python / ROS
python;beginner;device driver