|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools |
Rating:
|
Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Help with Test Robot program (NetBeans)
Thank you both for your help but it still doesn't work and at this point I do not know if it is a electrical or programming issue. Any other advice?
|
|
#2
|
||||||
|
||||||
|
Re: Help with Test Robot program (NetBeans)
It would help to post your modified code.
|
|
#3
|
||||
|
||||
|
Re: Help with Test Robot program (NetBeans)
Updated code:
[code]package edu.wpi.first.wpilibj.templates; import edu.wpi.first.wpilibj.RobotDrive; import edu.wpi.first.wpilibj.GenericHID; import edu.wpi.first.wpilibj.Joystick; import edu.wpi.first.wpilibj.Victor; //import edu.wpi.first.wpilibj.PWM; import edu.wpi.first.wpilibj.IterativeRobot; import edu.wpi.first.wpilibj.SpeedController; import edu.wpi.first.wpilibj.Timer; /** * The VM is configured to automatically run this class, and to call the * functions corresponding to each mode, as described in the IterativeRobot * documentation. If you change the name of this class or the package after * creating this project, you must also update the manifest file in the resource * directory. */ public class TestRobot extends IterativeRobot { private static RobotDrive driveTrain; private static GenericHID leftJoystick; private static GenericHID rightJoystick; private SpeedController leftVictor; private SpeedController rightVictor; //private static PWM rightPWM; //private static PWM leftPWM; private boolean m_robotMainOverridden; /** * This function is run when the robot is first started up and should be * used for any initialization code. */ public void robotInit() { leftJoystick = new Joystick(2); leftVictor = new Victor (1); //leftPWM = new PWM (2); rightJoystick = new Joystick(1); rightVictor = new Victor (1); //rightPWM = new PWM (1); driveTrain = new RobotDrive(leftVictor, rightVictor); } /** * This function is called periodically during autonomous */ public void autonomousPeriodic() { } /** * This function is called periodically during operator control */ public void teleopPeriodic() { getWatchdog().setEnabled(true); getWatchdog().setExpiration(0.2); while (isOperatorControl() && isEnabled()) { //driveTrain.arcadeDrive(rightJoystick); driveTrain.tankDrive(leftJoystick, rightJoystick); if(leftJoystick.getTrigger()) { leftVictor.set(0.7); } else { leftVictor.set(0.0); } if(rightJoystick.getTrigger()) { rightVictor.set(0.7); } else { rightVictor.set(0.0); } } getWatchdog().feed(); } public void robotMain() { // supplied default robotMain System.out.println("Information: No user-supplied robotMain()"); m_robotMainOverridden = false; } public void autonomous() { // supplied default autonomous() System.out.println("Provided Autonomous() method running"); } public void operatorControl() { // suppled default operatorControl() System.out.println("Provided OperatorControl() method running"); } public void startCompetition() { robotMain(); if (!m_robotMainOverridden) { while(true) { // Wait for robot to be enabled while (isDisabled()) { Timer.delay(.01); } // Now enabled - check if we should run Autonomous code if (isAutonomous()) { autonomous(); while (isAutonomous() && !isDisabled()) { Timer.delay(.01); } } else { operatorControl(); // run the operator control method while (isOperatorControl() && !isDisabled()) { Timer.delay(.01); } } } } } }[code] |
|
#4
|
||||
|
||||
|
Re: Help with Test Robot program (NetBeans)
After looking over the updated code, I have come up with some more points for you to consider:
- When subclassing IterativeRobot, there is no need to override the robotMain(), autonomous(), operatorControl(), or startCompetition() methods. operatorControl() and autonomous() are used by the SimpleRobot class, and robotMain() and startCompetition() have already been setup in the IterativeRobot class - IterativeRobot is an iterative style of programming as the name implies. The teleopPeriodic() method will be automatically called at a fixed interval (about 20 ms). This means that the method needs to return quickly, so you shouldn't place any loops inside of it. - Since the periodic methods are called repeatedly, any setup code should be placed in an init method. The IterativeRobot framework provides a robotInit() method that is called once at the start of the program as well as an init method called at the start of each game period (autonomousInit(), teleopInit(), disabledInit(), testInit()). - Some of the variables for your robot class were prefaced with the 'static' keyword and thus class variables. While this shouldn't break your code, you will usually want to use instance variables instead of class variables for the main robot class. - You are still using the watchdog. While I don't have documentation to back me up, I believe most would agree that it is usually better to avoid using the watchdog these days. The RobotDrive method has built in methods for handling safety/timeouts. See the code example at the end of my post. - Inside your teleopPeriodic() method there seems to be a conflict with controlling the motors. You start by calling driveTrain.tankDrive() which sets the left and right motors. But then underneath you have a series of if statements that sets both motors to one of two values: Code:
public void teleopPeriodic() {
driveTrain.tankDrive(leftJoystick, rightJoystick);
if(leftJoystick.getTrigger()) {
leftVictor.set(0.7);
} else {
leftVictor.set(0.0);
}
if(rightJoystick.getTrigger()) {
rightVictor.set(0.7);
} else {
rightVictor.set(0.0);
}
}
Below is a modified version of your code with these changes made. One other thing I changed was moving the constructors to the variable declarations to save space. Code:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.SpeedController;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class TestRobot extends IterativeRobot {
private Joystick leftJoystick = new Joystick(2);
private Joystick rightJoystick = new Joystick(1);
private SpeedController leftVictor = new Victor(2);
private SpeedController rightVictor = new Victor(1);
private RobotDrive driveTrain = new RobotDrive(leftVictor, rightVictor);
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
driveTrain.setSafetyEnabled(true);
driveTrain.setExpiration(0.2);
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
driveTrain.tankDrive(leftJoystick, rightJoystick);
if(leftJoystick.getTrigger()) {
leftVictor.set(0.7);
}
if(rightJoystick.getTrigger()) {
rightVictor.set(0.7);
}
}
}
Sorry for the wall of text. One last thing: when using the code tags, make sure the second tag has a slash after the opening bracket like this "[/code]". |
|
#5
|
||||
|
||||
|
Re: Help with Test Robot program (NetBeans)
Thank you so much for you help! It worked perfectly and everything makes much more sense now
![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|