Changes between Version 10 and Version 11 of ControlSystems/SoftwareTeam/Training/GettingStarted/DCMotor


Ignore:
Timestamp:
Nov 8, 2019, 11:00:11 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/DCMotor

    v10 v11  
    1 == DC Motor Control
    2 For stronger and faster movement, FRC robots use all sorts of motors.  There are large motors that propel the robot, small motors that drive flywheels or manipulators, etc.  Most DC motors use more power than can be supplied directly from the !RoboRIO so instead the roboRIO communicates with a dedicated high-power motor controller and tells it how fast and in which direction to move the motor.  FRC robots support several types of motor controllers, but there are two main types: simple PWM motor controllers like the Talon SR and intelligent motor controllers such as the [https://www.andymark.com/products/talon-srx-speed-controller Talon SRX] (aka CAN Talon).
     1/*----------------------------------------------------------------------------*/
     2/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
     3/* Open Source Software - may be modified and shared by FRC teams. The code   */
     4/* must be accompanied by the FIRST BSD license file in the root directory of */
     5/* the project.                                                               */
     6/*----------------------------------------------------------------------------*/
    37
    4 PWM motor controllers are connected to one of the PWM output ports on the roboRIO.  PWM stands for pulse-width modulation and communicates how much battery power to deliver to the motor ranging from -100% (full reverse) to +100% (full forward) or anything in-between.  Intelligent motor controllers contain their own microprocessor that can perform sophisticated management of the motor such as keeping it at a particular speed or following a motion profile or keeping a motor in a particular position (e.g. rotated to 30-degrees); we'll talk more about their capabilities later and you can read about them [https://buildmedia.readthedocs.org/media/pdf/phoenix-documentation/latest/phoenix-documentation.pdf here].
     8package frc.robot;
    59
    6 Create another program using the !TimedRobot java template and name it !MotorTest:
    7  
    8 === Smart (CAN) Motor Controller Example (Hazelnut)
     10import edu.wpi.first.wpilibj.TimedRobot;
     11import edu.wpi.first.wpilibj.XboxController;
     12import edu.wpi.first.wpilibj.GenericHID.Hand;
     13import com.ctre.phoenix.motorcontrol.can.*;
    914
    10 This example is for a robot that has a TalonSRX motor controller with its CAN bus address set to 3.  A Java Class is available that makes it easy to access the extensive capabilities of the TalonSRX; the class is provided by the manufacturer (Cross The Road Electronics aka CTRE) and must be installed on the laptop and added to the project.  Team laptops already have the CTRE framework installed; if your laptop does not, you can download and install it [http://www.ctr-electronics.com/control-system/hro.html#product_tabs_technical_resources here]. 
     15/**
     16 * The VM is configured to automatically run this class, and to call the
     17 * functions corresponding to each mode, as described in the TimedRobot
     18 * documentation. If you change the name of this class or the package after
     19 * creating this project, you must also update the build.gradle file in the
     20 * project.
     21 */
     22public class Robot extends TimedRobot {
     23  private XboxController xbox;
     24  private WPI_TalonSRX m_Left;
    1125
    12 To use this class, you must add it to the !MotorTest project:
    13 * WPILib->Manage Vendor Libraries
    14 * -> Install new library (offline)
    15 * -> Select ctre phoneix library  (or similar)
     26  /**
     27   * This function is run when the robot is first started up and should be
     28   * used for any initialization code.
     29   */
     30  @Override
     31  public void robotInit() {
     32    xbox = new XboxController(0);     // Xbox controller on port 0
     33    m_Left = new WPI_TalonSRX(4); // CAN Talon ID 4
     34  }
    1635
    17 Modify the auto-generated template code as follows:
     36  /**
     37   * This function is called every robot packet, no matter the mode. Use
     38   * this for items like diagnostics that you want ran during disabled,
     39   * autonomous, teleoperated and test.
     40   *
     41   * <p>This runs after the mode specific periodic functions, but before
     42   * LiveWindow and SmartDashboard integrated updating.
     43   */
     44  @Override
     45  public void robotPeriodic() {
     46  }
    1847
    19 * Import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController], [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html Hand], and all of the CAN motorcontrol classes (we will be using the [https://www.ctr-electronics.com/downloads/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_w_p_i___talon_s_r_x.html WPI_TalonSRX] class); note the use of '.*' to import all of the classes in a package:
    20 {{{
    21    import edu.wpi.first.wpilibj.XboxController;
    22    import edu.wpi.first.wpilibj.GenericHID.Hand;
    23    import com.ctre.phoenix.motorcontrol.can.*;
    24 }}}
    25 * declare variable for the Xbox controller and [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599703-repeatable-low-power-movement-controlling-servos-with-wpilib servo motor] in the Robot class
    26 {{{
    27    private XboxController xbox;
    28    private WPI_TalonSRX m_rearLeft;
    29 }}}
    30 * in robotInit() instantiate the controller and motor controller objects
    31 {{{
    32    xbox = new XboxController(0);     // Xbox controller on port 0
    33    m_rearLeft = new WPI_TalonSRX(4); // CAN Talon ID 4
    34 }}}
    35 * in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly
    36 {{{
    37    // Read xbox controller left joystick x axis
    38    // value returned is between -1.0 and 1.0
    39    double x = xbox.getX(Hand.kLeft);
    40    // DC Motor controllers apply between -1.0 (full reverse)
    41    // 0=stop and +1.0 (full forward) power
    42    // Set motor speed based on joystick
    43    m_rearLeft.set(x);
    44 }}}
     48  @Override
     49  public void autonomousInit() {
     50  }
    4551
    46 The complete Robot.java is [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/DCMotor/CANCode here]
     52  /**
     53   * This function is called periodically during autonomous.
     54   */
     55  @Override
     56  public void autonomousPeriodic() {
     57  }
    4758
    48 === PWM Motor Controller Example (Macadamia)
    49 This example is for a robot with a PWM motor controller such as a TalonSR or Spark connected to the !RoboRIO PWM port 1.
     59  /**
     60   * This function is called periodically during operator control.
     61   */
     62  @Override
     63  public void teleopPeriodic() {
     64    // Read xbox controller left joystick Y axis
     65    // value returned is between -1.0 and 1.0
    5066
    51 Modify the auto-generated template code as follows:
     67    // For xbox controller, uncomment the following line
     68    // double x = xbox.getY(Hand.kLeft);
     69    // For Logitech F310 controller, uncomment the following line
     70    double x = -xbox.getRawAxis(1);
     71   
     72    // DC Motor controllers apply between -1.0 (full reverse)
     73    // 0=stop and +1.0 (full forward) power
     74    // Set motor speed based on joystick
     75    m_Left.set(x);
     76  }
    5277
    53 * Import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController], [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html Hand], and [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/Talon.html Talon] classes:
    54 {{{
    55    import edu.wpi.first.wpilibj.XboxController;
    56    import edu.wpi.first.wpilibj.GenericHID.Hand;
    57    import edu.wpi.first.wpilibj.Talon;
    58 }}}
    59 * declare variable for the Xbox controller and [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599703-repeatable-low-power-movement-controlling-servos-with-wpilib servo motor] in the Robot class
    60 {{{
    61    private XboxController xbox;
    62    private Talon m_rearLeft;
    63 }}}
    64 * in robotInit() instantiate the controller and motor controller objects
    65 {{{
    66    xbox = new XboxController(0); // Xbox controller on USB port 0
    67    m_rearLeft = new Talon(1);    // TalonSR connected to roboRIO PWM 1
    68 }}}
    69 * in teleopPeriodic() read the xbox controller and adjust the DC motor accordingly
    70 {{{
    71    // Read xbox controller left joystick x axis
    72    // value returned is between -1.0 and 1.0
    73    double x = xbox.getX(Hand.kLeft);
    74    // DC Motor controllers apply between -1.0 (full reverse)
    75    // 0=stop and +1.0 (full forward) power
    76    // Set motor speed based on joystick
    77    m_rearLeft.set(x);
    78 }}}
    79 
    80 
    81 === Running the Program
    82 
    83 After you launch the driver station, you'll need to select Teleop mode and then press the Enable button.  When you enable teleop mode, teleopInit() is called once and then teleopPeriodic() is called repeatedly until you disable the robot.
    84 
    85 === Extra Credit ===
    86 Use the left joystick Y axis to control the left motor power and the right joystick Y axis to control the right motor power...now you have a driveable robot! [[BR]]
    87 NOTE: if you are using a Logitech F310 game controller, the right joystick is read differently; see [wiki:/ControlSystems/GameControllers/LogitechF310 here]
     78  /**
     79   * This function is called periodically during test mode.
     80   */
     81  @Override
     82  public void testPeriodic() {
     83  }
     84}