Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add Support for the Distance Sensor #257

Merged
merged 30 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0913f1a
Add Distance Header
kunwarsahni01 Sep 25, 2020
2ee04ea
Add get confidence
kunwarsahni01 Sep 25, 2020
a07f393
added distance.hpp
mooreBrendan Sep 25, 2020
bfc5a35
fixed the #ifndef in hpp file
mooreBrendan Sep 25, 2020
0a3d5f9
Added distance sensor to device type enum, also started C file for
Sep 25, 2020
fd2a4e2
Merge branch 'feature/distance-sensor' of https://github.com/purduesi…
Sep 25, 2020
dda2bbf
Added bad port or device error handling (Needs review)
Sep 25, 2020
f4d8f53
added the cpp file for the distance sensor and reverted main
mooreBrendan Sep 25, 2020
bdef884
Deleted unnecesary old comment regarding adding error handling
Sep 26, 2020
b15afaf
Added distance header files to api header.
Sep 29, 2020
5fa435c
Fixed error handling function
Sep 29, 2020
72b8016
Added missing semi colon
Sep 29, 2020
7e73e49
Added std:: to cpp files, and return types in the cpp files themselves.
Sep 29, 2020
cfd8e38
Removed unnecessary using namespace pros::c from cpp file.
Sep 29, 2020
7ad19a2
Saved file
Sep 29, 2020
050ec15
Change to Device over index
kunwarsahni01 Oct 2, 2020
19d032a
Update include/pros/distance.h
kunwarsahni01 Oct 2, 2020
7872b50
Update include/pros/distance.h
kunwarsahni01 Oct 2, 2020
6684e6e
Update include/pros/distance.h
kunwarsahni01 Oct 2, 2020
3bc747e
Update include/pros/distance.h
kunwarsahni01 Oct 2, 2020
bb2c816
Apply suggestions from code review
kunwarsahni01 Oct 2, 2020
ad938e5
Fix Formatting issues
kunwarsahni01 Oct 2, 2020
6c91e5d
Fixed bad macro and extraneous newline
Oct 3, 2020
db589a9
Merge branch 'feature/distance-sensor' of https://github.com/purduesi…
Oct 3, 2020
ee2e87f
Change to device sdk function
kunwarsahni01 Oct 3, 2020
7e94cfd
Add confidence documentation
kunwarsahni01 Oct 3, 2020
c46b794
Apply suggestions from code review
kunwarsahni01 Oct 6, 2020
af6768b
Remove new line
kunwarsahni01 Oct 6, 2020
803bf93
changed get_size to int32_t in c++
mooreBrendan Oct 6, 2020
5a66cee
Change to virtual
kunwarsahni01 Oct 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

#include "pros/adi.h"
#include "pros/colors.h"
#include "pros/distance.h"
#include "pros/ext_adi.h"
#include "pros/imu.h"
#include "pros/llemu.h"
Expand All @@ -59,6 +60,7 @@

#ifdef __cplusplus
#include "pros/adi.hpp"
#include "pros/distance.hpp"
#include "pros/imu.hpp"
#include "pros/llemu.hpp"
#include "pros/misc.hpp"
Expand Down
1 change: 1 addition & 0 deletions include/pros/apix.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ typedef enum v5_device_e {
E_DEVICE_NONE = 0,
E_DEVICE_MOTOR = 2,
E_DEVICE_IMU = 6,
E_DEVICE_DISTANCE = 7,
E_DEVICE_RADIO = 8,
E_DEVICE_VISION = 11,
E_DEVICE_ADI = 12,
Expand Down
101 changes: 101 additions & 0 deletions include/pros/distance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* \file pros/distance.h
*
* Contains prototypes for functions related to the VEX Distance sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/distance.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* Copyright (c) 2017-2020, Purdue University ACM SIGBots.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef _PROS_DISTANCE_H_
#define _PROS_DISTANCE_H_

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif

/**
* Get the currently measured distance from the sensor in mm
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \param port The V5 Distance Sensor port number from 1-21
* \return The distance value or PROS_ERR if the operation failed, setting
* errno.
*/
int32_t distance_get(uint8_t port);

/**
* Get the confidence in the distance reading
*
* This is a value that has a range of 0 to 63. 63 means high confidence,
* lower values imply less confidence. Confidence is only available
* when distance is > 200mm (the value 10 is returned in this scenario).
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \param port The V5 Distance Sensor port number from 1-21
* \return The confidence value or PROS_ERR if the operation failed, setting
* errno.
*/
int32_t distance_get_confidence(uint8_t port);

/**
* Get the current guess at relative object size
*
* This is a value that has a range of 0 to 400.
* A 18" x 30" grey card will return a value of approximately 75
* in typical room lighting.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \param port The V5 Distance Sensor port number from 1-21
* \return The size value or PROS_ERR if the operation failed, setting
* errno.
*/
int32_t distance_get_object_size(uint8_t port);

/**
* Get the object velocity in m/s
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \param port The V5 Distance Sensor port number from 1-21
* \return The velocity value or PROS_ERR if the operation failed, setting
* errno.
*/
double distance_get_object_velocity(uint8_t port);
kunwarsahni01 marked this conversation as resolved.
Show resolved Hide resolved

#ifdef __cplusplus
}
}
}
#endif

#endif
114 changes: 114 additions & 0 deletions include/pros/distance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* \file pros/distance.hpp
*
* Contains prototypes for the V5 Distance Sensor-related functions.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/distance.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright (c) 2017-2018, Purdue University ACM SIGBots.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef _PROS_DISTANCE_HPP_
#define _PROS_DISTANCE_HPP_

#include <cstdint>

#include "pros/distance.h"

namespace pros {
class Distance {
public:
/**
* Creates a Distance Sensor object for the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as a Distance Sensor
*
* \param port
* The V5 port number from 1-21
*/
explicit Distance(const std::uint8_t port);

/**
* Get the currently measured distance from the sensor in mm
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \return The distance value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get();

/**
* Get the confidence in the distance reading
*
* This is a value that has a range of 0 to 63. 63 means high confidence,
* lower values imply less confidence. Confidence is only available
* when distance is > 200mm.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \return The confidence value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get_confidence();

/**
* Get the current guess at relative object size
*
* This is a value that has a range of 0 to 400.
* A 18" x 30" grey card will return a value of approximately 75
* in typical room lighting.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \return The size value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get_object_size();

/**
* Get the object velocity in m/s
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of V5 ports (1-21).
* ENODEV - The port cannot be configured as an Distance Sensor
*
* \return The velocity value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual double get_object_velocity();

/**
* Gets the port number of the distance sensor.
*
* \return The distance sensor's port number.
*/
std::uint8_t get_port();

private:
const std::uint8_t _port;
};
} // namespace pros

#endif
53 changes: 53 additions & 0 deletions src/devices/vdml_distance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* \file devices/vdml_imu.c
*
* Contains functions for interacting with the VEX Inertial sensor.
*
* Copyright (c) 2017-2019, Purdue University ACM SIGBots.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <errno.h>

#include "pros/distance.h"
#include "v5_api.h"
#include "vdml/registry.h"
#include "vdml/vdml.h"

#define ERROR_DISTANCE_BAD_PORT(device, err_return) \
if (!(vexDeviceDistanceStatusGet(device->device_info) == 0x82 || \
vexDeviceDistanceStatusGet(device->device_info) == 0x86)) { \
errno = EAGAIN; \
return_port(port - 1, err_return); \
}

int32_t distance_get(uint8_t port) {
claim_port_i(port - 1, E_DEVICE_DISTANCE);
ERROR_DISTANCE_BAD_PORT(device, PROS_ERR);
int32_t rtn = vexDeviceDistanceDistanceGet(device->device_info);
return_port(port - 1, rtn);
}

int32_t distance_get_confidence(uint8_t port) {
claim_port_i(port - 1, E_DEVICE_DISTANCE);
ERROR_DISTANCE_BAD_PORT(device, PROS_ERR);
int32_t rtn = vexDeviceDistanceConfidenceGet(device->device_info);
return_port(port - 1, rtn);
}

int32_t distance_get_object_size(uint8_t port) {
claim_port_i(port - 1, E_DEVICE_DISTANCE);
ERROR_DISTANCE_BAD_PORT(device, PROS_ERR);
int32_t rtn = vexDeviceDistanceObjectSizeGet(device->device_info);
return_port(port - 1, rtn);
}

double distance_get_object_velocity(uint8_t port) {
claim_port_i(port - 1, E_DEVICE_DISTANCE);
ERROR_DISTANCE_BAD_PORT(device, PROS_ERR);
double rtn = vexDeviceDistanceObjectVelocityGet(device->device_info);
return_port(port - 1, rtn);
}
38 changes: 38 additions & 0 deletions src/devices/vdml_distance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* \file devices/vdml_distance.cpp
*
* Contains functions for interacting with the V5 ADI.
*
* Copyright (c) 2017-2020, Purdue University ACM SIGBots.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "pros/distance.hpp"

namespace pros {

Distance::Distance(const std::uint8_t port) : _port(port) {}

std::int32_t Distance::get() {
return pros::c::distance_get(_port);
}

std::int32_t Distance::get_confidence() {
return pros::c::distance_get_confidence(_port);
}

std::int32_t Distance::get_object_size() {
return pros::c::distance_get_object_size(_port);
}

double Distance::get_object_velocity() {
return pros::c::distance_get_object_velocity(_port);
}

std::uint8_t Distance::get_port() {
return _port;
}
} // namespace pros