View Single Post
  #2   Spotlight this post!  
Unread 24-11-2016, 16:33
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 296
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
Re: Basic motor programming help

Quote:
Originally Posted by FTC Team CC View Post
Code:
    public void autonomousPeriodic() {
    	while(isAutonomous() && isEnabled()) {
    		// ...
    		// ...
    		Timer.delay(2.0);
    		// ...
    	}
    }
Don't do this! Periodic functions are supposed to run at 50 Hz, so you should not be doing a long while loop or a delay in them. Also, autonomousPeriodic() will only run if isAutonomous() && isEnabled() is true, so you don't need to do that check yourself.
Instead, you should implement a looping state machine so you can ensure that each loop iteration is as short as possible. I've also done some refactoring for you

Code:
public class Robot extends IterativeRobot {
    static final long AUTO_DRIVE_TIME = 2000; // ms
    static final int CAN_ID_DRIVE0 = 1;
    static final long CAN_ID_DRIVE1 = 15;

    static final double[] AUTO_DRIVE_SPEED = new double[]{-0.5, 0.0};

    enum AutoState {
        DRIVING,
        STOPPED
    }
    
    RobotDrive m_robotDrive;
    CANTalon m_testMotor0;
    CANTalon m_testMotor1;

    AutoState m_autoState;
    long m_autoStartTime;
	
    public void robotInit() {
        m_testMotor0 = new CANTalon(CAN_ID_DRIVE0);
        m_testMotor1 = new CANTalon(CAN_ID_DRIVE1);
    	m_robotDrive = new RobotDrive(m_testMotor0, m_testMotor1);
    }
    
    public void autonomousInit() {
        m_autoState = AutoState.DRIVING;
        m_autoStartTime = System.currentTimeMillis();
    }
    
    public void autonomousPeriodic() {
    	switch(m_autoState) {
        case AutoState.DRIVING:
            m_robotDrive.drive(AUTO_DRIVE_SPEED[0], AUTO_DRIVE_SPEED[1]);
            if(System.currentTimeMillis() - m_autoStartTime >= AUTO_DRIVE_TIME) {
                m_autoState = AutoState.STOPPED;
            }
            break;
        case AutoState.STOPPED:
            m_robotDrive.drive(0, 0);
            break;
        }
    }
}
__________________
Creator of SmartDashboard.js, an extensible nodejs/webkit replacement for SmartDashboard


https://ligerbots.org
Reply With Quote