Thread: limit switches
View Single Post
  #23   Spotlight this post!  
Unread 10-05-2005, 15:17
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
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;
  }
}

Last edited by Dave Scheck : 11-05-2005 at 11:45.