Using distance sensor to find goal

Hi, I’m trying to figure out how to find a goal with a distance sensor. I was thinking to turn until the sensor reports a distance below 150(approximate distance to goal) and then stopping. But I don’t know how to code this.

So, what are you trying to do specifically? Is there a goal in front of your robot that you want to drive up to using the distance sensor? Is there a goal that isn’t in front of your robot that you want to turn towards, drive to, and then grab?

If you’re just trying to drive to a goal, you can use a PID controller or similar to drive towards the goal until your robot is below that 150 millimeter distance. If you’re trying to turn towards a goal using a distance sensor, that might be a bit more difficult due to other scoring elements, robots, etc. getting in the way, but it’s still doable if you turn until your distance sensor finds the mobile goal, then drive towards it using a similar movement controller.

Hopefully this answers your question. If you are looking for resources on PID controllers, they are all over this forum, the Internet, etc. Here are a few resources I would recommend:


The goal is not infront of me and I’m trying to turn the robot to face the goal.

I’ve tried this but it dosn’t seem to exit the while loop

      if(distance_sensor.get() < 150 ){
        goalDected = true;

It might be that the robot isn’t close enough for it to report 150 millimeters of distance. Try printing your distance on the controller screen. I’m not certain what coding environment you are using (looks like PROS to me, but not totally sure), but I assume somewhere in the API documentation you can find the functions needed to do this.

If the value on the screen never goes below 150, then you need to adjust your maximum distance. What mobile goal are you specifically going for, and from where? If you’re in the home zone and trying to go for a neutral mobile goal, for example, it might not be within 150 millimeters of your distance sensor. Keep in mind that this value needs to be reliable enough to detect the mobile goal that you’re trying to turn to, but also to not detect other objects.

1 Like

This sounds kind of pointless, unless you’re just trying to learn c++. The driver can just turn towards the goal. If it’s for auton that makes sense, but whatever, you want it done so I’ll try to help.

I would say to employ the use of a color sensor as well. That way other objects being around wouldn’t cause too much of an issue, because it could just run the ultrasonic through a check of the color sensor and then it moves toward it. I don’t know sensors at all though I am going to learn later this week