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 / 9111
9110  |  9112
Subject: 
Re: My RCX is HAUNTED
Newsgroups: 
lugnet.org.ca.rtltoronto
Date: 
Fri, 12 Sep 2003 16:36:21 GMT
Viewed: 
498 times
  
In lugnet.org.ca.rtltoronto, Kevin L. Clague wrote:
  Given that the RCX has so few variables, I've been known to use more code to
save variables.  It is a very clever strategy IMHO, and I applaud Chris for
thinking of it.

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 he have to slowly change many lines.  My code only need to change two lines
for a new target pattern and it only used one 16 bit variable for pattern
status.

I will let you boys pick it apart while I drive down to Toronto...

// Project X - RTL10
// Motor macros
#define X_DEC OnFwd(OUT_A)
#define X_INC OnRev(OUT_A)
#define X_STOP Off(OUT_A)

#define Y_DEC OnFwd(OUT_C)
#define Y_INC OnRev(OUT_C)
#define Y_STOP Off(OUT_C)

#define ARM_CLOSE OnRev(OUT_B)
#define ARM_OPEN OnFwd(OUT_B)
#define ARM_STOP Off(OUT_B)

// Sensor symbols
#define X_SENSOR SENSOR_3
#define Y_SENSOR SENSOR_2
#define LIGHT    SENSOR_1

#define BLOCK_LEVEL 820

// Cell grid constants
#define X_SIZE 4
#define Y_SIZE 4

#define MIN_X 0
#define MIN_Y 0
#define MAX_X (X_SIZE-1)
#define MAX_Y (Y_SIZE-1)

// Game constants
#define TOTAL_CELL (X_SIZE * Y_SIZE)
#define TOTAL_BLOCK 8
#define TOTAL_EMPTY (TOTAL_CELL - TOTAL_BLOCK)

// Arm status flags
#define OPEN 1
#define CLOSE 2

// Target pattern is 1001 0110 0110 1001
#define TARGET_PATTERN 0x9669
#define INV_TARGET_PATTERN 0x6996

// Global variables
int x, y;
int x_target, y_target;
int arm_status, arm_target;

void get_mask(int &mask, const int &x, const int &y)
{
  int idx = x + y * Y_SIZE;
  mask = 1;
  while(idx-- > 0) mask <<= 1;
}

task x_move()
{
    if(x < x_target)
    { X_INC;
      while(x != x_target)
      { // Moving
          while(X_SENSOR) Wait(1);  // Move till we no longer touch the marker.
          Wait(10);                 // Make sure it move away clearly.
          while(!X_SENSOR) Wait(1); // Keep moving till we touch the next
marker.
          x++;
      }
      if(x < MAX_X)
      {
          X_DEC;
          Wait(2);
      }
    }
    if(x > x_target)
    {
      X_DEC;
      while(x != x_target)
      { // Moving
          while(X_SENSOR) Wait(1);  // Move till we no longer touch the marker.
          Wait(10);                 // Make sure it move away clearly.
          while(!X_SENSOR) Wait(1); // Keep moving till we touch the next
marker.
          x--;
      }
      if(x > MIN_X)
      {
          X_INC;
          Wait(2);
      }
    }
    X_STOP;
}

task y_move()
{
    while(y != y_target)
    { // Moving
      if(y < y_target) Y_INC;
      else Y_DEC;

      while(Y_SENSOR) Wait(1);  // Move till we no longer touch the marker.
      Wait(20);                 // Make sure it move away clearly.
      while(!Y_SENSOR) Wait(1); // Keep moving till we touch the next marker.
      y += sign(y_target-y);
    }
    Y_STOP;
}

task arm_move()
{
    while(x != x_target || y != y_target) Wait(4);

    if(arm_status != arm_target)
    {
      if(arm_target == OPEN)
      {
        ARM_OPEN;
        Wait(75);
        arm_status = OPEN;
        Wait(20);
        ARM_STOP;
      }
      else
      {
        ARM_CLOSE;
        Wait(75);
        arm_status = CLOSE;
        Wait(20);
        ARM_STOP;
      }
    }
}

