PseudoResonance / Pixy2JavaAPI

Pixy2 API ported to Java for FIRST Robotics RoboRIO
37 stars 11 forks source link

Make Block Class Public #11

Closed nhalstead closed 4 years ago

nhalstead commented 4 years ago

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);
    }
}
PseudoResonance commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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!