View Single Post
  #6   Spotlight this post!  
Unread 08-04-2014, 16:42
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: Using multiple classes in java to control one pwm

Quote:
Originally Posted by sthreet View Post
Sorry, the one I was using as map was outputmap. I called it map in all the events though. (Map is inputmap, will go ahead and rename it.)

Spoiler for outputmap:


package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Talon;

public class outputmap
{
//output
public Talon leftdrive=new Talon(1);
public Talon rightdrive=new Talon(2);

public Compressor compress=new Compressor(1, 1);

public Talon loader=new Talon(3);
public Solenoid shootopen=new Solenoid(1);
public Solenoid shootclose=new Solenoid(2);
public DigitalInput winchstopper=new DigitalInput(2);
public DigitalInput winchstarter=new DigitalInput(3);

public Solenoid largeopen=new Solenoid(5);
public Solenoid largeclose=new Solenoid(6);

public Solenoid smallopen=new Solenoid(3);
public Solenoid smallclose=new Solenoid(4);

public Talon pickup=new Talon(4);

//inputs
public AnalogChannel distance=new AnalogChannel(1);

//constants
public int reversetimer=2000;
public int shoottimer=1000;
public int pistontimer=1000;
public int shootdistance=36;
public int slowdistance=24;
public int stopdistance=12;
}



Also, how do I make it public so I don't have to write:
Code:
import edu.wpi.first.wpilibj.templates.outputmap;

//inside of class
outputmap map=new outputmap();
every time?
If you make the variables in the outputmap class static (i.e. class variables instead of instance variables), then you can access them directly without an instance.

Code:
public class outputmap 
{
    //output
    public static Talon leftdrive=new Talon(1);
    public static Talon rightdrive=new Talon(2);
...
}

// Somewhere else in the code
Talon myMotor = outputmap.leftdrive;
As was mentioned before, this is an example of the Singleton design pattern. Note that static classes are not necessarily the best way to handle this scenario. Another method is to have the class manage a single shared instance. See the WPILib DriverStation class for example.
__________________

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