Skip to content

Commit

Permalink
🚸 Improve error Handling in VDML (#125)
Browse files Browse the repository at this point in the history
Summary
  - Changed port number validation to return ENXIO when outside the domain of port numbers
  - Changed port validation to return ENODEV when the requested device doesn't match the actual device
  - Vision Sensor read/get functions consistently use EAGAIN when the libv5rts tells us it couldn't get data
  - Remove docstrings for EACCES since it is practically impossible to get that errno (we wait for mutex indefinitely)
  - vision_get_by_sig consistently sets errno to EDOM when unable to find objects that match (valid) arguments

Right now, our returned errnos aren't that great.

Close #117

- [X] When objects are in vision sensors FOV, but none are of a particular signature, signature ID is correct and EDOM is errno

```cpp
errno = 0; // set to 0 to make sure errno is being set properly... normally wouldn't do this
// errno was 33 b/c there were no objects to see of signature 1
pros::lcd::print(1, "%d (%d)", vision.get_by_sig(0, 1).signature, errno);
errno = 0;
// errno was 0 b/c it worked fine
pros::lcd::print(2, "%d (%d)", vision.get_by_sig(0, 2).signature, errno);
errno = 0;
// errno was flickered between 0/33 b/c there were sometimes objects of 2 to see
pros::lcd::print(3, "%d (%d)", vision.get_by_sig(2, 2).signature, errno);
errno = 0;
// errno was EINVAL (22) because 8 is an invalid signature id
pros::lcd::print(4, "%d (%d)", vision.get_by_sig(8, 2).signature, errno);
errno = 0;
// errno was EADDRINUSE (112) b/c there was a radio plugged in
pros::lcd::print(5, "%d (%d)", pros::c::vision_get_by_sig(21, 0, 1).signature, errno);
errno = 0;
// errno was ENXIO (6) b/c there's no smart port #22
pros::lcd::print(6, "%d (%d)", pros::c::vision_get_by_sig(22, 0, 1).signature, errno);
// errno was ENODEV (19) b/c there's no motor plugged into 2 (or anything for that matter)
errno = 0;
pros::lcd::print(7, "%d (%d)", pros::c::motor_get_voltage(2), errno);
```
  • Loading branch information
baylessj authored and HotelCalifornia committed Jun 11, 2019
2 parents 195dfdb + afcbc32 commit dd24ac9
Show file tree
Hide file tree
Showing 17 changed files with 1,429 additions and 390 deletions.
121 changes: 49 additions & 72 deletions include/pros/adi.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ namespace c {
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which to return
Expand All @@ -138,8 +137,7 @@ adi_port_config_e_t adi_port_get_config(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which the value
Expand All @@ -154,8 +152,7 @@ int32_t adi_port_get_value(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
Expand All @@ -175,8 +172,7 @@ int32_t adi_port_set_config(uint8_t port, adi_port_config_e_t type);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which the value
Expand Down Expand Up @@ -243,9 +239,7 @@ int32_t adi_port_set_value(uint8_t port, int32_t value);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an analog input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
*
* \param port
* The ADI port to calibrate (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -262,9 +256,8 @@ int32_t adi_analog_calibrate(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an analog input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an analog input
*
* \param port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
Expand All @@ -285,9 +278,8 @@ int32_t adi_analog_read(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an analog input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an analog input
*
* \param port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
Expand All @@ -313,9 +305,8 @@ int32_t adi_analog_read_calibrated(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an analog input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an analog input
*
* \param port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
Expand All @@ -336,9 +327,8 @@ int32_t adi_analog_read_calibrated_HR(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as a digital input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a digital input
*
* \param port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -360,9 +350,8 @@ int32_t adi_digital_read(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as a digital input.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a digital input
*
* \param port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -379,9 +368,8 @@ int32_t adi_digital_get_new_press(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as a digital output.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a digital output
*
* \param port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -399,8 +387,7 @@ int32_t adi_digital_write(uint8_t port, const bool value);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
*
* \param port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -417,9 +404,8 @@ int32_t adi_pin_mode(uint8_t port, uint8_t mode);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Motor.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an motor
*
* \param port
* The ADI port to set (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -437,9 +423,8 @@ int32_t adi_motor_set(uint8_t port, int8_t speed);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Motor.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an motor
*
* \param port
* The ADI port to get (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -453,9 +438,8 @@ int32_t adi_motor_get(uint8_t port);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Motor.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an motor
*
* \param port
* The ADI port to set (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -480,9 +464,9 @@ typedef int32_t adi_encoder_t;
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Encoder.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to read
Expand All @@ -497,9 +481,9 @@ int32_t adi_encoder_get(adi_encoder_t enc);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Encoder.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an encoder
*
* \param port_top
* The "top" wire from the encoder sensor with the removable cover side
Expand All @@ -522,9 +506,9 @@ adi_encoder_t adi_encoder_init(uint8_t port_top, uint8_t port_bottom, const bool
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Encoder.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to reset
Expand All @@ -539,9 +523,8 @@ int32_t adi_encoder_reset(adi_encoder_t enc);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Encoder.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to stop
Expand All @@ -568,9 +551,8 @@ typedef int32_t adi_ultrasonic_t;
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Ultrasonic.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param ult
* The adi_ultrasonic_t object from adi_ultrasonic_init() to read
Expand All @@ -585,9 +567,8 @@ int32_t adi_ultrasonic_get(adi_ultrasonic_t ult);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Ultrasonic.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param port_ping
* The port connected to the orange OUTPUT cable. This should be in the
Expand All @@ -606,9 +587,8 @@ adi_ultrasonic_t adi_ultrasonic_init(uint8_t port_ping, uint8_t port_echo);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Ultrasonic.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param ult
* The adi_ultrasonic_t object from adi_ultrasonic_init() to stop
Expand Down Expand Up @@ -636,9 +616,8 @@ typedef int32_t adi_gyro_t;
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Gyro.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object for which the angle will be returned
Expand All @@ -657,8 +636,8 @@ double adi_gyro_get(adi_gyro_t gyro);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a gyro
*
* \param port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
Expand All @@ -676,9 +655,8 @@ adi_gyro_t adi_gyro_init(uint8_t port, double multiplier);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Gyro.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object for which the angle will be returned
Expand All @@ -693,9 +671,8 @@ int32_t adi_gyro_reset(adi_gyro_t gyro);
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of ADI Ports, or the given
* port is not configured as an ADI Gyro.
* EACCES - Another resource is currently trying to access the ADI.
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object to be shut down
Expand Down
Loading

0 comments on commit dd24ac9

Please sign in to comment.