Trying to do an interface for our robot

Hello every one!

I have made an interface for one robot we are doing in our Secondary School. We have made it with PyQt5. The interface works fine and now we want to use it with other Python file, go_to_specific_point_on_map.py which works fine without the interface.

When I try to put all together, I get some errors that I don’t know how to solve because I don’t know a lot about Python.

I have made several tries but I think my best one are:
ejecutaprueba4.py and ejecutaprueba5.py

With the first one I get the next error:
$ python ejecutaprueba4.py
[INFO] [1572298461.772443]: Go to (1.22, 2.56) pose
Traceback (most recent call last):
File “ejecutaprueba4.py”, line 94, in
window = Ui()
File “ejecutaprueba4.py”, line 16, in init
self.button.clicked.connect(self.ir(1))
File “ejecutaprueba4.py”, line 80, in ir
success = self.goto(position, quaternion)
File “ejecutaprueba4.py”, line 43, in goto
self.move_base.send_goal(goal)
AttributeError: ‘Ui’ object has no attribute ‘move_base’

And with the second one I get:
$ python ejecutaprueba5.py
Traceback (most recent call last):
File “ejecutaprueba5.py”, line 23, in
window = Ui()
File “ejecutaprueba5.py”, line 17, in init
irPuntoMapa = GoToPose()
File “/home/mubita/go_to_specific_point_on_map.py”, line 41, in init
self.move_base.wait_for_server(rospy.Duration(5))
File “/opt/ros/kinetic/lib/python2.7/dist-packages/actionlib/simple_action_client.py”, line 67, in wait_for_server
return self.action_client.wait_for_server(timeout)
File “/opt/ros/kinetic/lib/python2.7/dist-packages/actionlib/action_client.py”, line 584, in wait_for_server
timeout_time = rospy.get_rostime() + timeout
File “/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/rostime.py”, line 190, in get_rostime
raise rospy.exceptions.ROSInitException(“time is not initialized. Have you called init_node()?”)
rospy.exceptions.ROSInitException: time is not initialized. Have you called init_node()?

I think that the second one is programmed better but the first one run more time that the second. We have tried to program in different ways and try to find the error but I’m not able.

Thank you very much for your help.

Best regards

prueba.zip (4.6 KB)

ejecutaprueba4.py

from PyQt5 import QtWidgets, uic
import sys
import rospy
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
from geometry_msgs.msg import Pose, Point, Quaternion

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
	super(Ui, self).__init__()
	uic.loadUi('prueba.ui', self)

	# Find the button with the name "printButton"
	self.button = self.findChild(QtWidgets.QPushButton, 'pushButton')
	self.button.clicked.connect(self.ir(1))

	self.goal_sent = False

	# What to do if shut down (e.g. Ctrl-C or failure)
	rospy.on_shutdown(self.shutdown)
	
	# Tell the action client that we want to spin a thread by default
	self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
	rospy.loginfo("Wait for the action server to come up")

	# Allow up to 5 seconds for the action server to come up
	self.move_base.wait_for_server(rospy.Duration(5))

	self.show()

    def goto(self, pos, quat):

	# Send a goal
	self.goal_sent = True
	goal = MoveBaseGoal()
	goal.target_pose.header.frame_id = 'map'
	goal.target_pose.header.stamp = rospy.Time.now()
	goal.target_pose.pose = Pose(Point(pos['x'], pos['y'], 0.000),
	                             Quaternion(quat['r1'], quat['r2'], quat['r3'], quat['r4']))

	# Start moving
	self.move_base.send_goal(goal)

	# Allow TurtleBot up to 60 seconds to complete task
	success = self.move_base.wait_for_result(rospy.Duration(60)) 

	state = self.move_base.get_state()
	result = False

	if success and state == GoalStatus.SUCCEEDED:
	    # We made it!
	    result = True
	else:
	    self.move_base.cancel_goal()

	self.goal_sent = False
	return result

    def shutdown(self):
	if self.goal_sent:
	    self.move_base.cancel_goal()
	rospy.loginfo("Stop")
	rospy.sleep(1)

    def ir(self, cuadro):
	try:
	    rospy.init_node('nav_test', anonymous=False)

	    # Customize the following values so they are appropriate for your location

	    if cuadro == 1:
		position = {'x': 1.22, 'y' : 2.56}
		quaternion = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}
	    else:
		position = {'x': 1.22, 'y' : 2.56}
		quaternion = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}

	    rospy.loginfo("Go to (%s, %s) pose", position['x'], position['y'])
	    success = self.goto(position, quaternion)

	    if success:
	        rospy.loginfo("Hooray, reached the desired pose")
	    else:
	        rospy.loginfo("The base failed to reach the desired pose")
    
	    # Sleep to give the last log messages time to be sent
	    rospy.sleep(1)

	except rospy.ROSInterruptException:
	    rospy.loginfo("Ctrl-C caught. Quitting")

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

