Preseason Lesson3: Difference between revisions

From 1511Wookiee
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
<div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Automatically stop motor when limit switch is tripped'''</span></div> <div class="mw-parser-output">The goal is to move the motor using the joystick, but stop moving if a sensor it tripped (even if the joystick says to move). This simulates the driver moving an elevator up & down, but the code automatically stopping the motor when the elevator reaches it limits.  
<div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><div class="mw-parser-output"><span style="font-size: large;">'''Automatically stop motor when limit switch is tripped'''</span></div> <div class="mw-parser-output">The goal is to move the motor using the joystick, but stop moving if a sensor it tripped (even if the joystick says to move). This simulates the driver moving an elevator up & down, but the code automatically stopping the motor when the elevator reaches it limits.  
&nbsp;
&nbsp;


Line 37: Line 37:
             stop motor
             stop motor
         }
         }
    }
    else {
        stop motor
     }
     }


Line 62: Line 65:


Which switch did you need to trip when the joystick was forward? Is it the switch you expected? Why not? Look at the code and try to figure out why (look at the line for that switch).
Which switch did you need to trip when the joystick was forward? Is it the switch you expected? Why not? Look at the code and try to figure out why (look at the line for that switch).
</div> <div class="mw-parser-output">&nbsp;</div> <div class="mw-parser-output">&nbsp;</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
</div> <div class="mw-parser-output">&nbsp;</div> <div class="mw-parser-output">&nbsp;</div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>

Revision as of 14:14, 1 December 2019

Automatically stop motor when limit switch is tripped
The goal is to move the motor using the joystick, but stop moving if a sensor it tripped (even if the joystick says to move). This simulates the driver moving an elevator up & down, but the code automatically stopping the motor when the elevator reaches it limits.

 

It is expected that you completed Lesson 2 and have the code available.

 

The switches are consider digital inputs. Link to the DigitalInput documentation: DigitalInput

 

In Robot.h we will need an object/variable that is the switch. We will need two switches, one for each limit of travel. The class/object type is: frc::DigitialInput We will name the objects: m_switchA and m_switchB In Robot.h, in the 'private:' section add the following line:

   frc::DigitalInput m_switchA{x};
   frc::DigitalInput m_switchB{y};

The '{x}' and '{y}' indicate that the constructor will be called, and 'x' (or 'y') will be passed in for the channel parameter. Obviously the 'x' and 'y' need to be numbers, the channle number the switch is connected to. NOTE: The switches on the test board are labelled with which channel they are on.

 

The frc::DigitalInput class contains the method 'Get()', which is what we will use to see if the switch is tripped. The method returns 'true' if the switch is tripped.

 

In Robot.cpp, you will modify the code in the Robot::OperatorControl() method. You will add more logic to the 'if's that are checking for the joystick position from lesson 2 (see the red bold text in the sample code below).

You will want to first test if the trigger is pressed. If it is pressed, then you will look at the position to see if the position to determine how to turn the motor. Here is the pseudo code (i.e. not actual code, replace the English words with actual code):

   if (joystick trigger is pressed) {
       if ((joystick position is greater than 0.33) and (switch A is not tripped)) {
           spin motor forward 1/4 speed
       }
       else if ((joystick position is less than -0.33) and (switch B is no tripped)) {
           spin motor reverse 1/4 speed
       }
       else {
           stop motor
       }
   }
   else {
       stop motor
   }

 

 

After you have modified Robot.h and Robot.cpp, build the project. Once project builds without errors, let instructor know you are ready to try to delpoy and test. Do not deploy until told to.

 

Test

  1. pull trigger and move joystick forward - motor should turn
  2. trip the limit switch - the motor should stop turning
  3. release limit swtich and trip the 2nd limit swtich - the motor should still turn
  4. with the trigger pressed, pull the joystick backward - motor should turn, in opposite direction this time
  5. trip the 2nd limit switch - the motor should stop turning
  6. release 2nd limit switch and trip the 1st limit swtich - the motor should still turn
  7. release everything (switches, trigger and joystick)
  8. trip limit switch, pull trigger and move joystick - motor never turn
  9. repeat for other direction

 

Which switch did you need to trip when the joystick was forward? Is it the switch you expected? Why not? Look at the code and try to figure out why (look at the line for that switch).