Using multiple classes in java to control one pwm

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.
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:

[cRIO] Uncaught exception in
[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(
[cRIO] at edu.wpi.first.wpilibj.PWM.<init>(
[cRIO] at edu.wpi.first.wpilibj.SafePWM.<init>(
[cRIO] at edu.wpi.first.wpilibj.Talon.<init>(
[cRIO] at edu.wpi.first.wpilibj.templates.outputmap.<init>(
[cRIO] at<init>(
[cRIO] at edu.wpi.first.wpilibj.templates.IO.<init>(
[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
[cRIO] at
[cRIO] in virtual method 47 of com.sun.squawk.VMThread(bci=42)
[cRIO] in static method 3 of com.sun.squawk.VM(bci=6)



Can you paste your

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, you can just make new variables in the two other classes that point to the existing object.
Talon motor = map.getMotor();
Or just map.motor if you make it public, but encapsulation.

@sthreet: Or this, in which case 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.

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.)


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
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);

public AnalogChannel distance=new AnalogChannel(1);

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:

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.

public class outputmap 
    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.
outputmap x = new outputmap();

Static variables and methods, however, do not require this.
Please correct me if anything I said is horribly wrong :slight_smile:

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.