Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   limit switches (http://www.chiefdelphi.com/forums/showthread.php?t=37476)

Alan Anderson 28-04-2005 12:26

Re: limit switches
 
Quote:

Originally Posted by stephenthe1
actually, limit switches are the opposite of that. They are normally 0, or true, then once pressed, turn to 1...

Using the word "pressed" when talking about how the RC digital inputs work is probably a bad idea. Some switches are normally open and close the contacts when activated, while others are normally closed and open the contacts when activated. Some are built with both options at the same time, and you can choose which contacts to use when you connect the wires. They give different results when "pressed", and can be a source of great confusion for people trying to understand how to program for the digital inputs.

The RC digital inputs have a pullup resistor to +5 volts. Reading them will return a 1 when the switch is open, and a 0 when the switch is closed to ground.

eugenebrooks 28-04-2005 12:44

Re: limit switches
 
Quote:

Originally Posted by MikeWasHere05
Try adding a printf to your code, see what it puts out.

printf("Limit Switch: %f \n", rc_dig_in08);

printf("Limit Switch: %d \n", (int)rc_dig_in08);

Both the %d and (for our RC compiler) the cast to int are important...

Mike 28-04-2005 19:04

Re: limit switches
 
Heh, yeah sorry about that. It is %d not %f. It's little (seemingly) trivial things like that that always get me.

eugene: I've never used (int) before, and my printf's work fine.

CyberWolf_22 29-04-2005 01:06

Re: limit switches
 
I am not sure if the question has been answered or not but I can't really diagnose the problem with out the code but what my first idea would be is that there is not an else statement following his if statement to turn the motor off when the switch is not pressed.

if (button8 == 1)
{pwm01 = 254;}
else
{pwm01 = 127;}


Wait, after rereading some of the posts maybe this is not the problem.
Please post your code so we can further diagnose it.

stephenthe1 05-05-2005 11:45

Re: limit switches
 
thanks for all your help. I've got it working now. the problem was just in how I was accessing the limit switch in the code. there was a variable that wasn't being reset when the switch changed its state. thanks for all your help. Originally, this wasn't the only problem, we had problems wth our buttons that kept messing me up too. but now that's all solved. thanks for the help!!!
the code you posted worked fine. thanks again.

Gamer930 10-05-2005 13:10

Re: limit switches
 
I searched and couldn't find and couldn't think through the code for:

I have a motor and 2 Limit Switches that sets the upper and lower limits of an arm from our 2003 Robot.

I want to have it so when I hit a Trigger on the joystick it sends the motor forward until it hits the Top limit switch or if it is already at the top to reverse the motor until it hits the Bottom Limit Switch.

I have:
#define T_DRIVE pwm01
#define Up_T_Limit rc_dig_in01 /* Limit Switch on Full Up Set */
#define Down_T_Limit rc_dig_in02 /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN p2_sw_trig /* Button to change T state. */
/* T state variable. */
int intTState = 0;

Thanks in advance

<slightly off topic> This is for an off season project to convert our PBASIC 2003 robot into a vision robot for presentations </slightly off topic>

Ryan M. 10-05-2005 14:45

Re: limit switches
 
Quote:

Originally Posted by Gamer2028
...I want to have it so when I hit a Trigger on the joystick it sends the motor forward until it hits the Top limit switch or if it is already at the top to reverse the motor until it hits the Bottom Limit Switch.

I have:
Code:

#define T_DRIVE  pwm01
#define Up_T_Limit  rc_dig_in01  /* Limit Switch on Full Up Set */
#define Down_T_Limit  rc_dig_in02  /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN  p2_sw_trig /* Button to change T state. */
/* T state variable. */
int intTState = 0;

...

I'd try something like this:

Somewhere at top of file:
Code:

// Your stuff
#define T_DRIVE  pwm01
#define Up_T_Limit  rc_dig_in01  /* Limit Switch on Full Up Set */
#define Down_T_Limit  rc_dig_in02  /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN  p2_sw_trig /* Button to change T state. */

int intTState = 0; /* T state variable. */

// My stuff
#define RC_SWITCH_ON 0
#define RC_SWITCH_OFF !SWITCH_ON
#define OI_SWITCH_ON 1
#define OI_SWITCH_OFF !OI_SWITCH_ON

#define T_STATE_UP 1
#define T_STATE_DOWN !T_STATE_UP

Somewhere in main loop:
Code:

if(T_BUTTON_BTN == OI_SWITCH_ON)
{
    // Switch states. We could do it with intTState = !intTState,
    // but just for clarity...
    if(intTState == T_STATE_UP)
    {
        intTState = T_STATE_DOWN;
    }
    else
    {
        intTState = T_STATE_UP;
    }
}

if(intTState == T_STATE_UP && Up_T_Limit == SWITCH_OFF)
{
    // We need to go up more
    T_DRIVE = 255;
}
else if(intTState == T_STATE_DOWN && Down_T_limit == SWITCH_OFF)
{
    // We need to go down more
    T_DRIVE = 0;
}
else
{
    // We are at our goal. Stop motor
    T_DRIVE = 127;
}

Be advised that I have the code run the motor full speed. If there's no sort of cushion, you could break something. Slow it down if appropriate.

Dave Scheck 10-05-2005 15:17

Re: limit switches
 
Ryan, I don't think that your code is going to do what was requested. With your code, every time it gets called you will change your intTState. I think what you're trying to do is as follows...(This is uncompiled and untested, so be sure to understand the code before trying to run it)
Code:

#define T_DRIVE  pwm01
#define Up_T_Limit  rc_dig_in01  /* Limit Switch on Full Up Set */
#define Down_T_Limit  rc_dig_in02  /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN  p2_sw_trig /* Button to change T state. */

//NOTE: THIS VALUE MAY CHANGE IF YOUR SWITCH IS NORMALLY OPEN
#define RC_SWITCH_ON 0
// Added parens around the operation to be safe
#define RC_SWITCH_OFF (!SWITCH_ON)

#define OI_SWITCH_ON 1
// Added parens around the operation to be safe
#define OI_SWITCH_OFF (!OI_SWITCH_ON)

#define T_STATE_UP 1
// Added parens around the operation to be safe
#define T_STATE_DOWN (!T_STATE_UP)

// Depending on where this is you'll probably want to make it static
// Also I changed this from a hardcoded 0 to a constant (assuming it starts at the bottom)
static int intTState = T_STATE_UP; /* T state variable. */

...

if(T_BUTTON_BTN == OI_SWITCH_ON)
{
  // The button is pressed, let's move the motor
  if(intTState == T_STATE_UP)
  {
    // Currently moving up
    if(Up_T_Limit == RC_SWITCH_OFF)
    {
      // We need to go up more
      T_DRIVE = 255;
    }
    else
    {
      // We were moving up and hit the upper switch
      // Stop the motor and change the state
      initTState = T_STATE_DOWN;
      T_DRIVE = 127;
    }
  }
  else if(intTState == T_STATE_DOWN)
  {
    // Currently moving up
    if(Down_T_Limit == RC_SWITCH_OFF)
    {
      // We need to go down more
      T_DRIVE = 0;
    }
    else
    {
      // We were moving down and hit the lower switch
      // Stop the motor and change the state
      initTState = T_STATE_UP;
      T_DRIVE = 127;
    }
  }
  else
  {
    // We are in a bad state, stop the motor
    T_DRIVE = 127;
  }
}


Ryan M. 10-05-2005 17:28

Re: limit switches
 
Yep, you're right. I didn't think to include a "the press is from the last time through!" check. Your code should work, but just for the sake of completeness... :) (new stuff in bold)

