#!/usr/bin/env python3#coding = 'utf-8'from distutils.command.config import config
from email import message
import rospy
import paho.mqtt.client as mqtt
import time
import rospy
import json
from std_msgs.msg import String
from service.msg import data,pose
num =0defrobotInfoCallback(msg):
rospy.loginfo("Subcribe roobot Info: [%s] ", msg)
conf = msg.conf
tim = msg.time
print("wait high")try:
hight_msg = rospy.wait_for_message("new_pose_hight",pose,timeout=1)
hight = hight_msg.pos_h
except:
hight =0print("get height",hight)
distance_msg = rospy.wait_for_message("pose_rfid",pose,timeout=1)
dis = distance_msg.pos_r
dis =1print("get distance",dis)
rfid =1
rfid_msg = rospy.wait_for_message("pose_rfid",pose,timeout=1)
rfid = rfid_msg.pos_r
print("get rfid",rfid)global num
num +=1
pub_tim = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
txt_name = pub_tim
Message ={"method":0,"conf":conf,"time":tim,"distance":dis,"rfid":rfid,"heigh":hight,"p_time":pub_tim}
s =str(Message)
num_s =str(num)withopen('/home/r/rosWorkSpace/xiaoche_ws/src/main/src/save/'+"num :"+ num_s +" "+ txt_name +'.txt','w', encoding='utf-8')as f:# 将dic dumps json 格式进行写入
f.write(json.dumps(s))print(s)print("---------------------------------------")defrobot_subscriber():# ROS节点初始化
rospy.init_node('save', anonymous=True)# 创建一个Subscriber,订阅名为/data_info的topic,注册回调函数robotInfoCallback
rospy.Subscriber("data_info", data, robotInfoCallback)print("waiting data")# 循环等待回调函数
rospy.spin()if __name__ =='__main__':
robot_subscriber()
sqlite 储存
建立database 在默认home文件夹下
#!/usr/bin/python3import sqlite3
conn = sqlite3.connect('deadBroiler.db')print("数据库打开成功")
c = conn.cursor()
c.execute('''CREATE TABLE DEADBROILER
(
TIME TEXT PRIMARY KEY ,
METHOD INT ,
CONF DOUBLE ,
DISTANCE INT ,
RFID INT ,
HIGH INT ,
SPEED REAL ,
AHEAD INT ,
PTIME TEXT );''')print("数据表创建成功")
conn.commit()
conn.close()
储存节点
#!/usr/bin/env python3#coding = 'utf-8'from crypt import methods
from operator import methodcaller
from turtle import speed
import rospy
import time
import rospy
import json
from std_msgs.msg import String
import sqlite3
from service.msg import data,pose
num =0defrobotInfoCallback(msg):
rospy.loginfo("Subcribe roobot Info: [%s] ", msg)
conf = msg.conf
tim = msg.time
print("wait high")try:
hight_msg = rospy.wait_for_message("new_pose_hight",pose,timeout=1)
hight = hight_msg.pos_h
except:
hight =0print("get height",hight)
distance_msg = rospy.wait_for_message("pose_rfid",pose,timeout=1)
dis = distance_msg.pos_r
dis =1print("get distance",dis)
rfid =1
rfid_msg = rospy.wait_for_message("pose_rfid",pose,timeout=1)
rfid = rfid_msg.pos_r
print("get rfid",rfid)global num
num +=1
pub_tim = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
txt_name = pub_tim
Message =[tim,mthd,conf,dis,rfid,hight,spd,pub_tim]
conn = sqlite3.connect('deadBroiler.db')
c = conn.cursor()print("数据库打开成功")
c.execute("INSERT INTO DEADBROILER (TIME,METHOD,CONF,DISTANCE,RFID,HIGH,SPEED,AHEAD,PTIME) \
VALUES(?,?,?,?,?,?,?,?,?)",( Message[0],Message[1],Message[2],Message[3],Message[4],Message[5],Message[6],Message[7],Message[8]))
conn.commit()print("数据插入成功")
conn.close()
conn = sqlite3.connect('test.db')
c = conn.cursor()print("数据库打开成功")for data in Message :
c.execute("INSERT INTO COMPANY (METHOD,CONF,TIME,DISTANCE,RFID,HIGH,SPEED,PTIME) \
VALUES(?,?,?,?,?,?,?,?)",data["method"],data["conf"],data["time"],data["distance"],(data["rfid"],data["heigh"],data["p_time"]))
conn.commit()print("数据插入成功")
conn.close()defrobot_subscriber():# ROS节点初始化
rospy.init_node('save', anonymous=True)# 创建一个Subscriber,订阅名为/data_info的topic,注册回调函数robotInfoCallback
rospy.Subscriber("data_info", data, robotInfoCallback)print("waiting data")# 循环等待回调函数
rospy.spin()if __name__ =='__main__':
robot_subscriber()
当日死鸡数据储存
#!/usr/bin/env python#coding = 'utf-8'from distutils.command.config import config
from email import message
import rospy
import paho.mqtt.client as mqtt
import time
import rospy
import json
from std_msgs.msg import String
from service.msg import data,pose
import sqlite3
import datetime
num =0
date_tim ="db"+time.strftime('%Y%m%d', time.localtime())defrobot_subscriber():global date_tim
# 创建一个Subscriber,订阅名为/data_info的topic,注册回调函数robotInfoCallback
date_object = datetime.date.today()
conn = sqlite3.connect('/home/yong/RosWorkSpace2/two_ws/src/main/src/deadBroiler.db')print("数据库打开成功")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS [%s]
(
NUMBER INT,
TIME TEXT ,
METHOD INT ,
CONF DOUBLE ,
DISTANCE INT ,
RFID INT ,
HIGH INT ,
SPEED REAL ,
AHEAD INT ,
PTIME TEXT );'''% date_tim )print("数据表创建成功")
conn.commit()
conn.close()global num
##########
num +=1
conf =0
tim = datetime.date.today()
hight =0
dis =0
ahd =2
spd =0
rfid =0
method =0
pub_tim = tim
###########Message = {"method":0,"conf":conf,"time":tim,"distance":dis,"rfid":rfid,"hight":hight,"ahead":ahd,"speed":spd,"p_time":pub_tim}
Message =[num,tim,method,conf,dis,rfid,hight,spd,ahd,pub_tim]
conn = sqlite3.connect('/home/yong/RosWorkSpace2/two_ws/src/main/src/deadBroiler.db')
c = conn.cursor()print("数据库打开成功")#try:
table_name =date_tim
print(table_name)
c.execute('INSERT INTO '+table_name+' (NUMBER,TIME,METHOD,CONF,DISTANCE,RFID,HIGH,SPEED,AHEAD,PTIME) \
VALUES(?,?,?,?,?,?,?,?,?,?)',( Message[0],Message[1],Message[2],Message[3],Message[4],Message[5],Message[6],Message[7],Message[8],Message[9]))
conn.commit()
conn.close()print("数据插入成功")if __name__ =='__main__':
robot_subscriber()