Changes between Version 12 and Version 13 of ControlSystems/SoftwareTeam/Training/GettingStarted/DCMotor


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

Reverted to version 10.

Legend:

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

    v12 v13  
     1== DC Motor Control
     2For 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).
     3
     4PWM 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].
     5
     6Create another program using the !TimedRobot java template and name it !MotorTest:
     7 
     8=== Smart (CAN) Motor Controller Example (Hazelnut)
     9
     10This 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]. 
     11
     12To 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)
     16
     17Modify the auto-generated template code as follows:
     18
     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:
    120{{{
    2 /*----------------------------------------------------------------------------*/
    3 /* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
    4 /* Open Source Software - may be modified and shared by FRC teams. The code   */
    5 /* must be accompanied by the FIRST BSD license file in the root directory of */
    6 /* the project.                                                               */
    7 /*----------------------------------------------------------------------------*/
     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}}}
    845
    9 package frc.robot;
     46The complete Robot.java is [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/DCMotor/CANCode here]
    1047
    11 import edu.wpi.first.wpilibj.TimedRobot;
    12 import edu.wpi.first.wpilibj.XboxController;
    13 import edu.wpi.first.wpilibj.GenericHID.Hand;
    14 import com.ctre.phoenix.motorcontrol.can.*;
     48=== PWM Motor Controller Example (Macadamia)
     49This example is for a robot with a PWM motor controller such as a TalonSR or Spark connected to the !RoboRIO PWM port 1.
    1550
    16 /**
    17  * The VM is configured to automatically run this class, and to call the
    18  * functions corresponding to each mode, as described in the TimedRobot
    19  * documentation. If you change the name of this class or the package after
    20  * creating this project, you must also update the build.gradle file in the
    21  * project.
    22  */
    23 public class Robot extends TimedRobot {
    24   private XboxController xbox;
    25   private WPI_TalonSRX m_Left;
     51Modify the auto-generated template code as follows:
    2652
    27   /**
    28    * This function is run when the robot is first started up and should be
    29    * used for any initialization code.
    30    */
    31   @Override
    32   public void robotInit() {
    33     xbox = new XboxController(0);     // Xbox controller on port 0
    34     m_Left = new WPI_TalonSRX(4); // CAN Talon ID 4
    35   }
     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}}}
    3679
    37   /**
    38    * This function is called every robot packet, no matter the mode. Use
    39    * this for items like diagnostics that you want ran during disabled,
    40    * autonomous, teleoperated and test.
    41    *
    42    * <p>This runs after the mode specific periodic functions, but before
    43    * LiveWindow and SmartDashboard integrated updating.
    44    */
    45   @Override
    46   public void robotPeriodic() {
    47   }
    4880
    49   @Override
    50   public void autonomousInit() {
    51   }
     81=== Running the Program
    5282
    53   /**
    54    * This function is called periodically during autonomous.
    55    */
    56   @Override
    57   public void autonomousPeriodic() {
    58   }
     83After 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.
    5984
    60   /**
    61    * This function is called periodically during operator control.
    62    */
    63   @Override
    64   public void teleopPeriodic() {
    65     // Read xbox controller left joystick Y axis
    66     // value returned is between -1.0 and 1.0
    67 
    68     // For xbox controller, uncomment the following line
    69     // double x = xbox.getY(Hand.kLeft);
    70     // For Logitech F310 controller, uncomment the following line
    71     double x = -xbox.getRawAxis(1);
    72    
    73     // DC Motor controllers apply between -1.0 (full reverse)
    74     // 0=stop and +1.0 (full forward) power
    75     // Set motor speed based on joystick
    76     m_Left.set(x);
    77   }
    78 
    79   /**
    80    * This function is called periodically during test mode.
    81    */
    82   @Override
    83   public void testPeriodic() {
    84   }
    85 }
    86 }}}
     85=== Extra Credit ===
     86Use 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]]
     87NOTE: if you are using a Logitech F310 game controller, the right joystick is read differently; see [wiki:/ControlSystems/GameControllers/LogitechF310 here]