Go to Post FIRST teams do not allow other FIRST teams to fail. - [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 24-01-2014, 22:11
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Motor Programming

Okay - I want to have a motor that - when button 4 is pressed on the joystick the motor moves in one direction until he button is released - when button 5 is pressed the motor moves in the other direction until the button is released.

Here is the code I wrote:

Subsystem for the "ForkMotor":

Quote:
package edu.wpi.first.LFRobot2014.subsystems;

import edu.wpi.first.LFRobot2014.RobotMap;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.command.Subsystem;

/**
*
* @author FrankyMonezz
*/
public class ForkMotor extends Subsystem {
// Put methods for controlling this subsystem
// here. Call these from Commands.
SpeedController motor = new Jaguar(RobotMap.ForkMotorPort);

public void initDefaultCommand() {
// Set the default command for a subsystem here.
//setDefaultCommand(new MySpecialCommand());
}

public void up() {
motor.set(1);
}

public void down() {
motor.set(-1);
}
}

Command to move motor in one direction.

Quote:
package edu.wpi.first.LFRobot2014.commands;


/**
*
* @author FrankyMonezz
*/
public class LiftForks extends CommandBase {

public LiftForks() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(ForkMotor);
}

// Called just before this Command runs the first time
protected void initialize() {
}

// Called repeatedly when this Command is scheduled to run
protected void execute() {
ForkMotor.up();
}

// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return false;
}

// Called once after isFinished returns true
protected void end() {
}

// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}
Command to move the motor in the other direction:

Quote:
package edu.wpi.first.LFRobot2014.commands;

/**
*
* @author FrankyMonezz
*/
public class LowerForks extends CommandBase {

public LowerForks() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(ForkMotor);
}

// Called just before this Command runs the first time
protected void initialize() {
}

// Called repeatedly when this Command is scheduled to run
protected void execute() {
ForkMotor.down();
}

// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return false;
}

// Called once after isFinished returns true
protected void end() {
}

// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
}
All ports are already set correctly in RobotMap and buttons are correctly assigned using whileHeld() in OI - noting this will the above code work?

THANKS for the help!
Reply With Quote
  #2   Spotlight this post!  
Unread 24-01-2014, 22:44
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: Motor Programming

In the constructor for each of your commands, you should pass the instance variable from CommandBase instead of the Subsystem class name:

Code:
public LiftForks() {
    // Use requires() here to declare subsystem dependencies
    // eg. requires(chassis);
    requires(forkMotor);  // or whatever you named the instance variable
}
instead of
Code:
public LiftForks() {
    // Use requires() here to declare subsystem dependencies
    // eg. requires(chassis);
    requries(ForkMotor);
}
Another potential issue is that you don't appear to ever be stopping the motor. Even when the commands end, the motor will continue to spin (at least until the motor safety kicks in). One way to solve this issue would be to add a stop() method to your Subsystem, and then calling that method in each command's end() method.
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)


Last edited by Domenic Rodriguez : 24-01-2014 at 22:45. Reason: Grammar
Reply With Quote
  #3   Spotlight this post!  
Unread 24-01-2014, 23:33
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: Motor Programming

My Subsystem name is the same as the name in commandbase.

I did not end the command because I will use the button.whileHeld(new command) and should it shut the motor/command down when I release the button so I don't have to tell it to stop. Right now I am not having any issues with it - I haven't been able to test it though because I am having DS issues.

THANKS!
Reply With Quote
  #4   Spotlight this post!  
Unread 25-01-2014, 07:13
Domenic Rodriguez's Avatar
Domenic Rodriguez Domenic Rodriguez is offline
Registered User
FRC #0316 (LuNaTeCs)
Team Role: College Student
 
Join Date: Sep 2010
Rookie Year: 2011
Location: Grove City, PA
Posts: 213
Domenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura aboutDomenic Rodriguez has a spectacular aura about
Re: Motor Programming

Quote:
Originally Posted by LFRobotics View Post
My Subsystem name is the same as the name in commandbase.
I would recommend against using the same capitalization for the class name and the instance variable; it creates ambiguity as to which you are referring to. Common practice is to use headless camel case for variable names:
Code:
ForkMotor forkMotor = new ForkMotor();
Quote:
Originally Posted by LFRobotics View Post
I did not end the command because I will use the button.whileHeld(new command) and should it shut the motor/command down when I release the button so I don't have to tell it to stop...
Correct me if I'm wrong (it's been a while since I've worked with the command-based robot template), but just because your command is canceled doesn't mean the motor will shut off by itself. You need to explicitly tell it to stop moving with "motor.set(0)", otherwise it will just keep moving at the previous value it received (in your case either 1.0 or -1.0). This is why I recommend adding a ForkMotor#stop() method that get's called when your commands exit:

Code:
public class ForkMotor extends Subsystem {
    ....
    // Existing subsystem code
    ...

    public void stop() {
        motor.set(0.0);
    }
}
Code:
public class LiftForks extends CommandBase {
    public LiftForks() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        requires(ForkMotor);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        ForkMotor.up();
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }

    // Called once after isFinished returns true
    protected void end() {
        ForkMotor.stop();
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
        end();
    }
}
__________________

LuNaTeCs - Learning Under Nurturing Adults Teaching Engineering Concepts and Skills - Small and Mighty!

FRC 316 LuNaTeCs - Student (2011-2014), Lead Programmer (2011-2014), Team Captain (2013-2014), Operator (2013), Drive Coach (2014), Mentor (2015-????)
'11 Philly Regional Finalists, '13 Chestnut Hill Finalists, '13 Lenape Champions, '13 Archimedes Division, '14 Chestnut Hill Champions, '14 Lenape Champions
FTC 7071 EngiNerds - Founding Advisor (2013-2014) | FRC 5420 Velocity - Founding Advisor (2015)
Grove City College Class of '18, Electrical/Computer Engineering (B.S.E.E)

Reply With Quote
  #5   Spotlight this post!  
Unread 25-01-2014, 10:16
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: Motor Programming

Okay - yeah your right - THANKS a ton!
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 22:34.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi