Using Limit Switches with the Arm

This is a little urgent because the robot needs to be shipped tomorrow and it would be ideal to have a working code before that…
I am coding in EasyC and I’m having some problems with coding the limit switches to limit the arm movements so it doesn’t hit the bottom of the robot when extended.

This image will provide you with more information about what we want:
http://img205.imageshack.us/img205/5788/halpas0.jpg

When our Limit Switches equal 1, it is pushed down. Otherwise, they equal 0.
The rotator motor of the arm is a PWM. The extender is a relay switch.

VARIABLES USED:

unsigned char LimitSwitch1;
unsigned char LimitSwitch2; 
unsigned char LimitSwitch3; 
unsigned char LimitSwitch4; 
unsigned char YAxisInputArmRotation;


// Joystick Y-Axis Movement controls Arm Rotation Motor Forward / Backward PWM 
// Joystick Aux1 and Aux2 Movement controls Arm Extender Motor Forward / Backward 
// 
// Get Joystick Input for Aux1 and Aux2 
LimitSwitch1 = GetDigitalInput ( 5 ) ; 
LimitSwitch2 = GetDigitalInput ( 6 ) ; 
LimitSwitch3 = GetDigitalInput ( 7 ) ; 
LimitSwitch4 = GetDigitalInput ( 8 ) ; 

if ( LimitSwitch1 == 1 ) 
{ 
// If the Arm Extention Switch is DOWN (ON HOME POSITION) then only allow EXTRACTION and ROTATION MOVEMENT. 
// Up / Down Arm Rotation Movement -- Get Joystick Y-Axis Input 
OIToPWM ( 3 , 2 , 3 , 1 ) ; // Out Extender Arm Movement
OIToRelay ( 3 , 4 , 1 , 2 ) ; 
PrintToScreen ( "Limit Switch ONE is pushed down -- RETRACTION NOT ALLOWED.
" ) ; 
} 

if ( LimitSwitch1 == 1 && LimitSwitch2 == 1 ) 
{ 
// If Limit Switch 2 ( ARM ROTATION -- FRONT ) is down and LimitSwitch1 is down ( ARM IS RETRACTED ) then ALLOW ALL MOVEMENT. 
// Up / Down Arm Rotation Movement -- Get Joystick Y-Axis Input 
OIToPWM ( 3 , 2 , 3 , 1 ) ; // In / Out Extender Arm Movement 
OIToRelay ( 3 , 3 , 1 , 1 ) ; 
OIToRelay ( 3 , 4 , 1 , 2 ) ; PrintToScreen ( "Limit Switch ONE and Limit Switch TWO are down -- ARM IS RETRACTED, ALLOW ALL.
" ) ; 
}

if ( LimitSwitch1 == 0 && LimitSwitch2 == 1 ) 
{ 
// If Limit Switch 2 ( ARM ROTATION -- FRONT ) is down BUT Limit Switch 1 (ARM EXTENTION -- HOME ) IS NOT DOWN then ONLY ALLOW REVERSE ROTATION AND RETRATION 
// Up / Down Arm Rotation Movement -- Get Joystick Y-Axis Input 
YAxisInputArmRotation = GetOIAInput ( 3 , 2 ) ; 

if ( YAxisInputArmRotation <= 127 ) 
{ 
SetPWM ( 3 , YAxisInputArmRotation ) ; 
} 
// In Extender Arm Movement 
OIToRelay ( 3 , 4 , 1 , 2 ) ; 
PrintToScreen ( "Limit Switch TWO is down, ONE is NOT. -- RETRACTION AND REVERSE ROTATION ¬ ALLOWED
" ) ; 
} 

if ( LimitSwitch1 == 1 && LimitSwitch3 == 1 ) 
{ 
// If Limit Switch 3 ( ARM ROTATION -- BACK ) is down and LimitSwitch1 is down ( ARM IS RETRACTED¬ ) then ALLOW ALL MOVEMENT. 
// Up / Down Arm Rotation Movement -- Get Joystick Y-Axis Input 
OIToPWM ( 3 , 2 , 3 , 1 ) ; 
// In / Out Extender Arm Movement 
OIToRelay ( 3 , 3 , 1 , 1 ) ; 
OIToRelay ( 3 , 4 , 1 , 2 ) ; 
PrintToScreen ( "Limit Switch ONE and THREE are pushed down -- ARM IS RETRACTED, ALLOW ALL.
" ) ; 
} 

if ( LimitSwitch1 == 0 && LimitSwitch3 == 1 ) { 
// If Limit Switch 3 ( ARM ROTATION -- BACK ) is down and LimitSwitch1 is up ( ARM IS EXTENDED ) then only allow forward rotation and retraction 
// Up Arm Rotation Movement -- Get Joystick Y-Axis Input 
YAxisInputArmRotation = GetOIAInput ( 3 , 2 ) ; 
if ( YAxisInputArmRotation >= 127 ) { 
SetPWM ( 3 , YAxisInputArmRotation ) ; 
} 
// In Extender Arm Movement 
OIToRelay ( 3 , 4 , 1 , 2 ) ; 
PrintToScreen ( "Limit Switch THREE is down, ONE is not -- ALLOW RETRACTION and FORWARD ROTATION.
" ) ; } 

if ( LimitSwitch4 == 1 ) { 
// If Limit Switch 4 is down then move the opposite direction you were going. 
// Arm Rotation Movement is opposite what it was originally going... 
YAxisInputArmRotation = GetOIAInput ( 3 , 2 ) ; 

if ( YAxisInputArmRotation >= 127 ) 
{

} 

PrintToScreen ( "Limit Switch FOUR is pushed down.
" ) ; 
} 

if ( LimitSwitch1 == 0 && LimitSwitch2 == 0 && LimitSwitch3 == 0 && LimitSwitch4 == 0 ) 
{
// If Limit Switches are not down then DO WHATEVER YOU WANT. 
// Up / Down Arm Rotation Movement -- Get Joystick Y-Axis Input 
OIToPWM ( 3 , 2 , 3 , 1 ) ; 
// In / Out Extender Arm Movement 
OIToRelay ( 3 , 3 , 1 , 1 ) ; 
OIToRelay ( 3 , 4 , 1 , 2 ) ; 
PrintToScreen ( "NO Limit Switches are PUSHED DOWN. ALL MOVEMENT ALLOWED
" ) ; 
} 
}

I am just generally very unsure about my code and we’ve run into a problem while testing it: If the arm is moving in any way and one of the switches gets pressed, it totally ignores all user input and keeps turning that way with the last input that was recieved, until the switch is let go, which is TOTALLY not what we want. Is there any way I can reorganize it so that it works the way I need it to? I need it to make the arm STOP when the switch is toggled. Afterwards, I need it to only be able to turn or retract the OPPOSITE way it came from.

Please, any comments will help me, especially since I need to get this working by tomorrow… Thanks :slight_smile:

We’ve had similar problems with understanding what certain parts are supposed to do - it can get very confusing. Just tonight i tried a different tactic with one of the students and it seemed to make everything a LOT clearer. Set everything up in a big truth table, and then build your conditionals from that. I’m not sure i understand your situation well enough to help with a truth table for you specifically, but here’s the situation that we have:

two buttons on the controller: B1 spins a wheel in to suck up a ball, while B2 spins it out to spit out a ball. In both cases, we only want the wheel spinning as long as the button is pressed. If both buttons are pressed at once, we don’t care what is happening.

a limit switch: When the ball hits the limit switch, we know the ball is in our traveling position, so we don’t want the motors to be able to spin inwards anymore (but they CAN spin outwards).

so the truth table would be:


 B1 | B2 | S1 | wheels
----------------------
  T |  T |  T |  (not in)
  T |  T |  F |  (don't care)
  T |  F |  T |  stop
  T |  F |  F |  in
  F |  T |  T |  out
  F |  T |  F |  out
  F |  F |  T |  stop
  F |  F |  F |  stop

Which we can turn into psudocode with:


if (B1 is pressed and S1 is not pressed) 
    spin the wheels in   				 //this happens for lines 2 and 4
else if (B2 is pressed)					 
    spin the wheels out					 //this happens for lines 1, 5, and 6
else									 
    stop the wheels					 //this happens for lines 3, 7, and 8

I think if you did something similar, it might help you think about the situation a little easier. The key is finding the rows that have a similar output, and grouping those together into if statements.

have you considered just using an encoder?

Thank you both for your help. I’m pretty sure that I have a working limit switch code now :slight_smile:

I took your eagle and made a truth table. This helped me out a lot while I was coding…

L1|L2|L3|L4|Disallow|

T F F F Retraction - CHECK
T T F F Retraction - NOT INCLUDED
T F T F Retraction - NOT INCLUDED
T F F T Extention, Retraction, Rotation from where it came
F T F F Forward Rotation, Extention - CHECK
F F T F Reverse Rotation, Extention -
F F F T Extention, Retraction, Rotation from where it came
F F F F Allow all movement - NOT INLCUDED

I also found that my code was overcomplicated, and could have been simplified to the ones posted here: http://www.chiefdelphi.com/forums/showthread.php?t=60186

And popo, yeah I could have probably used an encoder, but this is my first year in FIRST (lol) and it is my team’s second year, so we’ve not yet had to chance to use those, and with one day left we probably won’t get to this season. I also didn’t want to let my team down, they did a lot of work cutting stuff to hold the limit switches in place :stuck_out_tongue:

With every year comes advancement…