Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Programing IR board problem (http://www.chiefdelphi.com/forums/showthread.php?t=64280)

Shivang1923 16-02-2008 15:48

Programing IR board problem
 
Hey. We are trying to program autonimous with IR board.

here is our code:
Code:

//**** Check the IR Sensor for a new command
  sensorReading  = PORTJ>>4;  // Combined digital inputs 15-18

  if (latch == 1)
  {
    if (sensorReading == 0)
    {
      latch = 0; // Take only the 1st reading to avoid being caught by a half & half state of the IR sensor
    }
  }
  else if (sensorReading != 0)
  {
      latch = 1;

      if (sensorReading == 8)      IR_cmd = 1;
      else if (sensorReading == 4) IR_cmd = 4;
      else if (sensorReading == 2) IR_cmd = 2;
      else if (sensorReading == 1) IR_cmd = 3;

        printf("IR_cmd = %d\r\n", IR_cmd);
}

 switch(IR_cmd)
 {
    case 0:
                  break;

    case 1:
                if (counter <= 50)
                {
                        pwm01 = pwm02 = 255;
                        counter++;
                }
                else
                {
                        counter = 0;
                        pwm01 = pwm02 = 127;
                }
            break;

    case 2:
      pwm01 = 200;
      pwm02 = 55;
      break;

    case 3:
        pwm01 = 55;
        pwm02 = 200;
        break;

    case 4:
        relay1_fwd = 1;
    }

This one loops forever. The problem is the counter = 0;.
If we remove it, the motors never stop moving

We want to have it configured so if button one is pressed, it will move forward for x seconds, and so on for the rest.


another problem is that the relay will not fire.

Any help will be appreciated. Thank you.

Mark McLeod 16-02-2008 17:25

Re: Programing IR board problem
 
If the relay is a single solenoid you might need to add:
relay1_rev = 0;

If it's a double solenoid then depending on how it's wired it might possibly be:
relay1_fwd = 0;
relay1_rev = 1;

How is counter initialized?
Make sure you made counter either global or static and initialized the value to zero, otherwise, counter will be zero everytime this gets executed and it will go on forever.

Resetting counter to zero will just continue the loop endlessly.

Shivang1923 16-02-2008 17:40

Re: Programing IR board problem
 
The variable is static, and initialized to zero.

Code:

static int counter = 0;

Mark McLeod 16-02-2008 18:22

Re: Programing IR board problem
 
Well, the only thing I see (if the "command = 0" line is removed to prevent the endless looping) is that IR command 1 would not work properly if you ever pressed it a second time. That could be fixed a number of ways, e.g., use a toggle to reset counter=0 when the button is pushed a second time.

Shivang1923 16-02-2008 20:27

Re: Programing IR board problem
 
so i would do this?

Code:

case 1:
                counter = 0;
                if (counter <= 50)
                {
                        pwm01 = pwm02 = 255;
                        counter++;
                }
                else
                {
                        pwm01 = pwm02 = 127;
                }
            break;


Mark McLeod 16-02-2008 22:55

Re: Programing IR board problem
 
Maybe something, such as,
Code:

static int counter = 0;
...
 
 //**** Check the IR Sensor for a new command
  sensorReading  = PORTJ>>4;  // Combined digital inputs 15-18
 
  if (latch == 1)
  {
    if (sensorReading == 0)
    {
      latch = 0; // Take only the 1st reading to avoid being caught by a half & half state of the IR sensor
    }
  }
  else if (sensorReading != 0)
  {
      latch = 1;
 
      if (sensorReading == 8)      IR_cmd = 1;
      else if (sensorReading == 4) IR_cmd = 4;
      else if (sensorReading == 2) IR_cmd = 2;
      else if (sensorReading == 1) IR_cmd = 3;
 
        printf("IR_cmd = %d\r\n", IR_cmd);
}
 
if (IR_cmd == 0)
{
counter = 0;  // To initialize counter whenever cmd 1 finishes executing
}
 
 switch(IR_cmd)
 {
    case 0:
          break;
 
    case 1:
        if (counter <= 50)
        {
            pwm01 = pwm02 = 255;
            counter++;
        }
        else
        {
          IR_cmd = 0;  // So cmd 1 won't try to repeat itself
            pwm01 = pwm02 = 127;
        }
        break;
 
    case 2:
      pwm01 = 200;
      pwm02 = 55;
      break;
 
    case 3:
        pwm01 = 55;
        pwm02 = 200;
        break;
 
    case 4:
        relay1_fwd = 1;
    }



All times are GMT -5. The time now is 20:26.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi