Captains log, stardate...
Nice! logging is done. I'll get csv-logfiles on every clean cycle.
poseLogFilename = 'poseDummy'
poseLogFile = open(poseLogFilename + ".csv", 'w')
poseLogWriter = csv.writer(poseLogFile)
rawLogFilename = 'rawDummy'
rawLogFile = open(rawLogFilename + ".csv", 'w')
rawLogWriter = csv.writer(rawLogFile)
#MQTT callback functions
def on_message(client, userdata, message):
data = message.payload
global poseLogFilename
global poseLogFile
global poseLogWriter
global rawLogFilename
global rawLogFile
global rawLogWriter
if message.topic == MQTT_Config.HA_name + "/button/" + DeviceName + "_" + DeviceID + "/set":
command=data.decode("utf-8")
# start logging
if command == "Clean":
# open logfile
print("Started cleaning cycle")
poseLogFile.close()
poseLogFilename = "./Roomba/Logs/" + datetime.datetime.now().isoformat('_', 'seconds') + "_pose"
poseLogFile = open(poseLogFilename + ".csv", 'w')
poseLogWriter = csv.writer(poseLogFile)
rawLogFile.close()
rawLogFilename = "./Roomba/Logs/" + datetime.datetime.now().isoformat('_', 'seconds')+ "_raw"
rawLogFile = open(rawLogFilename + ".csv", 'w')
rawLogWriter = csv.writer(rawLogFile)
if message.topic == MQTT_Config.HA_name + "/device_automation/" + DeviceName + "_" + DeviceID + "/event_DoneCleaning":
event=data.decode("utf-8")
# start logging
if event == "done cleaning":
# close log
print("Cleaning cycle is done. Save logfile and start postprocess")
poseLogFile.close()
poseLogFile = open('poseDummy.csv', 'w')
poseLogWriter = csv.writer(poseLogFile)
rawLogFile.close()
rawLogFile = open('rawDummy.csv', 'w')
rawLogWriter = csv.writer(rawLogFile)
# plot image from CSV file.
if message.topic == MQTT_Config.HA_name + "/device/roomba/raw":
rawJson=json.loads(data.decode("utf-8"))
# start logging
#print(rawJson['mEL'])
#print(rawJson['mER'])
#print(rawJson['rTh'])
#print(rawJson['Xacc'])
#print(rawJson['Yacc'])
#print(rawJson['Yaw'])
if not rawLogFile.closed:
rawLogData = [rawJson['mEL'],rawJson['mER'],rawJson['rTh'],rawJson['Xacc'],rawJson['Yacc'],rawJson['Yaw']]
rawLogWriter.writerow(rawLogData)
else:
print("Raw logfile was closed")
if message.topic == MQTT_Config.HA_name + "/device/roomba/pose":
poseJson=json.loads(data.decode("utf-8"))
# start logging
#print(poseJson['X'])
#print(poseJson['Y'])
#print(poseJson['rTh'])
if not poseLogFile.closed:
poseLogData = [poseJson['X'],poseJson['Y'],poseJson['rTh']]
poseLogWriter.writerow(poseLogData)
else:
print("Pose logfile was closed")
Only thing left now is to plot the pose-log to an image-file and post that with MQTT....
Anyone who knows how??
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.