Why is my program not responding after running this code?

edited February 2017 in Questions about Code

Why is my program not responding after running this code? I have to kill it in Task Manager after I run it, very inconvenient. I think it is because of how I am filling the array. Not sure if I am doing this correctly. This program is supposed to take data from a file, fill an array, read values from the array, perform math on some of these values, and display it. I just need help with the array parts. Processing 3.2.3, Win 10. Here is my code, I included it all. I hope you don't mind. If I missed something let me know and I will include it.

Sample of the data being processed:

24,38010,1.

23,38011,1.

27,38012,1.

25,38013,1.

27,38014,1.

27,38016,2.

28,38017,1.

27,38018,1.

Code:

    /*
    
     Future Changelog:
     (Overall: Clean code, good code practice, comments)
     Multi line buffer reading (Read only)(V.1):
     Dynamic grid size(V.1.2)
     Metric or Imperial detection and displaying(V.2)
     On screen buttons(V.1.1)
     File selection?
     Replay function(V.1.2)
     Zoom function(V.1.3)
     */
    
    //File
    PrintWriter output;
    BufferedReader reader;
    int [] v = {};
    int [] t = {};
    int [] s = {};
    //int [][][] data = {{v}, {t}, {s}};
    //Data
    float time = 0;             // horizontal position of the graph
    float oldtime = 0;
    float thrust = 0;             // vertical position of the graph
    float oldthrust = 0;
    //Running average
    float ravg = 0;
    float s1 = 0;
    float s2 = 0;
    float s3 = 0;
    float s4 = 0;
    float s5 = 0;
    float s6 = 0;
    float s7 = 0;
    float s8 = 0;
    float s9 = 0;
    float s10 = 0;
    float s11 = 0;
    float s12 = 0;
    float s13 = 0;
    float s14 = 0;
    float s15 = 0;
    float s16 = 0;
    float ravgOut = 0;
    float ravgOutold = 0;
    //Calculation
    float maxmem = 0;
    float total = 0;
    float avgval = 0;
    float totimp = 0;
    float IsP = 0;
    float mclass = 0;
    //Constants
    float cval = 0.142;
    float cval2 = 7.04225352112676;
    float grain = 0.032;
    float gconst = 9.81;
    //Misc.
    boolean pause;//Playback pause/play
    int speed = 0;//Playback speed
    int modclass = 0;//Motor Class modifier
    char txtclass = '!';//Motor Class character
    //graph
    int gry = 0;
    int grx = 0;
    
    void setup()
    {
      //Screen size
      fullScreen();
      //size(1024, 768);
      surface.setResizable(true);
      reader = createReader("sim.txt");  // Open the file
      //Text formating, backgound color, framerate
      textFont(createFont("Sans", 48));
      background(#AAAAAA); 
      frameRate(1000000);
      //delay for windowed mode
      delay(3000);
    }
    
    void draw()
    {
      Fileparse();
      if (time < 1)
      {
        graph();
      }
      //Runs when not paused
      if (!pause)
      {
    
        if (thrust != 0)
        {
          //Function calls
          //txt();
          thrust();        
          smoothing();
          maxval();
          avgval();
          totimp();
          IsP();
          mclass();
          time();
        }
      }
    }
    
    
    void Fileparse()
    {
      String line;
      String[] pieces;
      boolean trigger = false;
      while (trigger == false )
      {
        //Exception error handling (Unrecognized char etc.)
        try
        {
          line = reader.readLine();
        }
        catch (IOException e)
        {
          e.printStackTrace();
          line = null;
        }
        //If end of file, 
        if (line == null)
        {
          //txt();
          // Stop reading because of an error or file is empty
          trigger = true;
        }
        //File parsing (String to float)
        else
        {
          //if (thrust != 0)
          //{
          //  oldthrust = thrust;
          //}
          pieces = splitTokens(line, ",.");
          for (int i = 0; i < pieces.length; i++)
          {
            if (i==0)
            {
              v = append(v, int(pieces[i]));//*cval
            }
            if (i==1)
            {
              t = append(t, int(pieces[i]));//-38010
            }
            if (i==2)
            {
              s = append(s, int(pieces[i]));
            }
            print("v");
            printArray(v);
            print("t");
            printArray(t);
            print("s");
            printArray(s);
            println();
            delay(50);
          }
        }
      }
    }
    
    void keyReleased()//Keyboard function
    {
      if (key== 27) exit();
      if (key== 32) pause = !pause;
      if (key==',') speed = speed + 10;
      if (key=='.') speed = 0;
      if (key=='s') saveFrame(month() + "_" + day() + "_" + year() + "_" + "####.jpeg");
    }
    
    void time()
    {
      //Time display
      fill(#000000);
      stroke(#000000);
      text(time*.001, 250, 55);
      oldtime = time;
      //Delay for controlling playback speed  (Usually 5(ms))
      delay(speed);
    }
    
    void thrust()
    {
      stroke(#000000);
      strokeWeight(1);
      line(oldtime+30, height-(oldthrust/cval)-30, time+30, height-(thrust/cval)-30);
      //Raw values display
      fill(#000000);
      stroke(#000000);
      textSize(25);
      text(thrust, 50, 55);
    }
    
    void smoothing()//Running average
    {
      ravgOutold = ravgOut;
      ravg = thrust;
      s16 = s15;
      s15 = s14;
      s14 = s13;
      s13 = s12;
      s12 = s11;
      s11 = s10;
      s10 = s9;
      s9 = s8;
      s8 = s7;
      s7 = s6;
      s6 = s5;
      s5 = s4;
      s4 = s3;
      s3 = s2;
      s2 = s1;
      s1 = ravg;
      ravgOut = (s16 +s15 +s14 +s13 +s12 +s11 +s10 +s9 +s8 + s7 + s6 + s5 + s4 + s3 + s2 + s1)/16;
    
      stroke(#FF0C00);
      strokeWeight(2);
      line(time+30, height-(ravgOutold/cval)-30, time+30, height-(ravgOut/cval)-30);
      //Smoothing
      fill(#FF0C00);
      stroke(#FF0C00);
      text(ravgOut, 150, 55);
    }
    
    void maxval()//Max thrust
    {
      if (thrust > maxmem)
      {
        maxmem = thrust;
      }
    }
    
    void avgval()//Average thrust
    {
      total = total + thrust;
      avgval = total/time;
    }
    
    void totimp()//Total impulse
    {
      totimp = total*.001;
    }
    
    void IsP()//Specific Impulse
    {
      IsP = (totimp/grain)/gconst;
    }
    
    void mclass()//Motor class (NAR)
    {
      mclass = (totimp-modclass)*100/modclass;
    
      if (totimp<20)
      {
        modclass = 10;
        txtclass = 'D';
      } else if (totimp<40)
      {
        modclass = 20;
        txtclass = 'E';
      } else if (totimp<80)
      {
        modclass = 40;
        txtclass = 'F';
      } else if (totimp<160)
      {
        modclass = 80;
        txtclass = 'G';
      }
    }
    
    void graph()//Graph lines
    {
      stroke(#0000FF);
      strokeWeight(1);
      for (int i = 0; i < 1400; i = i+100)//X
      {
        line(i+30, (height-30)-(cval2*90), i+30, height-30);
        fill(#003B9B);
        stroke(#003B9B);
        textSize(20);
        if (grx < 1300)
        {
          text(grx, i+35, height-5);
        }
        grx = grx + 100;
      }
      for (float i = height; i > 100; i = i-cval2*5)//Y
      {
        line(30, i-30, 1330, i-30);
        fill(#003B9B);
        stroke(#003B9B);
        textSize(20);
        if (gry < 90)
        {
          text(gry, 5, i-35);
        }
        gry = gry + 5;
      }
    }
Tagged:

Answers

  • no frameRate, no delay please

  •     print("v");
        printArray(v);
        print("t");
        printArray(t);
        print("s");
        printArray(s);
        println();
    

    too much console output can kill sketches. comment this out and see if that helps (it's in a tight loop)

    don't call FileParse() from draw() - draw() runs 60 times a second. or 10000000 in your case. this is just asking for trouble.

  • edited February 2017

    I know I need to drop the Delay(), what am I doing wrong with framerate()? Where should I call FileParse() then?

    Also thanks for the replies. :D

  • What is the function of FileParse() ?

    Line 17 to 19, not sure why you are creating empty arrays.

    Your code doesn't run. What do you want to do with this code? it looks to me you want to plot data by first writing to a file and the reading the file?

    Kf

  • edited February 2017

    Function FileParse() contains all the parts pertaining to reading from the file, error handling, filling arrays. What you see are my attempts at filling arrays so I can then read from them. How I used to do this was I would read the file line by line, split() the data, and then plot it. This was line by line. It was suggested to me that filling an array first from the file then displaying it is better coding practice. I read data from the file into arrays then plot it. How do I fill those arrays?

    Btw, I am teaching myself this language, this accounts for why it might be confusing. There are some loose lines of code, sorry. :P

    Thanks for the reply!

  • So I think that will solve the problem just by looking at it. But I am not quite sure how the code works. I think it is creating a string array class containing strings (A 2d array?). I have never worked with objects or classes per se.

    Thanks for replying GoToLoop.

  • @Skybird0

    I suggest before you attempt to figure this code out, yu should understand the basics of Processing. The reference is good for you: https://processing.org/reference/ They provided basic details of how every part of the code works. That should be your starting point. Familiarize with the provided functions before you make any more changes to your program. A second step is to have a look at the examples provided: https://processing.org/examples/ For you, I recommend: Structure, Input, Data, Arrays, Advanced Data, File IO.

    It is not recommended to save the data in a file and then load each line to plot it on the draw() function. Instead, you could keep all your data in an array and plot it directly from that object. Then, before you exit your program, you could save the data in a file. This is one approach, mind that i don't know any other details of your application or its purpose.

    By the way, there are also tutorials: https://processing.org/tutorials/ and in your case, this one is strongly recommended as it will make your life easier: https://processing.org/tutorials/arrays/

    Kf

  • Thanks @kfrajer. I understand some of the basics of processing, what I don't understand I look up in the reference. I love the reference. I will look at those links, thanks. Yeah I don't need to save values to a file, I already made a program for that. :D

Sign In or Register to comment.