Log in

View Full Version : Basic motor programming help


FRC Team CC
24-11-2016, 14:51
Hello,

We are trying to get two motors to run with this program, but when we run it, the build is successful but nothing happens. We are using the Java Deploy feature to compile the program to the RoboRio. Both of our Talon SRX motors have successfully connected to our RoboRio using CAN cables and are showing orange alternating lights.

We are assuming that the motors should automatically run once we deploy the program, but we may be wrong about this. Is there something wrong in our code below or our method of compilation?

package org.usfirst.frc.team6560.robot;

//import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.SampleRobot;
//import edu.wpi.first.wpilibj.livewindow.LiveWindow;

/**
* 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 Robot extends SampleRobot {
RobotDrive myRobot;
Joystick stick;
int autoLoopCounter;
CANTalon testMotor0 = new CANTalon(1);
CANTalon testMotor1 = new CANTalon(15);

/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
myRobot = new RobotDrive(testMotor0, testMotor1);
//stick = new Joystick(0);
}

/*
* This function is run once each time the robot enters autonomous mode
*/
public void autonomousInit() {
//autoLoopCounter = 0;
}


/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
//testMotor0.set(0.2);
while(isAutonomous() && isEnabled()) {
myRobot.setSafetyEnabled(false);
myRobot.drive(-0.5, 0.0);
Timer.delay(2.0);
myRobot.drive(0.0, 0.0);
}
}

}

Thanks,
Charging Champions

euhlmann
24-11-2016, 16:33
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


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;
}
}
}

FRC Team CC
26-11-2016, 14:36
Thank you so much for your help. With a few adjustments, the code worked on our motors. We greatly appreciate it.

Thanks,
Charging Champions