How do I program CAN for Talon SRX in java?

Hello,

I am having trouble with CAN. The two left side drive motors spin for 5 seconds
then stop for 2 seconds and then it starts to spin again. If anybody has example
code we can see that would be great.

Thank you team 4012

There is another thread on CAN Talons randomly stopping but usually after a minute or more. There is apparently a fairly recent tool update to address that issue so you might want to check your versions and make sure you have the latest toolchain updates.

Are you in autonomous or teleo

Also, check the firmware version number on your SRX.

Down below is my code and we want the robot to drive with arcade with a xbox controller also plz help me to get it to turn. And we are using the standard wheels on the robot

package org.usfirst.frc.team5713.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Talon;

public class Robot extends IterativeRobot {
RobotDrive drive = new RobotDrive(1,2,3,4);
Joystick driveStick = new Joystick(0);
Joystick controlStick = new Joystick(1);
Talon frontLeft = new Talon(1);
Talon rearLeft = new Talon(2);
Talon frontRight = new Talon(3);
Talon rearRight = new Talon(4);

public void robotInit() {
drive = new RobotDrive(1,2,3,4);
driveStick = new Joystick(0);
controlStick = new Joystick(1);
frontLeft = new Talon(1);
frontLeft.enableDeadbandElimination(true);
frontLeft.set(+1.0);
rearLeft = new Talon(2);
rearLeft.enableDeadbandElimination(true);
rearLeft.set(-1.0);
frontRight = new Talon(3);
frontRight.enableDeadbandElimination(true);
frontRight.set(+1.0);
rearRight = new Talon(4);
rearRight.enableDeadbandElimination(true);
rearRight.set(-1.0);

public void teleopInit(){
drive = new RobotDrive(1,2,3,4);
driveStick = new Joystick(0);
controlStick = new Joystick(1);
frontLeft = new Talon(1);
frontLeft.enableDeadbandElimination(true);
frontLeft.set(-1.0);
rearLeft = new Talon(2);
rearLeft.enableDeadbandElimination(true);
rearLeft.set(+1.0);
frontRight = new Talon(3);
frontRight.enableDeadbandElimination(true);
frontRight.set(-1.0);
rearRight = new Talon(4);
rearRight.enableDeadbandElimination(true);
rearRight.set(+1.0);

}

public void teleopPeriodic() {
while (isOperatorControl() && isEnabled()) {
drive.setSafetyEnabled(true);

double joystickLeftY = driveStick.getRawAxis(2);
double joystickLeftX = driveStick.getRawAxis(1);
drive.arcadeDrive(joystickLeftY, joystickLeftX, true);
Timer.delay(0.01); } }

also our team is using standard wheels and we have the middle wheel and back wheel connected to drive train and the middle wheel has two motors and the same applies to the other middle wheel. for going forward and backward can someone plz help

Get off of our help thread make your own jeez~Matt

I apologize for flooding mattoreo310’s thread with this reply, but I do want to help CyberTeam5713.

You do not want to construct your objects multiple times. For example, you declare and construct the frontLeft speed controller like so:

Talon frontLeft = new Talon( 1 );

If you re-construct the speed controller (as you do in robotInit() and teleopInit()), then you will get an error (“resource already allocated”). You only need to construct the speed controllers and joysticks once; remove the duplicate calls.

If you are using a RobotDrive object and the arcadeDrive() method, then you do not want to set values to the speed controllers directly. In fact, if you construct the RobotDrive object the way you are doing it:

RobotDrive drive = new RobotDrive(1, 2, 3, 4);

then you do not need to construct any speed controller objects (for the drive train speed controllers). When you set the frontLeft, frontRight, etc. to values of +1 and -1 in teleopInit and then use the RobotDrive object in teleopPeriodic, the speed controllers get conflicting signals. DO NOT construct a RobotDrive object and separate speed controllers for the same motors.

Finally, in teleopInit(), you do not want to create a while loop that doesn’t exit. This is a technique used in SampleRobot projects, but it will give you no end of grief and unpredictable behavior in an IterativeRobot project. The teleopPeriodic() method will get executed every 20ms for you. You want to quickly take care of your business and then be ready for the next execution of teleopPeriodic(). Instead of putting your code in a loop, make it the whole body of teleopPeriodic() like so:

public void teleopPeriodic() {
  double joystickLeftY = driveStick.getRawAxis(2);
  double joystickLeftX = driveStick.getRawAxis(1);
  drive.arcadeDrive(joystickLeftY, joystickLeftX, true);
}

Also, note that there is no cause to delay this method since it will get called for you 20ms later. In a SampleRobot project, you use the delay to schedule the next execution cycle; that isn’t a concern that you need to handle in IterativeRobot projects and it can negatively impact your code (if you cause your method to take longer than 20ms to complete, then unpredictable things can happen).

To recap: you only need three fields in your code (well, the controlStick isn’t actually being used yet, but you will need it later):

RobotDrive drive = new RobotDrive(1, 2, 3, 4);
Joystick driveStick = new Joystick( 0 );
Joystick controlStick = new Joystick( 1 );

If you construct these when you declare the fields (outside of any methods), then you do not need anything in robotInit() or teleopInit(). You might find that you need code in those methods later, but you don’t right now.

Ah…someone just pointed out that you are also missing a few set brackets…one to close teleopInit() and another to close your class.

Good luck but PM if you have more questions so that we don’t flood this thread.