ejecutaprueba5.py

from PyQt5 import QtWidgets, uic
import sys
import rospy
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
from geometry_msgs.msg import Pose, Point, Quaternion
from go_to_specific_point_on_map import GoToPose

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
	super(Ui, self).__init__()
	uic.loadUi('prueba.ui', self)

	# Find the button with the name "printButton"
	self.button = self.findChild(QtWidgets.QPushButton, 'pushButton')
	irPuntoMapa = GoToPose()
	self.button.clicked.connect(irPuntoMapa.ir(1))

	self.show()

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

go_to_specific_point_on_map.py

It’s a modification of this:
https://github.com/markwsilliman/turtlebot/blob/master/go_to_specific_point_on_map.py

#!/usr/bin/env python

'''
Copyright (c) 2015, Mark Silliman
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

# TurtleBot must have minimal.launch & amcl_demo.launch
# running prior to starting this script
# For simulation: launch gazebo world & amcl_demo prior to run this script

import rospy
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import actionlib
from actionlib_msgs.msg import *
from geometry_msgs.msg import Pose, Point, Quaternion

class GoToPose():
    def __init__(self):

	self.goal_sent = False

	# What to do if shut down (e.g. Ctrl-C or failure)
	rospy.on_shutdown(self.shutdown)
	
	# Tell the action client that we want to spin a thread by default
	self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
	rospy.loginfo("Wait for the action server to come up")

	# Allow up to 5 seconds for the action server to come up
	self.move_base.wait_for_server(rospy.Duration(5))

    def goto(self, pos, quat):

	# Send a goal
	self.goal_sent = True
	goal = MoveBaseGoal()
	goal.target_pose.header.frame_id = 'map'
	goal.target_pose.header.stamp = rospy.Time.now()
	goal.target_pose.pose = Pose(Point(pos['x'], pos['y'], 0.000),
	                             Quaternion(quat['r1'], quat['r2'], quat['r3'], quat['r4']))

	# Start moving
	self.move_base.send_goal(goal)

	# Allow TurtleBot up to 60 seconds to complete task
	success = self.move_base.wait_for_result(rospy.Duration(60)) 

	state = self.move_base.get_state()
	result = False

	if success and state == GoalStatus.SUCCEEDED:
	    # We made it!
	    result = True
	else:
	    self.move_base.cancel_goal()

	self.goal_sent = False
	return result

    def shutdown(self):
	if self.goal_sent:
	    self.move_base.cancel_goal()
	rospy.loginfo("Stop")
	rospy.sleep(1)

    def ir(self, cuadro):
	try:
	    rospy.init_node('nav_test', anonymous=False)
	    navigator = GoToPose()

	    # Customize the following values so they are appropriate for your location

	    if cuadro == 1:
		position = {'x': 1.22, 'y' : 2.56}
		quaternion = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}
	    else:
		position = {'x': 1.22, 'y' : 2.56}
		quaternion = {'r1' : 0.000, 'r2' : 0.000, 'r3' : 0.000, 'r4' : 1.000}

	    rospy.loginfo("Go to (%s, %s) pose", position['x'], position['y'])
	    success = navigator.goto(position, quaternion)

	    if success:
	        rospy.loginfo("Hooray, reached the desired pose")
	    else:
	        rospy.loginfo("The base failed to reach the desired pose")

	    # Sleep to give the last log messages time to be sent
	    rospy.sleep(1)

	except rospy.ROSInterruptException:
	    rospy.loginfo("Ctrl-C caught. Quitting")
2 Likes