|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Basic motor programming help
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? Code:
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);
}
}
}
Charging Champions Last edited by FRC Team CC : 11-24-2016 at 03:11 PM. |
|
#2
|
||||
|
||||
|
Re: Basic motor programming help
Quote:
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;
}
}
}
|
|
#3
|
|||
|
|||
|
Re: Basic motor programming help
Thank you so much for your help. With a few adjustments, the code worked on our motors. We greatly appreciate it.
Thanks, Charging Champions |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|