View unanswered posts | View active topics It is currently Fri Dec 19, 2014 2:33 am






Reply to topic  [ 7 posts ] 
Scanning and displaying on NXT screen help! 
Author Message
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Scanning and displaying on NXT screen help!
Hello guys,

I started doing my own code for a scanner which is based on a Sudoku solver design.
The Idea is I want to scan a picture or number using light sensor, storing them in an array then displaying the scanned item on the screen using pixels.

I started so far doing some functions which will make the robot go forward and make a single scan, going a bit forward and doing another scan etc.

The problem i'm facing is the scanning, saving in an array, and displaying the scanned pixels/item on NXT screen

Code:
#pragma config(Sensor, S3,     ,               sensorLightActive)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void moveforward()
{
   motor[motorA] = 10;
   wait1Msec(10);
   motor[motorA] = 0;
   wait1Msec(10);
}
void scan()
{
   motor[motorB] = 20;
   wait1Msec(500);
   motor[motorB] = 0;
   wait1Msec(10);
   motor[motorB] = -20;
   wait1Msec(1000);
   motor[motorB] = 0;
   wait1Msec(10);
   motor[motorB] = 20;
   wait1Msec(500);
   motor[motorB] = 0;
   wait1Msec(10);
   moveforward();
   wait1Msec(10);
}
void lightdata(){
   int values[100];
   {
      for(int i = 0; i < 100; i++){
         values[i] = SensorValue[values];
         wait1Msec(10);
      }
   }
}
task main()
{

}


Any ideas or help
by the way, motor speed and waits are not accurite yet
Sayed Ahmed


Wed Dec 05, 2012 12:04 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 619
Post Re: Scanning and displaying on NXT screen help!
You are definitely on the right track. Think about this in plain English though, and it might help you figure out the program flow:

1) Start a predetermined point (start point).
2) Move forward a set amount of distance.
3) Check the light sensor to see if it detects the line/object/shape
-If it does, the value stored in the array should be a 1 (or 2, 3, 4, 0; it's whatever number you want to designate a line detection. Generally, 1 is the easiest to use)
-If it doesn't, the value stored in the array should be a 0 (or a value different from the 'line detected' value; again, a 0 works nicely for this purpose)
4) If a line/object/shape is detected, write a pixel to the NXT screen. If it isn't, do not write a pixel.
5) Repeat steps 2-4 for the entire row.
6) Repeat steps 2-5 to scan each row in their entirety.

This is one of many ways to perform the scan, store, and display actions. Basically, the program will move -> scan -> check value -> store -> display, and will repeat each process until the end of the row. Then, the robot will have to turn or move down to the next row (depending on how the robot is set up) and repeat the move -> scan -> check -> store -> display process until the end of that row, and so on until it reaches the maximum number of rows.

Move - Recommend using the motor commands with encoders (nMotorEncoderTarget[] in particular) to ensure your robot moves only as far as it needs to.

Next Row - Once you reach certain the end of the row, you will need to move the robot to the next row; there are several ways to do this depending on the exact robot setup.

Scan - The SensorValue() command will return the value of whichever sensor is specified; you can then store this value in an array if you'd like and check the array values later, or check the value first and store an appropriate, corresponding (light or dark) value into the array.

Check Value - A simple if/else statement will suffice here; if the SensorValue is above a threshold, store a certain value into the array; if it's below the threshold, store a different value in the array.

Store - This is where it will get a bit tricky; since you are dealing with both rows and columns, you will probably need to do a two-dimensional array. For example, 5 rows with 10 columns would need a two dimensional array such as:
Code:
int foo[5][10];

Display - You may want to do this at the end, once all of the values are collected. You will need to run through each array location individually and display either a pixel or no pixel depending on what value is stored in the array's location.

Tips: To run through all the values of an array, you'll usually want to utilize a 'for' loop:

Code:
//Assuming you have a 10-value array
int foo[10];

for (int a=0; a < 10; a++)
{
foo[a]=SensorValue(lightSensor);
//move commands
}


To run through a two-dimensional array, you simply need to nest the for loops inside one another:

Code:
//Assuming you have a 5 by 10 array
int foo[5][10];

for (int a = 0; a < 5 ; a++)
{
  for(int b = 0; b < 10; b++)
  {
   foo[a][b]=SensorValue(lightSensor);
   //move commands
  }
//Next row commands
}



Beyond that, it's a simple matter of tweaking the move commands to make sure you are getting a usable reading and displaying the object on the screen, which can be done by using the nxtSetPixel command.

Hope this will help get you started, keep us posted on the progress!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Mon Dec 10, 2012 2:17 pm
Profile
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Scanning and displaying on NXT screen help!
Thank you very much,
That helps i believe, i'll be doing it now and will post you later with my progress


