Arduino-Makefile icon indicating copy to clipboard operation
Arduino-Makefile copied to clipboard

Additional command - make plot

Open eyJhb opened this issue 7 years ago • 0 comments

I am currently using this makefile for my project, and I really enjoy it! But there is one feature that I am missing - the serial plotter that the Arduino IDE has.

Currently I am working with RF using the Arduino, and having a visual feel of how the signals look is a great help!

I have tried doing something similar to like the Python reset script, but I am struggling with the timing, since the plotting does not look like it should!

I will not open a PR yet, as it does not function as it should.... But here is what I have, if someone would like to build upon my idea.

from __future__ import print_function
import matplotlib.pyplot as plt
import serial
import time

current_millis = lambda: int(round(time.time() * 1000))

class arduino_plotter(object):
    def __init__(self):
        self.buffer = 1000
        self.data = []
        self.start_millis = current_millis()
        self.serial_port = "/dev/ttyACM0"
        self.serial_baudrate = 9600
        self.serial_timeout = 1.0

    def validateReading(self, reading):
        try:
            float(reading)
            return True
        except ValueError:
            return False

    def splitReadings(self, line):
        line = line.replace("\n", "").replace("\r", "")

        readings = []
        for reading in line.split(","):
            if not self.validateReading(reading):
                return False
            readings.append(float(reading))
        return readings

    def appendData(self, readings):
        graphNum = 0
        for reading in readings:
            # validate if graph array already initialized
            try:
                self.data[graphNum] 
            except IndexError:
                # append list with two lists for x,y [[x],[y]]
                self.data.append([[],[]])

            self.data[graphNum][0].append(current_millis()-self.start_millis)
            self.data[graphNum][1].append(reading)

            # check if we need to remove old items and remove
            for di in range(0, len(self.data[graphNum][0])-self.buffer):
                del self.data[graphNum][0][di]
                del self.data[graphNum][1][di]

            graphNum += 1

        return True

    def updateSerial(self):
        for x in range(0,1):
            with serial.Serial(self.serial_port, self.serial_baudrate, timeout=self.serial_timeout) as ser:
                data = ser.readline()

                readings = self.splitReadings(data)
                if not readings:
                    continue
                self.appendData(readings)
        return True

    def run(self):
        plt.ion()
        graph = plt.plot([],[])[0]
        plt.ylim((-5,5))

        updateCount = 0
        while True:
            # update from our serial
            if not self.updateSerial():
                continue

            # update our x, y data
            graph.set_xdata(self.data[0][0])
            graph.set_ydata(self.data[0][1])

            # auto scale x,y axis
            # ax = plt.gca()
            # ax.relim()
            # ax.autoscale_view()

            # draw it and pause to allow draw
            if updateCount % 1 == 0:
                maxTime = self.data[0][0][len(self.data[0][0])-1]
                plt.xlim((maxTime-1000,maxTime))
                plt.draw()
                plt.pause(0.00001)
            updateCount += 1




        
x = arduino_plotter()
x.run()
# while True:
#     x.updateSerial()
#     print(x.data)

eyJhb avatar Nov 06 '18 10:11 eyJhb