Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Block Class Public #11

Closed
nhalstead opened this issue Feb 21, 2020 · 3 comments
Closed

Make Block Class Public #11

nhalstead opened this issue Feb 21, 2020 · 3 comments

Comments

@nhalstead
Copy link

nhalstead commented Feb 21, 2020

When making some helper functions to adjust some of the values of the Block Instance, I got an error when trying to make a new instance of the Block Object.

Here is the error I get from the IDE (VS Code) about creating a new Instance of Block.

No enclosing instance of type Pixy2CCC is accessible. Must qualify the allocation with an enclosing instance of type Pixy2CCC (e.g. x.new A() where x is an instance of Pixy2CCC).

Yes I do see that this is related to issue #4 but I feel that this is a good reason to expose this class as public.

Here is some of the code that I can using to create a new instance and if interested later on in the season I could make a pull request to add in features from what I have added here.

import frc.robot.Constants.IntakeConstants;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import io.github.pseudoresonance.pixy2api.Pixy2;
import io.github.pseudoresonance.pixy2api.Pixy2CCC.Block;

public class Intake extends SubsystemBase {

	private Pixy2 pixy = Pixy2.createInstance(IntakeConstants.pixyLinkType);

	/**
	 * Converts the Block X and Y coordinates to be
	 *  relative to the center of the frame.
	 * 
	 * @return Block relative to the Center of the screen.
	 */
	private Block convertToCenterBlock(Block block) {
		Block newElement = new Block(
			block.getSignature(),
			this.getXFromOrigin(block),
			this.getYFromOrigin(block),
			block.getWidth(),
			block.getHeight(),
			block.getAngle(),
			block.getIndex(),
			block.getAge()
		);

		return newElement;
	}

	/**
	 * Get the X Offset from Center of the Frame to the
	 *  center of the given block.
	 * 
	 * @see https://stackoverflow.com/a/14880815/5779200
	 * @param block
	 * @return
	 */
	private Integer getXFromOrigin(Block block) {
		double frameHeight = this.pixy.getFrameHeight();
		double blockY = block.getY();

		return (int)(frameHeight / 2 + blockY);
	}

	/**
	 * Get the Y Offset from Center of the Frame to the
	 *  center of the given block.
	 * 
	 * @see https://stackoverflow.com/a/14880815/5779200
	 * @param block
	 * @return
	 */
	private Integer getYFromOrigin(Block block) {
		double frameWidth = this.pixy.getFrameWidth() / 2;
		double blockX = block.getX();

		return (int)(blockX + frameWidth / 2);
	}
}
@nhalstead nhalstead changed the title Make Block Public Make Block Class Public Feb 21, 2020
@PseudoResonance
Copy link
Owner

The issue here is not the access modifier, but instead it's because the block class is not static. At the end of the error it says x.new A() where x is an instance of Pixy2CCC, meaning that in order to create a new Block, you need an instance of Pixy2CCC. You should be able to use the Pixy2CCC object provided by the main Pixy2 class to instantiate a new Block with something like pixy.getCCC().new Block()

I will look at the code later today to see if it would make sense to make the class static however.

@nhalstead
Copy link
Author

Ahh. I see, I did take notice to this earlier and tried to write the same block of code you have put in the last comment and failed and lead myself down the wrong diagnosis, but I see the correct way to do it now.

Thanks, as far as making that class static, I don't see a reason to not have it static considering it does not depend on anything within the the Pixy2CCC class instance. Additionally a weak reason to have to have it separate is just how the community tends to organize classes is by putting them in their own files, not saying correct or wrong its just what is commonly seen.

@PseudoResonance
Copy link
Owner

The new version 1.4 makes these classes static, as like you said, they do not reference anything non-static and are not tied to the specific instances of their respective container classes.

I also added some other changes I had fixed to to keep the API as similar as possible to the original C++ Pixy2 API.

Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants