To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.org.ca.rtltorontoOpen lugnet.org.ca.rtltoronto in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Organizations / Canada / rtlToronto / 9114
9113  |  9115
Subject: 
Re: My RCX is HAUNTED
Newsgroups: 
lugnet.org.ca.rtltoronto
Date: 
Fri, 12 Sep 2003 19:40:08 GMT
Viewed: 
518 times
  
In lugnet.org.ca.rtltoronto, Ka-On Lee wrote:

<snip>

If we're posting Project X code--This was my favourite codeing ever--

// X-block - Runamok Design Team
// Rick De Haan, David Koudys
// Feb. 2002

//drive definitions

//X-Axis Definitions
#define xoff       SetOutput(OUT_A,OUT_OFF);
#define xon        SetOutput(OUT_A,OUT_ON);
#define xspeed(s)  SetPower(OUT_A,s);
#define xleft      SetDirection(OUT_A,OUT_REV);SetOutput(OUT_A,OUT_ON);
#define xright     SetDirection(OUT_A,OUT_FWD);SetOutput(OUT_A,OUT_ON);
#define xclear     ClearSensor(SENSOR_1);


//Y-Axis Definitions
#define yon        SetOutput(OUT_B,OUT_ON);
#define yoff       SetOutput(OUT_B,OUT_OFF);
#define yspeed(s)  SetPower(OUT_B,s);
#define yup        SetDirection(OUT_B,OUT_FWD);SetOutput(OUT_B,OUT_ON);
#define ydown      SetDirection(OUT_B,OUT_REV);SetOutput(OUT_B,OUT_ON);
#define yclear     ClearSensor(SENSOR_2);

//Z-Axis Definitions
#define zoff       SetOutput(OUT_C,OUT_OFF);
#define zon        SetOutput(OUT_C,OUT_ON);
#define zspeed(s)  SetPower(OUT_C,s);
#define zraise     SetDirection(OUT_C,OUT_FWD);SetOutput(OUT_C,OUT_ON);
#define zlower     SetDirection(OUT_C,OUT_REV);SetOutput(OUT_C,OUT_ON);


//Setting Intergers
//int num_blocks_correct;          //current # of blocks in the correct
locations
int white_block;                 //highest maximum light value of white block
int loc;                         //where we want the grabber to be
//int sem;                         //to stop other tasks from working when we
don't want them working
int xx;                          //xx coordinate where we want to be
int yy;                          //yy coordinate we want to be
int semx;
int semy;
int BlockInClaw;


//Patterns
int pattern[16];

//start program
task main()
{
  //Defining Sensor Inputs
  SetSensorType(SENSOR_1, SENSOR_TYPE_ROTATION); SetSensorMode(SENSOR_1,
SENSOR_MODE_ROTATION);
  SetSensorType(SENSOR_2, SENSOR_TYPE_ROTATION); SetSensorMode(SENSOR_2,
SENSOR_MODE_ROTATION);
  SetSensorType(SENSOR_3, SENSOR_TYPE_LIGHT); SetSensorMode(SENSOR_3,
SENSOR_MODE_RAW);

  //Setting Variables
//  num_blocks_correct = 0;
  white_block = 780;
//  loc = 1;
  semx=0;
  semy=0;
  BlockInClaw = 0;


  //Setting Pattern
  //  Current       Desired
  //  =======       =======
  //  One exists  - one wanted  =  0
  //  One exists  - none wanted = -1
  //  None exists - one wanted  =  1
  //  None exists - none wanted =  0
  //  Don't know  - one wanted  =  2
  //  Don't know  - none wanted = -2
  pattern[1] = 2;
  pattern[2] = -2;
  pattern[3] = -2;
  pattern[4] = 2;
  pattern[5] = -2;
  pattern[6] = 2;
  pattern[7] = 2;
  pattern[8] = -2;
  pattern[9] = -2;
  pattern[10] = 2;
  pattern[11] = 2;
  pattern[12] = -2;
  pattern[13] = 2;
  pattern[14] = -2;
  pattern[15] = -2;
  pattern[16] = 2;

//Sounds for events

//  PlaySound(0);          //set on co-ordinates
//  PlaySound(1);          //Start/Stop Program
//  PlaySound(2);          //cell incorrect
//  PlaySound(3);          //cell correct
//  PlaySound(4);          //grabber dropping block - ***Error***
//  PlaySound(5);          //??

  //Starting Main Program

  PlaySound(1);          //start

  xspeed(7);yspeed(7);zspeed(7);
//  xleft;ydown;zlower;Wait(50);xoff;yoff;zoff;Wait(20);
  ClearSensor(SENSOR_1);ClearSensor(SENSOR_2);

//start movex;
//start movey;

sort ();

//  start initial_scan;
//  start dropped_block;
}

//=====================================================================================
//Sort Function
//=====================================================================================
void sort()
{
int m;
int n;

m = 1;

while ( m <= 16)
{
find_free_block ();

loc = m;
move_xy();

    //block = yes - need block = no
    if ((SENSOR_3 < white_block) && (pattern[loc] == -2))
     {
        pattern [loc] = -1;
      }
    else if ((SENSOR_3 < white_block) && (pattern[loc] == 2))
     {
       pattern [loc] = 0;
      }
    else if ((SENSOR_3 > white_block) && (pattern[loc] == 2))
     {
       pattern [loc] = 1;
      }
else if ((SENSOR_3 > white_block) && (pattern[loc] == -2))
{
           pattern [loc] = 0;
      }

   if ((BlockInClaw == 0 ) && (pattern[loc] == -1))
{
pick_up_block();
pattern[loc] = 0;
// See if we have passed a spot where we
// could dump this block
n = 1;
while ( n < m )
{
if (pattern[n] == 1 )
{
loc = n;
move_xy();
drop_block();
pattern [n] = 0;
          n = m ;
}
        n++;
}

}

if ((BlockInClaw == 1 ) && (pattern[loc] == 1))
{
drop_block();
pattern[loc] = 0;
}

m++;
}

}


//task dropped_block()
//{
//  while (true)
//  {
//    if (SENSOR_3 > 1000)
//    stop move_xy;
//    xoff;yoff;
//    sem=0;
//  }
//}



//=====================================================================================
//FindFreeBlock
//=====================================================================================

void find_free_block ()
{
int j;
j = 1;
while ( j <= 16)
{
if (pattern[j] == -1)
{
      loc = j;
      move_xy();
pick_up_block ();
pattern[j] = 0 ;
return;
}
j++;
}

}

//=====================================================================================
//PickUpBlock
//=====================================================================================
//  This function will Pickup a block
void pick_up_block ()
{
zspeed(7);zraise;Wait(120);zspeed(0);
BlockInClaw = 1;
}

//=====================================================================================
//DropBlock
//=====================================================================================
void drop_block()
{
zspeed(7);zlower;Wait(35);zoff;
BlockInClaw = 0;
}



//=====================================================================================
//Move_xy ( )
//=====================================================================================
void move_xy()
{
  //set coordinates for where grabber should be
  int k  ;
  k = 1   ;
  while (k <= loc)
  {
  PlaySound(0);
  Wait(10);
  k++;
  }
  PlaySound(1);

  semx=3;semy=3;
  if ((loc >= 1) && (loc <= 4))
  {
    yy=1;
  }
  if ((loc >= 5) && (loc <= 8))
  {
    yy=2;
  }
  if ((loc >= 9) && (loc <= 12))
  {
    yy=3;
  }
  if ((loc >= 13) && (loc <= 16))
  {
    yy=4;
  }
  if ((loc == 1) || (loc == 8) || (loc == 9) || (loc == 16))
  {
    xx=1;
  }
  if ((loc == 2) || (loc == 7) || (loc == 10) || (loc == 15))
  {
    xx=2;
  }
  if ((loc == 3) || (loc == 6) || (loc == 11) || (loc == 14))
  {
    xx=3;
  }
  if ((loc == 4) || (loc == 5) || (loc == 12) || (loc == 13))
  {
    xx=4;
  }

// until ((semx == 0) && (semy == 0));
  movex();
  movey();
}


