Logging multiple-ultrasound data on a MySQL server with (arduino-ROS-Python) framework
I am trying to log the ultrasonic data from a RC car into a MySQL server. I am able to publish and subscribe to the data via rostopic. But I am having problem with the code for uploading the data which is being subscribed. I have 4 sensors published to 4 topics /ultrasoundFL,/ultrasoundFR,/ultrasoundRL,/ultrasoundRR.
import rospy
#from std_msgs.msg import String
from sensor_msgs.msg import Range
import os
from time import *
import time
import threading
import serial
import datetime
import sys
import MySQLdb as mdb
try:
con = mdb.connect('130.127.199.212', 'root', 'Fr24clemson', 'autonomous');
cur = con.cursor()
except:
print "Error opening serial port."
sys.exit(1)
resp = ""
droptable="DROP TABLE autonomous.ultrasonic_sensor_data"
cur.execute(droptable)
createtable="CREATE TABLE autonomous.ultrasonic_sensor_data (n_FL DOUBLE NULL ,w_FR DOUBLE NULL,n_RL DOUBLE NULL ,w_RR DOUBLE NULL,t_unixtime DOUBLE NULL,t_time TIMESTAMP)"
cur.execute(createtable)
try:
def ultrasoundFL_callback(data):
FL=float(data.range)
def ultrasoundFR_callback(data):
FR=float(data.range)
def ultrasoundRL_callback(data):
RL=float(data.range)
def ultrasoundRR_callback(data):
RR=float(data.range)
unixtime=datetime.datetime.now().strftime("%s")
sql = "insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(%s, %s, %s, %s, %s)" % (FL,FR,RL,RR, unixtime)
print sql
cur.execute(sql)
print "Rows inserted: %s" % cur.rowcount
con.commit()
#time.sleep(1)#set to whatever
resp = ""
except:
print sys.exc_info()[0]
def listener():
from sensor_msgs.msg import Range
rospy.init_node('serial', anonymous=True)
rospy.Subscriber("ultrasoundFL", Range, ultrasoundFL_callback)
print ("a")
rospy.Subscriber("ultrasoundFR", Range, ultrasoundFR_callback)
print ("b")
rospy.Subscriber("ultrasoundRL", Range, ultrasoundRL_callback)
print ("c")
rospy.Subscriber("ultrasoundRR", Range, ultrasoundRR_callback)
print ("c")
rospy.spin()
if __name__=='__main__':
listener()
I am getting this error: [ERROR] [WallTime: 1468975143.127478] bad callback: <function ultrasoundrr_callback="" at="" 0xb660010c=""> Traceback (most recent call last): File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback cb(msg) File "arduino_python_ros_sonar.py", line 36, in ultrasoundRR_callback sql = "insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(%s, %s, %s, %s, %s)" % (FL,FR,RL,RR, unixtime) NameError: global name 'FL' is not defined
You code is hard to read because it isn't formatted well; can you adjust the indentation so that it matches PEP-8 style?
You also say that "there is a problem" but you don't describe the problem. That makes it difficult to answer your question, if we don't know what the problem is.
i am getting error : NameError: global name 'FL' is not defined