Skip to content

SensorDistanceEx.atTarget() never returns true #221

@jleechpe

Description

@jleechpe

Describe the bug
Using SensorDistanceEx.atTarget() never returns true even when object is in the right range

To Reproduce

        dist1 = new SensorRevTOFDistance(hardwareMap, "Distance1");
        double targetDist = 100.0;
        int thresh = 5;
        DistanceTarget target1 = new DistanceTarget(DistanceUnit.MM, targetDist, thresh);
        dist1.addTarget(target1);
        double dist = dist1.getDistance(DistanceUnit.MM);
        telemetry.addData("Lib At Target", target1.atTarget(dist));

No matter where the object is placed the value never becomes true

Expected behavior
When an object is in the expected range it should return true

Additional context

Changing

        public boolean atTarget(double currentDistance) {
            currentDistance = unit.fromUnit(unit, currentDistance);
            double clippedRange = Range.clip(currentDistance, target - threshold, target + threshold);
            if (clippedRange >= currentDistance + threshold)
                return false;
            else return !(clippedRange <= currentDistance + threshold);
        }

to

        public boolean atTarget(double currentDistance) {
            currentDistance = unit.fromUnit(unit, currentDistance);
            double clippedRange = Range.clip(currentDistance, currentDistance - threshold, currentDistance + threshold);
            if (currentDistance > clippedRange)
                return false;
            else return !(currentDistance < clippedRange);
        }

Fixes the logic by ensuring that there is a range where it will return true but also doesn't result in threshold being doubled.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions