Limit Switch Coding help!

  1. 2 years ago

    JacketsSTEAMTeamY!Y

    18 Dec 2015 St. Martin, Mississippi 5735Y

    I am having trouble programming a limit switch for my NBN robot in ROBOTC. My robot's main goal is the high lift. Because of the way I built my robot, when the lift is down I can still pull the lifter down which cause the frame to bend forward. This is my first year doing VEX and I am not very good at coding yet. My solution to this was to put a limit switch just under the ramp so when it is down, the switch is pressed. I have my 4 lifter motors coded to Channel 3 on the controller. The way my sister teams and I decided to code this was to try and code it so that when the switch is pressed, Channel 3 can only be + so the motors could only push up. This did not work as the motors could still be pulled down.

    So classic limit switch code may look like this. The idea is to limit the motors movement to one direction if the limit switch is activated.

    #pragma config(Sensor, dgtl12, LimitSwitch,    sensorTouch)
    #pragma config(Motor,  port2,           motorA,        tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
    
    task main()
    {
        int motorValue = 0;
        
        while(1)
          {
          // Get joystick
          motorValue = vexRT[ Ch2 ];
          
          
          // Limit to down movement if limit switch is hit
          // limit switch is setup as touch sensor so pressed is 1
          if( SensorValue[ LimitSwitch ] == 1 ) {
            // are we going up ?
            if( motorValue > 0 ) {
              motorValue = 0;
            }
          }
          
          // Send control value to motor
          motor[ motorA ] = motorValue;
    
          // Don't hog cpu
          wait1Msec(25);
          }
    }
  2. Edited 2 years ago by FullMetalMentor

    Can you use the absolute value function on the Channel 3 reading? So no matter what Channel 3 is reading, the motor will be commanded with a positive number.

    abs()

  3. [deleted]

    18 Dec 2015
    Deleted last year by DRow
  4. JacketsSTEAMTeamY!Y

    18 Dec 2015 St. Martin, Mississippi 5735Y
    Edited 2 years ago by JacketsSTEAMTeamY!Y

    I tried using abs() but I can still pull the ramp further down. The picture is the updated code.

  5. @JacketsSTEAMTeamY!Y I tried using abs() but I can still pull the ramp further down. The picture is the updated code.

    Are you sure your limit switch is set up correctly, wired properly, plugged into the proper port, and is physically getting activated when the lift is down? If the switch never actually gets activated, then your motors will still be able to drive things down.

  6. Edited 2 years ago by OverlyOptimisticProgramer

    I am confused on the if statements, they seem to do absolutely nothing in your first picture and in your second picture, if the limit switch is pressed down and your ch3 is negative, the motors attached to ports 5,6,7, and 8 will go from negative 127 to positive 127. If the limit switch is not pushed down then your second if statement does nothing

  7. JacketsSTEAMTeamY!Y

    18 Dec 2015 St. Martin, Mississippi 5735Y

    The limit switch is plugged into port dgtl12 and I took the switch off and activated it by hand and nothing happened still.

  8. jpearman

    18 Dec 2015 Moderator, ROBOTC Tech Support, V5 Beta Moderator Answer Los Angeles 8888

    So classic limit switch code may look like this. The idea is to limit the motors movement to one direction if the limit switch is activated.

    #pragma config(Sensor, dgtl12, LimitSwitch,    sensorTouch)
    #pragma config(Motor,  port2,           motorA,        tmotorVex393_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
    
    task main()
    {
        int motorValue = 0;
        
        while(1)
          {
          // Get joystick
          motorValue = vexRT[ Ch2 ];
          
          
          // Limit to down movement if limit switch is hit
          // limit switch is setup as touch sensor so pressed is 1
          if( SensorValue[ LimitSwitch ] == 1 ) {
            // are we going up ?
            if( motorValue > 0 ) {
              motorValue = 0;
            }
          }
          
          // Send control value to motor
          motor[ motorA ] = motorValue;
    
          // Don't hog cpu
          wait1Msec(25);
          }
    }
  9. JacketsSTEAMTeamY!Y

    18 Dec 2015 St. Martin, Mississippi 5735Y
    Edited 2 years ago by JacketsSTEAMTeamY!Y

    My friend helped me format the code for my robot and the picture is the dummy code to get it to try to work. Unfortunately this code still does not work

  10. jpearman

    18 Dec 2015 Moderator, ROBOTC Tech Support, V5 Beta Moderator Los Angeles 8888

    @JacketsSTEAMTeamY!Y My friend helped me format the code for my robot and the picture is the dummy code to get it to try to work. Unfortunately this code still does not work

    Perhaps the switch is broken, or the dgtl12 port on the cortex broken. Do you see the value changing in the sensors debug window? If not try another switch and/or port.

  11. JacketsSTEAMTeamY!Y

    18 Dec 2015 St. Martin, Mississippi 5735Y

    I now know that I had the sensor in port SP in stead of port 12 because I could barely see my cortex.
    Both of the codes work but, I decided to use jpearman's code.

  12. @jpearman So classic limit switch code may look like this. The idea is to limit the motors movement to one direction if the limit switch is activated....

    Oh, yes, that's a very good point. That's one of those classic robot programming errors: You hit a limit switch and the motors are shut down, just as planned. But then when you command the robot to move the other direction, the motors remain turned off and you can never move again. Direction matters.

  13. last week

    my problem is that once it detects the switch it still coasts down rather than a sudden stop. and im using an exact copy of the "typical code"

  14. briancole

    Jul 4 Utah 4270C

    Don’t revive dead threads start new ones

  15. The_One_and_Only_Sal

    Jul 5 Suspended Washington URDON3

    @LauranceDarkez my problem is that once it detects the switch it still coasts down rather than a sudden stop. and im using an exact copy of the "typical code"

    I recommend using PID and a potentiometer instead of a switch if you want to stop at an exact spot. If you have taken classical physics, and learned about the Newtonian laws, such as Newton's Third Law (which you can apply to pushing on the platforms :P), you should know the motors have to accelerate and decelerate. Thus, your motors will not instantly stop when you tell them to. :D

    @briancole Don’t revive dead threads start new ones

    If you're going to chastise someone, at least offer some help. :D :D :D

    -Sal

  16. the potentiometer doesnt have enough movement. plus, im only having it stop at the end of travel for the lower end. not the upper end. but also i got the switch to work.

 

or Sign Up to reply!