This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
/
encoder sample
77 lines (60 loc) · 2.77 KB
/
encoder sample
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*----------------------------------------------------------------------------*/
/* 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.team6184.robot;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.BuiltInAccelerometer;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.SensorBase;
import edu.wpi.first.wpilibj.InterruptableSensorBase;
import edu.wpi.first.wpilibj.DigitalSource;
/**
* This is a sample program demonstrating how to use an ultrasonic sensor and
* proportional control to maintain a set distance from an object.
*/
public class Robot extends IterativeRobot {
protected DigitalSource m_aSource;
protected DigitalSource m_bSource;
Encoder sampleEncoder = new Encoder(0, 1, false, Encoder.EncodingType.k4X);
double distance = sampleEncoder.getRaw();
// distance in inches the robot wants to stay from an object
private static final double kHoldDistance = 40.0;
private BuiltInAccelerometer accel = new BuiltInAccelerometer();
// factor to convert sensor values to a distance in inches
private static final double kValueToInches = 0.125;
// proportional speed constant
private static final double kP = 0.05;
private static final int kLeftMotorPort = 0;
private static final int kRightMotorPort = 1;
private static final int kUltrasonicPort = 0;
private AnalogInput m_ultrasonic = new AnalogInput(kUltrasonicPort);
private DifferentialDrive m_robotDrive
= new DifferentialDrive(new Spark(kLeftMotorPort),
new Spark(kRightMotorPort));
/**
* Tells the robot to drive to a set distance (in inches) from an object
* using proportional control.
*/
public void robotInit() {
}
public void teleopPeriodic() {
// sensor returns a value from 0-4095 that is scaled to inches
//double currentDistance = m_ultrasonic.getValue() * kValueToInches;
//System.out.println(currentDistance);
int count = sampleEncoder.get();
// System.out.println(distance);
System.out.println(count);
// convert distance error to a motor speed
// double currentSpeed = (kHoldDistance - currentDistance) * kP;
// System.out.println(accel.getX() + ", " + accel.getY() + ", " + accel.getZ());
// drive robot
//m_robotDrive.arcadeDrive(currentSpeed, 0);
}
}