Somewhere at top of file:
Code:

// Your stuff
#define T_DRIVE  pwm01
#define Up_T_Limit  rc_dig_in01  /* Limit Switch on Full Up Set */
#define Down_T_Limit  rc_dig_in02  /* Limit Switch on Full Down Set */
#define T_BUTTON_BTN  p2_sw_trig /* Button to change T state. */

int intTState = 0; /* T state variable. */

// My stuff
#define RC_SWITCH_ON 0
#define RC_SWITCH_OFF !SWITCH_ON
#define OI_SWITCH_ON 1
#define OI_SWITCH_OFF !OI_SWITCH_ON

#define T_STATE_UP 1
#define T_STATE_DOWN !T_STATE_UP

int buttonPressedLastTime = 0;

Somewhere in main loop:
Code:

if(T_BUTTON_BTN == OI_SWITCH_ON)
{
    // Switch states only if this button press isn't from a loop before
    // (the user held it down).
    if(buttonPressedLastTime == 0)
    {

        if(intTState == T_STATE_UP)
        {
            intTState = T_STATE_DOWN;
        }
        else
        {
            intTState = T_STATE_UP;
        }
    }

    buttonPressedLastTime = 1;
}
else
{
    buttonPressedLastTime = 0;
}


if(intTState == T_STATE_UP && Up_T_Limit == RC_SWITCH_OFF)
{
    // We need to go up more
    T_DRIVE = 255;
}
else if(intTState == T_STATE_DOWN && Down_T_limit == RC_SWITCH_OFF)
{
    // We need to go down more
    T_DRIVE = 0;
}
else
{
    // We are at our goal. Stop motor
    T_DRIVE = 127;
}

I'm pretty sure that works... hasn't been compiled, but hey, I'm perfect. ;)

Gamer930 11-05-2005 07:26

Re: limit switches
 
Thanks for the help. I partly finished rewiring and converting it to 2004 last night. I will finish up this weekend and let you all know.

One question: When you say SWITCH_OFF that isn't defined. Is that suppose to be RC_SWITCH_OFF??

Dave Scheck 11-05-2005 11:44

Re: limit switches
 
Quote:

Originally Posted by Gamer2028
One question: When you say SWITCH_OFF that isn't defined. Is that suppose to be RC_SWITCH_OFF??

Good catch...lost track of constant names. I'll update my post to reflect this. It's good to see that you aren't just trying to drop the code in without reading through it. Hopefully you were able to get an understanding of what is being done and can expand it and reuse it in the future.

Ryan M. 11-05-2005 13:40

Re: limit switches
 
Quote:

Originally Posted by Gamer2028
One question: When you say SWITCH_OFF that isn't defined. Is that suppose to be RC_SWITCH_OFF??

Yep... good catch. I've updated my second post as well.

Alan Anderson 11-05-2005 15:56

Re: limit switches
 
Y'all need to change !SWITCH_ON to !RC_SWITCH_ON as well.

Dave Scheck 11-05-2005 16:37

Re: limit switches
 
Quote:

Originally Posted by Alan Anderson
Y'all need to change !SWITCH_ON to !RC_SWITCH_ON as well.

You are correct, however, I can't seem to edit my original post...


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

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