arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.15k stars 7.01k forks source link

Serial Plotter scaling issue when plotting only negative numbers #4292

Closed vicnevicne closed 8 years ago

vicnevicne commented 8 years ago

Hi,

It seems that when the sketch prints only numbers <= 0, the plotter has a scaling bug that causes only a part of the graph to be displayed.

Here is a video showing the issue with a simple case : http://screencast.com/t/fiU63ujP34wH

This is reproduced with the hourly build of 2015/12/11 03:44

Here is the above sketch in text:

#define PAUSE_MS 5

void setup() {
  Serial.begin(115200);
}

void loop() {

  // 20 positive sawtooth : OK
  for (int n = 0; n < 20; n++) {
    for(int value = 0; value < 100; value++) {
      Serial.println(value);
      delay(PAUSE_MS);
    }
  }

  // 20 negative sawtooth : NOK
  for (int n = 0; n < 20; n++) {
    for(int value = 0; value < 100; value++) {
      Serial.println(-value);
      delay(PAUSE_MS);
    }
  }
}

Plotter is a great feature by the way, and multi-trace brings it to a new level :-) Keep on the good work !

vicnevicne commented 8 years ago

I checked the code and it appears the cause is a typo, and the fix is just one char :-)

in processing.app.SerialPlotter.GraphPanel#computeBounds, at line 90, *the "/" should be a ""**, so

    double bMin = g.buffer.min() / 2.0;

should read :

    double bMin = g.buffer.min() * 2.0;

In the current situation, instead of having a scale twice as big as the graph, the scale is only half the graph in negative values. The fact that the bug shows up only when all values are <= 0 is a fortunate side-effect of the Tick computing.

Can someone apply that fix on the trunk ?

Kind regards,

Vicne

vicnevicne commented 8 years ago

Err. Did more tests, and the universal fix was not so trivial. In fact, division indeed makes sense for the minimum only if it is positive. Same for the maximum if it is negative. So here is the fix I propose (note I moved that calculation after the loop so it is performed only once and not for each graph) :

      for(Graph g : graphs) {
        double bMin = g.buffer.min();
        double bMax = g.buffer.max();
        minY = bMin < minY ? bMin : minY;
        maxY = bMax > maxY ? bMax : maxY;
      }
      minY = minY * ((minY<0)?2:0.5);
      maxY = maxY * ((maxY<0)?0.5:2);

KR, Vicne

vicnevicne commented 8 years ago

See pull request above for an updated sketch and comparison video.