task main()
{
  SetSensor(X_SENSOR,SENSOR_TOUCH);
  SetSensor(Y_SENSOR,SENSOR_TOUCH);
  SetSensor(LIGHT,SENSOR_LIGHT);
  SetSensorMode(LIGHT, SENSOR_MODE_RAW);

  SetPower(OUT_A | OUT_B | OUT_C, OUT_FULL);

  int pattern = 0;
  x = MIN_X; y = MIN_Y;
  x_target = MIN_X; y_target = MIN_Y;
  arm_status = OPEN; arm_target = OPEN;

  // Initial physical scan.
  {
    int block_cnt = 0, empty_cnt = 0, x_dir = 1, mask;

    while(1)
    {
      int temp = LIGHT;
      //Wait(2);
      if((temp + LIGHT) / 2 < BLOCK_LEVEL)
      {
        PlaySound(0);
        get_mask(mask, x, y);
        pattern |= mask;
        if(++block_cnt == TOTAL_BLOCK) break;
      }
      else
        if(++empty_cnt == TOTAL_EMPTY) break;

      if((x_dir == 1 && x_target == MAX_X) || (x_dir == -1 && x_target ==
MIN_X))
      {
        x_dir = -x_dir;
        y_target++;
        start y_move;
        while(y != y_target) Wait(2);
      }
      else
      {
        x_target += x_dir;
        start x_move;
        while(x != x_target) Wait(2);
      }
    }
    // Physical scan finished.  We eithered scanned enough blocks or empty
cells.

    if(empty_cnt == TOTAL_EMPTY)
    { // Now we know the rest of the unscaned cell have blocks, we need to
      // fill in the rest of the block bits by doing a virtual scan.
      int temp_x = x, temp_y = y;
      while(block_cnt < TOTAL_BLOCK)
      {
        if((x_dir==1 && temp_x == MAX_X) || (x_dir == -1 && temp_x == MIN_X))
        {
          x_dir = -x_dir;
          if(temp_y == MAX_Y)  break;
          else
            temp_y++;
        }
        else
          temp_x += x_dir;

        // Set the bit
        get_mask(mask, temp_x, temp_y);
        pattern |= mask;
        ++block_cnt;
      }
    } // Done filling the rest of the block bits.
  } // Close scan block.

  //
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // Move blocks until the current pattern match the target pattern.
  while(pattern != TARGET_PATTERN)
  {
    int to_x, to_y, x_dir, y_dir, mask;

    // Initial scan point - close to the current (x,y), and scan toward the
opposite corner.
    if(x_target < X_SIZE/2)
    { to_x = MIN_X; x_dir = 1; }
    else
    { to_x = MAX_X; x_dir = -1; }

    if(y_target < Y_SIZE/2)
    { to_y = MIN_Y; y_dir = 1; }
    else
    { to_y = MAX_Y; y_dir = -1; }

    // Find a block that need moving.
    while(1)
    {
      get_mask(mask, to_x, to_y);
      // If a block is at (to_x,to_y), but it is not in the target pattern,
      // then we can move it.
      if((pattern & INV_TARGET_PATTERN & mask))
      {
        break;
      }

      // Continue scan.
      to_x += x_dir;
      if(to_x > MAX_X)
      {
        to_x = MIN_X;
        to_y += y_dir;
        if(to_y > MAX_Y) to_y = MIN_Y;
        else if(to_y < MIN_Y) to_y = MAX_Y;
      }
      else
      if(to_x < MIN_X)
      {
        to_x = MAX_X;
        to_y += y_dir;
        if(to_y > MAX_Y) to_y = MIN_Y;
        else if(to_y < MIN_Y) to_y = MAX_Y;
      }
    }

    // Clear the block bit in the pattern.
    pattern &= ~mask;

    // Wait till the arm done releasing the block from previous pass.
    while(arm_status != arm_target) Wait(2);

    // Move the arm to the new pick up location, and pick up the block.
    x_target = to_x;
    start x_move;
    y_target = to_y;
    start y_move;
    arm_target = CLOSE;
    start arm_move;

    // Calcaulate where to move the block to.
    // Initial scan point - close to the current (x,y), and scan toward the
opposite corner.
    if(x_target < X_SIZE/2)
    { to_x = MIN_X; x_dir = 1; }
    else
    { to_x = MAX_X; x_dir = -1; }

    if(y_target < Y_SIZE/2)
    { to_y = MIN_Y; y_dir = 1; }
    else
    { to_y = MAX_Y; y_dir = -1; }

    while(1)
    {
      get_mask(mask, to_x, to_y);
      // If a space is at (to_x,to_y), but it is a block in the target pattern,
      // then we can fill it.
      if((~pattern) & TARGET_PATTERN & mask)
      {
        break;
      }
      // Continue scan.
      to_x += x_dir;
      if(to_x > MAX_X)
      {
        to_x = MIN_X;
        to_y += y_dir;
        if(to_y > MAX_Y) to_y = MIN_Y;
        else if(to_y < MIN_Y) to_y = MAX_Y;
      }
      else
      if(to_x < MIN_X)
      {
        to_x = MAX_X;
        to_y += y_dir;
        if(to_y > MAX_Y) to_y = MIN_Y;
        else if(to_y < MIN_Y) to_y = MAX_Y;
      }
    }

    // Set the dropped block bit in the pattern.
    pattern |= mask;

    // Wait till the arm done picking the block.
    while(arm_status != CLOSE) Wait(2);

    // Move the arm to the drop location.
    x_target = to_x;
    start x_move;
    y_target = to_y;
    start y_move;
    arm_target = OPEN;
    start arm_move;
  } // while pattern loop

  // Wait till the arm is there.
  while(x != x_target || y != y_target) Wait(2);

  arm_target = OPEN;
  start arm_move;

  // Wait till the arm done releasing the block.
  while(arm_status != OPEN) Wait(2);

  // End game!
  PlaySound(1);

  x_target = MIN_X; y_target = MIN_Y;
  start x_move;
  start y_move;

  while(x != x_target || y != y_target) Wait(2);

  PlaySound(3);
  PlaySound(3);
}



Message has 2 Replies:
  Re: My RCX is HAUNTED
 
(...) DON'T GET ME STARTED on how hard C is to read. if you do not know any language. C is just crazy (for the novice) I still don't know what the hell this means: x != x_target || y != y_target AND I DON'T WANT TO !!!! I think what's this is (...) (21 years ago, 12-Sep-03, to lugnet.org.ca.rtltoronto)
  Re: My RCX is HAUNTED
 
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 (...) (21 years ago, 12-Sep-03, to lugnet.org.ca.rtltoronto)

Message is in Reply To:
  Re: My RCX is HAUNTED
 
(...) Man, you guys sure fight well..... As a person whos programmed at every level of the machine, microcode, machine code, assembly code, high level code, I can tell you that in tight coding environments, keeping the variables to a minimum sure (...) (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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

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