Thanks to all on this sight for the guidance offered to all teams. We are working hard trying to figure out how to write code and we need some help. below is the code we have written so far. What we want to have happen is for our drive system to be arcade and we are using the left joystick on our xbox controller and we have two shooter motors. Ideally I would like to have the shooter motors turn on and off with one button but I am not sure how to write that so I attempted to start the motors with the press of one button and turn them off with another button. So far nothing works. The drive motors turn in opposite directions, so I attempted to invert the motors. This allowed us to drive forward and reverse but we could not turn. I am not concerned with autonomous at this time just teleop. I would appreciate any insight into problems with this code and suggestions on how to make it work. Thank you in advance. (sorry i’m not sure the best way to attache my code.)
package org.usfirst.frc.team5676.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.*;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SpeedController;
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 IterativeRobot {
RobotDrive DriveTrain41;
SpeedController victorsp1;
SpeedController victorsp2;
SpeedController victorsp3;
SpeedController victorsp4;
SpeedController victorsp5;
SpeedController victorsp6;
RobotDrive shootermotors;
Joystick xboxcontroller;
JoystickButton Leftjoystick;
JoystickButton xbutton;
JoystickButton ybutton;
int autoLoopCounter;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
DriveTrain41 = new RobotDrive(victorsp1,victorsp2,victorsp3, victorsp4);
shootermotors = new RobotDrive(victorsp5,victorsp6);
xboxcontroller = new Joystick(0);
xbutton = new JoystickButton(xboxcontroller,8);
ybutton = new JoystickButton(xboxcontroller, 9);
}
/**
* 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() {
if(autoLoopCounter < 100) //Check if we've completed 100 loops (approximately 2 seconds)
{
DriveTrain41.drive(-0.5, 0.0); // drive forwards half speed
autoLoopCounter++;
} else {
DriveTrain41.drive(0.0, 0.0); // stop robot
}
}
/**
* This function is called once each time the robot enters tele-operated mode
*/
public void teleopInit(){
}
/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
DriveTrain41.arcadeDrive(xboxcontroller);
shootermotors.setInvertedMotor(RobotDrive.MotorType.kFrontLeft, true);
if(xboxcontroller.getRawButton(8)){
shootermotors.drive(1,0);
}
else if (xboxcontroller.getRawButton(9)){
shootermotors.drive(0, 0);
}
}
/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
LiveWindow.run();
}
}
I’m not a coder, but I stayed at a Holiday Inn Express last night …
In Public Class, you create the objects for each motor controller (speedcontroller).
In Robotinit, you tell robotdrive which are your 4 drive motors.
Where do you associate the motorcontroller (victorsp1 for example) with a particular channel address (either PWM port, or CAN port)? Something like what you are doing for xboxcontroller where you associate it with Joystick(0), and then with xbutton and y button.
So you’re using teleop-periodic. This means that the teleop loop gets called over and over. You can get what you want out of the joystick buttons with this concept.
if (getButton)
shoot.start()
else
shoot.stop()
This will stop the motors if the button isn’t pressed. You don’t need another button to stop
I take that to mean that the button will have to be held down in order to turn the motors on. Is there a way that I can turn the motors on when I first press the button then turn them off when I press the button again?
Oh I see. So one way you can do it is set a boolean. Then check the boolean to see if it’s true.
Taken from Ether
teleop_init
button_previous = false;
...
teleop_periodic
button_now = get_button(); // get the button state (pressed or not pressed)
if (button_now && ! button_previous) // detect rising edge only
runMotor = ! runMotor; // if rising edge, toggle the direction boolean
button_previous = button_now; // save the button state for comparison in the next iteration
if (runMotor)
motor.set(1)
button_now = get_button(); // get the button state (pressed or not pressed)
if (button_now && ! button_previous) // detect rising edge only
runMotor = ! runMotor; // if rising edge, toggle the direction boolean
button_previous = button_now; // save the button state for comparison in the next iteration
if (runMotor)
motor.set(1)
In the past, with that logic, we ran into “bounce” problems with the buttons. As the buttons transition between states (pressed/unpressed), they throw off a lot of state transitions.
In other words, if you actually counted the number of transitions with a single button press, you can get 5 or more transitions. In essence, the 1/2 way point of a button is “undefined”, and can be either pressed or not pressed, and it can change between reads.
If it is simply starting a motor when pressed, and stopping a motor when released, you probably won’t notice it (the motor controller cycles between off/on really fast). If it is something else (like a toggle), you could end up in an unexpected state.
In order to avoid the problem, you have to set a timer. You reset the timer when a valid transition is detected. If the next detected transition is within 250 milliseconds (1/4 second), you ignore it. Remember to debounce the release too so you don’t get an accidental “press” when the button is “released”.
If you are trying to use two motors at the same time. The best way to synchronize them is use a PWM Y splitter. This will eliminate the possibilities of them not being synced.
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
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);
also our middle wheel and back are the only ones connecting to the drive train.
we have two motors on our middle wheel and two on the other middle wheel.
so we can go forward and backward
Did you confirm that your numbers for controlling the Talons are correct? Go to 10.TE.AM.2 and log into the RoboRio. Enable the lights on Talon(1), and hit save. Verify that your motor, Talon, RobioRio and code all are (1). Then do the same for Talon(2), etc .