How to Detect Device in Port

I am working on a piece of code that alerts the program if a device is missing. For example, the program knows that there is a motor in port 1 and detects if there is a device plugged in. I have been looking through the pros include files on GitHub and have found the command:
v5_device_e_t registry_get_plugged_type(uint8_t port);
It is in the pros/include/vdml/registry.h, unfortunately, that file doesn’t exist in my pros project.

Is there another function that can detect if a motor or vision sensor is plugged in or is there a way to get the above function to work?

2 Likes

I believe the function registry_get_plugged_type would be the one to use, however, it’s not meant for external use so it’s not declared in any of the user headers. If you include pros/apix.h and then declare it by writing extern "C" v5_device_e_t pros::c::registry_get_plugged_type(uint8_t port); you should be able to use it. Please note that because it is an internal function, it indexes port numbers starting from 0, not 1, so the valid range is 0-20 instead of 1-21. In addition, while it is unlikely, it is possible that the function is modified or removed in future PROS releases.

If you do plan on continuing with this, I would recommend filing a feature request here to see if a user accessible function could be added to properly support getting the plugged in device type.

Another way to accomplish this would be to try to use the device as either a motor or vision sensor and if PROS_ERR is returned and if errno is set to ENODEV no device is plugged in, and if errno is set to EADDRINUSE, a different type of device is plugged in. Though, to do this, you do have to attempt to perform an operation on the port, and I understand that this isn’t always optimal, so a dedicated function to get the plugged in device type would probably be better.

3 Likes

Thanks for ideas! I ended up doing something very similar, when you request the temp/ velocity or voltage of the motor when it isn’t plugged in it returns INT_MAX so I just detected that.