美文网首页
FRC | 四线编码器使用

FRC | 四线编码器使用

作者: T_K_233 | 来源:发表于2018-12-28 17:06 被阅读0次

    1. 基本介绍&工作原理

    四线编码器:

    将旋转位置或旋转量转换成模拟或数字信号的机电设备。其中两根线负责传输电流,另两根同时负责电信号传输,相互配合以起到正反转的效果。特别注意的是,该设备设定一圆周为400pulse,故编程时需要乘以系数360/400=0.9

    2. 接线方式

    CAN 总线的连接方式是: RoboRIO --> Talon 1 --> Talon 2 --> ... --> PDP
    角度传感器的接线方式是:RobotRIO(Analog In) --> Rotation Sensor
    按键的接线方式是: `RobotRIO(DIO) --> Digital Push Button

    3. 源代码

    /*----------------------------------------------------------------------------*/
    /* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
    /* Open Source Software - may be modified and shared by FRC teams. The code   */
    /* must be accompanied by the FIRST BSD license file in the root directory of */
    /* the project.                                                               */
    /*----------------------------------------------------------------------------*/
    
    package org.usfirst.frc.team5451.robot;
    
    import java.lang.invoke.SwitchPoint;
    
    import com.ctre.phoenix.motorcontrol.*;
    import com.ctre.phoenix.motorcontrol.can.TalonSRX;
    
    import edu.wpi.first.wpilibj.GenericHID.Hand;
    import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
    import edu.wpi.first.wpilibj.AnalogInput;
    import edu.wpi.first.wpilibj.Counter;
    import edu.wpi.first.wpilibj.DigitalInput;
    import edu.wpi.first.wpilibj.Encoder;
    import edu.wpi.first.wpilibj.IterativeRobot;
    import edu.wpi.first.wpilibj.Talon;
    import edu.wpi.first.wpilibj.XboxController;
    
    
    public class Robot extends IterativeRobot {
        private XboxController joystick = new XboxController(0); 
        private TalonSRX controller2 = new TalonSRX(10);
        private TalonSRX controller1 = new TalonSRX(11);
        
        private Encoder sampleEncoder = new Encoder(0, 1, false, Encoder.EncodingType.k4X);//定义四线编码器,其中0和1代表robotrio接口,false代表是否反转
        private Counter exampleCounterHi = new Counter(9);//定义三线编码器
        private DigitalInput switch1 = new DigitalInput(2);//定义按键
        private AnalogInput rotational_sensor = new AnalogInput(0);//定义角度传感器
        
        
        
        public static final int kPIDLoopIdx = 0;
        public static final int kTimeoutMs = 30;
            @Override
        public void robotInit() {
            sampleEncoder.setMaxPeriod(.1);
            sampleEncoder.setMinRate(10);
            sampleEncoder.setDistancePerPulse(0.9);//编码器设定一圆周为400单位,0.9=360/400,为系数
            sampleEncoder.setReverseDirection(true);//反转设定
            sampleEncoder.setSamplesToAverage(7);
            sampleEncoder.reset();
            
            exampleCounterHi.setMaxPeriod(.1);
            exampleCounterHi.setUpdateWhenEmpty(true);
            exampleCounterHi.setReverseDirection(false);//反转设定
            exampleCounterHi.setSamplesToAverage(10);
            exampleCounterHi.setDistancePerPulse(3.6);//变慢器设定一圆周为100单位,3.6=360/100,为系数
            
            controller1.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Absolute, kPIDLoopIdx, kTimeoutMs);
            controller1.setSensorPhase(false);
            controller1.setInverted(true);
    
            controller1.configNominalOutputForward(0, kTimeoutMs);
            controller1.configNominalOutputReverse(0, kTimeoutMs);
            controller1.configPeakOutputForward(1, kTimeoutMs);
            controller1.configPeakOutputReverse(-1, kTimeoutMs);
            
            controller1.config_kF(kPIDLoopIdx, 0.00, kTimeoutMs);
            controller1.config_kP(kPIDLoopIdx, 0.05, kTimeoutMs);
            controller1.config_kI(kPIDLoopIdx, 0.000295, kTimeoutMs);
            controller1.config_kD(kPIDLoopIdx, 0.003, kTimeoutMs);
        }
    
        @Override
        public void teleopPeriodic() {
        
            if (joystick.getAButton()) {
                
                sampleEncoder.reset();
            }
            int count = sampleEncoder.get(); 
            double distance = sampleEncoder.getRaw();
            double distance1 = sampleEncoder.getDistance();//得到四线编码器的旋转总角度
            double rate = sampleEncoder.getRate();//得到四线编码器的旋转频率
            boolean direction = sampleEncoder.getDirection();//得到旋转方向
            boolean stopped = sampleEncoder.getStopped();//确定编码器是否停止旋转
            boolean switch2 = switch1.get();//确定按键是否按下
            double rotation = rotational_sensor.getAverageValue()*0.0824;//得到角度传感器的旋转角度
            
            SmartDashboard.putNumber("counter distance", exampleCounterHi.getDistance());
            SmartDashboard.putNumber("counter rate", exampleCounterHi.getRate());
            SmartDashboard.putNumber("distance", distance1);
            SmartDashboard.putBoolean("direction", direction);
            SmartDashboard.putBoolean("stopped", stopped);
            SmartDashboard.putNumber("rate", rate);
            SmartDashboard.putBoolean("switch", switch2);
            SmartDashboard.putNumber("rotation", rotation);
                    //将数值显示在dashboard上
        }
    }
    

    相关文章

      网友评论

          本文标题:FRC | 四线编码器使用

          本文链接:https://www.haomeiwen.com/subject/nxpxhqtx.html