Go to Post PS. Never write to a forum when your cat is trying to lie on the keyboard while you type. It made my bad spelling worse. - Seth Mallory [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 07-04-2014, 21:07
sthreet's Avatar
sthreet sthreet is offline
Registered User
AKA: scott threet
FRC #4692
 
Join Date: Oct 2012
Rookie Year: 2012
Location: Toutle Lake
Posts: 84
sthreet is an unknown quantity at this point
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.
http://www.chiefdelphi.com/forums/at...5&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:
Spoiler for blah:

[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>(o utputmap.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)
Attached Thumbnails
Click image for larger version

Name:	tree.PNG
Views:	67
Size:	13.3 KB
ID:	16785  
__________________
Spoiler for gif:
Reply With Quote
  #2   Spotlight this post!  
Unread 07-04-2014, 21:11
NWChen's Avatar
NWChen NWChen is offline
Alum
no team
 
Join Date: Oct 2012
Rookie Year: 2012
Location: New York City
Posts: 205
NWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to behold
Re: Using multiple classes in java to control one pwm

Can you paste your map.java?

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.
__________________
2012 - 2015 • Team 2601


Last edited by NWChen : 08-04-2014 at 14:13. Reason: typo, "past" -> "paste"
Reply With Quote
  #3   Spotlight this post!  
Unread 08-04-2014, 13:54
vgdude999 vgdude999 is offline
Programmer/Driver
FRC #1751 (Comsewogue Warriors)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2012
Location: Port Jefferson Station, NY
Posts: 25
vgdude999 is an unknown quantity at this point
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.
__________________
2013: Rookie year, programmer-in-training; SBPLI Regional Finalists
2014: Co-Captain, Programmer, Driver; Seeded 15th
2015: Co-Captain, Programmer, Driver
Reply With Quote
  #4   Spotlight this post!  
Unread 08-04-2014, 14:12
NWChen's Avatar
NWChen NWChen is offline
Alum
no team
 
Join Date: Oct 2012
Rookie Year: 2012
Location: New York City
Posts: 205
NWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to beholdNWChen is a splendid one to behold
Re: Using multiple classes in java to control one pwm

Quote:
Originally Posted by vgdude999 View Post
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();
@sthreet: Or this, in which case map.java would have to initialize your Talon objects with the constants, e.g.
Code:
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.
__________________
2012 - 2015 • Team 2601

Reply With Quote
  #5   Spotlight this post!  
Unread 08-04-2014, 16:27
sthreet's Avatar
sthreet sthreet is offline
Registered User
AKA: scott threet
FRC #4692
 
Join Date: Oct 2012
Rookie Year: 2012
Location: Toutle Lake
Posts: 84
sthreet is an unknown quantity at this point
Re: Using multiple classes in java to control one pwm

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?
__________________
Spoiler for gif:
Reply With Quote
  #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
  #7   Spotlight this post!  
Unread 09-04-2014, 16:58
sthreet's Avatar
sthreet sthreet is offline
Registered User
AKA: scott threet
FRC #4692
 
Join Date: Oct 2012
Rookie Year: 2012
Location: Toutle Lake
Posts: 84
sthreet is an unknown quantity at this point
Re: Using multiple classes in java to control one pwm

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.
__________________
Spoiler for gif:
Reply With Quote
  #8   Spotlight this post!  
Unread 09-04-2014, 23:50
vgdude999 vgdude999 is offline
Programmer/Driver
FRC #1751 (Comsewogue Warriors)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2012
Location: Port Jefferson Station, NY
Posts: 25
vgdude999 is an unknown quantity at this point
Quote:
Originally Posted by sthreet View Post
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
__________________
2013: Rookie year, programmer-in-training; SBPLI Regional Finalists
2014: Co-Captain, Programmer, Driver; Seeded 15th
2015: Co-Captain, Programmer, Driver
Reply With Quote
  #9   Spotlight this post!  
Unread 10-04-2014, 06:19
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 vgdude999 View Post
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.
__________________

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

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