Subscrine Cmd_vel / If not
I want to create a subscriber with python. I want to get cmd_vel data and publish it with another Publisher. If I run twist keyboard.py , code work properly. However . If i don't execute teleop_keyboard.py code don't jump in "cmdgelen" and of course give me a an error NameError: global name 'x' is not defined... I want to initialize the code with x=0,y=0,z=0. After initilizing If "cmd_vel" give me any data, run with this data. How can code this ?
Code
def cmdgelen (msg):
rospy.loginfo("cmdgiris")
global x
global y
global z
x=msg.linear.x
y=msg.linear.y
z=msg.angular.z
def publishera():
output = HoldingRegister()
cxx=0
cyy=2
czz=0
#cmdgelen()
output.data = [x,y,z,cxx,cyy,czz]
output2 = HoldingRegister()
output2.data = [0,0,0,0,0,0]
rospy.loginfo("Sending arrays to the modbus server")
for i in xrange(10):
rospy.sleep(1)
pub.publish(output)
rospy.sleep(1)
pub.publish(output2)`
`if __name__=="__main__":
rospy.init_node("modbus_client_s7_1200")
host = "192.168.0.199"
port = 502
if rospy.has_param("~ip"):
host = rospy.get_param("~ip")
else:
rospy.loginfo("For not using the default IP %s, add an arg e.g.: '_ip:=\"192.168.0.198\"'",host)
if rospy.has_param("~port"):
port = rospy.get_param("~port")
else:
rospy.loginfo("For not using the default port %d, add an arg e.g.: '_port:=1234'",port)
# setup modbus client for siemens s7 PLC
modclient = S7ModbusClient(host,port)
rospy.loginfo("Setup complete")
cmd=rospy.Subscriber('cmd_vel', Twist, cmdgelen)
time.sleep(2)
pub = rospy.Publisher("modbus_wrapper/output",HoldingRegister,queue_size=500)
publishera()
Please format your question properly, especially the code you've included.
+100 for not posting a screenshot of an editor, but as-is, the code is very hard to read.
Copy-paste your code again, select all the lines and press
ctrl+k
(or click the Preformatted Text button (the one with101010
on it)).After you've done this, we can re-open.
Thank you your guidance. I hope this time is correct . After this warning i will be careful about. Thanks again