This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
/
testing_theultrasonicjava.java
121 lines (71 loc) · 3.12 KB
/
testing_theultrasonicjava.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*----------------------------------------------------------------------------*/
/* 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.Joystick;
/**
* 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 testing_theultrasonicjava extends IterativeRobot {
// distance in inches the robot wants to stay from an object
private static final double kHoldDistance = 100.0;
private BuiltInAccelerometer accel = new BuiltInAccelerometer();
// factor to convert sensor values to a distance in inches
private static final double kValueToInches = 0.125;
private Joystick m_leftStick;
private Joystick m_rightStick;
// 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;
final double BUFFER = 0.05;
private AnalogInput m_ultrasonic = new AnalogInput(kUltrasonicPort);
private DifferentialDrive m_robotDrive
= new DifferentialDrive(new Spark(1),
new Spark(0));
Spark mR = new Spark(0), mL = new Spark(1);
/**
* Tells the robot to drive to a set distance (in inches) from an object
* using proportional control.
*/ public void robotInit() {
m_leftStick = new Joystick(0);
m_rightStick = new Joystick(1);
}
public void teleopPeriodic() {
// sensor returns a value from 0-4095 that is scaled to inches
double currentDistance = m_ultrasonic.getValue() * kValueToInches;
// System.out.println(accel.getX() + ", " + accel.getY() + ", " + accel.getZ());
// convert distance error to a motor speed
double currentSpeed = (kHoldDistance - currentDistance) * kP;
System.out.println(currentDistance);
// drive robot
m_robotDrive.arcadeDrive(currentSpeed, 0);
double m;
if (m_leftStick.getRawButton(3))
m = 0.6; // Boost power
else
m = .6;
double y = m_leftStick.getY(); // How much "y"
double z = m_leftStick.getZ(); // How much "z"
if (y > BUFFER || y <= -BUFFER) {
mR.setSpeed(-y * currentSpeed);
mL.setSpeed(y * currentSpeed);
} else if (z > BUFFER || z <= -BUFFER) {
mR.setSpeed(-z*currentSpeed);
mL.setSpeed(z*currentSpeed);
} else {
mR.setSpeed(0);
mL.setSpeed(0);
}
}
}