Live mapping and better maps!
I added a live mapping feature. While cleaning, every 10 position readings are added to a scatter plot. The image of the plot is then published by MQTT.
I tried omitting saving to file and using an io-buffer instead, but I can't get this to work. This means it's not fast enough to send an updated plot every 500ms (the update-rate of positional data). I have it set now to post every 10 datapoints. So an updated plot every 5 secs.
plt.scatter(poseJson['X'],poseJson['Y'], color='r')
#-every 10 seconds records?
liveFeedCounter=liveFeedCounter+1
if liveFeedCounter == 10:
liveFeedCounter = 0
#img_buf = io.BytesIO()
#plt.savefig(img_buf, format='png')
#poseLogImageString = img_buf.read()
plt.savefig("live.png",format='png')
poseLogImageFile = open("live.png", "rb")
poseLogImageString = poseLogImageFile.read()
poseLogImageByteArray = bytes(poseLogImageString)
client.publish(MQTT_Config.HA_name + "/camera/" + DeviceName + "_" + DeviceID + "/map",poseLogImageByteArray,0,False)
poseLogImageFile.close()
#img_buf.close()
Also, the map created after a clean is much better. It's an overlay of a 2D histogram to show where the roomba has spent most of it's time. Then the positional points as scattered dots to indicate speed. And a line to get a sense of the completed track. This is all presented with equal axis to not distort the map and with a grid to get a sense of scale.
The plot should also be cleared in between cleaning cycles with plt.close()
plotData = np.array(plotData)
x,y = plotData.T
plt.close()
plt.hist2d(x,y, bins=25, cmin=1, norm=col.LogNorm())
#plt.plot(x,y,linestyle='--')
plt.scatter(x,y,marker='.',color='r')
plt.plot(x,y,linestyle='-',color='b')
#OutlineX = []
#OutlineX = []
#plt.plot(OutlineX,OutlineY)
plt.axis('equal')
plt.grid(True)
#plt.xlim([-2000, 8000])
#plt.ylim([-5000, 5000])
plt.savefig(poseLogFilename + ".png", format='png')
plt.close()
plt.axis('equal')
plt.grid(True)
# - publish map
poseLogImageFile = open(poseLogFilename+".png", "rb")
poseLogImageString = poseLogImageFile.read()
poseLogImageByteArray = bytes(poseLogImageString)
client.publish(MQTT_Config.HA_name + "/camera/" + DeviceName + "_" + DeviceID + "/map",poseLogImageByteArray,0,False)
Full python script is uploaded to files.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.