Hi, I am having an error whenever I run certain code on the roborio.
When i commented out all of the code in method driveForward() except the variable declarations, the console doesn’t output any errors, but when I uncomment and include leftEncoder.reset() and rightEncoder.reset(), I get an error that disables my robot. Any help is appreciated.
This is the code I am running:
package org.usfirst.frc.team5242.robot;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.BuiltInAccelerometer;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.SampleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.interfaces.Accelerometer;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
-
This is a sample program to demonstrate the use of a soft potentiometer and
-
proportional control to reach and maintain position setpoints on an elevator
-
mechanism. A joystick button is used to switch elevator setpoints.
-
WARNING: While it may look like a good choice to use for your code if you’re
-
inexperienced, don’t. Unless you know what you are doing, complex code will
-
be much more difficult under this system. Use IterativeRobot or Command-Based
-
instead if you’re new.
*/
public class Robot extends SampleRobot {
//Drive Objects
BuiltInAccelerometer accel;
RobotDrive drive;
Joystick stick;
Encoder leftEncoder;
Encoder rightEncoder;
Accelerometer accelerometer;final int kPotChannel = 1; // analog input pin
final int kMotorChannel = 7; // PWM channel
final int kJoystickChannel = 0; // usb number in DriverStation
final int kButtonNumber = 4; // joystick buttonfinal double kSetPoints] = { 1.0, 2.6, 4.3 }; // bottom, middle, and top
// elevator setpointsfinal double kP = 1.0; // proportional speed constant
double motorSpeed;
double currentPosition; // sensor voltage reading corresponding to current
// elevator position// make objects for the potentiometer, elevator motor controller, and
// joystick
AnalogInput potentiometer = new AnalogInput(kPotChannel);
Victor elevatorMotor = new Victor(kMotorChannel);
Joystick joystick = new Joystick(kJoystickChannel);public Robot() {
}public void driveForward2(float distance, float speed)
{}
public void driveForward(float distance, float speed)
{
int pulsesPerRev = 1024;
double inchesPerRev = 6.0 * 3.141;
double currentSpeed;
double currentDistance;
double acceleration;
double deceleration;
double driveDelay;currentSpeed = 0.0; currentDistance = 0.0; acceleration = 0.01; deceleration = 0.02; driveDelay = 0.01; //rightEncoder.reset(); //leftEncoder.reset(); rightEncoder.setDistancePerPulse(inchesPerRev / pulsesPerRev); /*while(currentDistance < distance) { SmartDashboard.putNumber("Distance", currentDistance); if (currentSpeed < speed) { currentSpeed = currentSpeed + acceleration; } //drive.tankDrive(currentSpeed, currentSpeed); drive.tankDrive(0, 0); Timer.delay(driveDelay); currentDistance = (rightEncoder.getDistance() + leftEncoder.getDistance()) / 2; } while (currentSpeed > 0) { currentSpeed = currentSpeed - deceleration; if (currentSpeed < 0.0) currentSpeed = 0.0; drive.tankDrive(currentSpeed, currentSpeed); Timer.delay(driveDelay); } */
}
/**
- Runs during autonomous.
*/
@Override
public void autonomous() {
//drive.setSafetyEnabled(false);
SmartDashboard.putNumber(“start go”, 1);
driveForward((float) 24.0,(float) 5.0);
SmartDashboard.putNumber(“stop”, 5);
//drive.setSafetyEnabled(true);
}
/**
-
Moves elevator to a selectable setpoint that can be changed by pressing a
-
button on the joystick. Proportional control is used to reach and
-
maintain the desired setpoint by obtaining values from the potentiometer
-
and comparing them to the setpoint value.
*/
@Override
public void operatorControl() {
boolean buttonState;
boolean prevButtonState = false;int index = 0; // setpoint array index
double currentSetpoint; // holds desired setpoint
currentSetpoint = kSetPoints[0]; // set to first setpointwhile (isOperatorControl() && isEnabled()) {
buttonState = joystick.getRawButton(kButtonNumber); // check if
// button is
// pressed// if button has been pressed and released once if (buttonState && !prevButtonState) { index = (index + 1) % kSetPoints.length; // increment set point, // reset if at end // of // array currentSetpoint = kSetPoints[index]; // set setpoint } prevButtonState = buttonState; // record previous button state currentPosition = potentiometer.getAverageVoltage(); // get position // value motorSpeed = (currentPosition - currentSetpoint) * kP; // convert // position // error // to // speed elevatorMotor.set(motorSpeed); // drive elevator motor
}
}
/**
- Runs during test mode
*/
@Override
public void test() {
}
}
- Runs during autonomous.