Wed Dec 12, 2012 2:41 pm
Profile
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Scanning and displaying on NXT screen help!
I am working to scan one row and save the values in an array of 26 elements.
i got it working however the program just doesn't stop when the scan is completed. it waits for a certain time and continue in the same direction. I need a way to stop and end the program. as well as the values of the array changes during the process of the scan. As i can see in the Global variables
value[0] = 44
value[1] = 44
etc

when a diffrent value is detected it changes to
value[0] = 66
value [1] = 66
etc
my new code is

Code:

#pragma config(Sensor, S3,     lightSensor,         sensorLightActive)
task main()
{
  wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor.
  while(true)
  {
    int value[50];
    int j;
    for (j = 0; j < 50; j++)
    {
      value[j] = SensorValue[lightSensor];
      nMotorEncoder[motorB] = 0;
      nMotorEncoderTarget[motorB] = 360;
      motor[motorB] = -10;
      nxtDisplayCenteredTextLine(3, "Sensor Value: %d", SensorValue[lightSensor]);    // Display "Sensor Value: ##"
      wait1Msec(100);    // Wait 100 milliseconds to help display correctly.
    }
   
  }
}


Looking for more help please in this part as i'm needing to work on the 2 dimensional array part as soon as i complete this one.


Thu Dec 13, 2012 3:29 am
Profile
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Scanning and displaying on NXT screen help!
I am done with the 1D array saving and scanning
now i will start in the 2D array saving scanning and moving

The current code i'm havving now

Code:
#pragma config(Sensor, S3,     lightSensor,         sensorLightActive)
task main()
{
  wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor.

  int value[25];
  int j;
  for (j = 0; j < 25; j++)
  {
   
    value[j] = SensorValue[lightSensor];
    {
      nMotorEncoder[motorB] = 0;
      nMotorEncoderTarget[motorB] = 58;
      motor[motorB] = 10;
      while(nMotorRunState[motorB] != runStateIdle)
      {
        // nothing
      }
    }
  }
}


Thu Dec 13, 2012 5:56 am
Profile
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Scanning and displaying on NXT screen help!
I have a question about the display,

since i'm going to use nxtSetPixel,
when we will say
if values[a] > threshold don't set pixel
else
set pixel.

that is a simple way to do it but how can i tell the x,y position of the pixel if we are assuming that x are rows and y are column

any help guys ?


Thu Dec 13, 2012 9:08 am
Profile
Rookie

Joined: Sun Dec 02, 2012 7:05 am
Posts: 8
Post Re: Scanning and displaying on NXT screen help!
This is the latest code i'm having now

Code:
#pragma config(Sensor, S3,     lightSensor,         sensorLightActive)
#Define thershold = 52
task main()
{
  wait1Msec(50); // The program waits 50 milliseconds to initialize the light sensor.
  int value[25][10]; // array
  int j;
  int i;
  nMotorEncoder[motorA] = 0;                    // this part of the code sets the moving forward motor as its take time when first time move
  nMotorEncoderTarget[motorA] = -350;
  motor[motorA] = -100;
  while(nMotorRunState[motorA] != runStateIdle)
  {
    // nothing
  }
  {
    for (i = 0; i < 10; i++)                  // our first loop for number of rows
    {
      nMotorEncoder[motorB] = 0;              // this part of code sets the arm to starting point
      nMotorEncoderTarget[motorB] = -1450;
      motor[motorB] = -10;
      while(nMotorRunState[motorB] != runStateIdle)
      {
        // nothing
      }
      nMotorEncoder[motorA] = 0;              // this part moves the robot forward for the rows
      nMotorEncoderTarget[motorA] = -350;
      motor[motorA] = -100;
      while(nMotorRunState[motorA] != runStateIdle)
      {
        // nothing
      }
      for (j = 0; j < 25; j++)                // this for loop for scanning and taking 25 values each row
      {
          value[j][i] = SensorValue[lightSensor];    // saves the sensor value in J
          eraseDisplay();
        if ( j < threshold)
        {
            nxtSetPixel ( (i) , (2*j+1));
        }
         
          // nxtDisplayCenteredTextLine(3, "Sensor Value: %d", SensorValue[lightSensor]);    // Display "Sensor Value: ##"
        wait1Msec(50);
        {
          nMotorEncoder[motorB] = 0;                  // this part of the code does the scanning
          nMotorEncoderTarget[motorB] = 58;
          motor[motorB] = 10;
          while(nMotorRunState[motorB] != runStateIdle)
          {
            // nothing
          }
        }
      }
    }
  }
}


the only thing I need help in is nxtSetPixel
i have only one pixel going everywhere, i need to set a pixel in each row if the value is less then the threshold which is 52. Can i have this last piece help please ?


Wed Jan 02, 2013 3:58 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.