ROBOTC.net forums
http://www.robotc.net/forums/

How to draw Bar charts on NXT with Robotc
http://www.robotc.net/forums/viewtopic.php?f=1&t=5770
Page 1 of 2

Author:  john6672 [ Sat Apr 27, 2013 3:56 pm ]
Post subject:  How to draw Bar charts on NXT with Robotc

Hello,

I am working on a code where i have to collect sensor readings for (light, sound, sonar) every second and display a graph at the end with min and max values.
I also have to display total readings which was easy. I have put together all the code except for the graph part. I would really appreciate if anyone would help me out on this.

Here my code so far :

Code:
task read()
{
   int light_readings[60];
  int sound_readings[60];
  int sonar_readings[60];
  int lightcount=0;
  int soundcount=0;
  int sonarcount=0;
  int tot=0;
  int maxLight=SensorValue[light];
  int minLight=SensorValue[light];
  int maxSound=SensorValue[sound];
  int minSound=SensorValue[sound];
  int maxSonar=SensorValue[sonar];
  int minSonar=SensorValue[sonar];
  ClearTimer(T1);

  while (time1[T1] < 10000)
  {
     if (SensorValue[light]<minLight)
    {
      minLight=SensorValue[light];
    }
     if (SensorValue[light]>maxLight)
    {
         maxLight=SensorValue[light];
    }
    if (SensorValue[sound]<minSound)
    {
         minSound=SensorValue[sound];
      }
      if (SensorValue[sound]>maxSound)
    {
         maxSound=SensorValue[sound];
    }
    if (SensorValue[sonar]<minSonar)
      {
        minSonar=SensorValue[sonar];
      }
      if (SensorValue[sonar]>maxSonar)
      {
         maxSonar=SensorValue[sonar];
     }

      light_readings = SensorValue[light];
     lightcount=lightcount +1;
     sound_readings = SensorValue[sound];
     soundcount= soundcount +1;
     sonar_readings = SensorValue[sonar];
     sonarcount= sonarcount +1;
     tot= lightcount + soundcount + sonarcount;
     wait1Msec(1000);

     while (time1[T1] >= 10000)
     {
        motor[motorB] = 0;
       motor[motorC] = 0;
       nxtDisplayString(1, "Light:", minLight, maxLight);
       nxtDisplayString(2, "Min=%i Max=%i", minLight, maxLight);
       nxtDisplayString(3, "Noise:", minSound, maxSound);
       nxtDisplayString(4, "Min:%i Max:%i", minSound, maxSound);
       nxtDisplayString(5, "Distance:", minSonar, maxSonar);
       nxtDisplayString(6, "Min:%i Max:%i", minSonar, maxSonar);
       nxtDisplayString(7, "Tot Readings:%i", tot);
    }
}

Author:  mightor [ Sat Apr 27, 2013 4:06 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

If you download my drivers, you'll find some progams that can do this. I think they're the mindsensors-lineleader-test*.c programs. One of the hitechnic-colourv2-test*.c examples has it, too, I believe.

= Xander

Author:  john6672 [ Sat Apr 27, 2013 4:20 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

mightor wrote:
If you download my drivers, you'll find some progams that can do this. I think they're the mindsensors-lineleader-test*.c programs. One of the hitechnic-colourv2-test*.c examples has it, too, I believe.

= Xander



I will check it out. thanks!!

Author:  john6672 [ Sat Apr 27, 2013 5:08 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

What I have understood so far is I have to use "nxtFillRect" function to draw the bar chart. So, let say I want to draw a graph for light sensor values, say maximum value is 85.

So this is how i would implement it :

nxtFillRect (10, 85, 70, 10);

wait1Msec(10000);

Correct me if am wrong and also tell me how i can substitute the actual max value of light sensor instead of 85?

Author:  magicode [ Sat Apr 27, 2013 5:58 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

The variables can be used just like numbers. You can do: nxtFillRect (10, maxLight, 70, 10);

Author:  john6672 [ Sat Apr 27, 2013 6:21 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

magicode wrote:
The variables can be used just like numbers. You can do: nxtFillRect (10, maxLight, 70, 10);


Oh that makes it easy :) I have one more question, how would i create a automatic calibration mechanism to calibrate the light sensor using the buttons on the robot brick?

Author:  mightor [ Sun Apr 28, 2013 1:26 am ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

You can do that with my drivers as well. Just take a look at lego-light* :)

= Xander

Author:  john6672 [ Mon Apr 29, 2013 10:31 am ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

Graph is working now I want to sort the readings of all three sensors in acending order and show a different graph for them. My question is how would i sort readings which are in array?

Author:  mightor [ Mon Apr 29, 2013 12:37 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

Check out my tutorial: http://botbench.com/blog/2012/07/21/tut ... your-data/

= Xander

Author:  john6672 [ Mon Apr 29, 2013 1:48 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

I used the exact same approach however the "sorted graphs" are not being displayed. This is the code so far :

Code:
#pragma config(Sensor, S1,     touch,          sensorTouch)
#pragma config(Sensor, S2,     light,          sensorLightActive)
#pragma config(Sensor, S3,     sound,          sensorSoundDBA)
#pragma config(Sensor, S4,     sonar,          sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
typedef struct {
int x;
int y;
}pixel;

void clear()
{

        nxtDisplayClearTextLine(1);
        nxtDisplayClearTextLine(2);
        nxtDisplayClearTextLine(3);
        nxtDisplayClearTextLine(4);
        nxtDisplayClearTextLine(5);
        nxtDisplayClearTextLine(6);
        nxtDisplayClearTextLine(7);

}


task read()
{
   int light_readings[60];
  int sound_readings[60];
  int sonar_readings[60];
  int lightcount=0;
  int soundcount=0;
  int sonarcount=0;
  int tot=0;
  int maxLight=SensorValue[light];
  int minLight=SensorValue[light];
  int maxSound=SensorValue[sound];
  int minSound=SensorValue[sound];
  int maxSonar=SensorValue[sonar];
  int minSonar=SensorValue[sonar];
  ClearTimer(T1);

  while (time1[T1] < 20000)
  {
     if (SensorValue[light]<minLight)
    {
      minLight=SensorValue[light];
    }
     if (SensorValue[light]>maxLight)
    {
         maxLight=SensorValue[light];
    }
    if (SensorValue[sound]<minSound)
    {
         minSound=SensorValue[sound];
      }
      if (SensorValue[sound]>maxSound)
    {
         maxSound=SensorValue[sound];
    }
    if (SensorValue[sonar]<minSonar)
      {
        minSonar=SensorValue[sonar];
       }
       if (SensorValue[sonar]>maxSonar)
      {
         maxSonar=SensorValue[sonar];
     }

      light_readings[lightcount] = SensorValue[light];
     lightcount=lightcount +1;
     sound_readings[soundcount] = SensorValue[sound];
     soundcount= soundcount +1;
     sonar_readings[sonarcount] = SensorValue[sonar];
     sonarcount= sonarcount +1;
     tot= lightcount + soundcount + sonarcount;
     wait1Msec(1000);

     if(motor[motorB]<0)
     {
        wait10Msec(150);
      }
      while (time1[T1] >= 20000)
     {
        motor[motorB] = 0;
       motor[motorC] = 0;
       nxtDisplayString(1, "Light:", minLight, maxLight);
       nxtDisplayString(2, "Min=%i Max=%i", minLight, maxLight);
       nxtDisplayString(3, "Noise:", minSound, maxSound);
       nxtDisplayString(4, "Min:%i Max:%i", minSound, maxSound);
       nxtDisplayString(5, "Distance:", minSonar, maxSonar);
       nxtDisplayString(6, "Min:%i Max:%i", minSonar, maxSonar);
       nxtDisplayString(7, "Tot Readings:%i", tot);

       if(nNxtButtonPressed == 1) // Unsorted Light Graph
      {
        wait10Msec(10);
        while(nNxtButtonPressed == -1)
        {
          while(true)
          {
             clear();

            pixel p[60];
            int i;
            for(i=0; i<60; i++)
            {
              p[i].x = i;
              p[i].y = light_readings[i]/2;
            }
            for(i=0; i<60; i++)
            {
              nxtDrawLine(p[i].x,0,p[i].x, p[i].y);
            }
            wait1Msec(100);
   


   if(nNxtButtonPressed == 1) // sorted Light Graph
            {
              wait10Msec(10);
              while(nNxtButtonPressed == -1)
              {
                while(true)
                {
                  clear();

      while (true)
          {

              for(int j = 0; j < (lightcount - 1); j++)
           {
                if (light_readings[j] > light_readings[j+1])
                 {
                      int temp;
      
                    temp = light_readings[j];

                      light_readings[j] = light_readings[j+1];

                    light_readings[j+1] = temp;
                   


                  pixel p[60];
                  int i;

                  for(i=0; i<60; i++)
                  {
                    p[i].x = i;
                    p[i].y = sound_readings[j];
                  }

                  for(i=0; i<60; i++)
                  {

                   nxtDrawLine(p[i].x,0,p[i].x, p[i].y);
                  }

                  wait1Msec(100);






           if(nNxtButtonPressed == 1) // Unsorted sound Graph
            {
              wait10Msec(10);
              while(nNxtButtonPressed == -1)
              {
                while(true)
                {
                   clear();

                  pixel p[60];
                  int i;
                  for(i=0; i<60; i++)
                  {
                    p[i].x = i;
                    p[i].y = sound_readings[i];
                  }
                  for(i=0; i<60; i++)
                  {
                    nxtDrawLine(p[i].x,0,p[i].x, p[i].y);
                  }
                  wait1Msec(100);

                  if(nNxtButtonPressed == 1) // Unsorted sonar Graph
                  {
                    wait10Msec(10);
                    while(nNxtButtonPressed == -1)
                    {
                      while(true)
                      {
                         clear();

                        pixel p[60];
                        int i;
                        for(i=0; i<60; i++)
                        {
                          p[i].x = i;
                          p[i].y = sonar_readings[i]/6;
                        }
                        for(i=0; i<60; i++)
                        {
                          nxtDrawLine(p[i].x,0,p[i].x, p[i].y);
                        }
                        wait1Msec(100);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

task main()
{
   wait10Msec(100);
  ClearTimer(T1);

  StartTask( read );
  while (time1(T1)<=20000)
  {
    int ranNum1 = 40-random(30);
    int ranNum2 = 40-random(30);
//Sonar Part
    if(SensorValue[sonar]<30)
    {
       motor[motorB] = -30;
      motor[motorC] = -30;
      wait10Msec(100);
      motor[motorB] = -30;
      motor[motorC] = 30;
      wait10Msec(100 - random(50));
    }
//Light Part
    if (SensorValue[light] >= 35 || SensorValue[light] <30)
    {
      motor[motorB] = ranNum1;
      motor[motorC] = ranNum2;
      wait1Msec(1);
    }

    else if(SensorValue[light] < 35)
    {
      wait10Msec(4);

      if(SensorValue[light] >= 31 && SensorValue[light] < 35)
      {
        motor[motorB] = -30;
        motor[motorC] = -30;
        wait10Msec(100);
        motor[motorB] = -30;
        motor[motorC] = 30;
        wait10Msec(100 - random(50));
      }
    }
  }
}


I am totally lost in the code :?

Author:  mightor [ Mon Apr 29, 2013 3:04 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

Put the sorting code in a separate function, same applies to the displaying code. This code has so many levels, I fear I will get lost without a map!

I will doa little clean up and paste the code in here. Bear with me!

= Xander

Author:  mightor [ Mon Apr 29, 2013 3:20 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

I am sorry, but your code is such a mess, I cannot fix this. Here is some advise:
  • Throw this code out
  • on a piece of paper, write down EXACTLY what you want your code to.
  • Break those things down into separate functions
= Xander

Author:  NeXT-Generation [ Mon Apr 29, 2013 3:22 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

This is messy even by my standards... :shock:

I'd suggest doing what Xander says, because used as I am to messy code, I can't figure it out either.

Author:  mightor [ Mon Apr 29, 2013 3:27 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

This code actually generated an error I had never seen before:
Code:
**Severe*:Out of memory space for nested { .. } blocks or procedures (1116/1024):Nested:496/496Pad:0Var/Temp:620/0
**Severe*:Out of memory space for nested { .. } blocks or procedures

I've seen plenty of errors in the 5+ years I've been using ROBOTC, but this one is definitely a doozy.

= Xander

Author:  NeXT-Generation [ Mon Apr 29, 2013 3:47 pm ]
Post subject:  Re: How to draw Bar charts on NXT with Robotc

Good lord. What a mess of errors.

Code:
File "NewFile_Template001.c" compiled on Apr 29 2013 15:46:01
**Severe*:Out of memory space for nested { .. } blocks or procedures (1360/1024):Nested:980/980Pad:0Var/Temp:380/0
**Severe*:Out of memory space for nested { .. } blocks or procedures
**Error**:Ummatched left brace '{'
**Error**:Ummatched left brace '{'
**Error**:Ummatched left brace '{'
**Error**:Ummatched left brace '{'
**Error**:Ummatched left brace '{'
**Error**:Ummatched left brace '{'
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**Error**:Task 'main' is not defined at global scope level
**Error**:Tasks must be defined at main scope level
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Bad Scope level -- internal error
**FATAL**:Too many scope levels
**FATAL**:Bad Scope level -- internal error
**FATAL**:Too many scope levels
**FATAL**:Too many scope levels
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**FATAL**:Bad Scope level -- internal error
**Error**:Expected->'}'. Found 'EOF'

Compile Statistics:  (NewFile_Template001.c)
    0.013 Total Compile Time (seconds)
        0 Total code bytes, (after 0 bytes removed during optimization)
        0 Constant Pool Size (in bytes)
        0 Used memory locations (of 15000), 2 Tasks, 1 Procedures
      264 User Source file lines,     1,292 tokens
    7,059 System Include file lines, 30,862 tokens
       70 Errors, 0 Warnings, 0 Info Messages
                                 CPU     ......Lines/sec.....
                             Seconds         Total     Source
     Scanner/parsing: Setup    0.007     1,100,820     39,685
    Scanner/parsing: System    0.000    15,426,221    556,127
Scanner/parsing: User Files    0.005     1,530,485     55,175
            Code Generation    0.000             0          0
              Code Optimize    0.000             0          0
                      Total    0.013       572,893     20,653
       43 Total symbols added to symbol table
    1.099 Avg hash bucket depth (1.0 is best possible)


This really needs to just be completely re-written.

Page 1 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/