void movex()
// This function is started as soon as the program is started and
// is constantly looping.
// Each time it loops it looks at the 'xx' variable, and will
// check to see that the claw is positioned correctly according
// to what is desirde in the 'xx' variable. If it is not in the
// correct position then we start moving.
{
//Co-ordinates of Field
int cell_spacing[4];             //co-ordinates
  //Setting Grid
  cell_spacing[1] = 2;
  cell_spacing[2] = 30;
  cell_spacing[3] = 60;
  cell_spacing[4] = 88;


until ( semx == 0 )
{
  if (cell_spacing[xx] == abs(SENSOR_1) && semx != 0 )
  {
  // If we have reached the correct spot then
  // we can stop moving in the x direction.
  xoff;
  semx=0;
  }
  else if (cell_spacing[xx] > abs(SENSOR_1))
  {

  //Here we set the speed depending upon how close we are
    //slow grabber down when close to cell
    if ((cell_spacing[xx] - 3) > abs(SENSOR_1))
    {
     xspeed(7);
    }
    else
    {
      xspeed(0);
    }

  //If we are not already moving (semx variable) then
  //we start moving
  if ( semx != 1 )
  {
    xright;
    semx = 1;
    }
  }

  else if (cell_spacing[xx] < abs(SENSOR_1))
  {

    //move grabber left from current position
    //slow grabber down when close to cell
    if ((cell_spacing[xx] + 3) < abs(SENSOR_1))
    {
    xspeed(7);
}
    else
    {
    xspeed(0);
    }

  //If we are not already moving (semx variable) then
  //we start moving
    if (semx != 2 )
    {
    xleft;
    semx = 2;
    }  // End if if semx <> 2
  }    // End if else if
} // End of until
} // end of function


void movey()
// This function is started as soon as the program is started and
// is constantly looping.
// Each time it loops it looks at the 'yy' variable, and will
// check to see that the claw is positioned correctly according
// to what is desirde in the 'xx' variable. If it is not in the
// correct position then we start moving.
{
//Co-ordinates of Field
int cell_spacing[4];             //co-ordinates
  //Setting Grid
  cell_spacing[1] = 2;
  cell_spacing[2] = 30;
  cell_spacing[3] = 60;
  cell_spacing[4] = 88;

until ( semy == 0 )
{
  if (cell_spacing[yy] == abs(SENSOR_2) && semy != 0 )
  {
  // If we have reached the correct spot then
  // we can stop moving in the x direction.
  yoff;
  semy=0;
  }
  else if (cell_spacing[yy] > abs(SENSOR_2))
  {

  //Here we set the speed depending upon how close we are
    //slow grabber down when close to cell
    if ((cell_spacing[yy] - 3) > abs(SENSOR_2))
    {
     yspeed(7);
    }
    else
    {
      yspeed(1);
    }

  //If we are not already moving (semx variable) then
  //we start moving
  if ( semy != 1 )
  {
    yup;
    semy = 1;
    }
  }

  else if (cell_spacing[yy] < abs(SENSOR_2))
  {

    //move grabber left from current position
    //slow grabber down when close to cell
    if ((cell_spacing[yy] + 3) < abs(SENSOR_2))
    {
    yspeed(7);
}
    else
    {
    yspeed(1);
    }

  //If we are not already moving (semx variable) then
  //we start moving
    if (semy != 2 )
    {
    ydown;
    semy = 2;
    }  // End if if semy <> 2
  }    // End if else if
} // End of until
} // end of function



Message is in Reply To:
  Re: My RCX is HAUNTED
 
(...) That code was just not elegant, but then script code are a lot harder to read than NQC. I would applaud if those code were machine generated by another code he wrote but they are not. If the target pattern is changed from X to something else (...) (21 years ago, 12-Sep-03, to lugnet.org.ca.rtltoronto)

38 Messages in This Thread:
















Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR