Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ catkin_package(
INCLUDE_DIRS include
LIBRARIES ros_essentials_cpp
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
DEPENDS system_lib
# DEPENDS system_lib
)

###########
Expand Down
81 changes: 36 additions & 45 deletions src/assignment/turtlesim_motion_pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,70 @@
import time
from std_srvs.srv import Empty

x=0
y=0
z=0
yaw=0
x = 0
y = 0
z = 0
yaw = 0


def poseCallback(pose_message):
global x
global y, z, yaw
x= pose_message.x
y= pose_message.y
x = pose_message.x
y = pose_message.y
yaw = pose_message.theta

#print "pose callback"
#print ('x = {}'.format(pose_message.x)) #new in python 3
#print ('y = %f' %pose_message.y) #used in python 2
#print ('yaw = {}'.format(pose_message.theta)) #new in python 3
# print "pose callback"
# print ('x = {}'.format(pose_message.x)) #new in python 3
# print ('y = %f' %pose_message.y) #used in python 2
# print ('yaw = {}'.format(pose_message.theta)) #new in python 3


def move(speed, distance):
#declare a Twist message to send velocity commands
# declare a Twist message to send velocity commands
velocity_message = Twist()
#get current location
x0=x
y0=y
#z0=z;
#yaw0=yaw;
velocity_message.linear.x =speed
# get current location
x0 = x
y0 = y
# z0 = z;
# yaw0 = yaw;
velocity_message.linear.x = speed
distance_moved = 0.0
loop_rate = rospy.Rate(10) # we publish the velocity at 10 Hz (10 times a second)
cmd_vel_topic='/cmd_vel'
loop_rate = rospy.Rate(10) # we publish the velocity at 10 Hz (10 times a second)
cmd_vel_topic = '/cmd_vel'
velocity_publisher = rospy.Publisher(cmd_vel_topic, Twist, queue_size=10)

while True :
while True:
rospy.loginfo("Turtlesim moves forwards")
velocity_publisher.publish(velocity_message)

loop_rate.sleep()

#rospy.Duration(1.0)

distance_moved = distance_moved+abs(0.5 * math.sqrt(((x-x0) ** 2) + ((y-y0) ** 2)))
print distance_moved
if not (distance_moved<distance):
# rospy.Duration(1.0)
distance_moved = distance_moved + abs(0.5 * math.sqrt(((x-x0) ** 2) + ((y-y0) ** 2)))
print(distance_moved)
if not (distance_moved < distance):
rospy.loginfo("reached")
break

#finally, stop the robot when the distance is moved
velocity_message.linear.x =0
# finally, stop the robot when the distance is moved
velocity_message.linear.x = 0
velocity_publisher.publish(velocity_message)



if __name__ == '__main__':
try:

rospy.init_node('turtlesim_motion_pose', anonymous=True)

#declare velocity publisher
cmd_vel_topic='/cmd_vel'
# declare velocity publisher
cmd_vel_topic = '/cmd_vel'
velocity_publisher = rospy.Publisher(cmd_vel_topic, Twist, queue_size=10)

position_topic = "/turtle1/pose"
pose_subscriber = rospy.Subscriber(position_topic, Pose, poseCallback)
pose_subscriber = rospy.Subscriber(position_topic, Pose, poseCallback)
time.sleep(2)
print 'move: '
move (1.0, 5.0)
print('move: ')
move(1.0, 5.0)
time.sleep(2)
print 'start reset: '
print('start reset: ')
rospy.wait_for_service('reset')
reset_turtle = rospy.ServiceProxy('reset', Empty)
reset_turtle()
print 'end reset: '
print('end reset: ')
rospy.spin()

except rospy.ROSInterruptException:
rospy.loginfo("node terminated.")
rospy.loginfo("node terminated.")