Preseason Lesson2

From 1511Wookiee
Revision as of 08:44, 29 November 2021 by Heydowns (talk | contribs)
Jump to navigationJump to search

Goal

In this lesson we read inputs from a joystick/gamepad/controller on the Driver Station and that input to make decisions in our program to control the same motor from lesson 1 in a more sophisticated manner.

Detailed Introduction

  • Before doing this lesson you should have completed Lesson 1 and have the code for that as a starting point.
  • In this lesson we will use more of the base WPILib library than we did in the past. Remember that the documentation for WPILib is linked on the main programming page in our wiki!
  • This lesson will begin to use control statements in our code - we will make use of C++ if/else statements

Detailed Goals

  • You will again be controlling Motor 5
  • We will be reading from a gamepad connected to the driver station. The gamepad will be assigned an address identifier of 0
  • The motor should turn at 1/4 speed only when the button labelled 1 is pressed and when the left stick is moved forward or backward
  • The direction the control stick is pressed should control the motor direction (forward or backward)!

Guided Help

Create a Joystick Object

To read the input from the controller, we will need to create a Joystick object in our main robot class. Here is a direct link to the documentation for the Joystick class. While the class is called 'Joystick', it can also be used to read from controllers/game pads connected to the Driver Station.

  • The process for creating our Joystick object is exactly the same as the process used to make our motor controller object in Lesson 1, so less instructions will be given here! You can always go back to lesson 1 instructions to refresh your memory if needed!
  • The Joystick class is declared in a header file named Joystick.h - the full path you need to use it is frc/Joystick.h
  • The Joystick class is in the frc namespace. You can refer to it by its full name 'frc::Joystick' or you can use simply 'Joystick' if you tell your program to use the entire 'frc' namespace.
  • Using the above information, create a Joystick object in your main robot class. You can choose any name you like for the object but the rest of the guided help below will use the name controller



Control motor direction using the joystick

The goal is to turn the motor at 1/4 speed forward or backward, using the joystick, but only if the joystick trigger is being pressed.

 

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

 

Link to the Joystick documentation: Joystick

 

Part 1

In Robot.h we will need an object/variable that is the Joystick. The class/object type is: frc::Joystick We will name the object: m_joystick In Robot.h, in the 'private:' section add the following line:

   frc::Joystick m_joystick{0};

The '{0}' indicates the contructor will be called and '0' wil be passed in for the port parameter.

 

The frc::Joystick class contains the method 'GetRawButton()', which is what we will use to see if the trigger is being pressed. The trigger is button 1.

The frc:Joystick class contains the method 'GetY()', which will tell us the position of the joystick. The values will range from -1.0 to 1.0.

 

In Robot.cpp, you will add the code in the Robot::OperatorControl() method. You will replace the 'm_motor.Set(0.25)' from lesson 1.

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):

while (IsOperatorControl() && IsEnabled()) {
   if (joystick trigger is pressed) {
       if (joystick position is greater than 0.33) {
           spin motor forward 1/4 speed
       }
       else if (joystick position is less than -0.33) {
           spin motor reverse 1/4 speed
       }
       else {
           stop motor
       }
   }
   frc::Wait(0.005);
}

The 0.33 and -0.33 is make sure the joystick has moved away from the center. Ideally, you would get 0 when the joystick is at the center. However, joysticks do not always go back to true 0.

To stop a motor you send it a speed of 0.

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. move joystick back and forth - motor should not turn
  2. pull tigger while moving joystick back and forth - motor should turn, and direction of its rotation based on the joystick being forward or backward
  3. pull trigger and move joystick forward (motor will be turning), release trigger and then release joystick. WHY IS MOTOR STILL TURNING?

 

Part 2

  1. modifiy code so motor does not turn
  2. build the project. when there are no error ask instructor to review your code and then ask to test the code