It's my first time using python in ROS and I'm stuck with the syntax, I want to make a python subscribe to a 1D array that I have already published using C++ and I also want to access the element of this array and use it to display a map on a window using PyQt, but I can't access the data inside the array because I don't know the syntax
there is an error in line 33 (msg.(i+j)==1
)
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from mainw import Ui_Form
#Subscriber
from std_msgs.msg import MultiArrayDimension
from std_msgs.msg import Int32MultiArray
import rospy
class main(QWidget, Ui_Form):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
for i in range(0,20):
for j in range(0,20):
self.tableWidget.setItem(i, j, QTableWidgetItem())
def callback(msg):
for i in range(0,19):
for j in range(0,19):
if(msg.(i+j)==1)
self.tableWidget.item(i,j).setBackground(QColor(170, 0, 0))
else if(data.data==2)
self.tableWidget.item(i, j).setBackground(QColor(170, 0, 0))
def TwoDMap():
rospy.init_node('TwoDMap', anonymous=True)
rospy.Subscriber("array", Int32MultiArray, callback)
rospy.spin()
if __name__ == '__main__':
TwoDMap()
app=QApplication(sys.argv)
window =main()
window.show()
app.exec_()
According to the Int32MultiArray message, you should have change like this:
if msg.data[i+j] == 1:
# do something
elif msg.data[i+j] == 2:
# do something else
Also your .callback()
method in the class hasn't self
argument, and you have some mistakes in methods call. Thus, your code will be as the following:
class Main(QWidget, Ui_Form):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
rospy.Subscriber("array", Int32MultiArray, self.callback)
for i in range(0,20):
for j in range(0,20):
self.tableWidget.setItem(i, j, QTableWidgetItem())
def callback(self, msg):
for i in range(0,19):
for j in range(0,19):
if msg.data[i+j] == 1:
self.tableWidget.item(i,j).setBackground(QColor(170, 0, 0))
elif msg.data[i+j] == 2:
self.tableWidget.item(i, j).setBackground(QColor(170, 0, 0))
if __name__ == '__main__':
rospy.init_node('TwoDMap', anonymous=True)
app = QApplication(sys.argv)
obj = Main()
obj.show()
app.exec_()
rospy.spin()