So I’m trying to figure out how to do multiple classes. This is the strucuture of the classes, where everything in events is a system on the robot. http://www.chiefdelphi.com/forums/attachment.php?attachmentid=16785&d=1396919131
The problem is, I put all the pwm stuff in map, so when the second one runs it complains about that pwm already being assigned. How do I get around this?
Here is the error I get, incase I misinterpreted it:
blah
[spoiler]
[cRIO] Uncaught exception in Thread.run():
[cRIO] on thread edu.wpi.first.wpilibj.templates.IO - main
[cRIO] edu.wpi.first.wpilibj.util.AllocationException: PWM channel 1 on module 1 is already allocated
[cRIO] at edu.wpi.first.wpilibj.PWM.initPWM(PWM.java:114)
[cRIO] at edu.wpi.first.wpilibj.PWM.<init>(PWM.java:144)
[cRIO] at edu.wpi.first.wpilibj.SafePWM.<init>(SafePWM.java:33)
[cRIO] at edu.wpi.first.wpilibj.Talon.<init>(Talon.java:48)
[cRIO] at edu.wpi.first.wpilibj.templates.outputmap.<init>(outputmap.java:12)
[cRIO] at edu.wpi.first.wpilibj.templates.events.shoot.<init>(shoot.java:8)
[cRIO] at edu.wpi.first.wpilibj.templates.IO.<init>(IO.java:24)
[cRIO] in virtual method 11 of com.sun.squawk.Klass(bci=53)
[cRIO] at com.sun.squawk.imp.MIDletMainWrapper.main(99)
[cRIO] in virtual method #95 of com.sun.squawk.Klass(bci=25)
[cRIO] at com.sun.squawk.Isolate.run(1506)
[cRIO] at java.lang.Thread.run(231)
[cRIO] in virtual method 47 of com.sun.squawk.VMThread(bci=42)
[cRIO] in static method 3 of com.sun.squawk.VM(bci=6)
[/spoiler]
If at any point a conflict in PWM ports is detected your project will return an error and a stacktrace like the one you pasted. You will probably need to reassign the port in code, as well as move the physical cable itself on your Digital Sidecar.
If you declare a talon object in map.java, you can just make new variables in the two other classes that point to the existing object.
Ex:
Talon motor = map.getMotor();
Or just map.motor if you make it public, but encapsulation.
@sthreet: Or this, in which case map.java would have to initialize your Talon objects with the constants, e.g.
Talon motor = new Motor(PWM_PORT_ON_DSC);
For this purpose you may want to look into the Singleton design pattern. Be careful that multiple classes aren’t attempting to access the Talon object at the same time, which appears to be your initial problem.
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;
}
[/spoiler]
Also, how do I make it public so I don’t have to write:
import edu.wpi.first.wpilibj.templates.outputmap;
//inside of class
outputmap map=new outputmap();
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.
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.
Thanks, it worked perfectly. Doesn’t seem to like multiple instances.
That is really weird, that you can access a static variable when you can’t access a non-static one, I can see something like public/private/semiprivate where semiprivate is the same as current public and public is the same as current public static.
I do come from python and javascript though, so not having types to variables must be weird here.
From my (very basic) understanding, non static variables and methods can only be used if an object of their class is created. So, if you kept the talons non static, you would have to actually create an instance of the outputmap.
So,
outputmap x = new outputmap();
Static variables and methods, however, do not require this.
Please correct me if anything I said is horribly wrong
This is correct. Non-static variables belong to a specific instance of a class; each instance has it’s own copy. Static variables belong to the class itself; they exist independent of an instance. Before, when the first instance of outputmap was created, it allocated the IO channels that OP was using. If a second instance was attempted, it would be trying to reallocate those IO ports. Since you can only have one instance of a given IO channel, an AllocationException was thrown.