Ask Your Question

Logging multiple-ultrasound data on a MySQL server with (arduino-ROS-Python) framework

asked 2016-07-19 19:20:52 -0500

angshumanG gravatar image

updated 2016-07-20 11:23:59 -0500

ahendrix gravatar image

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
  con = mdb.connect('', 'root', 'Fr24clemson', 'autonomous');
  cur = con.cursor()
    print "Error opening serial port."

    resp = ""
droptable="DROP TABLE autonomous.ultrasonic_sensor_data"
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)"

      def ultrasoundFL_callback(data):
      def ultrasoundFR_callback(data):
      def ultrasoundRL_callback(data):
      def ultrasoundRR_callback(data):
          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
          print "Rows inserted: %s" % cur.rowcount
          #time.sleep(1)#set to whatever
          resp = ""
        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")

if __name__=='__main__':

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/", line 720, in _invoke_callback cb(msg) File "", 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

edit retag flag offensive close merge delete


You code is hard to read because it isn't formatted well; can you adjust the indentation so that it matches PEP-8 style?

ahendrix gravatar image ahendrix  ( 2016-07-19 19:36:32 -0500 )edit

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.

ahendrix gravatar image ahendrix  ( 2016-07-19 19:37:51 -0500 )edit

i am getting error : NameError: global name 'FL' is not defined

angshumanG gravatar image angshumanG  ( 2016-07-19 19:43:07 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-07-20 12:14:00 -0500

ahendrix gravatar image

The variable that you create for each sensor is only valid inside its callback.

Your callbacks will be called asynchronously, whenever there is new data on a topic. This means that you can't assume that callbacks will be called in order.

Instead of trying to write all four reading to your database in the callback for one sensor (where you only have data from one sensor), I suggest a different approach. You have a few choices:

  • Gather the data into a single message with all of your sensor readings on the publisher, so that you only need to subscribe to one message
  • Store the data from each sensor separately in the database, so that you don't need to collect all four sensor readings
  • Use an Approximate Time Synchronizer from the message_filters package to synchronize your sensor readings so that you get a single callback with all four sensor readings. The message_filters python docs may be useful.
  • Build your own synchronization mechanism
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-07-19 19:10:31 -0500

Seen: 284 times

Last updated: Jul 20 '16