I’ve ran into confusion with the constructor for the Ultrasonic class. We are going to be using an Ultrasonic sensor for our gear auton.
I’ve really only programmed using PWM, so I’m confused on how this will work. I understand that it is digital, so I assume it will go into one of the DIO ports.
Here is the sensor that we are using http://firstchoicebyandymark.com/fc17-141
*we also have the one from the kit of parts but this one already has a cable and I assume it’s better.
Reading the API docs, it states that there is a pingChannel, and an echoChannel. If it’s plugged into the DIO port, wouldn’t there only be one channel that is accessible, or does each DIO port have an input and output channel?
Does the sensor need to use 2 ports if that’s not the case?
That diagram shows that pins 1 and 3 are power. Depending on the distance pin 4 changes voltage with pin 3 as a ground.
You would have to use the Analog Input(AI) on the RoboRio, but the issue is the AI only accepts 0-5v, so the sensor might break it.
I would recommend that you use the MaxBotix sensor included in the KOP, it has an appropriote voltage range, or you could use the TTL or PWM outputs that is has.
If my pose is confusing dont be afraid to ask questions.
This is a generic solution that works with a variety of ultrasonic sensors that do ping/echo, such as the low-cost HC-SR04. We are using a few of those on our robot. Each HC-SR04 does require TWO DIO’s - one as an output for the ping command, and one as an input for the echo.
The MaxBotix sensor described in the PDF data sheet above can also operate this same way, if you wire it to two DIO’s, one for ping/enable (pin 4) and one for echo/response (pin 2). They call this Realtime Triggered Operation, see page 7 of the PDF linked above.
You could also operate the MaxBotix sensor in an Analog mode or a PWM mode but those won’t work with the WPIlib Ultrasonic class. In PWM mode, you’d only require one DIO input. You’ll probably want to use a Counter to measure the pulse width:
The nice thing about using an HC-SR04 or some other sensor in ping/echo mode is the Ultrasonic class does all the work for you. Once wired, you just declare and instantiate the Ultrasonic object with the two port numbers, and call setAutomaticMode() to have the RoboRio constantly ping for distance in the background. Then whenever you need your distance you can just ask for it, already converted to inches. Plus, if you have multiple sensors, the Ultrasonic class will automatically take care of round-robin querying them, but note that this will slow down each sensor’s effective response time. One thing I discovered is that even if you instantiate multiple Ultrasonic object instances, you only have to call setAutomaticMode() once on any one of them. Don’t call it a second time or you will get thread exceptions.