Initial commit - test serial

This commit is contained in:
Cole A. Deck
2024-03-24 22:20:00 -05:00
commit a4b1c1b7ed
273 changed files with 43716 additions and 0 deletions

View File

@ -0,0 +1,875 @@
/**
* \file pros/adi.h
*
* Contains prototypes for interfacing with the ADI.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/adi.html to learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_ADI_H_
#define _PROS_ADI_H_
#include <stdbool.h>
#include <stdint.h>
#ifndef PROS_ERR
#define PROS_ERR (INT32_MAX)
#endif
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
/**
* Represents the port type for an ADI port.
*/
typedef enum adi_port_config_e {
E_ADI_ANALOG_IN = 0,
E_ADI_ANALOG_OUT = 1,
E_ADI_DIGITAL_IN = 2,
E_ADI_DIGITAL_OUT = 3,
#ifdef _INTELLISENSE
#define _DEPRECATE_DIGITAL_IN = E_ADI_DIGITAL_IN
#define _DEPRECATE_ANALOG_IN = E_ADI_ANALOG_IN
#else
#define _DEPRECATE_DIGITAL_IN __attribute__((deprecated("use E_ADI_DIGITAL_IN instead"))) = E_ADI_DIGITAL_IN
#define _DEPRECATE_ANALOG_IN __attribute__((deprecated("use E_ADI_ANALOG_IN instead"))) = E_ADI_ANALOG_IN
#endif
E_ADI_SMART_BUTTON _DEPRECATE_DIGITAL_IN,
E_ADI_SMART_POT _DEPRECATE_ANALOG_IN,
E_ADI_LEGACY_BUTTON _DEPRECATE_DIGITAL_IN,
E_ADI_LEGACY_POT _DEPRECATE_ANALOG_IN,
E_ADI_LEGACY_LINE_SENSOR _DEPRECATE_ANALOG_IN,
E_ADI_LEGACY_LIGHT_SENSOR _DEPRECATE_ANALOG_IN,
E_ADI_LEGACY_GYRO = 10,
E_ADI_LEGACY_ACCELEROMETER _DEPRECATE_ANALOG_IN,
#undef _DEPRECATE_DIGITAL_IN
#undef _DEPRECATE_ANALOG_IN
E_ADI_LEGACY_SERVO = 12,
E_ADI_LEGACY_PWM = 13,
E_ADI_LEGACY_ENCODER = 14,
E_ADI_LEGACY_ULTRASONIC = 15,
E_ADI_TYPE_UNDEFINED = 255,
E_ADI_ERR = PROS_ERR
} adi_port_config_e_t;
/**
* Represents the potentiometer version type.
*/
typedef enum adi_potentiometer_type_e {
E_ADI_POT_EDR = 0,
E_ADI_POT_V2
} adi_potentiometer_type_e_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define ADI_ANALOG_IN pros::E_ADI_ANALOG_IN
#define ADI_ANALOG_OUT pros::E_ADI_ANALOG_OUT
#define ADI_DIGITAL_IN pros::E_ADI_DIGITAL_IN
#define ADI_DIGITAL_OUT pros::E_ADI_DIGITAL_OUT
#define ADI_SMART_BUTTON pros::E_ADI_SMART_BUTTON
#define ADI_SMART_POT pros::E_ADI_SMART_POT
#define ADI_LEGACY_BUTTON pros::E_ADI_LEGACY_BUTTON
#define ADI_LEGACY_POT pros::E_ADI_LEGACY_POT
#define ADI_LEGACY_LINE_SENSOR pros::E_ADI_LEGACY_LINE_SENSOR
#define ADI_LEGACY_LIGHT_SENSOR pros::E_ADI_LEGACY_LIGHT_SENSOR
#define ADI_LEGACY_GYRO pros::E_ADI_LEGACY_GYRO
#define ADI_LEGACY_ACCELEROMETER pros::E_ADI_LEGACY_ACCELEROMETER
#define ADI_LEGACY_SERVO pros::E_ADI_LEGACY_SERVO
#define ADI_LEGACY_PWM pros::E_ADI_LEGACY_PWM
#define ADI_LEGACY_ENCODER pros::E_ADI_LEGACY_ENCODER
#define ADI_LEGACY_ULTRASONIC pros::E_ADI_LEGACY_ULTRASONIC
#define ADI_TYPE_UNDEFINED pros::E_ADI_TYPE_UNDEFINED
#define ADI_ERR pros::E_ADI_ERR
#else
#define ADI_ANALOG_IN E_ADI_ANALOG_IN
#define ADI_ANALOG_OUT E_ADI_ANALOG_OUT
#define ADI_DIGITAL_IN E_ADI_DIGITAL_IN
#define ADI_DIGITAL_OUT E_ADI_DIGITAL_OUT
#define ADI_SMART_BUTTON E_ADI_SMART_BUTTON
#define ADI_SMART_POT E_ADI_SMART_POT
#define ADI_LEGACY_BUTTON E_ADI_LEGACY_BUTTON
#define ADI_LEGACY_POT E_ADI_LEGACY_POT
#define ADI_LEGACY_LINE_SENSOR E_ADI_LEGACY_LINE_SENSOR
#define ADI_LEGACY_LIGHT_SENSOR E_ADI_LEGACY_LIGHT_SENSOR
#define ADI_LEGACY_GYRO E_ADI_LEGACY_GYRO
#define ADI_LEGACY_ACCELEROMETER E_ADI_LEGACY_ACCELEROMETER
#define ADI_LEGACY_SERVO E_ADI_LEGACY_SERVO
#define ADI_LEGACY_PWM E_ADI_LEGACY_PWM
#define ADI_LEGACY_ENCODER E_ADI_LEGACY_ENCODER
#define ADI_LEGACY_ULTRASONIC E_ADI_LEGACY_ULTRASONIC
#define ADI_TYPE_UNDEFINED E_ADI_TYPE_UNDEFINED
#define ADI_ERR E_ADI_ERR
#endif
#endif
#define INTERNAL_ADI_PORT 22
#define NUM_ADI_PORTS 8
#ifdef __cplusplus
namespace c {
#endif
/******************************************************************************/
/** General ADI Use Functions **/
/** **/
/** These functions allow for interaction with any ADI port type **/
/******************************************************************************/
/**
* Gets the configuration for the given ADI 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 ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which to return
* the configuration
*
* \return The ADI configuration for the given port
*/
adi_port_config_e_t adi_port_get_config(uint8_t port);
/**
* Gets the value for the given ADI 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 ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which the value
* will be returned
*
* \return The value stored for the given port
*/
int32_t adi_port_get_value(uint8_t port);
/**
* Configures an ADI port to act as a given sensor type.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
* \param type
* The configuration type for the port
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_port_set_config(uint8_t port, adi_port_config_e_t type);
/**
* Sets the value for the given ADI port.
*
* This only works on ports configured as outputs, and the behavior will change
* depending on the configuration of the 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 ADI Ports.
*
* \param port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which the value
* will be set
* \param value
* The value to set the ADI port to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_port_set_value(uint8_t port, int32_t value);
/******************************************************************************/
/** PROS 2 Compatibility Functions **/
/** **/
/** These functions provide similar functionality to the PROS 2 API **/
/******************************************************************************/
/**
* Used for adi_digital_write() to specify a logic HIGH state to output.
*
* In reality, using any non-zero expression or "true" will work to set a pin to
* HIGH.
*/
#define HIGH 1
/**
* Used for adi_digital_write() to specify a logic LOW state to output.
*
* In reality, using a zero expression or "false" will work to set a pin to LOW.
*/
#define LOW 0
/**
* adi_pin_mode() state for a digital input.
*/
#define INPUT 0x00
/**
* adi_pin_mode() state for a digital output.
*/
#define OUTPUT 0x01
/**
* adi_pin_mode() state for an analog input.
*/
#define INPUT_ANALOG 0x02
/**
* adi_pin_mode() state for an analog output.
*/
#define OUTPUT_ANALOG 0x03
/**
* Calibrates the analog sensor on the specified port and returns the new
* calibration value.
*
* This method assumes that the true sensor value is not actively changing at
* this time and computes an average from approximately 500 samples, 1 ms apart,
* for a 0.5 s period of calibration. The average value thus calculated is
* returned and stored for later calls to the adi_analog_read_calibrated() and
* adi_analog_read_calibrated_HR() functions. These functions will return
* the difference between this value and the current sensor value when called.
*
* Do not use this function when the sensor value might be unstable
* (gyro rotation, accelerometer movement).
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
*
* \return The average sensor value computed by this function
*/
int32_t adi_analog_calibrate(uint8_t port);
/**
* Gets the 12-bit value of the specified port.
*
* The value returned is undefined if the analog pin has been switched to a
* different mode.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
* returned
*
* \return The analog sensor value, where a value of 0 reflects an input voltage
* of nearly 0 V and a value of 4095 reflects an input voltage of nearly 5 V
*/
int32_t adi_analog_read(uint8_t port);
/**
* Gets the 12 bit calibrated value of an analog input port.
*
* The adi_analog_calibrate() function must be run first. This function is
* inappropriate for sensor values intended for integration, as round-off error
* can accumulate causing drift over time. Use adi_analog_read_calibrated_HR()
* instead.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
* returned
*
* \return The difference of the sensor value from its calibrated default from
* -4095 to 4095
*/
int32_t adi_analog_read_calibrated(uint8_t port);
/**
* Gets the 16 bit calibrated value of an analog input port.
*
* The adi_analog_calibrate() function must be run first. This is intended for
* integrated sensor values such as gyros and accelerometers to reduce drift due
* to round-off, and should not be used on a sensor such as a line tracker
* or potentiometer.
*
* The value returned actually has 16 bits of "precision", even though the ADC
* only reads 12 bits, so that error induced by the average value being between
* two values when integrated over time is trivial. Think of the value as the
* true value times 16.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
* returned
*
* \return The difference of the sensor value from its calibrated default from
* -16384 to 16384
*/
int32_t adi_analog_read_calibrated_HR(uint8_t port);
/**
* Gets the digital value (1 or 0) of a port configured as a digital input.
*
* If the port is configured as some other mode, the digital value which
* reflects the current state of the port is returned, which may or may not
* differ from the currently set value. The return value is undefined for ports
* configured as any mode other than a Digital Input.
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
*
* \return True if the pin is HIGH, or false if it is LOW
*/
int32_t adi_digital_read(uint8_t port);
/**
* Gets a rising-edge case for a digital button press.
*
* This function is not thread-safe.
* Multiple tasks polling a single button may return different results under the
* same circumstances, so only one task should call this function for any given
* button. E.g., Task A calls this function for buttons 1 and 2. Task B may call
* this function for button 3, but should not for buttons 1 or 2. A typical
* use-case for this function is to call inside opcontrol to detect new button
* presses, and not in any other tasks.
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
*
* \return 1 if the button is pressed and had not been pressed
* the last time this function was called, 0 otherwise.
*/
int32_t adi_digital_get_new_press(uint8_t port);
/**
* Sets the digital value (1 or 0) of a port configured as a digital output.
*
* If the port is configured as some other mode, behavior is undefined.
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
* \param value
* An expression evaluating to "true" or "false" to set the output to
* HIGH or LOW respectively, or the constants HIGH or LOW themselves
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_digital_write(uint8_t port, bool value);
/**
* Configures the port as an input or output with a variety of settings.
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
* \param mode
* One of INPUT, INPUT_ANALOG, INPUT_FLOATING, OUTPUT, or OUTPUT_OD
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_pin_mode(uint8_t port, uint8_t mode);
/**
* Sets the speed of the motor on 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 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')
* \param speed
* The new signed speed; -127 is full reverse and 127 is full forward,
* with 0 being off
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_motor_set(uint8_t port, int8_t speed);
/**
* Gets the last set speed of the motor on 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 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')
*
* \return The last set speed of the motor on the given port
*/
int32_t adi_motor_get(uint8_t port);
/**
* Stops the motor on 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 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')
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_motor_stop(uint8_t port);
/**
* Reference type for an initialized encoder.
*
* This merely contains the port number for the encoder, unlike its use as an
* object to store encoder data in PROS 2.
*/
typedef int32_t adi_encoder_t;
/**
* Gets the number of ticks recorded by the encoder.
*
* There are 360 ticks in one revolution.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return The signed and cumulative number of counts since the last start or
* reset
*/
int32_t adi_encoder_get(adi_encoder_t enc);
/**
* Creates an encoder object and configures the specified ports accordingly.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
* up. This should be in port 1, 3, 5, or 7 ('A', 'C', 'E', or 'G').
* \param port_bottom
* The "bottom" wire from the encoder sensor
* \param reverse
* If "true", the sensor will count in the opposite direction
*
* \return An adi_encoder_t object to be stored and used for later calls to
* encoder functions
*/
adi_encoder_t adi_encoder_init(uint8_t port_top, uint8_t port_bottom, bool reverse);
/**
* Sets the encoder value to zero.
*
* It is safe to use this method while an encoder is enabled. It is not
* necessary to call this method before stopping or starting an encoder.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_encoder_reset(adi_encoder_t enc);
/**
* Disables the encoder and voids the configuration on its ports.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_encoder_shutdown(adi_encoder_t enc);
/**
* Reference type for an initialized ultrasonic.
*
* This merely contains the port number for the ultrasonic, unlike its use as an
* object to store ultrasonic data in PROS 2.
*/
typedef int32_t adi_ultrasonic_t;
/**
* Gets the current ultrasonic sensor value in centimeters.
*
* If no object was found, zero is returned. If the ultrasonic sensor was never
* started, the return value is undefined. Round and fluffy objects can cause
* inaccurate values to be returned.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return The distance to the nearest object in m^-4 (10000 indicates 1 meter),
* measured from the sensor's mounting points.
*/
int32_t adi_ultrasonic_get(adi_ultrasonic_t ult);
/**
* Creates an ultrasonic object and configures the specified ports accordingly.
*
* This function uses the following values of errno when an error state is
* reached:
* 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 port
* 1, 3, 5, or 7 ('A', 'C', 'E', 'G').
* \param port_echo
* The port connected to the yellow INPUT cable. This should be in the
* next highest port following port_ping.
*
* \return An adi_ultrasonic_t object to be stored and used for later calls to
* ultrasonic functions
*/
adi_ultrasonic_t adi_ultrasonic_init(uint8_t port_ping, uint8_t port_echo);
/**
* Disables the ultrasonic sensor and voids the configuration on its ports.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_ultrasonic_shutdown(adi_ultrasonic_t ult);
/**
* Reference type for an initialized gyroscope.
*
* This merely contains the port number for the gyroscope, unlike its use as an
* object to store gyro data in PROS 2.
*/
typedef int32_t adi_gyro_t;
/**
* Gets the current gyro angle in tenths of a degree. Unless a multiplier is
* applied to the gyro, the return value will be a whole number representing
* the number of degrees of rotation times 10.
*
* There are 360 degrees in a circle, thus the gyro will return 3600 for one
* whole rotation.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return The gyro angle in degrees.
*/
double adi_gyro_get(adi_gyro_t gyro);
/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1300 ms
* calibration period.
*
* It is highly recommended that this function be called from initialize() when
* the robot is stationary to ensure proper calibration.
*
* This function uses the following values of errno when an error state is
* reached:
* 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')
* \param multiplier
* A scalar value that will be multiplied by the gyro heading value
* supplied by the ADI
*
* \return An adi_gyro_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
adi_gyro_t adi_gyro_init(uint8_t port, double multiplier);
/**
* Resets the gyroscope value to zero.
*
* This function uses the following values of errno when an error state is
* reached:
* 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
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_gyro_reset(adi_gyro_t gyro);
/**
* Disables the gyro and voids the configuration on its 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 ADI Ports
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object to be shut down
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t adi_gyro_shutdown(adi_gyro_t gyro);
/**
* Reference type for an initialized potentiometer.
*
* This merely contains the port number for the potentiometer, unlike its use as an
* object to store potentiometer data in PROS 2.
*/
typedef int32_t adi_potentiometer_t;
/**
* Initializes a potentiometer on the given port of the original potentiometer.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
*
* \param port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
*
* \return An adi_potentiometer_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
adi_potentiometer_t adi_potentiometer_init(uint8_t port);
/**
* Initializes a potentiometer on 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 ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
*
* \param port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
* \param potentiometer_type
* An adi_potentiometer_type_e_t enum value specifying the potentiometer version type
*
* \return An adi_potentiometer_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
adi_potentiometer_t adi_potentiometer_type_init(uint8_t port, adi_potentiometer_type_e_t potentiometer_type);
/**
* Gets the current potentiometer angle in tenths of a degree.
*
* The original potentiometer rotates 250 degrees thus returning an angle between 0-250 degrees.
* Potentiometer V2 rotates 330 degrees thus returning an angle between 0-330 degrees.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
*
* \param potentiometer
* The adi_potentiometer_t object for which the angle will be returned
*
* \return The potentiometer angle in degrees.
*/
double adi_potentiometer_get_angle(adi_potentiometer_t potentiometer);
/**
* Reference type for an initialized addressable led.
*
* This merely contains the port number for the led, unlike its use as an
* object to store led data in PROS 2.
*/
typedef int32_t adi_led_t;
/**
* Initializes a led on the given port of the original led.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - The ADI port given is not a valid port as defined below
* EADDRINUSE - The port is not configured for ADI output
*
* \param port
* The ADI port to initialize as a led (from 1-8, 'a'-'h', 'A'-'H')
*
* \return An adi_led_t object containing the given port, or PROS_ERR if the
* initialization failed, setting errno
*/
adi_led_t adi_led_init(uint8_t port);
/**
* @brief Clear the entire led strip of color
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t adi_led_clear_all(adi_led_t led, uint32_t* buffer, uint32_t buffer_length);
/**
* @brief Set the entire led strip using the colors contained in the buffer
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t adi_led_set(adi_led_t led, uint32_t* buffer, uint32_t buffer_length);
/**
* @brief Set the entire led strip to one color
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @param color color to set all the led strip value to
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t adi_led_set_all(adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t color);
/**
* @brief Set one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of the input buffer
* @param color color to clear all the led strip to
* @param pixel_position position of the pixel to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t adi_led_set_pixel(adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t color, uint32_t pixel_position);
/**
* @brief Clear one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of the input buffer
* @param pixel_position position of the pixel to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t adi_led_clear_pixel(adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t pixel_position);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_ADI_H_

View File

@ -0,0 +1,897 @@
/**
* \file pros/adi.hpp
*
* Contains prototypes for interfacing with the ADI.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/adi.html to learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_ADI_HPP_
#define _PROS_ADI_HPP_
#include <cstdint>
#include <tuple>
#include <utility>
#include <vector>
#include "pros/adi.h"
namespace pros {
/** type definition for the pair of smart port and adi port for the basic adi devices */
using ext_adi_port_pair_t = std::pair<std::uint8_t, std::uint8_t>;
/** type definition for the triplet of smart port and two adi ports for the two wire adi devices*/
using ext_adi_port_tuple_t = std::tuple<std::uint8_t, std::uint8_t, std::uint8_t>;
class ADIPort {
public:
/**
* Configures an ADI port to act as a given sensor type.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param type
* The configuration type for the port
*/
explicit ADIPort(std::uint8_t adi_port, adi_port_config_e_t type = E_ADI_TYPE_UNDEFINED);
/**
* Configures an ADI port on an adi expander to act as a given sensor type.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the ADI port number
* (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param type
* The configuration type for the port
*/
ADIPort(ext_adi_port_pair_t port_pair, adi_port_config_e_t type = E_ADI_TYPE_UNDEFINED);
/**
* Gets the configuration for the given ADI port.
*
* \return The ADI configuration for the given port
*/
std::int32_t get_config() const;
/**
* Gets the value for the given ADI port.
*
* \return The value stored for the given port
*/
std::int32_t get_value() const;
/**
* Configures an ADI port to act as a given sensor type.
*
* \param type
* The configuration type for the port
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_config(adi_port_config_e_t type) const;
/**
* Sets the value for the given ADI port.
*
* This only works on ports configured as outputs, and the behavior will
* change depending on the configuration of the port.
*
* \param value
* The value to set the ADI port to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_value(std::int32_t value) const;
protected:
std::uint8_t _smart_port;
std::uint8_t _adi_port;
};
class ADIAnalogIn : protected ADIPort {
public:
/**
* Configures an ADI port to act as an Analog Input.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
explicit ADIAnalogIn(std::uint8_t adi_port);
/**
* Configures an ADI port on an adi expander to act as an Analog Input.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
ADIAnalogIn(ext_adi_port_pair_t port_pair);
/**
* Calibrates the analog sensor on the specified port and returns the new
* calibration value.
*
* This method assumes that the true sensor value is not actively changing at
* this time and computes an average from approximately 500 samples, 1 ms
* apart, for a 0.5 s period of calibration. The average value thus calculated
* is returned and stored for later calls to the
* pros::ADIAnalogIn::get_value_calibrated() and
* pros::ADIAnalogIn::get_value_calibrated_HR() functions. These functions
* will return the difference between this value and the current sensor value
* when called.
*
* Do not use this function when the sensor value might be unstable (gyro
* rotation, accelerometer movement).
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an analog input
*
* \return The average sensor value computed by this function
*/
std::int32_t calibrate() const;
/**
* Gets the 12 bit calibrated value of an analog input port.
*
* The pros::ADIAnalogIn::calibrate() function must be run first. This
* function is inappropriate for sensor values intended for integration, as
* round-off error can accumulate causing drift over time. Use
* pros::ADIAnalogIn::get_value_calibrated_HR() instead.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an analog input
*
* \return The difference of the sensor value from its calibrated default from
* -4095 to 4095
*/
std::int32_t get_value_calibrated() const;
/**
* Gets the 16 bit calibrated value of an analog input port.
*
* The pros::ADIAnalogIn::calibrate() function must be run first. This is
* intended for integrated sensor values such as gyros and accelerometers to
* reduce drift due to round-off, and should not be used on a sensor such as a
* line tracker or potentiometer.
*
* The value returned actually has 16 bits of "precision", even though the ADC
* only reads 12 bits, so that error induced by the average value being
* between two values when integrated over time is trivial. Think of the value
* as the true value times 16.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an analog input
*
* \return The difference of the sensor value from its calibrated default from
* -16384 to 16384
*/
std::int32_t get_value_calibrated_HR() const;
/**
* Gets the 12-bit value of the specified port.
*
* The value returned is undefined if the analog pin has been switched to a
* different mode.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an analog input
*
* \return The analog sensor value, where a value of 0 reflects an input
* voltage of nearly 0 V and a value of 4095 reflects an input voltage of
* nearly 5 V
*/
using ADIPort::get_value;
};
// using ADIPotentiometer = ADIAnalogIn;
using ADILineSensor = ADIAnalogIn;
using ADILightSensor = ADIAnalogIn;
using ADIAccelerometer = ADIAnalogIn;
class ADIAnalogOut : private ADIPort {
public:
/**
* Configures an ADI port to act as an Analog Output.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*/
explicit ADIAnalogOut(std::uint8_t adi_port);
/**
* Configures an ADI port on an adi_expander to act as an Analog Output.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*
*/
ADIAnalogOut(ext_adi_port_pair_t port_pair);
/**
* Sets the value for the given ADI port.
*
* This only works on ports configured as outputs, and the behavior will
* change depending on the configuration of the port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an analog output
*
* \param value
* The value to set the ADI port to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
using ADIPort::set_value;
};
class ADIDigitalOut : private ADIPort {
public:
/**
* Configures an ADI port to act as a Digital Output.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param init_state
* The initial state for the port
*/
explicit ADIDigitalOut(std::uint8_t adi_port, bool init_state = LOW);
/**
* Configures an ADI port on an adi_expander to act as a Digital Output.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param init_state
* The initial state for the port
*/
ADIDigitalOut(ext_adi_port_pair_t port_pair, bool init_state = LOW);
/**
* Sets the value for the given ADI port.
*
* This only works on ports configured as outputs, and the behavior will
* change depending on the configuration of the port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a digital output
*
* \param value
* The value to set the ADI port to
*
* \return if the operation was successful or PROS_ERR if the operation failed, setting errno.
*/
using ADIPort::set_value;
};
class ADIDigitalIn : private ADIPort {
public:
/**
* Configures an ADI port to act as a Digital Input.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*/
explicit ADIDigitalIn(std::uint8_t adi_port);
/**
* Configures an ADI port on an adi_expander to act as a Digital Input.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*/
ADIDigitalIn(ext_adi_port_pair_t port_pair);
/**
* Gets a rising-edge case for a digital button press.
*
* This function is not thread-safe.
* Multiple tasks polling a single button may return different results under
* the same circumstances, so only one task should call this function for any
* given button. E.g., Task A calls this function for buttons 1 and 2. Task B
* may call this function for button 3, but should not for buttons 1 or 2. A
* typical use-case for this function is to call inside opcontrol to detect
* new button presses, and not in any other tasks.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a digital input
*
* \return 1 if the button is pressed and had not been pressed the last time
* this function was called, 0 otherwise.
*/
std::int32_t get_new_press() const;
/**
* Gets the value for the given ADI port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a digital input
*
* \return The value stored for the given port
*/
using ADIPort::get_value;
};
using ADIButton = ADIDigitalIn;
class ADIMotor : private ADIPort {
public:
/**
* Configures an ADI port to act as a Motor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*/
explicit ADIMotor(std::uint8_t adi_port);
/**
* Configures an ADI port on an adi_expander to act as a Motor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
*/
ADIMotor(ext_adi_port_pair_t port_pair);
/**
* Stops the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a motor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t stop() const;
/**
* Sets the speed of the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a motor
*
* \param value
* The new signed speed; -127 is full reverse and 127 is full forward,
* with 0 being off
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
using ADIPort::set_value;
/**
* Gets the last set speed of the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a motor
*
* \return The last set speed of the motor on the given port
*/
using ADIPort::get_value;
};
class ADIEncoder : private ADIPort {
public:
/**
* Configures a set of ADI ports to act as an Encoder.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port_top
* The "top" wire from the encoder sensor with the removable cover side up
* \param adi_port_bottom
* The "bottom" wire from the encoder sensor
* \param reverse
* If "true", the sensor will count in the opposite direction
*/
ADIEncoder(std::uint8_t adi_port_top, std::uint8_t adi_port_bottom, bool reversed = false);
/**
* Configures a set of ADI ports on an adi_expander to act as an Encoder.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_tuple
* The tuple of the smart port number, the "top" wire from the encoder
* sensor with the removable cover side up, and the "bottom" wire from
* the encoder sensor
* \param reverse
* If "true", the sensor will count in the opposite direction
*/
ADIEncoder(ext_adi_port_tuple_t port_tuple, bool reversed = false);
// Delete copy constructor to prevent a compilation error from the constructor above.
ADIEncoder(ADIEncoder &) = delete;
/**
* Sets the encoder value to zero.
*
* It is safe to use this method while an encoder is enabled. It is not
* necessary to call this method before stopping or starting an encoder.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a motor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t reset() const;
/**
* Gets the number of ticks recorded by the encoder.
*
* There are 360 ticks in one revolution.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a motor
*
* \return The signed and cumulative number of counts since the last start or
* reset
*/
std::int32_t get_value() const;
};
class ADIUltrasonic : private ADIPort {
public:
/**
* Configures a set of ADI ports to act as an Ultrasonic sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_ping
* The port connected to the orange OUTPUT cable. This should be in port
* 1, 3, 5, or 7 ('A', 'C', 'E', 'G').
* \param port_echo
* The port connected to the yellow INPUT cable. This should be in the
* next highest port following port_ping.
*/
ADIUltrasonic(std::uint8_t adi_port_ping, std::uint8_t adi_port_echo);
/**
* Configures a set of ADI ports on an adi_expander to act as an Ultrasonic sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_tuple
* The tuple of the smart port number, the port connected to the orange
* OUTPUT cable (1, 3, 5, 7 or 'A', 'C', 'E', 'G'), and the port
* connected to the yellow INPUT cable (the next) highest port
* following port_ping).
*/
ADIUltrasonic(ext_adi_port_tuple_t port_tuple);
/**
* Gets the current ultrasonic sensor value in centimeters.
*
* If no object was found, zero is returned. If the ultrasonic sensor was
* never started, the return value is undefined. Round and fluffy objects can
* cause inaccurate values to be returned.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as an ultrasonic
*
* \return The distance to the nearest object in m^-4 (10000 indicates 1
* meter), measured from the sensor's mounting points.
*/
std::int32_t get_value() const;
};
class ADIGyro : private ADIPort {
public:
/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1300ms
* calibration period.
*
* It is highly recommended that an ADIGyro object be created in initialize()
* when the robot is stationary to ensure proper calibration. If an ADIGyro
* object is declared at the global scope, a hardcoded 1300ms delay at the
* beginning of initialize will be necessary to ensure that the gyro's
* returned values are correct at the beginning of autonomous/opcontrol.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
* \param multiplier
* A scalar value that will be multiplied by the gyro heading value
* supplied by the ADI
*/
explicit ADIGyro(std::uint8_t adi_port, double multiplier = 1);
/**
* Initializes a gyroscope on the given port of an adi expander. If the given
* port has not previously been configured as a gyro, then this function starts
* a 1300ms calibration period.
*
* It is highly recommended that an ADIGyro object be created in initialize()
* when the robot is stationary to ensure proper calibration. If an ADIGyro
* object is declared at the global scope, a hardcoded 1300ms delay at the
* beginning of initialize will be necessary to ensure that the gyro's
* returned values are correct at the beginning of autonomous/opcontrol.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param multiplier
* A scalar value that will be multiplied by the gyro heading value
* supplied by the ADI
*/
ADIGyro(ext_adi_port_pair_t port_pair, double multiplier = 1);
/**
* Gets the current gyro angle in tenths of a degree. Unless a multiplier is
* applied to the gyro, the return value will be a whole number representing
* the number of degrees of rotation times 10.
*
* There are 360 degrees in a circle, thus the gyro will return 3600 for one
* whole rotation.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a gyro
*
* \return The gyro angle in degrees.
*/
double get_value() const;
/**
* Resets the gyroscope value to zero.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a gyro
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t reset() const;
};
class ADIPotentiometer : public ADIAnalogIn {
public:
/**
* Configures an ADI port to act as a Potentiometer.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param potentiometer_type
* An adi_potentiometer_type_e_t enum value specifying the potentiometer version type
*/
ADIPotentiometer(std::uint8_t adi_port, adi_potentiometer_type_e_t potentiometer_type = E_ADI_POT_EDR);
/**
* Configures an ADI port on an adi_expander to act as a Potentiometer.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param potentiometer_type
* An adi_potentiometer_type_e_t enum value specifying the potentiometer version type
*/
ADIPotentiometer(ext_adi_port_pair_t port_pair, adi_potentiometer_type_e_t potentiometer_type = E_ADI_POT_EDR);
/**
* Gets the current potentiometer angle in tenths of a degree.
*
* The original potentiometer rotates 250 degrees thus returning an angle between 0-250 degrees.
* Potentiometer V2 rotates 330 degrees thus returning an angle between 0-330 degrees.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
* \return The potentiometer angle in degrees.
*/
double get_angle() const;
/**
* Gets the 12-bit value of the specified port.
*
* The value returned is undefined if the analog pin has been switched to a
* different mode.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a potentiometer
*
* \return The analog sensor value, where a value of 0 reflects an input
* voltage of nearly 0 V and a value of 4095 reflects an input voltage of
* nearly 5 V
*/
using ADIAnalogIn::get_value;
/**
* Calibrates the potentiometer on the specified port and returns the new
* calibration value.
*
* This method assumes that the potentiometer value is not actively changing at
* this time and computes an average from approximately 500 samples, 1 ms
* apart, for a 0.5 s period of calibration. The average value thus calculated
* is returned and stored for later calls to the
* pros::ADIPotentiometer::get_value_calibrated() function. This function
* will return the difference between this value and the current sensor value
* when called.
*
* Do not use this function when the potentiometer value might be unstable (rotating the potentiometer)
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a potentiometer
*
* \return The average potentiometer value computed by this function
*/
using ADIAnalogIn::calibrate;
/**
* Gets the 12 bit calibrated value of a potentiometer port.
*
* The pros::ADIPotentiometer::calibrate() function must be run first.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port is not configured as a potentiometer
*
* \return The difference of the potentiometer value from its calibrated default from
* -4095 to 4095
*/
using ADIAnalogIn::get_value_calibrated;
};
class ADILed : protected ADIPort {
public:
/**
* @brief Configures an ADI port to act as a LED.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param length
* The number of LEDs in the chain
*/
ADILed(std::uint8_t adi_port, std::uint32_t length);
/**
* @brief Configures an ADI port on a adi_expander to act as a LED.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param port_pair
* The pair of the smart port number (from 1-22) and the
* ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param length
* The number of LEDs in the chain
*/
ADILed(ext_adi_port_pair_t port_pair, std::uint32_t length);
/**
* @brief Operator overload to access the buffer in the ADILed class, it is
* recommended that you call .update(); after doing any operations with this.
*
* @param i 0 indexed pixel of the lED
* @return uint32_t& the address of the buffer at i to modify
*/
std::uint32_t& operator[] (size_t i);
/**
* @brief Clear the entire led strip of color
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
std::int32_t clear_all();
std::int32_t clear();
/**
* @brief Force the LED strip to update with the current buffered values, this
* should be called after any changes to the buffer using the [] operator.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
std::int32_t update() const;
/**
* @brief Set the entire led strip to one color
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @param color color to set all the led strip value to
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
std::int32_t set_all(uint32_t color);
/**
* @brief Set one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @param color color to clear all the led strip to
* @param pixel_position position of the pixel to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
std::int32_t set_pixel(uint32_t color, uint32_t pixel_position);
/**
* @brief Clear one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @param pixel_position position of the pixel to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
std::int32_t clear_pixel(uint32_t pixel_position);
/**
* @brief Get the length of the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A parameter is out of bounds/incorrect
* EADDRINUSE - The port is not configured for ADI output
*
* @return The length (in pixels) of the LED strip
*/
std::int32_t length();
protected:
std::vector<uint32_t> _buffer;
};
// Alias for ADILed
using ADILED = ADILed;
} // namespace pros
#endif // _PROS_ADI_HPP_

View File

@ -0,0 +1,108 @@
/**
* \file pros/api_legacy.h
*
* PROS 2 Legacy API header
*
* Contains declarations for functions that are name-compatible with the PROS 2
* API. Some functions from the PROS 2 API are not useful or cannot be
* implemented in PROS 3, but most common functions are available.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_API_LEGACY_H_
#define _PROS_API_LEGACY_H_
#include "api.h"
#ifdef __cplusplus
#define _NAMESPACE pros::
#define _CNAMESPACE pros::c::
#else
#define _NAMESPACE
#define _CNAMESPACE
#endif
/**
* From adi.h
*/
#define analogCalibrate(port) adi_analog_calibrate(port)
#define analogRead(port) adi_analog_read(port)
#define analogReadCalibrated(port) adi_analog_read_calibrated(port)
#define analogReadCalibratedHR(port) adi_analog_read_calibrated_HR(port)
#define digitalRead(port) adi_digital_read(port)
#define digitalWrite(port, value) adi_digital_write(port, value)
#define pinMode(port, mode) adi_pin_mode(port, mode)
#define adiMotorSet(port, speed) adi_motor_set(port, speed)
#define adiMotorGet(port) adi_motor_get(port)
#define adiMotorStop(port) adi_motor_stop(port)
#define encoderGet(enc) adi_encoder_get(enc)
#define encoderInit(portTop, portBottom, reverse) adi_encoder_init(portTop, portBottom, reverse)
#define encoderShutdown(enc) adi_encoder_shutdown(enc)
#define ultrasonicGet(ult) adi_ultrasonic_get(ult)
#define ultrasonicInit(portEcho, portPing) adi_ultrasonic_init(portEcho, portPing)
#define ultrasonicShutdown(ult) adi_ultrasonic_shutdown(ult)
typedef _CNAMESPACE adi_encoder_t Encoder;
typedef _CNAMESPACE adi_ultrasonic_t Ultrasonic;
/**
* From llemu.h
*/
#define lcdInit lcd_initialize
#define lcdReadButtons lcd_read_buttons
#define lcdClear lcd_clear
#define lcdClearLine lcd_clear_line
#define lcdShutdown lcd_shutdown
#define lcdPrint(line, fmt, ...) lcd_print(line, fmt, __VA_ARGS__)
#define lcdSetText(line, text) lcd_set_text(line, text)
/**
* From misc.h
*/
#define isEnabled() (!competition_is_disabled())
#define isAutonomous competition_is_autonomous
#define isOnline competition_is_connected
#define isJoystickConnected(id) controller_is_connected(id)
#define joystickGetAnalog(id, channel) controller_get_analog(id, channel)
/**
* From rtos.h
*/
#define taskCreate(taskCode, stackDepth, parameters, priority) \
task_create(taskCode, parameters, priority, stackDepth, "")
#define taskDelete(task) task_delete(task)
#define taskDelay task_delay
#define taskDelayUntil(previousWakeTime, cycleTime) task_delay_until(previousWakeTime, cycleTime)
#define taskPriorityGet(task) task_get_priority(task)
#define taskPrioritySet(task, newPriority) task_priority_set(task, newPriority)
#define taskGetState(task) task_get_state(task)
#define taskSuspend(task) task_suspend(task)
#define taskResume(task) task_resume(task)
#define taskGetCount task_get_count
#define mutexCreate mutex_create
#define mutexTake(mutex, blockTime) mutex_take(mutex, blockTime)
#define mutexGive(mutex) mutex_give(mutex)
typedef _NAMESPACE task_t TaskHandle;
typedef _NAMESPACE mutex_t Mutex;
/**
* From motors.h
*/
#define motorSet(port, speed) motor_move(port, speed)
#define motorGet(port) motor_get_voltage(port)
#define motorStop(port) motor_move(port, 0)
#undef _NAMESPACE
#undef _CNAMESPACE
#endif // _PROS_API_LEGACY_H_

View File

@ -0,0 +1,593 @@
/**
* \file pros/apix.h
*
* PROS Extended API header
*
* Contains additional declarations for use by advaned users of PROS. These
* functions do not typically have as much error handling or require deeper
* knowledge of real time operating systems.
*
* Visit https://pros.cs.purdue.edu/v5/extended/api.html to learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_API_EXTENDED_H_
#define _PROS_API_EXTENDED_H_
#include "api.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#include "display/lvgl.h"
#pragma GCC diagnostic pop
#include "pros/serial.h"
#ifdef __cplusplus
#include "pros/serial.hpp"
namespace pros::c {
extern "C" {
#endif
/******************************************************************************/
/** RTOS FACILITIES **/
/** **/
/** **/
/**See https://pros.cs.purdue.edu/v5/extended/multitasking.html to learn more**/
/******************************************************************************/
typedef void* queue_t;
typedef void* sem_t;
/**
* Unblocks a task in the Blocked state (e.g. waiting for a delay, on a
* semaphore, etc.).
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#abort_delay for
* details.
*/
bool task_abort_delay(task_t task);
/**
* Notify a task when a target task is being deleted.
*
* This function will configure the PROS kernel to call
* task_notify_ext(task_to_notify, value, action, NULL) when target_task is
* deleted.
*
*
* \param target_task
* The task being watched for deletion
* \param task_to_notify
* The task to notify when target_task is deleted
* \param value
* The value to supply to task_notify_ext
* \param notify_action
* The action to supply to task_notify_ext
*/
void task_notify_when_deleting(task_t target_task, task_t task_to_notify, uint32_t value,
notify_action_e_t notify_action);
/**
* Creates a recursive mutex which can be locked recursively by the owner.
*
* See
* https://pros.cs.purdue.edu/v5/extended/multitasking.html#recursive_mutexes
* for details.
*
* \return A newly created recursive mutex.
*/
mutex_t mutex_recursive_create(void);
/**
* Takes a recursive mutex.
*
* See
* https://pros.cs.purdue.edu/v5/extended/multitasking.html#recursive_mutexes
* for details.
*
* \param mutex
* A mutex handle created by mutex_recursive_create
* \param wait_time
* Amount of time to wait before timing out
*
* \return 1 if the mutex was obtained, 0 otherwise
*/
bool mutex_recursive_take(mutex_t mutex, uint32_t timeout);
/**
* Gives a recursive mutex.
*
* See
* https://pros.cs.purdue.edu/v5/extended/multitasking.html#recursive_mutexes
* for details.
*
* \param mutex
* A mutex handle created by mutex_recursive_create
*
* \return 1 if the mutex was obtained, 0 otherwise
*/
bool mutex_recursive_give(mutex_t mutex);
/**
* Returns a handle to the current owner of a mutex.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#extra for
* details.
*
* \param mutex
* A mutex handle
*
* \return A handle to the current task that owns the mutex, or NULL if the
* mutex isn't owned.
*/
task_t mutex_get_owner(mutex_t mutex);
/**
* Creates a counting sempahore.
*
* See https://pros.cs.purdue.edu/v5/tutorials/multitasking.html#semaphores for
*details.
*
* \param max_count
* The maximum count value that can be reached.
* \param init_count
* The initial count value assigned to the new semaphore.
*
* \return A newly created semaphore. If an error occurred, NULL will be
* returned and errno can be checked for hints as to why sem_create failed.
*/
sem_t sem_create(uint32_t max_count, uint32_t init_count);
/**
* Deletes a semaphore (or binary semaphore)
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#semaphores for
* details.
*
* \param sem
* Semaphore to delete
*/
void sem_delete(sem_t sem);
/**
* Creates a binary semaphore.
*
* See
* https://pros.cs.purdue.edu/v5/extended/multitasking#.htmlbinary_semaphores
* for details.
*
* \return A newly created semaphore.
*/
sem_t sem_binary_create(void);
/**
* Waits for the semaphore's value to be greater than 0. If the value is already
* greater than 0, this function immediately returns.
*
* See https://pros.cs.purdue.edu/v5/tutorials/multitasking.html#semaphores for
* details.
*
* \param sem
* Semaphore to wait on
* \param timeout
* Time to wait before the semaphore's becomes available. A timeout of 0
* can be used to poll the sempahore. TIMEOUT_MAX can be used to block
* indefinitely.
*
* \return True if the semaphore was successfully take, false otherwise. If
* false is returned, then errno is set with a hint about why the sempahore
* couldn't be taken.
*/
bool sem_wait(sem_t sem, uint32_t timeout);
/**
* Increments a semaphore's value.
*
* See https://pros.cs.purdue.edu/v5/tutorials/multitasking.html#semaphores for
* details.
*
* \param sem
* Semaphore to post
*
* \return True if the value was incremented, false otherwise. If false is
* returned, then errno is set with a hint about why the semaphore couldn't be
* taken.
*/
bool sem_post(sem_t sem);
/**
* Returns the current value of the semaphore.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#extra for
* details.
*
* \param sem
* A semaphore handle
*
* \return The current value of the semaphore (e.g. the number of resources
* available)
*/
uint32_t sem_get_count(sem_t sem);
/**
* Creates a queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param length
* The maximum number of items that the queue can contain.
* \param item_size
* The number of bytes each item in the queue will require.
*
* \return A handle to a newly created queue, or NULL if the queue cannot be
* created.
*/
queue_t queue_create(uint32_t length, uint32_t item_size);
/**
* Posts an item to the front of a queue. The item is queued by copy, not by
* reference.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle
* \param item
* A pointer to the item that will be placed on the queue.
* \param timeout
* Time to wait for space to become available. A timeout of 0 can be used
* to attempt to post without blocking. TIMEOUT_MAX can be used to block
* indefinitely.
*
* \return True if the item was preprended, false otherwise.
*/
bool queue_prepend(queue_t queue, const void* item, uint32_t timeout);
/**
* Posts an item to the end of a queue. The item is queued by copy, not by
* reference.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle
* \param item
* A pointer to the item that will be placed on the queue.
* \param timeout
* Time to wait for space to become available. A timeout of 0 can be used
* to attempt to post without blocking. TIMEOUT_MAX can be used to block
* indefinitely.
*
* \return True if the item was preprended, false otherwise.
*/
bool queue_append(queue_t queue, const void* item, uint32_t timeout);
/**
* Receive an item from a queue without removing the item from the queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle
* \param buffer
* Pointer to a buffer to which the received item will be copied
* \param timeout
* The maximum amount of time the task should block waiting for an item to receive should the queue be empty at
* the time of the call. TIMEOUT_MAX can be used to block indefinitely.
*
* \return True if an item was copied into the buffer, false otherwise.
*/
bool queue_peek(queue_t queue, void* const buffer, uint32_t timeout);
/**
* Receive an item from the queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle
* \param buffer
* Pointer to a buffer to which the received item will be copied
* \param timeout
* The maximum amount of time the task should block
* waiting for an item to receive should the queue be empty at the time
* of the call. queue_recv() will return immediately if timeout
* is zero and the queue is empty.
*
* \return True if an item was copied into the buffer, false otherwise.
*/
bool queue_recv(queue_t queue, void* const buffer, uint32_t timeout);
/**
* Return the number of messages stored in a queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle.
*
* \return The number of messages available in the queue.
*/
uint32_t queue_get_waiting(const queue_t queue);
/**
* Return the number of spaces left in a queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* The queue handle.
*
* \return The number of spaces available in the queue.
*/
uint32_t queue_get_available(const queue_t queue);
/**
* Delete a queue.
*
* See https://pros.cs.purdue.edu/v5/extended/multitasking.html#queues for
* details.
*
* \param queue
* Queue handle to delete
*/
void queue_delete(queue_t queue);
/**
* Resets a queue to an empty state
*
* \param queue
* Queue handle to reset
*/
void queue_reset(queue_t queue);
/******************************************************************************/
/** Device Registration **/
/******************************************************************************/
/*
* List of possible v5 devices
*
* This list contains all current V5 Devices, and mirrors V5_DeviceType from the
* api.
*/
typedef enum v5_device_e {
E_DEVICE_NONE = 0,
E_DEVICE_MOTOR = 2,
E_DEVICE_ROTATION = 4,
E_DEVICE_IMU = 6,
E_DEVICE_DISTANCE = 7,
E_DEVICE_RADIO = 8,
E_DEVICE_VISION = 11,
E_DEVICE_ADI = 12,
E_DEVICE_OPTICAL = 16,
E_DEVICE_GPS = 20,
E_DEVICE_SERIAL = 129,
E_DEVICE_GENERIC __attribute__((deprecated("use E_DEVICE_SERIAL instead"))) = E_DEVICE_SERIAL,
E_DEVICE_UNDEFINED = 255
} v5_device_e_t;
/*
* Registers a device in the given zero-indexed port
*
* Registers a device of the given type in the given port into the registry, if
* that type of device is detected to be plugged in to that 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 (0-20), or a
* a different device than specified is plugged in.
* EADDRINUSE - The port is already registered to another device.
*
* \param port
* The port number to register the device
* \param device
* The type of device to register
*
* \return 1 upon success, PROS_ERR upon failure
*/
int registry_bind_port(uint8_t port, v5_device_e_t device_type);
/*
* Deregisters a devices from the given zero-indexed port
*
* Removes the device registed in the given port, if there is one.
*
* 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 (0-20).
*
* \param port
* The port number to deregister
*
* \return 1 upon success, PROS_ERR upon failure
*/
int registry_unbind_port(uint8_t port);
/*
* Returns the type of device registered to the zero-indexed 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 (0-20).
*
* \param port
* The V5 port number from 0-20
*
* \return The type of device that is registered into the port (NOT what is
* plugged in)
*/
v5_device_e_t registry_get_bound_type(uint8_t port);
/*
* Returns the type of the device plugged into the zero-indexed 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 (0-20).
*
* \param port
* The V5 port number from 0-20
*
* \return The type of device that is plugged into the port (NOT what is
* registered)
*/
v5_device_e_t registry_get_plugged_type(uint8_t port);
/******************************************************************************/
/** Filesystem **/
/******************************************************************************/
/**
* Control settings of the serial driver.
*
* \param action
* An action to perform on the serial driver. See the SERCTL_* macros for
* details on the different actions.
* \param extra_arg
* An argument to pass in based on the action
*/
int32_t serctl(const uint32_t action, void* const extra_arg);
/**
* Control settings of the microSD card driver.
*
* \param action
* An action to perform on the microSD card driver. See the USDCTL_* macros
* for details on the different actions.
* \param extra_arg
* An argument to pass in based on the action
*/
// Not yet implemented
// int32_t usdctl(const uint32_t action, void* const extra_arg);
/**
* Control settings of the way the file's driver treats the file
*
* \param file
* A valid file descriptor number
* \param action
* An action to perform on the file's driver. See the *CTL_* macros for
* details on the different actions. Note that the action passed in must
* match the correct driver (e.g. don't perform a SERCTL_* action on a
* microSD card file)
* \param extra_arg
* An argument to pass in based on the action
*/
int32_t fdctl(int file, const uint32_t action, void* const extra_arg);
/**
* Action macro to pass into serctl or fdctl that activates the stream
* identifier.
*
* When used with serctl, the extra argument must be the little endian
* representation of the stream identifier (e.g. "sout" -> 0x74756f73)
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_ACTIVATE 10
/**
* Action macro to pass into serctl or fdctl that deactivates the stream
* identifier.
*
* When used with serctl, the extra argument must be the little endian
* representation of the stream identifier (e.g. "sout" -> 0x74756f73)
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_DEACTIVATE 11
/**
* Action macro to pass into fdctl that enables blocking writes for the file
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_BLKWRITE 12
/**
* Action macro to pass into fdctl that makes writes non-blocking for the file
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_NOBLKWRITE 13
/**
* Action macro to pass into serctl that enables advanced stream multiplexing
* capabilities
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_ENABLE_COBS 14
/**
* Action macro to pass into serctl that disables advanced stream multiplexing
* capabilities
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/filesystem.html#serial
* to learn more.
*/
#define SERCTL_DISABLE_COBS 15
/**
* Action macro to check if there is data available from the Generic Serial
* Device
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*/
#define DEVCTL_FIONREAD 16
/**
* Action macro to check if there is space available in the Generic Serial
* Device's output buffer
*
* The extra argument is not used with this action, provide any value (e.g.
* NULL) instead
*/
#define DEVCTL_FIONWRITE 18
/**
* Action macro to set the Generic Serial Device's baudrate.
*
* The extra argument is the baudrate.
*/
#define DEVCTL_SET_BAUDRATE 17
#ifdef __cplusplus
}
}
#endif
#endif // _PROS_API_EXTENDED_H_

View File

@ -0,0 +1,171 @@
/*
* \file pros/colors.h
*
* Contains macro definitions of colors (as `uint32_t`)
*
* 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_COLORS_H_
#define _PROS_COLORS_H_
#define RGB2COLOR(R, G, B) ((R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff))
#define COLOR2R(COLOR) ((COLOR >> 16) & 0xff)
#define COLOR2G(COLOR) ((COLOR >> 8) & 0xff)
#define COLOR2B(COLOR) (COLOR & 0xff)
#define COLOR_ALICE_BLUE 0x00F0F8FF
#define COLOR_ANTIQUE_WHITE 0x00FAEBD7
#define COLOR_AQUA 0x0000FFFF
#define COLOR_AQUAMARINE 0x007FFFD4
#define COLOR_AZURE 0x00F0FFFF
#define COLOR_BEIGE 0x00F5F5DC
#define COLOR_BISQUE 0x00FFE4C4
#define COLOR_BLACK 0x00000000
#define COLOR_BLANCHED_ALMOND 0x00FFEBCD
#define COLOR_BLUE 0x000000FF
#define COLOR_BLUE_VIOLET 0x008A2BE2
#define COLOR_BROWN 0x00A52A2A
#define COLOR_BURLY_WOOD 0x00DEB887
#define COLOR_CADET_BLUE 0x005F9EA0
#define COLOR_CHARTREUSE 0x007FFF00
#define COLOR_CHOCOLATE 0x00D2691E
#define COLOR_CORAL 0x00FF7F50
#define COLOR_CORNFLOWER_BLUE 0x006495ED
#define COLOR_CORNSILK 0x00FFF8DC
#define COLOR_CRIMSON 0x00DC143C
#define COLOR_CYAN 0x0000FFFF
#define COLOR_DARK_BLUE 0x0000008B
#define COLOR_DARK_CYAN 0x00008B8B
#define COLOR_DARK_GOLDENROD 0x00B8860B
#define COLOR_DARK_GRAY 0x00A9A9A9
#define COLOR_DARK_GREEN 0x00006400
#define COLOR_DARK_KHAKI 0x00BDB76B
#define COLOR_DARK_MAGENTA 0x008B008B
#define COLOR_DARK_OLIVE_GREEN 0x00556B2F
#define COLOR_DARK_ORANGE 0x00FF8C00
#define COLOR_DARK_ORCHID 0x009932CC
#define COLOR_DARK_RED 0x008B0000
#define COLOR_DARK_SALMON 0x00E9967A
#define COLOR_DARK_SEA_GREEN 0x008FBC8F
#define COLOR_DARK_SLATE_GRAY 0x002F4F4F
#define COLOR_DARK_TURQUOISE 0x0000CED1
#define COLOR_DARK_VIOLET 0x009400D3
#define COLOR_DEEP_PINK 0x00FF1493
#define COLOR_DEEP_SKY_BLUE 0x0000BFFF
#define COLOR_DIM_GRAY 0x00696969
#define COLOR_DODGER_BLUE 0x001E90FF
#define COLOR_FIRE_BRICK 0x00B22222
#define COLOR_FLORAL_WHITE 0x00FFFAF0
#define COLOR_FOREST_GREEN 0x00228B22
#define COLOR_FUCHSIA 0x00FF00FF
#define COLOR_GAINSBORO 0x00DCDCDC
#define COLOR_GHOST_WHITE 0x00F8F8FF
#define COLOR_GOLD 0x00FFD700
#define COLOR_GOLDENROD 0x00DAA520
#define COLOR_GRAY 0x00808080
#define COLOR_GREEN 0x00008000
#define COLOR_GREEN_YELLOW 0x00ADFF2F
#define COLOR_HONEYDEW 0x00F0FFF0
#define COLOR_HOT_PINK 0x00FF69B4
#define COLOR_INDIAN_RED 0x00CD5C5C
#define COLOR_INDIGO 0x004B0082
#define COLOR_IVORY 0x00FFFFF0
#define COLOR_KHAKI 0x00F0E68C
#define COLOR_LAVENDER 0x00E6E6FA
#define COLOR_LAVENDER_BLUSH 0x00FFF0F5
#define COLOR_LAWN_GREEN 0x007CFC00
#define COLOR_LEMON_CHIFFON 0x00FFFACD
#define COLOR_LIGHT_BLUE 0x00ADD8E6
#define COLOR_LIGHT_CORAL 0x00F08080
#define COLOR_LIGHT_CYAN 0x00E0FFFF
#define COLOR_LIGHT_GOLDENROD_YELLOW 0x00FAFAD2
#define COLOR_LIGHT_GREEN 0x0090EE90
#define COLOR_LIGHT_GRAY 0x00D3D3D3
#define COLOR_LIGHT_PINK 0x00FFB6C1
#define COLOR_LIGHT_SALMON 0x00FFA07A
#define COLOR_LIGHT_SEA_GREEN 0x0020B2AA
#define COLOR_LIGHT_SKY_BLUE 0x0087CEFA
#define COLOR_LIGHT_SLATE_GRAY 0x00778899
#define COLOR_LIGHT_STEEL_BLUE 0x00B0C4DE
#define COLOR_LIGHT_YELLOW 0x00FFFFE0
#define COLOR_LIME 0x0000FF00
#define COLOR_LIME_GREEN 0x0032CD32
#define COLOR_LINEN 0x00FAF0E6
#define COLOR_MAGENTA 0x00FF00FF
#define COLOR_MAROON 0x00800000
#define COLOR_MEDIUM_AQUAMARINE 0x0066CDAA
#define COLOR_MEDIUM_BLUE 0x000000CD
#define COLOR_MEDIUM_ORCHID 0x00BA55D3
#define COLOR_MEDIUM_PURPLE 0x009370DB
#define COLOR_MEDIUM_SEA_GREEN 0x003CB371
#define COLOR_MEDIUM_SLATE_BLUE 0x007B68EE
#define COLOR_MEDIUM_SPRING_GREEN 0x0000FA9A
#define COLOR_MEDIUM_TURQUOISE 0x0048D1CC
#define COLOR_MEDIUM_VIOLET_RED 0x00C71585
#define COLOR_MIDNIGHT_BLUE 0x00191970
#define COLOR_MINT_CREAM 0x00F5FFFA
#define COLOR_MISTY_ROSE 0x00FFE4E1
#define COLOR_MOCCASIN 0x00FFE4B5
#define COLOR_NAVAJO_WHITE 0x00FFDEAD
#define COLOR_NAVY 0x00000080
#define COLOR_OLD_LACE 0x00FDF5E6
#define COLOR_OLIVE 0x00808000
#define COLOR_OLIVE_DRAB 0x006B8E23
#define COLOR_ORANGE 0x00FFA500
#define COLOR_ORANGE_RED 0x00FF4500
#define COLOR_ORCHID 0x00DA70D6
#define COLOR_PALE_GOLDENROD 0x00EEE8AA
#define COLOR_PALE_GREEN 0x0098FB98
#define COLOR_PALE_TURQUOISE 0x00AFEEEE
#define COLOR_PALE_VIOLET_RED 0x00DB7093
#define COLOR_PAPAY_WHIP 0x00FFEFD5
#define COLOR_PEACH_PUFF 0x00FFDAB9
#define COLOR_PERU 0x00CD853F
#define COLOR_PINK 0x00FFC0CB
#define COLOR_PLUM 0x00DDA0DD
#define COLOR_POWDER_BLUE 0x00B0E0E6
#define COLOR_PURPLE 0x00800080
#define COLOR_RED 0x00FF0000
#define COLOR_ROSY_BROWN 0x00BC8F8F
#define COLOR_ROYAL_BLUE 0x004169E1
#define COLOR_SADDLE_BROWN 0x008B4513
#define COLOR_SALMON 0x00FA8072
#define COLOR_SANDY_BROWN 0x00F4A460
#define COLOR_SEA_GREEN 0x002E8B57
#define COLOR_SEASHELL 0x00FFF5EE
#define COLOR_SIENNA 0x00A0522D
#define COLOR_SILVER 0x00C0C0C0
#define COLOR_SKY_BLUE 0x0087CEEB
#define COLOR_SLATE_BLUE 0x006A5ACD
#define COLOR_SLATE_GRAY 0x00708090
#define COLOR_SNOW 0x00FFFAFA
#define COLOR_SPRING_GREEN 0x0000FF7F
#define COLOR_STEEL_BLUE 0x004682B4
#define COLOR_TAN 0x00D2B48C
#define COLOR_TEAL 0x00008080
#define COLOR_THISTLE 0x00D8BFD8
#define COLOR_TOMATO 0x00FF6347
#define COLOR_TURQUOISE 0x0040E0D0
#define COLOR_VIOLET 0x00EE82EE
#define COLOR_WHEAT 0x00F5DEB3
#define COLOR_WHITE 0x00FFFFFF
#define COLOR_WHITE_SMOKE 0x00F5F5F5
#define COLOR_YELLOW 0x00FFFF00
#define COLOR_YELLOW_GREEN 0x009ACD32
#define COLOR_DARK_GREY COLOR_DARK_GRAY
#define COLOR_DARK_SLATE_GREY COLOR_DARK_SLATE_GRAY
#define COLOR_DIM_GREY COLOR_DIM_GRAY
#define COLOR_GREY COLOR_GRAY
#define COLOR_LIGHT_GREY COLOR_LIGHT_GRAY
#define COLOR_LIGHT_SLATE_GREY COLOR_LIGHT_SLATE_GRAY
#define COLOR_SLATE_GREY COLOR_SLATE_GRAY
#endif // _PROS_COLORS_H_

View File

@ -0,0 +1,170 @@
/**
* \file pros/colors.hpp
*
* Contains enum class definitions of colors
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* Copyright (c) 2017-2022 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_COLORS_HPP_
#define _PROS_COLORS_HPP_
namespace pros{
enum class Color {
alice_blue = 0x00F0F8FF,
antique_white = 0x00FAEBD7,
aqua = 0x0000FFFF,
aquamarine = 0x007FFFD4,
azure = 0x00F0FFFF,
beige = 0x00F5F5DC,
bisque = 0x00FFE4C4,
black = 0x00000000,
blanched_almond = 0x00FFEBCD,
blue = 0x000000FF,
blue_violet = 0x008A2BE2,
brown = 0x00A52A2A,
burly_wood = 0x00DEB887,
cadet_blue = 0x005F9EA0,
chartreuse = 0x007FFF00,
chocolate = 0x00D2691E,
coral = 0x00FF7F50,
cornflower_blue = 0x006495ED,
cornsilk = 0x00FFF8DC,
crimson = 0x00DC143C,
cyan = 0x0000FFFF,
dark_blue = 0x0000008B,
dark_cyan = 0x00008B8B,
dark_goldenrod = 0x00B8860B,
dark_gray = 0x00A9A9A9,
dark_grey = dark_gray,
dark_green = 0x00006400,
dark_khaki = 0x00BDB76B,
dark_magenta = 0x008B008B,
dark_olive_green = 0x00556B2F,
dark_orange = 0x00FF8C00,
dark_orchid = 0x009932CC,
dark_red = 0x008B0000,
dark_salmon = 0x00E9967A,
dark_sea_green = 0x008FBC8F,
dark_slate_gray = 0x002F4F4F,
dark_slate_grey = dark_slate_gray,
dark_turquoise = 0x0000CED1,
dark_violet = 0x009400D3,
deep_pink = 0x00FF1493,
deep_sky_blue = 0x0000BFFF,
dim_gray = 0x00696969,
dim_grey = dim_gray,
dodger_blue = 0x001E90FF,
fire_brick = 0x00B22222,
floral_white = 0x00FFFAF0,
forest_green = 0x00228B22,
fuchsia = 0x00FF00FF,
gainsboro = 0x00DCDCDC,
ghost_white = 0x00F8F8FF,
gold = 0x00FFD700,
goldenrod = 0x00DAA520,
gray = 0x00808080,
grey = gray,
green = 0x00008000,
green_yellow = 0x00ADFF2F,
honeydew = 0x00F0FFF0,
hot_pink = 0x00FF69B4,
indian_red = 0x00CD5C5C,
indigo = 0x004B0082,
ivory = 0x00FFFFF0,
khaki = 0x00F0E68C,
lavender = 0x00E6E6FA,
lavender_blush = 0x00FFF0F5,
lawn_green = 0x007CFC00,
lemon_chiffon = 0x00FFFACD,
light_blue = 0x00ADD8E6,
light_coral = 0x00F08080,
light_cyan = 0x00E0FFFF,
light_goldenrod_yellow = 0x00FAFAD2,
light_green = 0x0090EE90,
light_gray = 0x00D3D3D3,
light_grey = light_gray,
light_pink = 0x00FFB6C1,
light_salmon = 0x00FFA07A,
light_sea_green = 0x0020B2AA,
light_sky_blue = 0x0087CEFA,
light_slate_gray = 0x00778899,
light_slate_grey = light_slate_gray,
light_steel_blue = 0x00B0C4DE,
light_yellow = 0x00FFFFE0,
lime = 0x0000FF00,
lime_green = 0x0032CD32,
linen = 0x00FAF0E6,
magenta = 0x00FF00FF,
maroon = 0x00800000,
medium_aquamarine = 0x0066CDAA,
medium_blue = 0x000000CD,
medium_orchid = 0x00BA55D3,
medium_purple = 0x009370DB,
medium_sea_green = 0x003CB371,
medium_slate_blue = 0x007B68EE,
medium_spring_green = 0x0000FA9A,
medium_turquoise = 0x0048D1CC,
medium_violet_red = 0x00C71585,
midnight_blue = 0x00191970,
mint_cream = 0x00F5FFFA,
misty_rose = 0x00FFE4E1,
moccasin = 0x00FFE4B5,
navajo_white = 0x00FFDEAD,
navy = 0x00000080,
old_lace = 0x00FDF5E6,
olive = 0x00808000,
olive_drab = 0x006B8E23,
orange = 0x00FFA500,
orange_red = 0x00FF4500,
orchid = 0x00DA70D6,
pale_goldenrod = 0x00EEE8AA,
pale_green = 0x0098FB98,
pale_turquoise = 0x00AFEEEE,
pale_violet_red = 0x00DB7093,
papay_whip = 0x00FFEFD5,
peach_puff = 0x00FFDAB9,
peru = 0x00CD853F,
pink = 0x00FFC0CB,
plum = 0x00DDA0DD,
powder_blue = 0x00B0E0E6,
purple = 0x00800080,
red = 0x00FF0000,
rosy_brown = 0x00BC8F8F,
royal_blue = 0x004169E1,
saddle_brown = 0x008B4513,
salmon = 0x00FA8072,
sandy_brown = 0x00F4A460,
sea_green = 0x002E8B57,
seashell = 0x00FFF5EE,
sienna = 0x00A0522D,
silver = 0x00C0C0C0,
sky_blue = 0x0087CEEB,
slate_blue = 0x006A5ACD,
slate_gray = 0x00708090,
slate_grey = slate_gray,
snow = 0x00FFFAFA,
spring_green = 0x0000FF7F,
steel_blue = 0x004682B4,
tan = 0x00D2B48C,
teal = 0x00008080,
thistle = 0x00D8BFD8,
tomato = 0x00FF6347,
turquoise = 0x0040E0D0,
violet = 0x00EE82EE,
wheat = 0x00F5DEB3,
white = 0x00FFFFFF,
white_smoke = 0x00F5F5F5,
yellow = 0x00FFFF00,
yellow_green = 0x009ACD32,
};
} // namespace pros
#endif //_PROS_COLORS_HPP_

View File

@ -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 Copyright (c) 2017-2023, 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);
#ifdef __cplusplus
}
}
}
#endif
#endif

View File

@ -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-2021, 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

View File

@ -0,0 +1,29 @@
/**
* \file pros/error.h
*
* Contains macro definitions for return types, mostly errors
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_ERROR_H_
#define _PROS_ERROR_H_
#include "limits.h"
// Different Byte Size Errors
#define PROS_ERR_BYTE (INT8_MAX)
#define PROS_ERR_2_BYTE (INT16_MAX)
#define PROS_ERR (INT32_MAX)
#define PROS_ERR_F (INFINITY)
// Return This on Success
#define PROS_SUCCESS (1)
#endif

View File

@ -0,0 +1,793 @@
/**
* \file pros/ext_adi.h
*
* Contains prototypes for interfacing with the 3-Wire Expander.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/adi.html to learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_EXT_ADI_H_
#define _PROS_EXT_ADI_H_
#include <stdbool.h>
#include <stdint.h>
#include "adi.h"
#include "pros/adi.h"
#ifndef PROS_ERR
#define PROS_ERR (INT32_MAX)
#endif
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
#ifdef __cplusplus
namespace c {
#endif
/******************************************************************************/
/** General ADI Use Functions **/
/** **/
/** These functions allow for interaction with any ADI port type **/
/******************************************************************************/
/**
* Gets the configuration for the given ADI port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which to return
* the configuration
*
* \return The ADI configuration for the given port
*/
adi_port_config_e_t ext_adi_port_get_config(uint8_t smart_port, uint8_t adi_port);
/**
* Gets the value for the given ADI port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which to return
* the configuration
*
* \return The value stored for the given port
*/
int32_t ext_adi_port_get_value(uint8_t smart_port, uint8_t adi_port);
/**
* Configures an ADI port to act as a given sensor type.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param type
* The configuration type for the port
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_port_set_config(uint8_t smart_port, uint8_t adi_port, adi_port_config_e_t type);
/**
* Sets the value for the given ADI port.
*
* This only works on ports configured as outputs, and the behavior will change
* depending on the configuration of the port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') for which the value
* will be set
* \param value
* The value to set the ADI port to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_port_set_value(uint8_t smart_port, uint8_t adi_port, int32_t value);
/**
* Calibrates the analog sensor on the specified port and returns the new
* calibration value.
*
* This method assumes that the true sensor value is not actively changing at
* this time and computes an average from approximately 500 samples, 1 ms apart,
* for a 0.5 s period of calibration. The average value thus calculated is
* returned and stored for later calls to the adi_analog_read_calibrated() and
* adi_analog_read_calibrated_HR() functions. These functions will return
* the difference between this value and the current sensor value when called.
*
* Do not use this function when the sensor value might be unstable
* (gyro rotation, accelerometer movement).
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to calibrate (from 1-8, 'a'-'h', 'A'-'H')
*
* \return The average sensor value computed by this function
*/
int32_t ext_adi_analog_calibrate(uint8_t smart_port, uint8_t adi_port);
/**
* Gets the 12-bit value of the specified port.
*
* The value returned is undefined if the analog pin has been switched to a
* different mode.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an analog input
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
* returned
*
* \return The analog sensor value, where a value of 0 reflects an input voltage
* of nearly 0 V and a value of 4095 reflects an input voltage of nearly 5 V
*/
int32_t ext_adi_analog_read(uint8_t smart_port, uint8_t adi_port);
/**
* Gets the 12 bit calibrated value of an analog input port.
*
* The adi_analog_calibrate() function must be run first. This function is
* inappropriate for sensor values intended for integration, as round-off error
* can accumulate causing drift over time. Use adi_analog_read_calibrated_HR()
* instead.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an analog input
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
* returned
*
* \return The difference of the sensor value from its calibrated default from
* -4095 to 4095
*/
int32_t ext_adi_analog_read_calibrated(uint8_t smart_port, uint8_t adi_port);
/**
* Gets the 16 bit calibrated value of an analog input port.
*
* The adi_analog_calibrate() function must be run first. This is intended for
* integrated sensor values such as gyros and accelerometers to reduce drift due
* to round-off, and should not be used on a sensor such as a line tracker
* or potentiometer.
*
* The value returned actually has 16 bits of "precision", even though the ADC
* only reads 12 bits, so that error induced by the average value being between
* two values when integrated over time is trivial. Think of the value as the
* true value times 16.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an analog input
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port (from 1-8, 'a'-'h', 'A'-'H') for which the value will be
* returned
*
* \return The difference of the sensor value from its calibrated default from
* -16384 to 16384
*/
int32_t ext_adi_analog_read_calibrated_HR(uint8_t smart_port, uint8_t adi_port);
/**
* Gets the digital value (1 or 0) of a port configured as a digital input.
*
* If the port is configured as some other mode, the digital value which
* reflects the current state of the port is returned, which may or may not
* differ from the currently set value. The return value is undefined for ports
* configured as any mode other than a Digital Input.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a digital input
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
*
* \return True if the pin is HIGH, or false if it is LOW
*/
int32_t ext_adi_digital_read(uint8_t smart_port, uint8_t adi_port);
/**
* Gets a rising-edge case for a digital button press.
*
* This function is not thread-safe.
* Multiple tasks polling a single button may return different results under the
* same circumstances, so only one task should call this function for any given
* button. E.g., Task A calls this function for buttons 1 and 2. Task B may call
* this function for button 3, but should not for buttons 1 or 2. A typical
* use-case for this function is to call inside opcontrol to detect new button
* presses, and not in any other tasks.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a digital input
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to read (from 1-8, 'a'-'h', 'A'-'H')
*
* \return 1 if the button is pressed and had not been pressed
* the last time this function was called, 0 otherwise.
*/
int32_t ext_adi_digital_get_new_press(uint8_t smart_port, uint8_t adi_port);
/**
* Sets the digital value (1 or 0) of a port configured as a digital output.
*
* If the port is configured as some other mode, behavior is undefined.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a digital output
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param value
* An expression evaluating to "true" or "false" to set the output to
* HIGH or LOW respectively, or the constants HIGH or LOW themselves
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_digital_write(uint8_t smart_port, uint8_t adi_port, bool value);
/**
* Configures the port as an input or output with a variety of settings.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param mode
* One of INPUT, INPUT_ANALOG, INPUT_FLOATING, OUTPUT, or OUTPUT_OD
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_pin_mode(uint8_t smart_port, uint8_t adi_port, uint8_t mode);
/**
* Sets the speed of the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an motor
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port number (from 1-8, 'a'-'h', 'A'-'H') to configure
* \param speed
* The new signed speed; -127 is full reverse and 127 is full forward,
* with 0 being off
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_motor_set(uint8_t smart_port, uint8_t adi_port, int8_t speed);
/**
* Gets the last set speed of the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an motor
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to get (from 1-8, 'a'-'h', 'A'-'H')
*
* \return The last set speed of the motor on the given port
*/
int32_t ext_adi_motor_get(uint8_t smart_port, uint8_t adi_port);
/**
* Stops the motor on the given port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an motor
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to set (from 1-8, 'a'-'h', 'A'-'H')
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_motor_stop(uint8_t smart_port, uint8_t adi_port);
/**
* Reference type for an initialized encoder.
*
* This merely contains the port number for the encoder, unlike its use as an
* object to store encoder data in PROS 2.
*/
typedef int32_t ext_adi_encoder_t;
/**
* Gets the number of ticks recorded by the encoder.
*
* There are 360 ticks in one revolution.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to read
*
* \return The signed and cumulative number of counts since the last start or
* reset
*/
int32_t ext_adi_encoder_get(ext_adi_encoder_t enc);
/**
* Creates an encoder object and configures the specified ports accordingly.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an encoder
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port_top
* The "top" wire from the encoder sensor with the removable cover side
* up. This should be in port 1, 3, 5, or 7 ('A', 'C', 'E', or 'G').
* \param adi_port_bottom
* The "bottom" wire from the encoder sensor
* \param reverse
* If "true", the sensor will count in the opposite direction
*
* \return An adi_encoder_t object to be stored and used for later calls to
* encoder functions
*/
ext_adi_encoder_t ext_adi_encoder_init(uint8_t smart_port, uint8_t adi_port_top, uint8_t adi_port_bottom, bool reverse);
/**
* Sets the encoder value to zero.
*
* It is safe to use this method while an encoder is enabled. It is not
* necessary to call this method before stopping or starting an encoder.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to reset
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_encoder_reset(ext_adi_encoder_t enc);
/**
* Disables the encoder and voids the configuration on its ports.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an encoder
*
* \param enc
* The adi_encoder_t object from adi_encoder_init() to stop
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_encoder_shutdown(ext_adi_encoder_t enc);
/**
* Reference type for an initialized ultrasonic.
*
* This merely contains the port number for the ultrasonic, unlike its use as an
* object to store encoder data in PROS 2.
*/
typedef int32_t ext_adi_ultrasonic_t;
/**
* Gets the current ultrasonic sensor value in centimeters.
*
* If no object was found, zero is returned. If the ultrasonic sensor was never
* started, the return value is undefined. Round and fluffy objects can cause
* inaccurate values to be returned.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param ult
* The adi_ultrasonic_t object from adi_ultrasonic_init() to read
*
* \return The distance to the nearest object in m^-4 (10000 indicates 1 meter),
* measured from the sensor's mounting points.
*/
int32_t ext_adi_ultrasonic_get(ext_adi_ultrasonic_t ult);
/**
* Creates an ultrasonic object and configures the specified ports accordingly.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port_ping
* The port connected to the orange OUTPUT cable. This should be in port
* 1, 3, 5, or 7 ('A', 'C', 'E', 'G').
* \param adi_port_echo
* The port connected to the yellow INPUT cable. This should be in the
* next highest port following port_ping.
*
* \return An adi_ultrasonic_t object to be stored and used for later calls to
* ultrasonic functions
*/
ext_adi_ultrasonic_t ext_adi_ultrasonic_init(uint8_t smart_port, uint8_t adi_port_ping, uint8_t adi_port_echo);
/**
* Disables the ultrasonic sensor and voids the configuration on its ports.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as an ultrasonic
*
* \param ult
* The adi_ultrasonic_t object from adi_ultrasonic_init() to stop
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_ultrasonic_shutdown(ext_adi_ultrasonic_t ult);
/**
* Reference type for an initialized gyroscope.
*
* This merely contains the port number for the gyroscope, unlike its use as an
* object to store gyro data in PROS 2.
*
* (Might Be useless with the wire expander.)
*/
typedef int32_t ext_adi_gyro_t;
/**
* Gets the current gyro angle in tenths of a degree. Unless a multiplier is
* applied to the gyro, the return value will be a whole number representing
* the number of degrees of rotation times 10.
*
* There are 360 degrees in a circle, thus the gyro will return 3600 for one
* whole rotation.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object for which the angle will be returned
*
* \return The gyro angle in degrees.
*/
double ext_adi_gyro_get(ext_adi_gyro_t gyro);
/**
* Initializes a gyroscope on the given port. If the given port has not
* previously been configured as a gyro, then this function starts a 1300 ms
* calibration period.
*
* It is highly recommended that this function be called from initialize() when
* the robot is stationary to ensure proper calibration.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a gyro
*
* \param smart_port
* The smart port number that the ADI Expander is in
* \param adi_port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
* \param multiplier
* A scalar value that will be multiplied by the gyro heading value
* supplied by the ADI
*
* \return An adi_gyro_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
ext_adi_gyro_t ext_adi_gyro_init(uint8_t smart_port, uint8_t adi_port, double multiplier);
/**
* Resets the gyroscope value to zero.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object for which the angle will be returned
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_gyro_reset(ext_adi_gyro_t gyro);
/**
* Disables the gyro and voids the configuration on its port.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - Either the ADI port value or the smart port value is not within its
* valid range (ADI port: 1-8, 'a'-'h', or 'A'-'H'; smart port: 1-21).
* EADDRINUSE - The port is not configured as a gyro
*
* \param gyro
* The adi_gyro_t object to be shut down
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t ext_adi_gyro_shutdown(ext_adi_gyro_t gyro);
/**
* Reference type for an initialized potentiometer.
*
* This merely contains the port number for the potentiometer, unlike its use as an
* object to store gyro data in PROS 2.
*/
typedef int32_t ext_adi_potentiometer_t;
/**
* Initializes a potentiometer on 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 ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
*
* \param smart_port
* The smart port with the adi expander (1-21)
* \param adi_port
* The ADI port to initialize as a gyro (from 1-8, 'a'-'h', 'A'-'H')
* \param potentiometer_type
* An adi_potentiometer_type_e_t enum value specifying the potentiometer version type
*
* \return An adi_potentiometer_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
ext_adi_potentiometer_t ext_adi_potentiometer_init(uint8_t smart_port, uint8_t adi_port, adi_potentiometer_type_e_t potentiometer_type);
/**
* Gets the current potentiometer angle in tenths of a degree.
*
* The original potentiometer rotates 250 degrees thus returning an angle between 0-250 degrees.
* Potentiometer V2 rotates 333 degrees thus returning an angle between 0-333 degrees.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EADDRINUSE - The port is not configured as a potentiometer
*
* \param potentiometer
* The adi_potentiometer_t object for which the angle will be returned
*
* \return The potentiometer angle in degrees.
*/
double ext_adi_potentiometer_get_angle(ext_adi_potentiometer_t potentiometer);
/**
* Reference type for an initialized addressable led, which stores its smart and adi port.
*/
typedef int32_t ext_adi_led_t;
/**
* Initializes a led on 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 ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* \param smart_port
* The smart port with the adi expander (1-21)
* \param adi_port
* The ADI port to initialize as a led (from 1-8, 'a'-'h', 'A'-'H')
*
* \return An ext_adi_led_t object containing the given port, or PROS_ERR if the
* initialization failed.
*/
ext_adi_led_t ext_adi_led_init(uint8_t smart_port, uint8_t adi_port);
/**
* @brief Clear the entire led strip of color
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t ext_adi_led_clear_all(ext_adi_led_t led, uint32_t* buffer, uint32_t buffer_length);
/**
* @brief Set the entire led strip using the colors contained in the buffer
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t ext_adi_led_set(ext_adi_led_t led, uint32_t* buffer, uint32_t buffer_length);
/**
* @brief Set the entire led strip to one color
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of buffer to clear
* @param color color to set all the led strip value to
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t ext_adi_led_set_all(ext_adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t color);
/**
* @brief Set one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of the input buffer
* @param color color to clear all the led strip to
* @param pixel_position position of the pixel to clear (0 indexed)
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t ext_adi_led_set_pixel(ext_adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t color, uint32_t pixel_position);
/**
* @brief Clear one pixel on the led strip
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The given value is not within the range of ADI Ports
* EINVAL - A given value is not correct, or the buffer is null
* EADDRINUSE - The port is not configured for ADI output
*
* @param led port of type adi_led_t
* @param buffer array of colors in format 0xRRGGBB, recommended that individual RGB value not to exceed 0x80 due to current draw
* @param buffer_length length of the input buffer
* @param pixel_position position of the pixel to clear (0 indexed)
* @return PROS_SUCCESS if successful, PROS_ERR if not
*/
int32_t ext_adi_led_clear_pixel(ext_adi_led_t led, uint32_t* buffer, uint32_t buffer_length, uint32_t pixel_position);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_ADI_H_

View File

@ -0,0 +1,398 @@
/**
* \file pros/gps.h
*
* Contains prototypes for functions related to the VEX GPS.
*
* Visit https://pros.cs.purdue.edu/v5/api/c/gps.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_GPS_H_
#define _PROS_GPS_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
typedef struct __attribute__((__packed__)) gps_status_s {
double x; ///< X Position (meters)
double y; ///< Y Position (meters)
double pitch; ///< Percieved Pitch based on GPS + IMU
double roll; ///< Percieved Roll based on GPS + IMU
double yaw; ///< Percieved Yaw based on GPS + IMU
} gps_status_s_t;
struct gps_raw_s {
double x; ///< Percieved Pitch based on GPS + IMU
double y; ///< Percieved Roll based on GPS + IMU
double z; ///< Percieved Yaw based on GPS + IMU
};
typedef struct gps_raw_s gps_accel_s_t;
typedef struct gps_raw_s gps_gyro_s_t;
/**
* Set the GPS's offset relative to the center of turning in meters,
* as well as its initial position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param xOffset
* Cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Cartesian 4-Quadrant Y offset from center of turning (meters)
* \param xInitial
* Initial 4-Quadrant X Position, with (0,0) being at the center of the field (meters)
* \param yInitial
* Initial 4-Quadrant Y Position, with (0,0) being at the center of the field (meters)
* \param headingInitial
* Heading with 0 being north on the field, in degrees [0,360) going clockwise
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_initialize_full(uint8_t port, double xInitial, double yInitial, double headingInitial, double xOffset,
double yOffset);
/**
* Set the GPS's offset relative to the center of turning in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param xOffset
* Cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Cartesian 4-Quadrant Y offset from center of turning (meters)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_set_offset(uint8_t port, double xOffset, double yOffset);
/**
* Get the GPS's location relative to the center of turning/origin in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param xOffset
* Pointer to cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Pointer to cartesian 4-Quadrant Y offset from center of turning (meters)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_get_offset(uint8_t port, double* xOffset, double* yOffset);
/**
* Sets the robot's location relative to the center of the field in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param xInitial
* Initial 4-Quadrant X Position, with (0,0) being at the center of the field (meters)
* \param yInitial
* Initial 4-Quadrant Y Position, with (0,0) being at the center of the field (meters)
* \param headingInitial
* Heading with 0 being north on the field, in degrees [0,360) going clockwise
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_set_position(uint8_t port, double xInitial, double yInitial, double headingInitial);
/**
* Set the GPS sensor's data rate in milliseconds, only applies to IMU on GPS.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param rate
* Data rate in milliseconds (Minimum: 5 ms)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_set_data_rate(uint8_t port, uint32_t rate);
/**
* Get the possible RMS (Root Mean Squared) error in meters for GPS position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return Possible RMS (Root Mean Squared) error in meters for GPS position.
* If the operation failed, returns PROS_ERR_F and errno is set.
*/
double gps_get_error(uint8_t port);
/**
* Gets the position and roll, yaw, and pitch of the GPS.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return A struct (gps_status_s_t) containing values mentioned above.
* If the operation failed, all the structure's members are filled with
* PROS_ERR_F and errno is set.
*/
gps_status_s_t gps_get_status(uint8_t port);
/**
* Gets the X position in meters of the robot relative to the starting position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The X position in meters. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_x_position(uint8_t port);
/**
* Gets the Y position in meters of the robot relative to the starting position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The Y position in meters. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_y_position(uint8_t port);
/**
* Gets the pitch of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The pitch in (-90,90] degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_pitch(uint8_t port);
/**
* Gets the roll of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The roll in (-180,180] degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_roll(uint8_t port);
/**
* Gets the yaw of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The yaw in (-180,180] degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_yaw(uint8_t port);
/**
* Get the heading in [0,360) degree values.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The heading in [0,360) degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
double gps_get_heading(uint8_t port);
/**
* Get the heading in the max double value and min double value scale.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
*
* \return The heading in [DOUBLE_MIN, DOUBLE_MAX] values. If the operation
* fails, returns PROS_ERR_F and errno is set.
*/
double gps_get_heading_raw(uint8_t port);
/**
* Gets the GPS sensor's elapsed rotation value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \return The elased heading in degrees. If the operation fails, returns
* PROS_ERR_F and errno is set.
*/
double gps_get_rotation(uint8_t port);
/**
* Set the GPS sensor's rotation value to target value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \param target
* Target rotation value to set rotation value to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_set_rotation(uint8_t port, double target);
/**
* Tare the GPS sensor's rotation value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t gps_tare_rotation(uint8_t port);
/**
* Get the GPS's raw gyroscope values
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS port number from 1-21
* \return The raw gyroscope values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
gps_gyro_s_t gps_get_gyro_rate(uint8_t port);
/**
* Get the GPS's raw accelerometer values
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS's port number from 1-21
* \return The raw accelerometer values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
gps_accel_s_t gps_get_accel(uint8_t port);
#ifdef __cplusplus
}
}
}
#endif
#endif

View File

@ -0,0 +1,359 @@
/**
* \file pros/gps.hpp
*
* Contains prototypes for functions related to the VEX GPS.
*
* Visit https://pros.cs.purdue.edu/v5/api/cpp/gps.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_GPS_HPP_
#define _PROS_GPS_HPP_
#include <stdbool.h>
#include <cstdint>
#include "pros/gps.h"
namespace pros {
class Gps {
const std::uint8_t _port;
public:
Gps(const std::uint8_t port) : _port(port){};
Gps(const std::uint8_t port, double xInitial, double yInitial, double headingInitial) : _port(port) {
pros::c::gps_set_position(port, xInitial, yInitial, headingInitial);
};
Gps(const std::uint8_t port, double xOffset, double yOffset) : _port(port) {
pros::c::gps_set_offset(port, xOffset, yOffset);
};
Gps(const std::uint8_t port, double xInitial, double yInitial, double headingInitial, double xOffset, double yOffset)
: _port(port) {
pros::c::gps_initialize_full(port, xInitial, yInitial, headingInitial, xOffset, yOffset);
};
/**
* Set the GPS's offset relative to the center of turning in meters,
* as well as its initial position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param xOffset
* Cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Cartesian 4-Quadrant Y offset from center of turning (meters)
* \param xInitial
* Initial 4-Quadrant X Position, with (0,0) being at the center of the field (meters)
* \param yInitial
* Initial 4-Quadrant Y Position, with (0,0) being at the center of the field (meters)
* \param headingInitial
* Heading with 0 being north on the field, in degrees [0,360) going clockwise
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t initialize_full(double xInitial, double yInitial, double headingInitial, double xOffset,
double yOffset) const;
/**
* Set the GPS's offset relative to the center of turning in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param xOffset
* Cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Cartesian 4-Quadrant Y offset from center of turning (meters)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_offset(double xOffset, double yOffset) const;
/**
* Get the GPS's location relative to the center of turning/origin in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param xOffset
* Pointer to cartesian 4-Quadrant X offset from center of turning (meters)
* \param yOffset
* Pointer to cartesian 4-Quadrant Y offset from center of turning (meters)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t get_offset(double* xOffset, double* yOffset) const;
/**
* Sets the robot's location relative to the center of the field in meters.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param xInitial
* Initial 4-Quadrant X Position, with (0,0) being at the center of the field (meters)
* \param yInitial
* Initial 4-Quadrant Y Position, with (0,0) being at the center of the field (meters)
* \param headingInitial
* Heading with 0 being north on the field, in degrees [0,360) going clockwise
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_position(double xInitial, double yInitial, double headingInitial) const;
/**
* Set the GPS sensor's data rate in milliseconds, only applies to IMU on GPS.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param rate
* Data rate in milliseconds (Minimum: 5 ms)
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_data_rate(std::uint32_t rate) const;
/**
* Get the possible RMS (Root Mean Squared) error in meters for GPS position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \return Possible RMS (Root Mean Squared) error in meters for GPS position.
* If the operation failed, returns PROS_ERR_F and errno is set.
*/
virtual double get_error() const;
/**
* Gets the position and roll, yaw, and pitch of the GPS.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return A struct (gps_status_s_t) containing values mentioned above.
* If the operation failed, all the structure's members are filled with
* PROS_ERR_F and errno is set.
*/
virtual pros::c::gps_status_s_t get_status() const;
/**
* Gets the X position in meters of the robot relative to the starting position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The X position in meters. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_x_position() const;
/**
* Gets the Y position in meters of the robot relative to the starting position.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The Y position in meters. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_y_position() const;
/**
* Gets the pitch of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The pitch in [0,360) degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_pitch() const;
/**
* Gets the roll of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The roll in [0,360) degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_roll() const;
/**
* Gets the yaw of the GPS in degrees relative to the starting orientation.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The yaw in [0,360) degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_yaw() const;
/**
* Get the heading in [0,360) degree values.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
*
* \return The heading in [0,360) degree values. If the operation failed,
* returns PROS_ERR_F and errno is set.
*/
virtual double get_heading() const;
/**
* Get the heading in the max double value and min double value scale.
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \return The heading in [DOUBLE_MIN, DOUBLE_MAX] values. If the operation
* fails, returns PROS_ERR_F and errno is set.
*/
virtual double get_heading_raw() const;
/**
* Gets the GPS sensor's elapsed rotation value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \return The elased heading in degrees. If the operation fails, returns
* PROS_ERR_F and errno is set.
*/
virtual double get_rotation() const;
/**
* Set the GPS sensor's rotation value to target value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param target
* Target rotation value to set rotation value to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_rotation(double target) const;
/**
* Tare the GPS sensor's rotation value
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_rotation() const;
/**
* Get the GPS's raw gyroscope values
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \return The raw gyroscope values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
virtual pros::c::gps_gyro_s_t get_gyro_rate() const;
/**
* Get the GPS's raw accelerometer values
*
* 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 GPS
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 GPS's port number from 1-21
* \return The raw accelerometer values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
virtual pros::c::gps_accel_s_t get_accel() const;
}; // Gps Class
using GPS = Gps;
} // namespace pros
#endif

View File

@ -0,0 +1,546 @@
/**
* \file pros/imu.h
*
* Contains prototypes for functions related to the VEX Inertial sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/imu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_IMU_H_
#define _PROS_IMU_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
typedef enum imu_status_e {
E_IMU_STATUS_READY = 0, // IMU is connected but not currently calibrating
E_IMU_STATUS_CALIBRATING = 19, // IMU is calibrating
E_IMU_STATUS_ERROR = 0xFF, // NOTE: used for returning an error from the get_status function, not that the IMU is
// necessarily in an error state
} imu_status_e_t;
typedef struct __attribute__((__packed__)) quaternion_s {
double x;
double y;
double z;
double w;
} quaternion_s_t;
struct imu_raw_s {
double x;
double y;
double z;
};
typedef struct imu_raw_s imu_gyro_s_t;
typedef struct imu_raw_s imu_accel_s_t;
typedef struct __attribute__((__packed__)) euler_s {
double pitch;
double roll;
double yaw;
} euler_s_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define IMU_STATUS_CALIBRATING pros::E_IMU_STATUS_CALIBRATING
#define IMU_STATUS_ERROR pros::E_IMU_STATUS_ERROR
#else
#define IMU_STATUS_CALIBRATING E_IMU_STATUS_CALIBRATING
#define IMU_STATUS_ERROR E_IMU_STATUS_ERROR
#endif
#endif
#define IMU_MINIMUM_DATA_RATE 5
/**
* Calibrate IMU
*
* Calibration takes approximately 2 seconds, but this function only blocks
* until the IMU status flag is set properly to E_IMU_STATUS_CALIBRATING,
* with a minimum blocking time of 5ms.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is already calibrating, or time out setting the status flag.
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed setting errno.
*/
int32_t imu_reset(uint8_t port);
/**
* Calibrate IMU and Blocks while Calibrating
*
* Calibration takes approximately 2 seconds and blocks during this period,
* with a timeout for this operation being set a 3 seconds as a safety margin.
* Like the other reset function, this function also blocks until the IMU
* status flag is set properly to E_IMU_STATUS_CALIBRATING, with a minimum
* blocking time of 5ms and a timeout of 1 second if it's never set.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is already calibrating, or time out setting the status flag.
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed (timing out or port claim failure), setting errno.
*/
int32_t imu_reset_blocking(uint8_t port);
/**
* Set the Inertial Sensor's refresh interval in milliseconds.
*
* The rate may be specified in increments of 5ms, and will be rounded down to
* the nearest increment. The minimum allowable refresh rate is 5ms. The default
* rate is 10ms.
*
* As values are copied into the shared memory buffer only at 10ms intervals,
* setting this value to less than 10ms does not mean that you can poll the
* sensor's values any faster. However, it will guarantee that the data is as
* recent as possible.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param rate The data refresh interval in milliseconds
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_data_rate(uint8_t port, uint32_t rate);
/**
* Get the total number of degrees the Inertial Sensor has spun about the z-axis
*
* This value is theoretically unbounded. Clockwise rotations are represented
* with positive degree values, while counterclockwise rotations are represented
* with negative ones.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The degree value or PROS_ERR_F if the operation failed, setting
* errno.
*/
double imu_get_rotation(uint8_t port);
/**
* Get the Inertial Sensor's heading relative to the initial direction of its
* x-axis
*
* This value is bounded by [0,360). Clockwise rotations are represented with
* positive degree values, while counterclockwise rotations are represented with
* negative ones.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The degree value or PROS_ERR_F if the operation failed, setting
* errno.
*/
double imu_get_heading(uint8_t port);
/**
* Get a quaternion representing the Inertial Sensor's orientation
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The quaternion representing the sensor's orientation. If the
* operation failed, all the quaternion's members are filled with PROS_ERR_F and
* errno is set.
*/
quaternion_s_t imu_get_quaternion(uint8_t port);
/**
* Get the Euler angles representing the Inertial Sensor's orientation
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The Euler angles representing the sensor's orientation. If the
* operation failed, all the structure's members are filled with PROS_ERR_F and
* errno is set.
*/
euler_s_t imu_get_euler(uint8_t port);
/**
* Get the Inertial Sensor's pitch angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The pitch angle, or PROS_ERR_F if the operation failed, setting
* errno.
*/
double imu_get_pitch(uint8_t port);
/**
* Get the Inertial Sensor's roll angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The roll angle, or PROS_ERR_F if the operation failed, setting errno.
*/
double imu_get_roll(uint8_t port);
/**
* Get the Inertial Sensor's yaw angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The yaw angle, or PROS_ERR_F if the operation failed, setting errno.
*/
double imu_get_yaw(uint8_t port);
/**
* Get the Inertial Sensor's raw gyroscope values
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The raw gyroscope values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
imu_gyro_s_t imu_get_gyro_rate(uint8_t port);
/**
* Get the Inertial Sensor's raw acceleroneter values
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The raw accelerometer values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
imu_accel_s_t imu_get_accel(uint8_t port);
/**
* Get the Inertial Sensor's status
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The Inertial Sensor's status code, or PROS_ERR if the operation
* failed, setting errno.
*/
imu_status_e_t imu_get_status(uint8_t port);
// NOTE: not used
// void imu_set_mode(uint8_t port, uint32_t mode);
// uint32_t imu_get_mode(uint8_t port);
//Value reset functions:
/**
* Resets the current reading of the Inertial Sensor's heading to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_heading(uint8_t port);
/**
* Resets the current reading of the Inertial Sensor's rotation to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_rotation(uint8_t port);
/**
* Resets the current reading of the Inertial Sensor's pitch to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_pitch(uint8_t port);
/**
* Resets the current reading of the Inertial Sensor's roll to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_roll(uint8_t port);
/**
* Resets the current reading of the Inertial Sensor's yaw to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_yaw(uint8_t port);
/**
* Reset all 3 euler values of the Inertial Sensor to 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare_euler(uint8_t port);
/**
* Resets all 5 values of the Inertial Sensor to 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_tare(uint8_t port);
//Value set functions:
/**
* Sets the current reading of the Inertial Sensor's euler values to
* target euler values. Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target euler values for the euler values to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_euler(uint8_t port, euler_s_t target);
/**
* Sets the current reading of the Inertial Sensor's rotation to target value
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the rotation value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_rotation(uint8_t port, double target);
/**
* Sets the current reading of the Inertial Sensor's heading to target value
* Target will default to 360 if above 360 and default to 0 if below 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the heading value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_heading(uint8_t port, double target);
/**
* Sets the current reading of the Inertial Sensor's pitch to target value
* Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the pitch value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_pitch(uint8_t port, double target);
/**
* Sets the current reading of the Inertial Sensor's roll to target value
* Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the roll value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_roll(uint8_t port, double target);
/**
* Sets the current reading of the Inertial Sensor's yaw to target value
* Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the yaw value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t imu_set_yaw(uint8_t port, double target);
#ifdef __cplusplus
}
}
}
#endif
#endif

View File

@ -0,0 +1,459 @@
/**
* \file pros/imu.hpp
*
* Contains prototypes for functions related to the VEX Inertial sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/imu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_IMU_HPP_
#define _PROS_IMU_HPP_
#include <cstdint>
#include "pros/imu.h"
namespace pros {
class Imu {
const std::uint8_t _port;
public:
Imu(const std::uint8_t port) : _port(port){};
/**
* Calibrate IMU
*
* Calibration takes approximately 2 seconds and blocks during this period if
* the blocking param is true, with a timeout for this operation being set a 3
* seconds as a safety margin. This function also blocks until the IMU
* status flag is set properly to E_IMU_STATUS_CALIBRATING, with a minimum
* blocking time of 5ms and a timeout of 1 second if it's never set.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is already calibrating, or time out setting the status flag.
*
* \param blocking
* Whether this function blocks during calibration.
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t reset(bool blocking = false) const;
/**
* Set the Inertial Sensor's refresh interval in milliseconds.
*
* The rate may be specified in increments of 5ms, and will be rounded down to
* the nearest increment. The minimum allowable refresh rate is 5ms. The default
* rate is 10ms.
*
* As values are copied into the shared memory buffer only at 10ms intervals,
* setting this value to less than 10ms does not mean that you can poll the
* sensor's values any faster. However, it will guarantee that the data is as
* recent as possible.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param rate
* The data refresh interval in milliseconds
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_data_rate(std::uint32_t rate) const;
/**
* Get the total number of degrees the Inertial Sensor has spun about the z-axis
*
* This value is theoretically unbounded. Clockwise rotations are represented
* with positive degree values, while counterclockwise rotations are represented
* with negative ones.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The degree value or PROS_ERR_F if the operation failed, setting
* errno.
*/
virtual double get_rotation() const;
/**
* Get the Inertial Sensor's heading relative to the initial direction of its
* x-axis
*
* This value is bounded by [0,360). Clockwise rotations are represented with
* positive degree values, while counterclockwise rotations are represented with
* negative ones.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The degree value or PROS_ERR_F if the operation failed, setting
* errno.
*/
virtual double get_heading() const;
/**
* Get a quaternion representing the Inertial Sensor's orientation
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The quaternion representing the sensor's orientation. If the
* operation failed, all the quaternion's members are filled with PROS_ERR_F and
* errno is set.
*/
virtual pros::c::quaternion_s_t get_quaternion() const;
/**
* Get the Euler angles representing the Inertial Sensor's orientation
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The Euler angles representing the sensor's orientation. If the
* operation failed, all the structure's members are filled with PROS_ERR_F and
* errno is set.
*/
virtual pros::c::euler_s_t get_euler() const;
/**
* Get the Inertial Sensor's pitch angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The pitch angle, or PROS_ERR_F if the operation failed, setting
* errno.
*/
virtual double get_pitch() const;
/**
* Get the Inertial Sensor's roll angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The roll angle, or PROS_ERR_F if the operation failed, setting errno.
*/
virtual double get_roll() const;
/**
* Get the Inertial Sensor's yaw angle bounded by (-180,180)
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The yaw angle, or PROS_ERR_F if the operation failed, setting errno.
*/
virtual double get_yaw() const;
/**
* Get the Inertial Sensor's raw gyroscope values
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The raw gyroscope values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
virtual pros::c::imu_gyro_s_t get_gyro_rate() const;
/**
* Resets the current reading of the Inertial Sensor's rotation to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_rotation() const;
/**
* Resets the current reading of the Inertial Sensor's heading to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_heading() const;
/**
* Resets the current reading of the Inertial Sensor's pitch to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_pitch() const;
/**
* Resets the current reading of the Inertial Sensor's yaw to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_yaw() const;
/**
* Resets the current reading of the Inertial Sensor's roll to zero
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_roll() const;
/**
* Resets all 5 values of the Inertial Sensor to 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare() const;
/**
* Reset all 3 euler values of the Inertial Sensor to 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t tare_euler() const;
/**
* Sets the current reading of the Inertial Sensor's heading to target value
* Target will default to 360 if above 360 and default to 0 if below 0.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the heading value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_heading(const double target) const;
/**
* Sets the current reading of the Inertial Sensor's rotation to target value
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the rotation value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_rotation(const double target) const;
/**
* Sets the current reading of the Inertial Sensor's yaw to target value
* Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for yaw value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_yaw(const double target) const;
/**
* Sets the current reading of the Inertial Sensor's pitch to target value
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target value for the pitch value to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_pitch(const double target) const;
/**
* Sets the current reading of the Inertial Sensor's roll to target value
* Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target euler values for the euler values to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_roll(const double target) const;
/**
* Sets the current reading of the Inertial Sensor's euler values to
* target euler values. Will default to +/- 180 if target exceeds +/- 180.
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \param target
* Target euler values for the euler values to be set to
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_euler(const pros::c::euler_s_t target) const;
/**
* Get the Inertial Sensor's raw accelerometer values
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The raw accelerometer values. If the operation failed, all the
* structure's members are filled with PROS_ERR_F and errno is set.
*/
virtual pros::c::imu_accel_s_t get_accel() const;
/**
* Get the Inertial Sensor's status
*
* 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 Inertial Sensor
* EAGAIN - The sensor is still calibrating
*
* \param port
* The V5 Inertial Sensor port number from 1-21
* \return The Inertial Sensor's status code, or PROS_ERR if the operation
* failed, setting errno.
*/
virtual pros::c::imu_status_e_t get_status() const;
/**
* Check whether the IMU is calibrating
*
* \return true if the V5 Inertial Sensor is calibrating or false
* false if it is not.
*/
virtual bool is_calibrating() const;
};
using IMU = Imu;
} // namespace pros
#endif

View File

@ -0,0 +1,275 @@
/**
* \file pros/link.h
*
* Contains prototypes for functions related to the robot to robot communications.
*
* Visit https://pros.cs.purdue.edu/v5/api/c/link.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_LINK_H_
#define _PROS_LINK_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
typedef enum link_type_e {
E_LINK_RECIEVER = 0,
E_LINK_TRANSMITTER,
E_LINK_RX = E_LINK_RECIEVER,
E_LINK_TX = E_LINK_TRANSMITTER
} link_type_e_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define LINK_RECIEVER pros::E_LINK_RECIEVER
#define LINK_TRANSMITTER pros::E_LINK_TRANSMITTER
#define LINK_RX pros::E_LINK_RX
#define LINK_TX pros::E_LINK_TX
#else
#define LINK_RECIEVER E_LINK_RECIEVER
#define LINK_TRANSMITTER E_LINK_TRANSMITTER
#define LINK_RX E_LINK_RX
#define LINK_TX E_LINK_TX
#endif
#endif
#define LINK_BUFFER_SIZE 512
#ifdef __cplusplus
namespace c {
#endif
/**
* Initializes a link on a radio port, with an indicated type. There might be a
* 1 to 2 second delay from when this function is called to when the link is initializes.
* PROS currently only supports the use of one radio per brain.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
* \param link_id
* Unique link ID in the form of a string, needs to be different from other links in
* the area.
* \param type
* Indicates whether the radio link on the brain is a transmitter or reciever,
* with the transmitter having double the transmitting bandwidth as the recieving
* end (1040 bytes/s vs 520 bytes/s).
*
* \return PROS_ERR if initialization fails, 1 if the initialization succeeds.
*/
uint32_t link_init(uint8_t port, const char* link_id, link_type_e_t type);
/**
* Initializes a link on a radio port, with an indicated type and the ability for
* vexlink to override the controller radio. There might be a 1 to 2 second delay
* from when this function is called to when the link is initializes.
* PROS currently only supports the use of one radio per brain.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
* \param link_id
* Unique link ID in the form of a string, needs to be different from other links in
* the area.
* \param type
* Indicates whether the radio link on the brain is a transmitter or reciever,
* with the transmitter having double the transmitting bandwidth as the recieving
* end (1040 bytes/s vs 520 bytes/s).
*
* \return PROS_ERR if initialization fails, 1 if the initialization succeeds.
*/
uint32_t link_init_override(uint8_t port, const char* link_id, link_type_e_t type);
/**
* Checks if a radio link on a port is active or not.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
*
* \return If a radio is connected to a port and it's connected to a link.
*/
bool link_connected(uint8_t port);
/**
* Returns the bytes of data available to be read
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
*
* \return PROS_ERR if port is not a link/radio, else the bytes available to be
* read by the user.
*/
uint32_t link_raw_receivable_size(uint8_t port);
/**
* Returns the bytes of data available in transmission buffer.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
*
* \return PROS_ERR if port is not a link/radio,
*/
uint32_t link_raw_transmittable_size(uint8_t port);
/**
* Send raw serial data through vexlink.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EBUSY - The transmitter buffer is still busy with a previous transmission, and there is no
* room in the FIFO buffer (queue) to transmit the data.
* EINVAL - The data given is NULL
*
* \param port
* The port of the radio for the intended link.
* \param data
* Buffer with data to send
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully transmitted
* data size if it succeeded.
*/
uint32_t link_transmit_raw(uint8_t port, void* data, uint16_t data_size);
/**
* Receive raw serial data through vexlink.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EINVAL - The destination given is NULL, or the size given is larger than the FIFO buffer
* or destination buffer.
*
* \param port
* The port of the radio for the intended link.
* \param dest
* Destination buffer to read data to
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully received
* data size if it succeeded.
*/
uint32_t link_receive_raw(uint8_t port, void* dest, uint16_t data_size);
/**
* Send packeted message through vexlink, with a checksum and start byte.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EBUSY - The transmitter buffer is still busy with a previous transmission, and there is no
* room in the FIFO buffer (queue) to transmit the data.
* EINVAL - The data given is NULL
*
* \param port
* The port of the radio for the intended link.
* \param data
* Buffer with data to send
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully transmitted
* data size if it succeeded.
*/
uint32_t link_transmit(uint8_t port, void* data, uint16_t data_size);
/**
* Receive packeted message through vexlink, with a checksum and start byte.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EINVAL - The destination given is NULL, or the size given is larger than the FIFO buffer
* or destination buffer.
* EBADMSG - Protocol error related to start byte, data size, or checksum.
*
* \param port
* The port of the radio for the intended link.
* \param dest
* Destination buffer to read data to
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link or protocol error, and the successfully
* transmitted data size if it succeeded.
*/
uint32_t link_receive(uint8_t port, void* dest, uint16_t data_size);
/**
* Clear the receive buffer of the link, and discarding the data.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
*
* \return PROS_ERR if port is not a link, and the successfully received
* data size if it succeeded.
*/
uint32_t link_clear_receive_buf(uint8_t port);
#ifdef __cplusplus
}
}
}
#endif
#endif

View File

@ -0,0 +1,200 @@
/**
* \file pros/link.hpp
*
* Contains prototypes for functions related to robot to robot communications.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/link.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_LINK_HPP_
#define _PROS_LINK_HPP_
#include <cstdint>
#include <string>
#include "pros/link.h"
namespace pros {
class Link {
private:
std::uint8_t _port;
public:
/**
* Initializes a link on a radio port, with an indicated type. There might be a
* 1 to 2 second delay from when this function is called to when the link is initializes.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \param port
* The port of the radio for the intended link.
* \param link_id
* Unique link ID in the form of a string, needs to be different from other links in
* the area.
* \param type
* Indicates whether the radio link on the brain is a transmitter or reciever,
* with the transmitter having double the transmitting bandwidth as the recieving
* end (1040 bytes/s vs 520 bytes/s).
* \param ov
* Indicates if the radio on the given port needs vexlink to override the controller radio
*
* \return PROS_ERR if initialization fails, 1 if the initialization succeeds.
*/
Link(const std::uint8_t port, const std::string link_id, link_type_e_t type, bool ov = false);
/**
* Checks if a radio link on a port is active or not.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \return If a radio is connected to a port and it's connected to a link.
*/
bool connected();
/**
* Returns the bytes of data number of without protocol available to be read
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \return PROS_ERR if port is not a link/radio, else the bytes available to be
* read by the user.
*/
std::uint32_t raw_receivable_size();
/**
* Returns the bytes of data available in transmission buffer.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
*
* \return PROS_ERR if port is not a link/radio,
*/
std::uint32_t raw_transmittable_size();
/**
* Send raw serial data through vexlink.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EBUSY - The transmitter buffer is still busy with a previous transmission, and there is no
* room in the FIFO buffer (queue) to transmit the data.
* EINVAL - The data given is NULL
*
* \param data
* Buffer with data to send
* \param data_size
* Buffer with data to send
*
* \return PROS_ERR if port is not a link, and the successfully transmitted
* data size if it succeeded.
*/
std::uint32_t transmit_raw(void* data, std::uint16_t data_size);
/**
* Receive raw serial data through vexlink.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EINVAL - The destination given is NULL, or the size given is larger than the FIFO buffer
* or destination buffer.
*
* \param dest
* Destination buffer to read data to
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully received
* data size if it succeeded.
*/
std::uint32_t receive_raw(void* dest, std::uint16_t data_size);
/**
* Send packeted message through vexlink, with a checksum and start byte.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EBUSY - The transmitter buffer is still busy with a previous transmission, and there is no
* room in the FIFO buffer (queue) to transmit the data.
* EINVAL - The data given is NULL
*
* \param data
* Buffer with data to send
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully transmitted
* data size if it succeeded.
*/
std::uint32_t transmit(void* data, std::uint16_t data_size);
/**
* Receive packeted message through vexlink, with a checksum and start byte.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* EINVAL - The destination given is NULL, or the size given is larger than the FIFO buffer
* or destination buffer.
* EBADMSG - Protocol error related to start byte, data size, or checksum.
* \param dest
* Destination buffer to read data to
* \param data_size
* Bytes of data to be read to the destination buffer
*
* \return PROS_ERR if port is not a link, and the successfully received
* data size if it succeeded.
*/
std::uint32_t receive(void* dest, std::uint16_t data_size);
/**
* Clear the receive buffer of the link, and discarding the data.
*
* 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 radio.
* ENXIO - The sensor is still calibrating, or no link is connected via the radio.
* \return PROS_ERR if port is not a link, 1 if the operation succeeded.
*/
std::uint32_t clear_receive_buf();
};
} // namespace pros
#endif

View File

@ -0,0 +1,255 @@
/*
* \file pros/llemu.h
*
* Legacy LCD Emulator
*
* This file defines a high-level API for emulating the three-button, UART-based
* VEX LCD, containing a set of functions that facilitate the use of a software-
* emulated version of the classic VEX LCD module.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/llemu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_LLEMU_H_
#define _PROS_LLEMU_H_
#include <errno.h>
#include <stdbool.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#include "display/lvgl.h"
#pragma GCC diagnostic pop
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
typedef void (*lcd_btn_cb_fn_t)(void);
#define LCD_BTN_LEFT 4
#define LCD_BTN_CENTER 2
#define LCD_BTN_RIGHT 1
typedef struct lcd_s {
lv_obj_t* frame;
lv_obj_t* screen;
lv_obj_t* lcd_text[8];
lv_obj_t* btn_container;
lv_obj_t* btns[3]; // < 0 => left; 1 => center; 2 => right
lcd_btn_cb_fn_t callbacks[3]; // < 0 => left; 1 => center; 2 => right
volatile uint8_t touch_bits; // < 4 => left; 2 => center; 1 => right (no
// multitouch support)
} lcd_s_t;
#ifdef __cplusplus
namespace c {
#endif
/**
* Checks whether the emulated three-button LCD has already been initialized.
*
* \return True if the LCD has been initialized or false if not.
*/
bool lcd_is_initialized(void);
/**
* Creates an emulation of the three-button, UART-based VEX LCD on the display.
*
* \return True if the LCD was successfully initialized, or false if it has
* already been initialized.
*/
bool lcd_initialize(void);
/**
* Turns off the Legacy LCD Emulator.
*
* Calling this function will clear the entire display, and you will not be able
* to call any further LLEMU functions until another call to lcd_initialize.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_shutdown(void);
/**
* Displays a formatted string on the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line on which to display the text [0-7]
* \param fmt
* Format string
* \param ...
* Optional list of arguments for the format string
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_print(int16_t line, const char* fmt, ...);
/**
* Displays a string on the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line on which to display the text [0-7]
* \param text
* The text to display
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_set_text(int16_t line, const char* text);
/**
* Clears the contents of the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_clear(void);
/**
* Clears the contents of a line of the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line to clear
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_clear_line(int16_t line);
/**
* Registers a callback function for the leftmost button.
*
* When the leftmost button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t (void (*cb)(void))
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_register_btn0_cb(lcd_btn_cb_fn_t cb);
/**
* Registers a callback function for the center button.
*
* When the center button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t (void (*cb)(void))
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_register_btn1_cb(lcd_btn_cb_fn_t cb);
/**
* Registers a callback function for the rightmost button.
*
* When the rightmost button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t (void (*cb)(void))
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool lcd_register_btn2_cb(lcd_btn_cb_fn_t cb);
/**
* Gets the button status from the emulated three-button LCD.
*
* The value returned is a 3-bit integer where 1 0 0 indicates the left button
* is pressed, 0 1 0 indicates the center button is pressed, and 0 0 1
* indicates the right button is pressed. 0 is returned if no buttons are
* currently being pressed.
*
* Note that this function is provided for legacy API compatibility purposes,
* with the caveat that the V5 touch screen does not actually support pressing
* multiple points on the screen at the same time.
*
* \return The buttons pressed as a bit mask
*/
uint8_t lcd_read_buttons(void);
/**
* Changes the color of the LCD background to a provided color expressed in
* type lv_color_t.
*
* \param color
* A color of type lv_color_t
*
* \return void
*/
void lcd_set_background_color(lv_color_t color);
/**
* Changes the text color of the LCD to a provided color expressed in
* type lv_color_t.
*
* \param color
* A color of type lv_color_t
*
* \return void
*/
void lcd_set_text_color(lv_color_t color);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_LLEMU_H_

View File

@ -0,0 +1,262 @@
/*
* \file pros/llemu.hpp
*
* Legacy LCD Emulator
*
* This file defines a high-level API for emulating the three-button, UART-based
* VEX LCD, containing a set of functions that facilitate the use of a software-
* emulated version of the classic VEX LCD module.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/llemu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_LLEMU_HPP_
#define _PROS_LLEMU_HPP_
#include <cstdint>
#include <string>
#include "pros/llemu.h"
namespace pros {
namespace lcd {
/**
* Checks whether the emulated three-button LCD has already been initialized.
*
* \return True if the LCD has been initialized or false if not.
*/
bool is_initialized(void);
/**
* Creates an emulation of the three-button, UART-based VEX LCD on the display.
*
* \return True if the LCD was successfully initialized, or false if it has
* already been initialized.
*/
bool initialize(void);
/**
* Turns off the Legacy LCD Emulator.
*
* Calling this function will clear the entire display, and you will not be able
* to call any further LLEMU functions until another call to lcd_initialize.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool shutdown(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
namespace {
template <typename T>
T convert_args(T arg) {
return arg;
}
const char* convert_args(const std::string& arg) {
return arg.c_str();
}
} // namespace
#pragma GCC diagnostic pop
/**
* Displays a formatted string on the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line on which to display the text [0-7]
* \param fmt
* Format string
* \param ...
* Optional list of arguments for the format string
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
template <typename... Params>
bool print(std::int16_t line, const char* fmt, Params... args) {
return pros::c::lcd_print(line, fmt, convert_args(args)...);
}
/**
* Displays a string on the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line on which to display the text [0-7]
* \param text
* The text to display
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool set_text(std::int16_t line, std::string text);
/**
* Clears the contents of the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool clear(void);
/**
* Clears the contents of a line of the emulated three-button LCD screen.
*
* This function uses the following values of errno when an error state is
* reached:
* ENXIO - The LCD has not been initialized. Call lcd_initialize() first.
* EINVAL - The line number specified is not in the range [0-7]
*
* \param line
* The line to clear
*
* \return True if the operation was successful, or false otherwise, setting
* errno values as specified above.
*/
bool clear_line(std::int16_t line);
using lcd_btn_cb_fn_t = void (*)(void);
/**
* Registers a callback function for the leftmost button.
*
* When the leftmost button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t(void (*cb)(void))
*/
void register_btn0_cb(lcd_btn_cb_fn_t cb);
/**
* Registers a callback function for the center button.
*
* When the center button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t(void (*cb)(void))
*/
void register_btn1_cb(lcd_btn_cb_fn_t cb);
/**
* Registers a callback function for the rightmost button.
*
* When the rightmost button on the emulated three-button LCD is pressed, the
* user-provided callback function will be invoked.
*
* \param cb
* A callback function of type lcd_btn_cb_fn_t(void (*cb)(void))
*/
void register_btn2_cb(lcd_btn_cb_fn_t cb);
/**
* Gets the button status from the emulated three-button LCD.
*
* The value returned is a 3-bit integer where 1 0 0 indicates the left button
* is pressed, 0 1 0 indicates the center button is pressed, and 0 0 1
* indicates the right button is pressed. 0 is returned if no buttons are
* currently being pressed.
*
* Note that this function is provided for legacy API compatibility purposes,
* with the caveat that the V5 touch screen does not actually support pressing
* multiple points on the screen at the same time.
*
* \return The buttons pressed as a bit mask
*/
std::uint8_t read_buttons(void);
/**
* Changes the color of the LCD background to a provided color expressed in
* type lv_color_t.
*
* \param color
* A color of type lv_color_t
*
* \return void
*/
void set_background_color(lv_color_t color);
/**
* Changes the color of the LCD background to a provided color expressed in RGB
* form, with three values of type uint8_t.
*
* \param r
* A value of type uint8_t, with a range of 0 to 255, representing the
* red value of a color
*
* \param g
* A value of type uint8_t, with a range of 0 to 255, representing the
* green value of a color
*
* \param b
* A value of type uint8_t, with a range of 0 to 255, representing the
* blue value of a color
*
* \return void
*/
void set_background_color(std::uint8_t r, std::uint8_t g, std::uint8_t b);
/**
* Changes the text color of the LCD to a provided color expressed in
* type lv_color_t.
*
* \param color
* A color of type lv_color_t
*
* \return void
*/
void set_text_color(lv_color_t color);
/**
* Changes the text color of the LCD to a provided color expressed in RGB
* form, with three values of type uint8_t.
*
* \param r
* A value of type uint8_t, with a range of 0 to 255, representing the
* red value of a color
*
* \param g
* A value of type uint8_t, with a range of 0 to 255, representing the
* green value of a color
*
* \param b
* A value of type uint8_t, with a range of 0 to 255, representing the
* blue value of a color
*
* \return void
*/
void set_text_color(std::uint8_t r, std::uint8_t g, std::uint8_t b);
} // namespace lcd
} // namespace pros
#endif // _PROS_LLEMU_HPP_

View File

@ -0,0 +1,483 @@
/**
* \file pros/misc.h
*
* Contains prototypes for miscellaneous functions pertaining to the controller,
* battery, and competition control.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/controller.html to
* learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reservered.
*
* 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_MISC_H_
#define _PROS_MISC_H_
#include <stdint.h>
#define NUM_V5_PORTS (22)
/******************************************************************************/
/** V5 Competition **/
/******************************************************************************/
#define COMPETITION_DISABLED (1 << 0)
#define COMPETITION_AUTONOMOUS (1 << 1)
#define COMPETITION_CONNECTED (1 << 2)
/**
* Get the current status of the competition control.
*
* \return The competition control status as a mask of bits with
* COMPETITION_{ENABLED,AUTONOMOUS,CONNECTED}.
*/
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
uint8_t competition_get_status(void);
#ifdef __cplusplus
}
}
}
#endif
#define competition_is_disabled() ((competition_get_status() & COMPETITION_DISABLED) != 0)
#define competition_is_connected() ((competition_get_status() & COMPETITION_CONNECTED) != 0)
#define competition_is_autonomous() ((competition_get_status() & COMPETITION_AUTONOMOUS) != 0)
/******************************************************************************/
/** V5 Controller **/
/******************************************************************************/
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
typedef enum { E_CONTROLLER_MASTER = 0, E_CONTROLLER_PARTNER } controller_id_e_t;
typedef enum {
E_CONTROLLER_ANALOG_LEFT_X = 0,
E_CONTROLLER_ANALOG_LEFT_Y,
E_CONTROLLER_ANALOG_RIGHT_X,
E_CONTROLLER_ANALOG_RIGHT_Y
} controller_analog_e_t;
typedef enum {
E_CONTROLLER_DIGITAL_L1 = 6,
E_CONTROLLER_DIGITAL_L2,
E_CONTROLLER_DIGITAL_R1,
E_CONTROLLER_DIGITAL_R2,
E_CONTROLLER_DIGITAL_UP,
E_CONTROLLER_DIGITAL_DOWN,
E_CONTROLLER_DIGITAL_LEFT,
E_CONTROLLER_DIGITAL_RIGHT,
E_CONTROLLER_DIGITAL_X,
E_CONTROLLER_DIGITAL_B,
E_CONTROLLER_DIGITAL_Y,
E_CONTROLLER_DIGITAL_A
} controller_digital_e_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define CONTROLLER_MASTER pros::E_CONTROLLER_MASTER
#define CONTROLLER_PARTNER pros::E_CONTROLLER_PARTNER
#define ANALOG_LEFT_X pros::E_CONTROLLER_ANALOG_LEFT_X
#define ANALOG_LEFT_Y pros::E_CONTROLLER_ANALOG_LEFT_Y
#define ANALOG_RIGHT_X pros::E_CONTROLLER_ANALOG_RIGHT_X
#define ANALOG_RIGHT_Y pros::E_CONTROLLER_ANALOG_RIGHT_Y
#define DIGITAL_L1 pros::E_CONTROLLER_DIGITAL_L1
#define DIGITAL_L2 pros::E_CONTROLLER_DIGITAL_L2
#define DIGITAL_R1 pros::E_CONTROLLER_DIGITAL_R1
#define DIGITAL_R2 pros::E_CONTROLLER_DIGITAL_R2
#define DIGITAL_UP pros::E_CONTROLLER_DIGITAL_UP
#define DIGITAL_DOWN pros::E_CONTROLLER_DIGITAL_DOWN
#define DIGITAL_LEFT pros::E_CONTROLLER_DIGITAL_LEFT
#define DIGITAL_RIGHT pros::E_CONTROLLER_DIGITAL_RIGHT
#define DIGITAL_X pros::E_CONTROLLER_DIGITAL_X
#define DIGITAL_B pros::E_CONTROLLER_DIGITAL_B
#define DIGITAL_Y pros::E_CONTROLLER_DIGITAL_Y
#define DIGITAL_A pros::E_CONTROLLER_DIGITAL_A
#else
#define CONTROLLER_MASTER E_CONTROLLER_MASTER
#define CONTROLLER_PARTNER E_CONTROLLER_PARTNER
#define ANALOG_LEFT_X E_CONTROLLER_ANALOG_LEFT_X
#define ANALOG_LEFT_Y E_CONTROLLER_ANALOG_LEFT_Y
#define ANALOG_RIGHT_X E_CONTROLLER_ANALOG_RIGHT_X
#define ANALOG_RIGHT_Y E_CONTROLLER_ANALOG_RIGHT_Y
#define DIGITAL_L1 E_CONTROLLER_DIGITAL_L1
#define DIGITAL_L2 E_CONTROLLER_DIGITAL_L2
#define DIGITAL_R1 E_CONTROLLER_DIGITAL_R1
#define DIGITAL_R2 E_CONTROLLER_DIGITAL_R2
#define DIGITAL_UP E_CONTROLLER_DIGITAL_UP
#define DIGITAL_DOWN E_CONTROLLER_DIGITAL_DOWN
#define DIGITAL_LEFT E_CONTROLLER_DIGITAL_LEFT
#define DIGITAL_RIGHT E_CONTROLLER_DIGITAL_RIGHT
#define DIGITAL_X E_CONTROLLER_DIGITAL_X
#define DIGITAL_B E_CONTROLLER_DIGITAL_B
#define DIGITAL_Y E_CONTROLLER_DIGITAL_Y
#define DIGITAL_A E_CONTROLLER_DIGITAL_A
#endif
#endif
/*
Given an id and a port, this macro sets the port
variable based on the id and allows the mutex to take that port.
Returns error (in the function/scope it's in) if the controller
failed to connect or an invalid id is given.
*/
#define CONTROLLER_PORT_MUTEX_TAKE(id, port) \
switch (id) { \
case E_CONTROLLER_MASTER: \
port = V5_PORT_CONTROLLER_1; \
break; \
case E_CONTROLLER_PARTNER: \
port = V5_PORT_CONTROLLER_2; \
break; \
default: \
errno = EINVAL; \
return PROS_ERR; \
} \
if (!internal_port_mutex_take(port)) { \
errno = EACCES; \
return PROS_ERR; \
} \
/******************************************************************************/
/** Date and Time **/
/******************************************************************************/
extern const char* baked_date;
extern const char* baked_time;
typedef struct {
uint16_t year; // Year - 1980
uint8_t day;
uint8_t month; // 1 = January
} date_s_t;
typedef struct {
uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t sec_hund; // hundredths of a second
} time_s_t;
#ifdef __cplusplus
namespace c {
#endif
/**
* Checks if the controller is connected.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
*
* \return 1 if the controller is connected, 0 otherwise
*/
int32_t controller_is_connected(controller_id_e_t id);
/**
* Gets the value of an analog channel (joystick) on a controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param channel
* The analog channel to get.
* Must be one of ANALOG_LEFT_X, ANALOG_LEFT_Y, ANALOG_RIGHT_X,
* ANALOG_RIGHT_Y
*
* \return The current reading of the analog channel: [-127, 127].
* If the controller was not connected, then 0 is returned
*/
int32_t controller_get_analog(controller_id_e_t id, controller_analog_e_t channel);
/**
* Gets the battery capacity of the given controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER
*
* \return The controller's battery capacity
*/
int32_t controller_get_battery_capacity(controller_id_e_t id);
/**
* Gets the battery level of the given controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER
*
* \return The controller's battery level
*/
int32_t controller_get_battery_level(controller_id_e_t id);
/**
* Checks if a digital channel (button) on the controller is currently pressed.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param button
* The button to read.
* Must be one of DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
*
* \return 1 if the button on the controller is pressed.
* If the controller was not connected, then 0 is returned
*/
int32_t controller_get_digital(controller_id_e_t id, controller_digital_e_t button);
/**
* Returns a rising-edge case for a controller button press.
*
* This function is not thread-safe.
* Multiple tasks polling a single button may return different results under the
* same circumstances, so only one task should call this function for any given
* button. E.g., Task A calls this function for buttons 1 and 2. Task B may call
* this function for button 3, but should not for buttons 1 or 2. A typical
* use-case for this function is to call inside opcontrol to detect new button
* presses, and not in any other tasks.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param button
* The button to read. Must be one of
* DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
*
* \return 1 if the button on the controller is pressed and had not been pressed
* the last time this function was called, 0 otherwise.
*/
int32_t controller_get_digital_new_press(controller_id_e_t id, controller_digital_e_t button);
/**
* Sets text to the controller LCD screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param line
* The line number at which the text will be displayed [0-2]
* \param col
* The column number at which the text will be displayed [0-14]
* \param fmt
* The format string to print to the controller
* \param ...
* The argument list for the format string
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t controller_print(controller_id_e_t id, uint8_t line, uint8_t col, const char* fmt, ...);
/**
* Sets text to the controller LCD screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param line
* The line number at which the text will be displayed [0-2]
* \param col
* The column number at which the text will be displayed [0-14]
* \param str
* The pre-formatted string to print to the controller
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t controller_set_text(controller_id_e_t id, uint8_t line, uint8_t col, const char* str);
/**
* Clears an individual line of the controller screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param line
* The line number to clear [0-2]
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t controller_clear_line(controller_id_e_t id, uint8_t line);
/**
* Clears all of the lines on the controller screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well. On vexOS version 1.0.0 this function will block
* for 110ms.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t controller_clear(controller_id_e_t id);
/**
* Rumble the controller.
*
* \note Controller rumble activation is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - A value other than E_CONTROLLER_MASTER or E_CONTROLLER_PARTNER is
* given.
* EACCES - Another resource is currently trying to access the controller port.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
* \param rumble_pattern
* A string consisting of the characters '.', '-', and ' ', where dots
* are short rumbles, dashes are long rumbles, and spaces are pauses.
* Maximum supported length is 8 characters.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t controller_rumble(controller_id_e_t id, const char* rumble_pattern);
/**
* Gets the current voltage of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current voltage of the battery
*/
int32_t battery_get_voltage(void);
/**
* Gets the current current of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current current of the battery
*/
int32_t battery_get_current(void);
/**
* Gets the current temperature of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current temperature of the battery
*/
double battery_get_temperature(void);
/**
* Gets the current capacity of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current capacity of the battery
*/
double battery_get_capacity(void);
/**
* Checks if the SD card is installed.
*
* \return 1 if the SD card is installed, 0 otherwise
*/
int32_t usd_is_installed(void);
#ifdef __cplusplus
}
}
}
#endif
#endif // _PROS_MISC_H_

View File

@ -0,0 +1,331 @@
/**
* \file pros/misc.hpp
*
* Contains prototypes for miscellaneous functions pertaining to the controller,
* battery, and competition control.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/controller.html to
* learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reservered.
*
* 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_MISC_HPP_
#define _PROS_MISC_HPP_
#include "pros/misc.h"
#include <cstdint>
#include <string>
namespace pros {
class Controller {
public:
/**
* Creates a controller object for the given controller id.
*
* \param id
* The ID of the controller (e.g. the master or partner controller).
* Must be one of CONTROLLER_MASTER or CONTROLLER_PARTNER
*/
Controller(controller_id_e_t id);
/**
* Checks if the controller is connected.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \return 1 if the controller is connected, 0 otherwise
*/
std::int32_t is_connected(void);
/**
* Gets the value of an analog channel (joystick) on a controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param channel
* The analog channel to get.
* Must be one of ANALOG_LEFT_X, ANALOG_LEFT_Y, ANALOG_RIGHT_X,
* ANALOG_RIGHT_Y
*
* \return The current reading of the analog channel: [-127, 127].
* If the controller was not connected, then 0 is returned
*/
std::int32_t get_analog(controller_analog_e_t channel);
/**
* Gets the battery capacity of the controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \return The controller's battery capacity
*/
std::int32_t get_battery_capacity(void);
/**
* Gets the battery level of the controller.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \return The controller's battery level
*/
std::int32_t get_battery_level(void);
/**
* Checks if a digital channel (button) on the controller is currently
* pressed.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param button
* The button to read. Must be one of
* DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
*
* \return 1 if the button on the controller is pressed.
* If the controller was not connected, then 0 is returned
*/
std::int32_t get_digital(controller_digital_e_t button);
/**
* Returns a rising-edge case for a controller button press.
*
* This function is not thread-safe.
* Multiple tasks polling a single button may return different results under
* the same circumstances, so only one task should call this function for any
* given button. E.g., Task A calls this function for buttons 1 and 2.
* Task B may call this function for button 3, but should not for buttons
* 1 or 2. A typical use-case for this function is to call inside opcontrol
* to detect new button presses, and not in any other tasks.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param button
* The button to read. Must be one of
* DIGITAL_{RIGHT,DOWN,LEFT,UP,A,B,Y,X,R1,R2,L1,L2}
*
* \return 1 if the button on the controller is pressed and had not been
* pressed the last time this function was called, 0 otherwise.
*/
std::int32_t get_digital_new_press(controller_digital_e_t button);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
template <typename T>
T convert_args(T arg) {
return arg;
}
const char* convert_args(const std::string& arg) {
return arg.c_str();
}
#pragma GCC diagnostic pop
/**
* Sets text to the controller LCD screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param line
* The line number at which the text will be displayed [0-2]
* \param col
* The column number at which the text will be displayed [0-14]
* \param fmt
* The format string to print to the controller
* \param ...
* The argument list for the format string
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
template <typename... Params>
std::int32_t print(std::uint8_t line, std::uint8_t col, const char* fmt, Params... args) {
return pros::c::controller_print(_id, line, col, fmt, convert_args(args)...);
}
/**
* Sets text to the controller LCD screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param line
* The line number at which the text will be displayed [0-2]
* \param col
* The column number at which the text will be displayed [0-14]
* \param str
* The pre-formatted string to print to the controller
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_text(std::uint8_t line, std::uint8_t col, const char* str);
std::int32_t set_text(std::uint8_t line, std::uint8_t col, const std::string& str);
/**
* Clears an individual line of the controller screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param line
* The line number to clear [0-2]
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t clear_line(std::uint8_t line);
/**
* Rumble the controller.
*
* \note Controller rumble activation is currently in beta, so continuous, fast
* updates will not work well.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \param rumble_pattern
* A string consisting of the characters '.', '-', and ' ', where dots
* are short rumbles, dashes are long rumbles, and spaces are pauses.
* Maximum supported length is 8 characters.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t rumble(const char* rumble_pattern);
/**
* Clears all of the lines on the controller screen.
*
* \note Controller text setting is currently in beta, so continuous, fast
* updates will not work well. On vexOS version 1.0.0 this function will
* block for 110ms.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the controller
* port.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t clear(void);
private:
controller_id_e_t _id;
};
namespace battery {
/**
* Gets the current voltage of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current voltage of the battery
*/
double get_capacity(void);
/**
* Gets the current current of the battery in milliamps, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current current of the battery
*/
int32_t get_current(void);
/**
* Gets the current temperature of the battery, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current temperature of the battery
*/
double get_temperature(void);
/**
* Gets the current capacity of the battery in millivolts, as reported by VEXos.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCES - Another resource is currently trying to access the battery port.
*
* \return The current capacity of the battery
*/
int32_t get_voltage(void);
} // namespace battery
namespace competition {
/**
* Get the current status of the competition control.
*
* \return The competition control status as a mask of bits with
* COMPETITION_{ENABLED,AUTONOMOUS,CONNECTED}.
*/
std::uint8_t get_status(void);
std::uint8_t is_autonomous(void);
std::uint8_t is_connected(void);
std::uint8_t is_disabled(void);
} // namespace competition
namespace usd {
/**
* Checks if the SD card is installed.
*
* \return 1 if the SD card is installed, 0 otherwise
*/
std::int32_t is_installed(void);
} // namespace usd
} // namespace pros
#endif // _PROS_MISC_HPP_

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,307 @@
/**
* \file pros/optical.h
*
* Contains prototypes for functions related to the VEX Optical sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/imu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_OPTICAL_H_
#define _PROS_OPTICAL_H_
#include <stdbool.h>
#include <stdint.h>
#include "error.h"
#define OPT_GESTURE_ERR (INT8_MAX)
#define OPT_COUNT_ERR (INT16_MAX)
#define OPT_TIME_ERR PROS_ERR
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
typedef enum optical_direction_e {
NO_GESTURE = 0,
UP = 1,
DOWN = 2,
RIGHT = 3,
LEFT = 4,
ERROR = PROS_ERR
} optical_direction_e_t;
typedef struct optical_rgb_s {
double red;
double green;
double blue;
double brightness;
} optical_rgb_s_t;
typedef struct optical_raw_s {
uint32_t clear;
uint32_t red;
uint32_t green;
uint32_t blue;
} optical_raw_s_t;
typedef struct optical_gesture_s {
uint8_t udata;
uint8_t ddata;
uint8_t ldata;
uint8_t rdata;
uint8_t type;
uint8_t pad;
uint16_t count;
uint32_t time;
} optical_gesture_s_t;
/**
* Get the detected color hue
*
* This is not available if gestures are being detected. Hue has a
* range of 0 to 359.999
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return hue value if the operation was successful or PROS_ERR_F if the operation
* failed, setting errno.
*/
double optical_get_hue(uint8_t port);
/**
* Get the detected color saturation
*
* This is not available if gestures are being detected. Saturation has a
* range of 0 to 1.0
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return saturation value if the operation was successful or PROS_ERR_F if
* the operation failed, setting errno.
*/
double optical_get_saturation(uint8_t port);
/**
* Get the detected color brightness
*
* This is not available if gestures are being detected. Brightness has a
* range of 0 to 1.0
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return brightness value if the operation was successful or PROS_ERR_F if
* the operation failed, setting errno.
*/
double optical_get_brightness(uint8_t port);
/**
* Get the detected proximity value
*
* This is not available if gestures are being detected. proximity has
* a range of 0 to 255.
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return poximity value if the operation was successful or PROS_ERR if
* the operation failed, setting errno.
*/
int32_t optical_get_proximity(uint8_t port);
/**
* Set the pwm value of the White LED
*
* value that ranges from 0 to 100
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t optical_set_led_pwm(uint8_t port, uint8_t value);
/**
* Get the pwm value of the White LED
*
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return LED pwm value that ranges from 0 to 100 if the operation was
* successful or PROS_ERR if the operation failed, setting errno.
*/
int32_t optical_get_led_pwm(uint8_t port);
/**
* Get the processed RGBC data from the sensor
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return rgb value if the operation was successful or an optical_rgb_s_t with
* all fields set to PROS_ERR if the operation failed, setting errno.
*/
optical_rgb_s_t optical_get_rgb(uint8_t port);
/**
* Get the raw, unprocessed RGBC data from the sensor
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return raw rgb value if the operation was successful or an optical_raw_s_t
* with all fields set to PROS_ERR if the operation failed, setting errno.
*/
optical_raw_s_t optical_get_raw(uint8_t port);
/**
* Get the most recent gesture data from the sensor
*
* Gestures will be cleared after 500mS
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return gesture value if the operation was successful or PROS_ERR if
* the operation failed, setting errno.
*/
optical_direction_e_t optical_get_gesture(uint8_t port);
/**
* Get the most recent raw gesture data from the sensor
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return gesture value if the operation was successful or an optical_gesture_s_t
* with all fields set to PROS_ERR if the operation failed, setting errno.
*/
optical_gesture_s_t optical_get_gesture_raw(uint8_t port);
/**
* Enable gesture detection on the sensor
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t optical_enable_gesture(uint8_t port);
/**
* Disable gesture detection on the sensor
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t optical_disable_gesture(uint8_t port);
/**
* Get integration time (update rate) of the optical sensor in milliseconds, with
* minimum time being
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \return Integration time in milliseconds if the operation is successful
* or PROS_ERR if the operation failed, setting errno.
*/
double optical_get_integration_time(uint8_t port);
/**
* Set integration time (update rate) of the optical sensor in milliseconds.
*
* 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 Optical Sensor
*
* \param port
* The V5 Optical Sensor port number from 1-21
* \param time
* The desired integration time in milliseconds
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t optical_set_integration_time(uint8_t port, double time);
#ifdef __cplusplus
}
}
}
#endif
#endif

View File

@ -0,0 +1,266 @@
/**
* \file pros/optical.hpp
*
* Contains prototypes for functions related to the VEX Optical sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/imu.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_OPTICAL_HPP_
#define _PROS_OPTICAL_HPP_
#include <stdbool.h>
#include <cstdint>
#include "pros/optical.h"
namespace pros {
class Optical {
public:
/**
* Creates an Optical 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 an Optical Sensor
*
* \param port
* The V5 port number from 1-21
*/
explicit Optical(const std::uint8_t port);
explicit Optical(std::uint8_t port, double time);
/**
* Get the detected color hue
*
* This is not available if gestures are being detected. Hue has a
* range of 0 to 359.999
*
* 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 Optical Sensor
*
* \return hue value if the operation was successful or PROS_ERR_F if the operation
* failed, setting errno.
*/
virtual double get_hue();
/**
* Get the detected color saturation
*
* This is not available if gestures are being detected. Saturation has a
* range of 0 to 1.0
*
* 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 Optical Sensor
*
* \return saturation value if the operation was successful or PROS_ERR_F if
* the operation failed, setting errno.
*/
virtual double get_saturation();
/**
* Get the detected color brightness
*
* This is not available if gestures are being detected. Brightness has a
* range of 0 to 1.0
*
* 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 Optical Sensor
*
* \return brightness value if the operation was successful or PROS_ERR_F if
* the operation failed, setting errno.
*/
virtual double get_brightness();
/**
* Get the detected proximity value
*
* This is not available if gestures are being detected. proximity has
* a range of 0 to 255.
*
* 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 Optical Sensor
*
* \return poximity value if the operation was successful or PROS_ERR if
* the operation failed, setting errno.
*/
virtual std::int32_t get_proximity();
/**
* Set the pwm value of the White LED on the sensor
*
* value that ranges from 0 to 100
*
* 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 Optical Sensor
*
* \return The Error code encountered
*/
virtual std::int32_t set_led_pwm(uint8_t value);
/**
* Get the pwm value of the White LED on the sensor
*
* value that ranges from 0 to 100
*
* 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 Optical Sensor
*
* \return LED pwm value if the operation was successful or PROS_ERR if
* the operation failed, setting errno.
*/
virtual std::int32_t get_led_pwm();
/**
* Get the processed RGBC data from the sensor
*
* 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 Optical Sensor
*
* \return rgb value if the operation was successful or an optical_rgb_s_t
* with all fields set to PROS_ERR if the operation failed, setting errno.
*/
virtual pros::c::optical_rgb_s_t get_rgb();
/**
* Get the raw un-processed RGBC data from the sensor
*
* 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 Optical Sensor
*
* \return raw rgb value if the operation was successful or an optical_raw_s_t
* with all fields set to PROS_ERR if the operation failed, setting errno.
*/
virtual pros::c::optical_raw_s_t get_raw();
/**
* Get the most recent gesture data from the sensor
*
* Gestures will be cleared after 500mS
* 0 = no gesture
* 1 = up (towards cable)
* 2 = down
* 3 = right
* 4 = left
*
* 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 Optical Sensor
*
* \return gesture value if the operation was successful or PROS_ERR if
* the operation failed, setting errno.
*/
virtual pros::c::optical_direction_e_t get_gesture();
/**
* Get the most recent raw gesture data from the sensor
*
* 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 Optical Sensor
*
* \return gesture value if the operation was successful or an optical_gesture_s_t
* with all fields set to PROS_ERR if the operation failed, setting errno.
*/
virtual pros::c::optical_gesture_s_t get_gesture_raw();
/**
* Enable gesture detection on the sensor
*
* 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 Optical Sensor
*
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
virtual std::int32_t enable_gesture();
/**
* Disable gesture detection on the sensor
*
* 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 Optical Sensor
*
* \return 1 if the operation is successful or PROS_ERR if the operation failed,
* setting errno.
*/
virtual std::int32_t disable_gesture();
/**
* Set integration time (update rate) of the optical sensor in milliseconds, with
* minimum time being 3 ms and maximum time being 712 ms. Default is 100 ms, with the
* optical sensor communciating with the V5 brain every 20 ms.
*
* 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 Optical Sensor
*
* \return 1 if the operation is successful or PROS_ERR_F if the operation failed,
* setting errno.
*/
double get_integration_time();
/**
* Get integration time (update rate) of the optical sensor in milliseconds.
*
* 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 Optical Sensor
*
* \param time
* The desired integration time in milliseconds
* \return Integration time in milliseconds if the operation is successful
* or PROS_ERR if the operation failed, setting errno.
*/
std::int32_t set_integration_time(double time);
/**
* Gets the port number of the Optical Sensor.
*
* \return The Optical Sensor's port number.
*/
virtual std::uint8_t get_port();
private:
const std::uint8_t _port;
};
} // namespace pros
#endif

View File

@ -0,0 +1,223 @@
/**
* \file pros/rotation.h
*
* Contains prototypes for functions related to the VEX Rotation Sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/rotation.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_ROTATION_H_
#define _PROS_ROTATION_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
#define ROTATION_MINIMUM_DATA_RATE 5
/**
* Reset Rotation Sensor
*
* Reset the current absolute position to be the same as the
* Rotation Sensor angle.
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_reset(uint8_t port);
/**
* Set the Rotation Sensor's refresh interval in milliseconds.
*
* The rate may be specified in increments of 5ms, and will be rounded down to
* the nearest increment. The minimum allowable refresh rate is 5ms. The default
* rate is 10ms.
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \param rate The data refresh interval in milliseconds
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_set_data_rate(uint8_t port, uint32_t rate);
/**
* Set the Rotation Sensor position reading to a desired rotation value
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \param position
* The position in terms of ticks
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_set_position(uint8_t port, uint32_t position);
/**
* Reset the Rotation Sensor position to 0
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_reset_position(uint8_t port);
/**
* Get the Rotation Sensor's current position in centidegrees
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return The position value or PROS_ERR_F if the operation failed, setting
* errno.
*/
int32_t rotation_get_position(uint8_t port);
/**
* Get the Rotation Sensor's current velocity in centidegrees per second
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return The velocity value or PROS_ERR_F if the operation failed, setting
* errno.
*/
int32_t rotation_get_velocity(uint8_t port);
/**
* Get the Rotation Sensor's current angle in centidegrees (0-36000)
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return The angle value (0-36000) or PROS_ERR_F if the operation failed, setting
* errno.
*/
int32_t rotation_get_angle(uint8_t port);
/**
* Set the Rotation Sensor's direction reversed flag
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \param value
* Determines if the direction of the Rotation Sensor is reversed or not.
*
* \return 1 if operation succeeded or PROS_ERR if the operation failed, setting
* errno.
*/
int32_t rotation_set_reversed(uint8_t port, bool value);
/**
* Reverse the Rotation Sensor's direction
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_reverse(uint8_t port);
/**
* Initialize the Rotation Sensor with a reverse flag
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \param reverse_flag
* Determines if the Rotation Sensor is reversed or not.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t rotation_init_reverse(uint8_t port, bool reverse_flag);
/**
* Get the Rotation Sensor's reversed flag
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
*
* \return Boolean value of if the Rotation Sensor's direction is reversed or not
* or PROS_ERR if the operation failed, setting errno.
*/
int32_t rotation_get_reversed(uint8_t port);
#ifdef __cplusplus
} //namespace C
} //namespace pros
} //extern "C"
#endif
#endif

View File

@ -0,0 +1,190 @@
/**
* \file pros/rotation.hpp
*
* Contains prototypes for functions related to the VEX Rotation Sensor.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/rotation.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_ROTATION_HPP_
#define _PROS_ROTATION_HPP_
#include <cstdint>
#include "pros/rotation.h"
namespace pros {
class Rotation {
const std::uint8_t _port;
public:
Rotation(const std::uint8_t port) : _port(port){};
Rotation(const std::uint8_t port, const bool reverse_flag);
/**
* Reset the Rotation Sensor
*
* Reset the current absolute position to be the same as the
* Rotation Sensor angle.
*
* 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 Rotation Sensor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t reset();
/**
* Set the Rotation Sensor's refresh interval in milliseconds.
*
* The rate may be specified in increments of 5ms, and will be rounded down to
* the nearest increment. The minimum allowable refresh rate is 5ms. The default
* rate is 10ms.
*
* As values are copied into the shared memory buffer only at 10ms intervals,
* setting this value to less than 10ms does not mean that you can poll the
* sensor's values any faster. However, it will guarantee that the data is as
* recent as possible.
*
* 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 Rotation Sensor
*
* \param rate The data refresh interval in milliseconds
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_data_rate(std::uint32_t rate) const;
/**
* Set the Rotation Sensor position reading to a desired rotation value
*
* 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 Rotation Sensor
*
* \param position
* The position in terms of ticks
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_position(std::uint32_t position);
/**
* Reset the Rotation Sensor to a desired rotation value
*
* 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 Rotation Sensor
*
* \param position
* The position in terms of ticks
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t reset_position(void);
/**
* Get the Rotation Sensor's current position in centidegrees
*
* 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 Rotation Sensor
*
* \return The position value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get_position();
/**
* Get the Rotation Sensor's current velocity in centidegrees per second
*
* 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 Rotation Sensor
*
* \param port
* The V5 Rotation Sensor port number from 1-21
* \return The
value or PROS_ERR_F if the operation failed, setting
* errno.
*/
virtual std::int32_t get_velocity();
/**
* Get the Rotation Sensor's current position in centidegrees
*
* 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 Rotation Sensor
*
* \return The angle value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get_angle();
/**
* Set the Rotation Sensor's direction reversed flag
*
* 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 Rotation Sensor
*
* \param value
* Determines if the direction of the rotational sensor is
* reversed or not.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_reversed(bool value);
/**
* Reverse the Rotation Sensor's direction.
*
* 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 Rotation Sensor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t reverse();
/**
* Get the Rotation Sensor's reversed flag
*
* 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 Rotation Sensor
*
* \return Reversed value or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t get_reversed();
};
} // namespace pros
#endif

View File

@ -0,0 +1,442 @@
/**
* \file pros/rtos.h
*
* Contains declarations for the PROS RTOS kernel for use by typical VEX
* programmers.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html to
* learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_RTOS_H_
#define _PROS_RTOS_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
// The highest priority that can be assigned to a task. Beware of deadlock.
#define TASK_PRIORITY_MAX 16
// The lowest priority that can be assigned to a task.
// This may cause severe performance problems and is generally not recommended.
#define TASK_PRIORITY_MIN 1
// The default task priority, which should be used for most tasks.
// Default tasks such as autonomous() inherit this priority.
#define TASK_PRIORITY_DEFAULT 8
// The recommended stack size for a new task. This stack size is used for
// default tasks such as autonomous(). This equates to 32,768 bytes, or 128
// times the default stack size for a task in PROS 2.
#define TASK_STACK_DEPTH_DEFAULT 0x2000
// The minimal stack size for a task. This equates to 2048 bytes, or 8 times the
// default stack size for a task in PROS 2.
#define TASK_STACK_DEPTH_MIN 0x200
// The maximum number of characters allowed in a task's name.
#define TASK_NAME_MAX_LEN 32
// The maximum timeout value that can be given to, for instance, a mutex grab.
#define TIMEOUT_MAX ((uint32_t)0xffffffffUL)
typedef void* task_t;
typedef void (*task_fn_t)(void*);
typedef enum {
E_TASK_STATE_RUNNING = 0,
E_TASK_STATE_READY,
E_TASK_STATE_BLOCKED,
E_TASK_STATE_SUSPENDED,
E_TASK_STATE_DELETED,
E_TASK_STATE_INVALID
} task_state_e_t;
typedef enum {
E_NOTIFY_ACTION_NONE,
E_NOTIFY_ACTION_BITS,
E_NOTIFY_ACTION_INCR,
E_NOTIFY_ACTION_OWRITE,
E_NOTIFY_ACTION_NO_OWRITE
} notify_action_e_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define TASK_STATE_RUNNING pros::E_TASK_STATE_RUNNING
#define TASK_STATE_READY pros::E_TASK_STATE_READY
#define TASK_STATE_BLOCKED pros::E_TASK_STATE_BLOCKED
#define TASK_STATE_SUSPENDED pros::E_TASK_STATE_SUSPENDED
#define TASK_STATE_DELETED pros::E_TASK_STATE_DELETED
#define TASK_STATE_INVALID pros::E_TASK_STATE_INVALID
#define NOTIFY_ACTION_NONE pros::E_NOTIFY_ACTION_NONE
#define NOTIFY_ACTION_BITS pros::E_NOTIFY_ACTION_BITS
#define NOTIFY_ACTION_INCR pros::E_NOTIFY_ACTION_INCR
#define NOTIFY_ACTION_OWRITE pros::E_NOTIFY_ACTION_OWRITE
#define NOTIFY_ACTION_NO_OWRITE pros::E_NOTIFY_ACTION_NO_OWRITE
#else
#define TASK_STATE_RUNNING E_TASK_STATE_RUNNING
#define TASK_STATE_READY E_TASK_STATE_READY
#define TASK_STATE_BLOCKED E_TASK_STATE_BLOCKED
#define TASK_STATE_SUSPENDED E_TASK_STATE_SUSPENDED
#define TASK_STATE_DELETED E_TASK_STATE_DELETED
#define TASK_STATE_INVALID E_TASK_STATE_INVALID
#define NOTIFY_ACTION_NONE E_NOTIFY_ACTION_NONE
#define NOTIFY_ACTION_BITS E_NOTIFY_ACTION_BITS
#define NOTIFY_ACTION_INCR E_NOTIFY_ACTION_INCR
#define NOTIFY_ACTION_OWRITE E_NOTIFY_ACTION_OWRITE
#define NOTIFY_ACTION_NO_OWRITE E_NOTIFY_ACTION_NO_OWRITE
#endif
#endif
typedef void* mutex_t;
/**
* Refers to the current task handle
*/
#ifdef __cplusplus
#define CURRENT_TASK ((pros::task_t)NULL)
#else
#define CURRENT_TASK ((task_t)NULL)
#endif
#ifdef __cplusplus
namespace c {
#endif
/**
* Gets the number of milliseconds since PROS initialized.
*
* \return The number of milliseconds since PROS initialized
*/
uint32_t millis(void);
/**
* Gets the number of microseconds since PROS initialized,
*
* \return The number of microseconds since PROS initialized
*/
uint64_t micros(void);
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Pointer to the task entry function
* \param parameters
* Pointer to memory that will be used as a parameter for the task being
* created. This memory should not typically come from stack, but rather
* from dynamically (i.e., malloc'd) or statically allocated memory.
* \param prio
* The priority at which the task should run.
* TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
* \param stack_depth
* The number of words (i.e. 4 * stack_depth) available on the task's
* stack. TASK_STACK_DEPTH_DEFAULT is typically sufficienct.
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
* \return A handle by which the newly created task can be referenced. If an
* error occurred, NULL will be returned and errno can be checked for hints as
* to why task_create failed.
*/
task_t task_create(task_fn_t function, void* const parameters, uint32_t prio, const uint16_t stack_depth,
const char* const name);
/**
* Removes a task from the RTOS real time kernel's management. The task being
* deleted will be removed from all ready, blocked, suspended and event lists.
*
* Memory dynamically allocated by the task is not automatically freed, and
* should be freed before the task is deleted.
*
* \param task
* The handle of the task to be deleted. Passing NULL will cause the
* calling task to be deleted.
*/
void task_delete(task_t task);
/**
* Delays a task for a given number of milliseconds.
*
* This is not the best method to have a task execute code at predefined
* intervals, as the delay time is measured from when the delay is requested.
* To delay cyclically, use task_delay_until().
*
* \param milliseconds
* The number of milliseconds to wait (1000 milliseconds per second)
*/
void task_delay(const uint32_t milliseconds);
void delay(const uint32_t milliseconds);
/**
* Delays a task until a specified time. This function can be used by periodic
* tasks to ensure a constant execution frequency.
*
* The task will be woken up at the time *prev_time + delta, and *prev_time will
* be updated to reflect the time at which the task will unblock.
*
* \param prev_time
* A pointer to the location storing the setpoint time. This should
* typically be initialized to the return value of millis().
* \param delta
* The number of milliseconds to wait (1000 milliseconds per second)
*/
void task_delay_until(uint32_t* const prev_time, const uint32_t delta);
/**
* Gets the priority of the specified task.
*
* \param task
* The task to check
*
* \return The priority of the task
*/
uint32_t task_get_priority(task_t task);
/**
* Sets the priority of the specified task.
*
* If the specified task's state is available to be scheduled (e.g. not blocked)
* and new priority is higher than the currently running task, a context switch
* may occur.
*
* \param task
* The task to set
* \param prio
* The new priority of the task
*/
void task_set_priority(task_t task, uint32_t prio);
/**
* Gets the state of the specified task.
*
* \param task
* The task to check
*
* \return The state of the task
*/
task_state_e_t task_get_state(task_t task);
/**
* Suspends the specified task, making it ineligible to be scheduled.
*
* \param task
* The task to suspend
*/
void task_suspend(task_t task);
/**
* Resumes the specified task, making it eligible to be scheduled.
*
* \param task
* The task to resume
*/
void task_resume(task_t task);
/**
* Gets the number of tasks the kernel is currently managing, including all
* ready, blocked, or suspended tasks. A task that has been deleted, but not yet
* reaped by the idle task will also be included in the count. Tasks recently
* created may take one context switch to be counted.
*
* \return The number of tasks that are currently being managed by the kernel.
*/
uint32_t task_get_count(void);
/**
* Gets the name of the specified task.
*
* \param task
* The task to check
*
* \return A pointer to the name of the task
*/
char* task_get_name(task_t task);
/**
* Gets a task handle from the specified name
*
* The operation takes a relatively long time and should be used sparingly.
*
* \param name
* The name to query
*
* \return A task handle with a matching name, or NULL if none were found.
*/
task_t task_get_by_name(const char* name);
/**
* Get the currently running task handle. This could be useful if a task
* wants to tell another task about itself.
*
* \return The currently running task handle.
*/
task_t task_get_current();
/**
* Sends a simple notification to task and increments the notification counter.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param task
* The task to notify
*
* \return Always returns true.
*/
uint32_t task_notify(task_t task);
/**
*
* Utilizes task notifications to wait until specified task is complete and deleted,
* then continues to execute the program. Analogous to std::thread::join in C++.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param task
* The task to wait on.
*
* \return void
*/
void task_join(task_t task);
/**
* Sends a notification to a task, optionally performing some action. Will also
* retrieve the value of the notification in the target task before modifying
* the notification value.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param task
* The task to notify
* \param value
* The value used in performing the action
* \param action
* An action to optionally perform on the receiving task's notification
* value
* \param prev_value
* A pointer to store the previous value of the target task's
* notification, may be NULL
*
* \return Dependent on the notification action.
* For NOTIFY_ACTION_NO_WRITE: return 0 if the value could be written without
* needing to overwrite, 1 otherwise.
* For all other NOTIFY_ACTION values: always return 0
*/
uint32_t task_notify_ext(task_t task, uint32_t value, notify_action_e_t action, uint32_t* prev_value);
/**
* Waits for a notification to be nonzero.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param clear_on_exit
* If true (1), then the notification value is cleared.
* If false (0), then the notification value is decremented.
* \param timeout
* Specifies the amount of time to be spent waiting for a notification
* to occur.
*
* \return The value of the task's notification value before it is decremented
* or cleared
*/
uint32_t task_notify_take(bool clear_on_exit, uint32_t timeout);
/**
* Clears the notification for a task.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param task
* The task to clear
*
* \return False if there was not a notification waiting, true if there was
*/
bool task_notify_clear(task_t task);
/**
* Creates a mutex.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \return A handle to a newly created mutex. If an error occurred, NULL will be
* returned and errno can be checked for hints as to why mutex_create failed.
*/
mutex_t mutex_create(void);
/**
* Takes and locks a mutex, waiting for up to a certain number of milliseconds
* before timing out.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \param mutex
* Mutex to attempt to lock.
* \param timeout
* Time to wait before the mutex becomes available. A timeout of 0 can
* be used to poll the mutex. TIMEOUT_MAX can be used to block
* indefinitely.
*
* \return True if the mutex was successfully taken, false otherwise. If false
* is returned, then errno is set with a hint about why the the mutex
* couldn't be taken.
*/
bool mutex_take(mutex_t mutex, uint32_t timeout);
/**
* Unlocks a mutex.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \param mutex
* Mutex to unlock.
*
* \return True if the mutex was successfully returned, false otherwise. If
* false is returned, then errno is set with a hint about why the mutex
* couldn't be returned.
*/
bool mutex_give(mutex_t mutex);
/**
* Deletes a mutex
*
* \param mutex
* Mutex to unlock.
*/
void mutex_delete(mutex_t mutex);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_RTOS_H_

View File

@ -0,0 +1,658 @@
/**
* \file pros/rtos.hpp
*
* Contains declarations for the PROS RTOS kernel for use by typical VEX
* programmers.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html to
* learn more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_RTOS_HPP_
#define _PROS_RTOS_HPP_
#include "pros/rtos.h"
#undef delay
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <memory>
#include <optional>
#include <type_traits>
namespace pros {
class Task {
public:
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Pointer to the task entry function
* \param parameters
* Pointer to memory that will be used as a parameter for the task
* being created. This memory should not typically come from stack,
* but rather from dynamically (i.e., malloc'd) or statically
* allocated memory.
* \param prio
* The priority at which the task should run.
* TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
* \param stack_depth
* The number of words (i.e. 4 * stack_depth) available on the task's
* stack. TASK_STACK_DEPTH_DEFAULT is typically sufficienct.
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
Task(task_fn_t function, void* parameters = nullptr, std::uint32_t prio = TASK_PRIORITY_DEFAULT,
std::uint16_t stack_depth = TASK_STACK_DEPTH_DEFAULT, const char* name = "");
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Pointer to the task entry function
* \param parameters
* Pointer to memory that will be used as a parameter for the task
* being created. This memory should not typically come from stack,
* but rather from dynamically (i.e., malloc'd) or statically
* allocated memory.
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
Task(task_fn_t function, void* parameters, const char* name);
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Callable object to use as entry function
* \param prio
* The priority at which the task should run.
* TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
* \param stack_depth
* The number of words (i.e. 4 * stack_depth) available on the task's
* stack. TASK_STACK_DEPTH_DEFAULT is typically sufficienct.
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
template <class F>
static task_t create(F&& function, std::uint32_t prio = TASK_PRIORITY_DEFAULT,
std::uint16_t stack_depth = TASK_STACK_DEPTH_DEFAULT, const char* name = "") {
static_assert(std::is_invocable_r_v<void, F>);
return pros::c::task_create(
[](void* parameters) {
std::unique_ptr<std::function<void()>> ptr{static_cast<std::function<void()>*>(parameters)};
(*ptr)();
},
new std::function<void()>(std::forward<F>(function)), prio, stack_depth, name);
}
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Callable object to use as entry function
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
template <class F>
static task_t create(F&& function, const char* name) {
return Task::create(std::forward<F>(function), TASK_PRIORITY_DEFAULT, TASK_STACK_DEPTH_DEFAULT, name);
}
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Callable object to use as entry function
* \param prio
* The priority at which the task should run.
* TASK_PRIO_DEFAULT plus/minus 1 or 2 is typically used.
* \param stack_depth
* The number of words (i.e. 4 * stack_depth) available on the task's
* stack. TASK_STACK_DEPTH_DEFAULT is typically sufficient.
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
template <class F>
explicit Task(F&& function, std::uint32_t prio = TASK_PRIORITY_DEFAULT,
std::uint16_t stack_depth = TASK_STACK_DEPTH_DEFAULT, const char* name = "")
: Task(
[](void* parameters) {
std::unique_ptr<std::function<void()>> ptr{static_cast<std::function<void()>*>(parameters)};
(*ptr)();
},
new std::function<void()>(std::forward<F>(function)), prio, stack_depth, name) {
static_assert(std::is_invocable_r_v<void, F>);
}
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* This function uses the following values of errno when an error state is
* reached:
* ENOMEM - The stack cannot be used as the TCB was not created.
*
* \param function
* Callable object to use as entry function
* \param name
* A descriptive name for the task. This is mainly used to facilitate
* debugging. The name may be up to 32 characters long.
*
*/
template <class F>
Task(F&& function, const char* name)
: Task(std::forward<F>(function), TASK_PRIORITY_DEFAULT, TASK_STACK_DEPTH_DEFAULT, name) {}
/**
* Create a C++ task object from a task handle
*
* \param task
* A task handle from task_create() for which to create a pros::Task
* object.
*/
explicit Task(task_t task);
/**
* Get the currently running Task
*/
static Task current();
/**
* Creates a new task and add it to the list of tasks that are ready to run.
*
* \param in
* A task handle from task_create() for which to create a pros::Task
* object.
*/
Task& operator=(task_t in);
/**
* Removes the Task from the RTOS real time kernel's management. This task
* will be removed from all ready, blocked, suspended and event lists.
*
* Memory dynamically allocated by the task is not automatically freed, and
* should be freed before the task is deleted.
*/
void remove();
/**
* Gets the priority of the specified task.
*
* \return The priority of the task
*/
std::uint32_t get_priority();
/**
* Sets the priority of the specified task.
*
* If the specified task's state is available to be scheduled (e.g. not
* blocked) and new priority is higher than the currently running task,
* a context switch may occur.
*
* \param prio
* The new priority of the task
*/
void set_priority(std::uint32_t prio);
/**
* Gets the state of the specified task.
*
* \return The state of the task
*/
std::uint32_t get_state();
/**
* Suspends the specified task, making it ineligible to be scheduled.
*/
void suspend();
/**
* Resumes the specified task, making it eligible to be scheduled.
*
* \param task
* The task to resume
*/
void resume();
/**
* Gets the name of the specified task.
*
* \return A pointer to the name of the task
*/
const char* get_name();
/**
* Convert this object to a C task_t handle
*/
explicit operator task_t() {
return task;
}
/**
* Sends a simple notification to task and increments the notification
* counter.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \return Always returns true.
*/
std::uint32_t notify();
/**
* Utilizes task notifications to wait until specified task is complete and deleted,
* then continues to execute the program. Analogous to std::thread::join in C++.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \return void
*/
void join();
/**
* Sends a notification to a task, optionally performing some action. Will
* also retrieve the value of the notification in the target task before
* modifying the notification value.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param value
* The value used in performing the action
* \param action
* An action to optionally perform on the receiving task's notification
* value
* \param prev_value
* A pointer to store the previous value of the target task's
* notification, may be NULL
*
* \return Dependent on the notification action.
* For NOTIFY_ACTION_NO_WRITE: return 0 if the value could be written without
* needing to overwrite, 1 otherwise.
* For all other NOTIFY_ACTION values: always return 0
*/
std::uint32_t notify_ext(std::uint32_t value, notify_action_e_t action, std::uint32_t* prev_value);
/**
* Waits for a notification to be nonzero.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \param clear_on_exit
* If true (1), then the notification value is cleared.
* If false (0), then the notification value is decremented.
* \param timeout
* Specifies the amount of time to be spent waiting for a notification
* to occur.
*
* \return The value of the task's notification value before it is decremented
* or cleared
*/
static std::uint32_t notify_take(bool clear_on_exit, std::uint32_t timeout);
/**
* Clears the notification for a task.
*
* See https://pros.cs.purdue.edu/v5/tutorials/topical/notifications.html for
* details.
*
* \return False if there was not a notification waiting, true if there was
*/
bool notify_clear();
/**
* Delays a task for a given number of milliseconds.
*
* This is not the best method to have a task execute code at predefined
* intervals, as the delay time is measured from when the delay is requested.
* To delay cyclically, use task_delay_until().
*
* \param milliseconds
* The number of milliseconds to wait (1000 milliseconds per second)
*/
static void delay(const std::uint32_t milliseconds);
/**
* Delays a task until a specified time. This function can be used by
* periodic tasks to ensure a constant execution frequency.
*
* The task will be woken up at the time *prev_time + delta, and *prev_time
* will be updated to reflect the time at which the task will unblock.
*
* \param prev_time
* A pointer to the location storing the setpoint time. This should
* typically be initialized to the return value from pros::millis().
* \param delta
* The number of milliseconds to wait (1000 milliseconds per second)
*/
static void delay_until(std::uint32_t* const prev_time, const std::uint32_t delta);
/**
* Gets the number of tasks the kernel is currently managing, including all
* ready, blocked, or suspended tasks. A task that has been deleted, but not
* yet reaped by the idle task will also be included in the count.
* Tasks recently created may take one context switch to be counted.
*
* \return The number of tasks that are currently being managed by the kernel.
*/
static std::uint32_t get_count();
private:
task_t task{};
};
// STL Clock compliant clock
struct Clock {
using rep = std::uint32_t;
using period = std::milli;
using duration = std::chrono::duration<rep, period>;
using time_point = std::chrono::time_point<Clock>;
const bool is_steady = true;
/**
* Gets the current time.
*
* Effectively a wrapper around pros::millis()
*
* \return The current time
*/
static time_point now();
};
class Mutex {
std::shared_ptr<std::remove_pointer_t<mutex_t>> mutex;
public:
Mutex();
// disable copy and move construction and assignment per Mutex requirements
// (see https://en.cppreference.com/w/cpp/named_req/Mutex)
Mutex(const Mutex&) = delete;
Mutex(Mutex&&) = delete;
Mutex& operator=(const Mutex&) = delete;
Mutex& operator=(Mutex&&) = delete;
/**
* Takes and locks a mutex indefinetly.
*
* See
* https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \return True if the mutex was successfully taken, false otherwise. If false
* is returned, then errno is set with a hint about why the the mutex
* couldn't be taken.
*/
bool take();
/**
* Takes and locks a mutex, waiting for up to a certain number of milliseconds
* before timing out.
*
* See
* https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \param timeout
* Time to wait before the mutex becomes available. A timeout of 0 can
* be used to poll the mutex. TIMEOUT_MAX can be used to block
* indefinitely.
*
* \return True if the mutex was successfully taken, false otherwise. If false
* is returned, then errno is set with a hint about why the the mutex
* couldn't be taken.
*/
bool take(std::uint32_t timeout);
/**
* Unlocks a mutex.
*
* See
* https://pros.cs.purdue.edu/v5/tutorials/topical/multitasking.html#mutexes
* for details.
*
* \return True if the mutex was successfully returned, false otherwise. If
* false is returned, then errno is set with a hint about why the mutex
* couldn't be returned.
*/
bool give();
/**
* Takes and locks a mutex, waiting for up to TIMEOUT_MAX milliseconds.
*
* Effectively equivalent to calling pros::Mutex::take with TIMEOUT_MAX as
* the parameter.
*
* Conforms to named requirment BasicLockable
* \see https://en.cppreference.com/w/cpp/named_req/BasicLockable
*
* \note Consider using a std::unique_lock, std::lock_guard, or
* std::scoped_lock instead of interacting with the Mutex directly.
*
* \exception std::system_error Mutex could not be locked within TIMEOUT_MAX
* milliseconds. see errno for details.
*/
void lock();
/**
* Unlocks a mutex.
*
* Equivalent to calling pros::Mutex::give.
*
* Conforms to named requirement BasicLockable
* \see https://en.cppreference.com/w/cpp/named_req/BasicLockable
*
* \note Consider using a std::unique_lock, std::lock_guard, or
* std::scoped_lock instead of interacting with the Mutex direcly.
*/
void unlock();
/**
* Try to lock a mutex.
*
* Returns immediately if unsucessful.
*
* Conforms to named requirement Lockable
* \see https://en.cppreference.com/w/cpp/named_req/Lockable
*
* \return True when lock was acquired succesfully, or false otherwise.
*/
bool try_lock();
/**
* Takes and locks a mutex, waiting for a specified duration.
*
* Equivalent to calling pros::Mutex::take with a duration specified in
* milliseconds.
*
* Conforms to named requirement TimedLockable
* \see https://en.cppreference.com/w/cpp/named_req/TimedLockable
*
* \param rel_time Time to wait before the mutex becomes available.
* \return True if the lock was acquired succesfully, otherwise false.
*/
template <typename Rep, typename Period>
bool try_lock_for(const std::chrono::duration<Rep, Period>& rel_time) {
return take(std::chrono::duration_cast<Clock::duration>(rel_time).count());
}
/**
* Takes and locks a mutex, waiting until a specified time.
*
* Conforms to named requirement TimedLockable
* \see https://en.cppreference.com/w/cpp/named_req/TimedLockable
*
* \param abs_time Time point until which to wait for the mutex.
* \return True if the lock was acquired succesfully, otherwise false.
*/
template <typename Duration>
bool try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time) {
return take(std::max(static_cast<uint32_t>(0), (abs_time - Clock::now()).count()));
}
};
template <typename Var>
class MutexVar;
template <typename Var>
class MutexVarLock {
Mutex& mutex;
Var& var;
friend class MutexVar<Var>;
constexpr MutexVarLock(Mutex& mutex, Var& var) : mutex(mutex), var(var) {}
public:
/**
* Accesses the value of the mutex-protected variable.
*/
constexpr Var& operator*() const {
return var;
}
/**
* Accesses the value of the mutex-protected variable.
*/
constexpr Var* operator->() const {
return &var;
}
~MutexVarLock() {
mutex.unlock();
}
};
template <typename Var>
class MutexVar {
Mutex mutex;
Var var;
public:
/**
* Creates a mutex-protected variable which is initialized with the given
* constructor arguments.
*
* \param args
The arguments to provide to the Var constructor.
*/
template <typename... Args>
MutexVar(Args&&... args) : mutex(), var(std::forward<Args>(args)...) {}
/**
* Try to lock the mutex-protected variable.
*
* \param timeout
* Time to wait before the mutex becomes available, in milliseconds. A
* timeout of 0 can be used to poll the mutex.
*
* \return A std::optional which contains a MutexVarLock providing access to
* the protected variable if locking is successful.
*/
std::optional<MutexVarLock<Var>> try_lock(std::uint32_t timeout) {
if (mutex.take(timeout)) {
return {{mutex, var}};
} else {
return {};
}
}
/**
* Try to lock the mutex-protected variable.
*
* \param timeout
* Time to wait before the mutex becomes available. A timeout of 0 can
* be used to poll the mutex.
*
* \return A std::optional which contains a MutexVarLock providing access to
* the protected variable if locking is successful.
*/
template <typename Rep, typename Period>
std::optional<MutexVarLock<Var>> try_lock(const std::chrono::duration<Rep, Period>& rel_time) {
try_lock(std::chrono::duration_cast<Clock::duration>(rel_time).count());
}
/**
* Lock the mutex-protected variable, waiting indefinitely.
*
* \return A MutexVarLock providing access to the protected variable.
*/
MutexVarLock<Var> lock() {
while (!mutex.take(TIMEOUT_MAX))
;
return {mutex, var};
}
};
/**
* Gets the number of milliseconds since PROS initialized.
*
* \return The number of milliseconds since PROS initialized
*/
using pros::c::millis;
/**
* Gets the number of microseconds since PROS initialized.
*
* \return The number of microseconds since PROS initialized
*/
using pros::c::micros;
/**
* Delays a task for a given number of milliseconds.
*
* This is not the best method to have a task execute code at predefined
* intervals, as the delay time is measured from when the delay is requested.
* To delay cyclically, use task_delay_until().
*
* \param milliseconds
* The number of milliseconds to wait (1000 milliseconds per second)
*/
using pros::c::delay;
} // namespace pros
#endif // _PROS_RTOS_HPP_

View File

@ -0,0 +1,499 @@
/**
* \file screen.h
*
* Brain screen display and touch functions.
*
* Contains user calls to the v5 screen for touching and displaying graphics.
*
* \copyright (c) 2017-2023, 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_SCREEN_H_
#define _PROS_SCREEN_H_
#include <stdarg.h>
#include <stdbool.h>
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdint.h>
#include "pros/colors.h" // c color macros
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
/**
* ! Different font sizes that can be used in printing text.
*/
typedef enum {
E_TEXT_SMALL = 0, ///< Small text font size
E_TEXT_MEDIUM, ///< Normal/Medium text font size
E_TEXT_LARGE, ///< Large text font size
E_TEXT_MEDIUM_CENTER, ///< Medium centered text
E_TEXT_LARGE_CENTER ///< Large centered text
} text_format_e_t;
/**
* ! Enum indicating what the current touch status is for the touchscreen.
*/
typedef enum {
E_TOUCH_RELEASED = 0, ///< Last interaction with screen was a quick press
E_TOUCH_PRESSED, ///< Last interaction with screen was a release
E_TOUCH_HELD, ///< User is holding screen down
E_TOUCH_ERROR ///< An error occured while taking/returning the mutex
} last_touch_e_t;
/**
* ! Struct representing screen touch status, screen last x, screen last y, press count, release count.
*/
typedef struct screen_touch_status_s {
last_touch_e_t touch_status; ///< Represents if the screen is being held, released, or pressed.
int16_t x; ///< Represents the x value of the location of the touch.
int16_t y; ///< Represents the y value of the location of the touch.
int32_t press_count; ///< Represents how many times the screen has be pressed.
int32_t release_count; ///< Represents how many times the user released after a touch on the screen.
} screen_touch_status_s_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define TEXT_SMALL pros::E_TEXT_SMALL
#define TEXT_MEDIUM pros::E_TEXT_MEDIUM
#define TEXT_LARGE pros::E_TEXT_LARGE
#define TEXT_MEDIUM_CENTER pros::E_TEXT_MEDIUM_CENTER
#define TEXT_LARGE_CENTER pros::E_TEXT_LARGE_CENTER
#define TOUCH_RELEASED pros::E_TOUCH_RELEASED
#define TOUCH_PRESSED pros::E_TOUCH_PRESSED
#define TOUCH_HELD pros::E_TOUCH_HELD
#else
#define TEXT_SMALL E_TEXT_SMALL
#define TEXT_MEDIUM E_TEXT_MEDIUM
#define TEXT_LARGE E_TEXT_LARGE
#define TEXT_MEDIUM_CENTER E_TEXT_MEDIUM_CENTER
#define TEXT_LARGE_CENTER E_TEXT_LARGE_CENTER
#define TOUCH_RELEASED E_TOUCH_RELEASED
#define TOUCH_PRESSED E_TOUCH_PRESSED
#define TOUCH_HELD E_TOUCH_HELD
#endif
#endif
typedef void (*touch_event_cb_fn_t)();
#ifdef __cplusplus
namespace c {
#endif
/******************************************************************************/
/** Screen Graphical Display Functions **/
/** **/
/** These functions allow programmers to display shapes on the v5 screen **/
/******************************************************************************/
/**
* Set the pen color for subsequent graphics operations
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param color The pen color to set (it is recommended to use values
* from the enum defined in colors.h)
*
* \return Returns 1 if the mutex was successfully returned, or PROS_ERR if
* there was an error either taking or returning the screen mutex.
*/
uint32_t screen_set_pen(uint32_t color);
/**
* Set the eraser color for erasing and the current background.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param color The background color to set (it is recommended to use values
* from the enum defined in colors.h)
*
* \return Returns 1 if the mutex was successfully returned, or
* PROS_ERR if there was an error either taking or returning the screen mutex.
*/
uint32_t screen_set_eraser(uint32_t color);
/**
* Get the current pen color.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return The current pen color in the form of a value from the enum defined
* in colors.h, or PROS_ERR if there was an error taking or returning
* the screen mutex.
*/
uint32_t screen_get_pen(void);
/**
* Get the current eraser color.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return The current eraser color in the form of a value from the enum
* defined in colors.h, or PROS_ERR if there was an error taking or
* returning the screen mutex.
*/
uint32_t screen_get_eraser(void);
/**
* Clear display with eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_erase(void);
/**
* Scroll lines on the display upwards.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param start_line The line from which scrolling will start
* \param lines The number of lines to scroll up
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_scroll(int16_t start_line, int16_t lines);
/**
* Scroll lines within a region on the display
*
* This function behaves in the same way as `screen_scroll`, except that you
* specify a rectangular region within which to scroll lines instead of a start
* line.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first corner of the
* rectangular region
* \param x1, y1 The (x,y) coordinates of the second corner of the
* rectangular region
* \param lines The number of lines to scroll upwards
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_scroll_area(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t lines);
/**
* Copy a screen region (designated by a rectangle) from an off-screen buffer
* to the screen
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first corner of the
* rectangular region of the screen
* \param x1, y1 The (x,y) coordinates of the second corner of the
* rectangular region of the screen
* \param buf Off-screen buffer containing screen data
* \param stride Off-screen buffer width in pixels, such that image size
* is stride-padding
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_copy_area(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint32_t* buf, int32_t stride);
/**
* Draw a single pixel on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the pixel
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_draw_pixel(int16_t x, int16_t y);
/**
* Erase a pixel from the screen (Sets the location)
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the erased
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_erase_pixel(int16_t x, int16_t y);
/**
* Draw a line on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x, y) coordinates of the first point of the line
* \param x1, y1 The (x, y) coordinates of the second point of the line
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_draw_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/**
* Erase a line on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x, y) coordinates of the first point of the line
* \param x1, y1 The (x, y) coordinates of the second point of the line
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_erase_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/**
* Draw a rectangle on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_draw_rect(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/**
* Erase a rectangle on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_erase_rect(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/**
* Fill a rectangular region of the screen using the current pen
* color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_fill_rect(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
/**
* Draw a circle on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_draw_circle(int16_t x, int16_t y, int16_t radius);
/**
* Erase a circle on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_erase_circle(int16_t x, int16_t y, int16_t radius);
/**
* Fill a circular region of the screen using the current pen
* color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_fill_circle(int16_t x, int16_t y, int16_t radius);
/******************************************************************************/
/** Screen Text Display Functions **/
/** **/
/** These functions allow programmers to display text on the v5 screen **/
/******************************************************************************/
/**
* Print a formatted string to the screen on the specified line
*
* Will default to a medium sized font by default if invalid txt_fmt is given.
*
* \param txt_fmt Text format enum that determines if the text is medium, large, medium_center, or large_center. (DOES NOT SUPPORT SMALL)
* \param line The line number on which to print
* \param text Format string
* \param ... Optional list of arguments for the format string
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_print(text_format_e_t txt_fmt, const int16_t line, const char* text, ...);
/**
* Print a formatted string to the screen at the specified point
*
* Will default to a medium sized font by default if invalid txt_fmt is given.
*
* Text formats medium_center and large_center will default to medium and large respectively.
*
* \param txt_fmt Text format enum that determines if the text is small, medium, or large.
* \param x The y coordinate of the top left corner of the string
* \param y The x coordinate of the top left corner of the string
* \param text Format string
* \param ... Optional list of arguments for the format string
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
uint32_t screen_print_at(text_format_e_t txt_fmt, const int16_t x, const int16_t y, const char* text, ...);
/**
* Print a formatted string to the screen on the specified line
*
* Same as `display_printf` except that this uses a `va_list` instead of the
* ellipsis operator so this can be used by other functions.
*
* Will default to a medium sized font by default if invalid txt_fmt is given.
* Exposed mostly for writing libraries and custom functions.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param txt_fmt Text format enum that determines if the text is medium, large, medium_center, or large_center. (DOES NOT SUPPORT SMALL)
* \param line The line number on which to print
* \param text Format string
* \param args List of arguments for the format string
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* while taking or returning the screen mutex.
*/
uint32_t screen_vprintf(text_format_e_t txt_fmt, const int16_t line, const char* text, va_list args);
/**
* Print a formatted string to the screen at the specified coordinates
*
* Same as `display_printf_at` except that this uses a `va_list` instead of the
* ellipsis operator so this can be used by other functions.
*
* Will default to a medium sized font by default if invalid txt_fmt is given.
*
* Text formats medium_center and large_center will default to medium and large respectively.
* Exposed mostly for writing libraries and custom functions.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param txt_fmt Text format enum that determines if the text is small, medium, or large.
* \param x, y The (x,y) coordinates of the top left corner of the string
* \param text Format string
* \param args List of arguments for the format string
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* while taking or returning the screen mutex.
*/
uint32_t screen_vprintf_at(text_format_e_t txt_fmt, const int16_t x, const int16_t y, const char* text, va_list args);
/******************************************************************************/
/** Screen Touch Functions **/
/** **/
/** These functions allow programmers to access **/
/** information about screen touches **/
/******************************************************************************/
/**
* Gets the touch status of the last touch of the screen.
*
* \return The last_touch_e_t enum specifier that indicates the last touch status of the screen (E_TOUCH_EVENT_RELEASE, E_TOUCH_EVENT_PRESS, or E_TOUCH_EVENT_PRESS_AND_HOLD).
* This will be released by default if no action was taken.
* If an error occured, the screen_touch_status_s_t will have its last_touch_e_t
* enum specifier set to E_TOUCH_ERR, and other values set to -1.
*/
screen_touch_status_s_t screen_touch_status(void);
/**
* Assigns a callback function to be called when a certain touch event happens.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param cb Function pointer to callback when event type happens
* \param event_type Touch event that will trigger the callback.
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* while taking or returning the screen mutex.
*/
uint32_t screen_touch_callback(touch_event_cb_fn_t cb, last_touch_e_t event_type);
#ifdef __cplusplus
} //namespace c
} //namespace pros
}
#endif
#endif

View File

@ -0,0 +1,385 @@
/**
* \file screen.hpp
*
* Brain screen display and touch functions.
*
* Contains user calls to the v5 screen for touching and displaying graphics.
*
* \copyright (c) 2017-2023, 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_SCREEN_HPP_
#define _PROS_SCREEN_HPP_
#include "pros/screen.h"
#include <cstdint>
#include <string>
namespace pros {
namespace screen {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
namespace {
template <typename T>
T convert_args(T arg) {
return arg;
}
const char* convert_args(const std::string& arg) {
return arg.c_str();
}
} // namespace
#pragma GCC diagnostic pop
/******************************************************************************/
/** Screen Graphical Display Functions **/
/** **/
/** These functions allow programmers to display shapes on the v5 screen **/
/******************************************************************************/
/**
* Set the pen color for subsequent graphics operations
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param color The pen color to set (it is recommended to use values
* from the enum defined in colors.h)
*
* \return Returns 1 if the mutex was successfully returned, or PROS_ERR if
* there was an error either taking or returning the screen mutex.
*/
std::uint32_t set_pen(const std::uint32_t color);
/**
* Set the eraser color for erasing and the current background.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param color The background color to set (it is recommended to use values
* from the enum defined in colors.h)
*
* \return Returns 1 if the mutex was successfully returned, or PROS_ERR
* if there was an error either taking or returning the screen mutex.
*/
std::uint32_t set_eraser(const std::uint32_t color);
/**
* Get the current pen color.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return The current pen color in the form of a value from the enum
* defined in colors.h, or PROS_ERR if there was an error taking or
* returning the screen mutex.
*/
std::uint32_t get_pen();
/**
* Get the current eraser color.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return The current eraser color in the form of a value from the enum
* defined in colors.h, or PROS_ERR if there was an error taking or
* returning the screen mutex.
*/
std::uint32_t get_eraser();
/**
* Clear display with eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t erase();
/**
* Scroll lines on the display upwards.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param start_line The line from which scrolling will start
* \param lines The number of lines to scroll up
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t scroll(const std::int16_t start_line, const std::int16_t lines);
/**
* Scroll lines within a region on the display
*
* This function behaves in the same way as `screen_scroll`, except that you
* specify a rectangular region within which to scroll lines instead of a start
* line.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first corner of the
* rectangular region
* \param x1, y1 The (x,y) coordinates of the second corner of the
* rectangular region
* \param lines The number of lines to scroll upwards
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t scroll_area(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1, std::int16_t lines);
/**
* Copy a screen region (designated by a rectangle) from an off-screen buffer
* to the screen
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first corner of the
* rectangular region of the screen
* \param x1, y1 The (x,y) coordinates of the second corner of the
* rectangular region of the screen
* \param buf Off-screen buffer containing screen data
* \param stride Off-screen buffer width in pixels, such that image size
* is stride-padding
*
* \return 1 if there were no errors, or PROS_ERR if an error occured taking
* or returning the screen mutex.
*/
std::uint32_t copy_area(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1, uint32_t* buf, const std::int32_t stride);
/**
* Draw a single pixel on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the pixel
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t draw_pixel(const std::int16_t x, const std::int16_t y);
/**
* Erase a pixel from the screen (Sets the location)
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the erased
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t erase_pixel(const std::int16_t x, const std::int16_t y);
/**
* Draw a line on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x, y) coordinates of the first point of the line
* \param x1, y1 The (x, y) coordinates of the second point of the line
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t draw_line(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1);
/**
* Erase a line on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x, y) coordinates of the first point of the line
* \param x1, y1 The (x, y) coordinates of the second point of the line
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t erase_line(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1);
/**
* Draw a rectangle on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t draw_rect(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1);
/**
* Erase a rectangle on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t erase_rect(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1);
/**
* Fill a rectangular region of the screen using the current pen
* color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x0, y0 The (x,y) coordinates of the first point of the rectangle
* \param x1, y1 The (x,y) coordinates of the second point of the rectangle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t fill_rect(const std::int16_t x0, const std::int16_t y0, const std::int16_t x1, const std::int16_t y1);
/**
* Draw a circle on the screen using the current pen color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t draw_circle(const std::int16_t x, const std::int16_t y, const std::int16_t radius);
/**
* Erase a circle on the screen using the current eraser color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t erase_circle(const std::int16_t x, const std::int16_t y, const std::int16_t radius);
/**
* Fill a circular region of the screen using the current pen
* color
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param x, y The (x,y) coordinates of the center of the circle
* \param r The radius of the circle
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* taking or returning the screen mutex.
*/
std::uint32_t fill_circle(const std::int16_t x, const std::int16_t y, const std::int16_t radius);
/******************************************************************************/
/** Screen Text Display Functions **/
/** **/
/** These functions allow programmers to display text on the v5 screen **/
/******************************************************************************/
/**
* Print a formatted string to the screen, overwrite available for printing at location too.
*
* Will default to a medium sized font by default if invalid txt_fmt is given.
*
* \param txt_fmt Text format enum that determines if the text is medium, large, medium_center, or large_center. (DOES NOT SUPPORT SMALL)
* \param line The line number on which to print
* \param x The (x,y) coordinates of the top left corner of the string
* \param y The (x,y) coordinates of the top left corner of the string
* \param fmt Format string
* \param ... Optional list of arguments for the format string
*/
template <typename... Params>
void print(pros::text_format_e_t txt_fmt, const std::int16_t line, const char* text, Params... args){
pros::c::screen_print(txt_fmt, line, text, convert_args(args)...);
}
template <typename... Params>
void print(pros::text_format_e_t txt_fmt, const std::int16_t x, const std::int16_t y, const char* text, Params... args){
pros::c::screen_print_at(txt_fmt, x, y, text, convert_args(args)...);
}
/******************************************************************************/
/** Screen Touch Functions **/
/** **/
/** These functions allow programmers to access **/
/** information about screen touches **/
/******************************************************************************/
/**
* Gets the touch status of the last touch of the screen.
*
* \return The last_touch_e_t enum specifier that indicates the last touch status of the screen (E_TOUCH_EVENT_RELEASE, E_TOUCH_EVENT_PRESS, or E_TOUCH_EVENT_PRESS_AND_HOLD).
* This will be released by default if no action was taken.
* If an error occured, the screen_touch_status_s_t will have its
* last_touch_e_t enum specifier set to E_TOUCH_ERR, and other values set to -1.
*/
screen_touch_status_s_t touch_status();
/**
* Assigns a callback function to be called when a certain touch event happens.
*
* This function uses the following values of errno when an error state is
* reached:
* EACCESS - Another resource is currently trying to access the screen mutex.
*
* \param cb Function pointer to callback when event type happens
* \param event_type Touch event that will trigger the callback.
*
* \return 1 if there were no errors, or PROS_ERR if an error occured
* while taking or returning the screen mutex.
*/
std::uint32_t touch_callback(touch_event_cb_fn_t cb, last_touch_e_t event_type);
} //namespace screen
} //namespace pros
#endif //header guard

View File

@ -0,0 +1,247 @@
/**
* \file pros/serial.h
*
* Contains prototypes for the V5 Generic Serial related functions.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/serial.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, 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_SERIAL_H_
#define _PROS_SERIAL_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
namespace c {
#endif
/******************************************************************************/
/** Serial communication functions **/
/** **/
/** These functions allow programmers to communicate using UART over RS485 **/
/******************************************************************************/
/**
* Enables generic serial on the given port.
*
* \note This function must be called before any of the generic serial
* functions will work.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t serial_enable(uint8_t port);
/**
* Sets the baudrate for the serial port to operate at.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
* \param baudrate
* The baudrate to operate at
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t serial_set_baudrate(uint8_t port, int32_t baudrate);
/**
* Clears the internal input and output FIFO buffers.
*
* This can be useful to reset state and remove old, potentially unneeded data
* from the input FIFO buffer or to cancel sending any data in the output FIFO
* buffer.
*
* \note This function does not cause the data in the output buffer to be
* written, it simply clears the internal buffers. Unlike stdout, generic
* serial does not use buffered IO (the FIFO buffers are written as soon
* as possible).
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t serial_flush(uint8_t port);
/**
* Returns the number of bytes available to be read in the the port's FIFO
* input buffer.
*
* \note This function does not actually read any bytes, is simply returns the
* number of bytes available to be read.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return The number of bytes avaliable to be read or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t serial_get_read_avail(uint8_t port);
/**
* Returns the number of bytes free in the port's FIFO output buffer.
*
* \note This function does not actually write any bytes, is simply returns the
* number of bytes free in the port's buffer.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return The number of bytes free or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t serial_get_write_free(uint8_t port);
/**
* Reads the next byte avaliable in the port's input buffer without removing it.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return The next byte avaliable to be read, -1 if none are available, or
* PROS_ERR if the operation failed, setting errno.
*/
int32_t serial_peek_byte(uint8_t port);
/**
* Reads the next byte avaliable in the port's input buffer.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
*
* \return The next byte avaliable to be read, -1 if none are available, or
* PROS_ERR if the operation failed, setting errno.
*/
int32_t serial_read_byte(uint8_t port);
/**
* Reads up to the next length bytes from the port's input buffer and places
* them in the user supplied buffer.
*
* \note This function will only return bytes that are currently avaliable to be
* read and will not block waiting for any to arrive.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
* \param buffer
* The location to place the data read
* \param length
* The maximum number of bytes to read
*
* \return The number of bytes read or PROS_ERR if the operation failed, setting
* errno.
*/
int32_t serial_read(uint8_t port, uint8_t* buffer, int32_t length);
/**
* Write the given byte to the port's output buffer.
*
* \note Data in the port's output buffer is written to the serial port as soon
* as possible on a FIFO basis and can not be done manually by the user.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
* EIO - Serious internal write error.
*
* \param port
* The V5 port number from 1-21
* \param buffer
* The byte to write
*
* \return The number of bytes written or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t serial_write_byte(uint8_t port, uint8_t buffer);
/**
* Writes up to length bytes from the user supplied buffer to the port's output
* buffer.
*
* \note Data in the port's output buffer is written to the serial port as soon
* as possible on a FIFO basis and can not be done manually by the user.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
* EIO - Serious internal write error.
*
* \param port
* The V5 port number from 1-21
* \param buffer
* The data to write
* \param length
* The maximum number of bytes to write
*
* \return The number of bytes written or PROS_ERR if the operation failed,
* setting errno.
*/
int32_t serial_write(uint8_t port, uint8_t* buffer, int32_t length);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_SERIAL_H_

View File

@ -0,0 +1,228 @@
/**
* \file pros/serial.hpp
*
* Contains prototypes for the V5 Generic Serial related functions.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/serial.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright (c) 2017-2021, 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_SERIAL_HPP_
#define _PROS_SERIAL_HPP_
#include <cstdint>
#include "pros/serial.h"
namespace pros {
class Serial {
public:
/**
* Creates a Serial object for the given port and specifications.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param port
* The V5 port number from 1-21
* \param baudrate
* The baudrate to run the port at
*/
explicit Serial(std::uint8_t port, std::int32_t baudrate);
explicit Serial(std::uint8_t port);
/******************************************************************************/
/** Serial communication functions **/
/** **/
/** These functions allow programmers to communicate using UART over RS485 **/
/******************************************************************************/
/**
* Sets the baudrate for the serial port to operate at.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param baudrate
* The baudrate to operate at
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t set_baudrate(std::int32_t baudrate) const;
/**
* Clears the internal input and output FIFO buffers.
*
* This can be useful to reset state and remove old, potentially unneeded data
* from the input FIFO buffer or to cancel sending any data in the output FIFO
* buffer.
*
* \note This function does not cause the data in the output buffer to be
* written, it simply clears the internal buffers. Unlike stdout, generic
* serial does not use buffered IO (the FIFO buffers are written as soon
* as possible).
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t flush() const;
/**
* Returns the number of bytes available to be read in the the port's FIFO
* input buffer.
*
* \note This function does not actually read any bytes, is simply returns the
* number of bytes available to be read.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \return The number of bytes avaliable to be read or PROS_ERR if the operation
* failed, setting errno.
*/
virtual std::int32_t get_read_avail() const;
/**
* Returns the number of bytes free in the port's FIFO output buffer.
*
* \note This function does not actually write any bytes, is simply returns the
* number of bytes free in the port's buffer.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \return The number of bytes free or PROS_ERR if the operation failed,
* setting errno.
*/
virtual std::int32_t get_write_free() const;
/**
* Gets the port number of the serial port.
*
* \return The serial port's port number.
*/
std::uint8_t get_port() const;
/**
* Reads the next byte avaliable in the port's input buffer without removing it.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \return The next byte avaliable to be read, -1 if none are available, or
* PROS_ERR if the operation failed, setting errno.
*/
virtual std::int32_t peek_byte() const;
/**
* Reads the next byte avaliable in the port's input buffer.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \return The next byte avaliable to be read, -1 if none are available, or
* PROS_ERR if the operation failed, setting errno.
*/
virtual std::int32_t read_byte() const;
/**
* Reads up to the next length bytes from the port's input buffer and places
* them in the user supplied buffer.
*
* \note This function will only return bytes that are currently avaliable to be
* read and will not block waiting for any to arrive.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
*
* \param buffer
* The location to place the data read
* \param length
* The maximum number of bytes to read
*
* \return The number of bytes read or PROS_ERR if the operation failed, setting
* errno.
*/
virtual std::int32_t read(std::uint8_t* buffer, std::int32_t length) const;
/**
* Write the given byte to the port's output buffer.
*
* \note Data in the port's output buffer is written to the serial port as soon
* as possible on a FIFO basis and can not be done manually by the user.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
* EIO - Serious internal write error.
*
* \param buffer
* The byte to write
*
* \return The number of bytes written or PROS_ERR if the operation failed,
* setting errno.
*/
virtual std::int32_t write_byte(std::uint8_t buffer) const;
/**
* Writes up to length bytes from the user supplied buffer to the port's output
* buffer.
*
* \note Data in the port's output buffer is written to the serial port as soon
* as possible on a FIFO basis and can not be done manually by the user.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - The given value is not within the range of V5 ports (1-21).
* EACCES - Another resource is currently trying to access the port.
* EIO - Serious internal write error.
*
* \param buffer
* The data to write
* \param length
* The maximum number of bytes to write
*
* \return The number of bytes written or PROS_ERR if the operation failed,
* setting errno.
*/
virtual std::int32_t write(std::uint8_t* buffer, std::int32_t length) const;
private:
const std::uint8_t _port;
};
namespace literals {
const pros::Serial operator"" _ser(const unsigned long long int m);
} // namespace literals
} // namespace pros
#endif // _PROS_SERIAL_HPP_

View File

@ -0,0 +1,557 @@
/**
* \file pros/vision.h
*
* Contains prototypes for the VEX Vision Sensor-related functions.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_VISION_H_
#define _PROS_VISION_H_
#define VISION_OBJECT_ERR_SIG 255
// Parameters given by VEX
#define VISION_FOV_WIDTH 316
#define VISION_FOV_HEIGHT 212
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
namespace pros {
#endif
/**
* This enumeration defines the different types of objects
* that can be detected by the Vision Sensor
*/
typedef enum vision_object_type {
E_VISION_OBJECT_NORMAL = 0,
E_VISION_OBJECT_COLOR_CODE = 1,
E_VISION_OBJECT_LINE = 2
} vision_object_type_e_t;
/**
* This structure contains the parameters used by the Vision Sensor
* to detect objects.
*/
typedef struct __attribute__((__packed__)) vision_signature {
uint8_t id;
uint8_t _pad[3];
float range;
int32_t u_min;
int32_t u_max;
int32_t u_mean;
int32_t v_min;
int32_t v_max;
int32_t v_mean;
uint32_t rgb;
uint32_t type;
} vision_signature_s_t;
/**
* Color codes are just signatures with multiple IDs and a different type.
*/
typedef uint16_t vision_color_code_t;
/**
* This structure contains a descriptor of an object detected
* by the Vision Sensor
*/
typedef struct __attribute__((__packed__)) vision_object {
// Object signature
uint16_t signature;
// Object type, e.g. normal, color code, or line detection
vision_object_type_e_t type;
// left boundary coordinate of the object
int16_t left_coord;
// top boundary coordinate of the object
int16_t top_coord;
// width of the object
int16_t width;
// height of the object
int16_t height;
// Angle of a color code object in 0.1 degree units (e.g. 10 -> 1 degree, 155
// -> 15.5 degrees)
uint16_t angle;
// coordinates of the middle of the object (computed from the values above)
int16_t x_middle_coord;
int16_t y_middle_coord;
} vision_object_s_t;
typedef enum vision_zero {
E_VISION_ZERO_TOPLEFT = 0, // (0,0) coordinate is the top left of the FOV
E_VISION_ZERO_CENTER = 1 // (0,0) coordinate is the center of the FOV
} vision_zero_e_t;
#ifdef PROS_USE_SIMPLE_NAMES
#ifdef __cplusplus
#define VISION_OBJECT_NORMAL pros::E_VISION_OBJECT_NORMAL
#define VISION_OBJECT_COLOR_CODE pros::E_VISION_OBJECT_COLOR_CODE
#define VISION_OBJECT_LINE pros::E_VISION_OBJECT_LINE
#define VISION_ZERO_TOPLEFT pros::E_VISION_ZERO_TOPLEFT
#define VISION_ZERO_CENTER pros::E_VISION_ZERO_CENTER
#else
#define VISION_OBJECT_NORMAL E_VISION_OBJECT_NORMAL
#define VISION_OBJECT_COLOR_CODE E_VISION_OBJECT_COLOR_CODE
#define VISION_OBJECT_LINE E_VISION_OBJECT_LINE
#define VISION_ZERO_TOPLEFT E_VISION_ZERO_TOPLEFT
#define VISION_ZERO_CENTER E_VISION_ZERO_CENTER
#endif
#endif
#ifdef __cplusplus
namespace c {
#endif
/**
* Clears the vision sensor LED color, reseting it back to its default behavior,
* displaying the most prominent object signature color.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_clear_led(uint8_t port);
/**
* Creates a signature from the vision sensor utility
*
* \param id
* The signature ID
* \param u_min
* Minimum value on U axis
* \param u_max
* Maximum value on U axis
* \param u_mean
* Mean value on U axis
* \param v_min
* Minimum value on V axis
* \param v_max
* Maximum value on V axis
* \param v_mean
* Mean value on V axis
* \param range
* Scale factor
* \param type
* Signature type
*
* \return A vision_signature_s_t that can be set using vision_set_signature
*/
vision_signature_s_t vision_signature_from_utility(const int32_t id, const int32_t u_min, const int32_t u_max,
const int32_t u_mean, const int32_t v_min, const int32_t v_max,
const int32_t v_mean, const float range, const int32_t type);
/**
* Creates a color code that represents a combination of the given signature
* IDs. If fewer than 5 signatures are to be a part of the color code, pass 0
* for the additional function parameters.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - Fewer than two signatures have been provided or one of the
* signatures is out of its [1-7] range (or 0 when omitted).
*
* \param port
* The V5 port number from 1-21
* \param sig_id1
* The first signature id [1-7] to add to the color code
* \param sig_id2
* The second signature id [1-7] to add to the color code
* \param sig_id3
* The third signature id [1-7] to add to the color code
* \param sig_id4
* The fourth signature id [1-7] to add to the color code
* \param sig_id5
* The fifth signature id [1-7] to add to the color code
*
* \return A vision_color_code_t object containing the color code information.
*/
vision_color_code_t vision_create_color_code(uint8_t port, const uint32_t sig_id1, const uint32_t sig_id2,
const uint32_t sig_id3, const uint32_t sig_id4, const uint32_t sig_id5);
/**
* Gets the nth largest object according to size_id.
*
* 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 vision sensor
* EDOM - size_id is greater than the number of available objects.
* EHOSTDOWN - Reading the vision sensor failed for an unknown reason.
*
* \param port
* The V5 port number from 1-21
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
*
* \return The vision_object_s_t object corresponding to the given size id, or
* PROS_ERR if an error occurred.
*/
vision_object_s_t vision_get_by_size(uint8_t port, const uint32_t size_id);
/**
* Gets the nth largest object of the given signature according to size_id.
*
* 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 vision sensor
* EINVAL - sig_id is outside the range [1-8]
* EDOM - size_id is greater than the number of available objects.
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param port
* The V5 port number from 1-21
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param signature
* The signature ID [1-7] for which an object will be returned.
*
* \return The vision_object_s_t object corresponding to the given signature and
* size_id, or PROS_ERR if an error occurred.
*/
vision_object_s_t vision_get_by_sig(uint8_t port, const uint32_t size_id, const uint32_t sig_id);
/**
* Gets the nth largest object of the given color code according to size_id.
*
* 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 vision sensor
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param port
* The V5 port number from 1-21
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param color_code
* The vision_color_code_t for which an object will be returned
*
* \return The vision_object_s_t object corresponding to the given color code
* and size_id, or PROS_ERR if an error occurred.
*/
vision_object_s_t vision_get_by_code(uint8_t port, const uint32_t size_id, const vision_color_code_t color_code);
/**
* Gets the exposure parameter of the Vision Sensor. See
* https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting
* for more detials.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
*
* \return The current exposure setting from [0,150], PROS_ERR if an error
* occurred
*/
int32_t vision_get_exposure(uint8_t port);
/**
* Gets the number of objects currently detected by the Vision Sensor.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
*
* \return The number of objects detected on the specified vision sensor.
* Returns PROS_ERR if the port was invalid or an error occurred.
*/
int32_t vision_get_object_count(uint8_t port);
/**
* Get the white balance parameter of the Vision Sensor.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
*
* \return The current RGB white balance setting of the sensor
*/
int32_t vision_get_white_balance(uint8_t port);
/**
* Prints the contents of the signature as an initializer list to the terminal.
*
* \param sig
* The signature for which the contents will be printed
*
* \return 1 if no errors occured, PROS_ERR otherwise
*/
int32_t vision_print_signature(const vision_signature_s_t sig);
/**
* Reads up to object_count object descriptors into object_arr.
*
* 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), or
* fewer than object_count number of objects were found.
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
*
* \param port
* The V5 port number from 1-21
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param object_count
* The number of objects to read
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
int32_t vision_read_by_size(uint8_t port, const uint32_t size_id, const uint32_t object_count,
vision_object_s_t* const object_arr);
/**
* Reads up to object_count object descriptors into object_arr.
*
* 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), or
* fewer than object_count number of objects were found.
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
*
* \param port
* The V5 port number from 1-21
* \param object_count
* The number of objects to read
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param signature
* The signature ID [1-7] for which objects will be returned.
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
int32_t vision_read_by_sig(uint8_t port, const uint32_t size_id, const uint32_t sig_id, const uint32_t object_count,
vision_object_s_t* const object_arr);
/**
* Reads up to object_count object descriptors into object_arr.
*
* 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), or
* fewer than object_count number of objects were found.
* ENODEV - The port cannot be configured as a vision sensor
*
* \param port
* The V5 port number from 1-21
* \param object_count
* The number of objects to read
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param color_code
* The vision_color_code_t for which objects will be returned
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
int32_t vision_read_by_code(uint8_t port, const uint32_t size_id, const vision_color_code_t color_code,
const uint32_t object_count, vision_object_s_t* const object_arr);
/**
* Gets the object detection signature with the given id number.
*
* \param port
* The V5 port number from 1-21
* \param signature_id
* The signature id to read
*
* \return A vision_signature_s_t containing information about the signature.
*/
vision_signature_s_t vision_get_signature(uint8_t port, const uint8_t signature_id);
/**
* Stores the supplied object detection signature onto the vision sensor.
*
* NOTE: This saves the signature in volatile memory, and the signature will be
* lost as soon as the sensor is powered down.
*
* \param port
* The V5 port number from 1-21
* \param signature_id
* The signature id to store into
* \param[in] signature_ptr
* A pointer to the signature to save
*
* \return 1 if no errors occured, PROS_ERR otherwise
*/
int32_t vision_set_signature(uint8_t port, const uint8_t signature_id, vision_signature_s_t* const signature_ptr);
/**
* Enables/disables auto white-balancing on the Vision Sensor.
*
* 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 vision sensor
* EINVAL - enable was not 0 or 1
*
* \param port
* The V5 port number from 1-21
* \param enabled
* Pass 0 to disable, 1 to enable
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_auto_white_balance(uint8_t port, const uint8_t enable);
/**
* Sets the exposure parameter of the Vision Sensor. See
* https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting
* for more detials.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
* \param percent
* The new exposure setting from [0,150]
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_exposure(uint8_t port, const uint8_t exposure);
/**
* Sets the vision sensor LED color, overriding the automatic behavior.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
* \param rgb
* An RGB code to set the LED to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_led(uint8_t port, const int32_t rgb);
/**
* Sets the white balance parameter of the Vision Sensor.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
* \param rgb
* The new RGB white balance setting of the sensor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_white_balance(uint8_t port, const int32_t rgb);
/**
* Sets the (0,0) coordinate for the Field of View.
*
* This will affect the coordinates returned for each request for a
* vision_object_s_t from the sensor, so it is recommended that this function
* only be used to configure the sensor at the beginning of its use.
*
* 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 vision sensor
*
* \param port
* The V5 port number from 1-21
* \param zero_point
* One of vision_zero_e_t to set the (0,0) coordinate for the FOV
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_zero_point(uint8_t port, vision_zero_e_t zero_point);
/**
* Sets the Wi-Fi mode of the Vision sensor
*
* This functions uses the following values of errno when an error state is
* reached:
* ENXIO - The given port is not within the range of V5 ports (1-21)
* EACCESS - Anothe resources is currently trying to access the port
*
* \param port
* The V5 port number from 1-21
* \param enable
* Disable Wi-Fi on the Vision sensor if 0, enable otherwise (e.g. 1)
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
int32_t vision_set_wifi_mode(uint8_t port, const uint8_t enable);
#ifdef __cplusplus
} // namespace c
} // namespace pros
}
#endif
#endif // _PROS_VISION_H_

View File

@ -0,0 +1,445 @@
/**
* \file pros/vision.hpp
*
* Contains prototypes for the VEX Vision Sensor-related functions in C++.
*
* Visit https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html to learn
* more.
*
* This file should not be modified by users, since it gets replaced whenever
* a kernel upgrade occurs.
*
* \copyright Copyright (c) 2017-2023, Purdue University ACM SIGBots.
* All rights reserved.
*
* 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_VISION_HPP_
#define _PROS_VISION_HPP_
#include "pros/vision.h"
#include <cstdint>
namespace pros {
class Vision {
public:
/**
* Create a Vision Sensor object on 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 vision sensor
*
* \param port
* The V5 port number from 1-21
* \param zero_point
* One of vision_zero_e_t to set the (0,0) coordinate for the FOV
*/
Vision(std::uint8_t port, vision_zero_e_t zero_point = E_VISION_ZERO_TOPLEFT);
/**
* Clears the vision sensor LED color, reseting it back to its default
* behavior, displaying the most prominent object signature color.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t clear_led(void) const;
/**
* Creates a signature from the vision sensor utility
*
* \param id
* The signature ID
* \param u_min
* Minimum value on U axis
* \param u_max
* Maximum value on U axis
* \param u_mean
* Mean value on U axis
* \param v_min
* Minimum value on V axis
* \param v_max
* Maximum value on V axis
* \param v_mean
* Mean value on V axis
* \param rgb
* Scale factor
* \param type
* Signature type
*
* \return A vision_signature_s_t that can be set using Vision::set_signature
*/
static vision_signature_s_t signature_from_utility(const std::int32_t id, const std::int32_t u_min,
const std::int32_t u_max, const std::int32_t u_mean,
const std::int32_t v_min, const std::int32_t v_max,
const std::int32_t v_mean, const float range,
const std::int32_t type);
/**
* Creates a color code that represents a combination of the given signature
* IDs.
*
* This function uses the following values of errno when an error state is
* reached:
* EINVAL - Fewer than two signatures have been provided or one of the
* signatures is out of its [1-7] range (or 0 when omitted).
*
* \param sig_id1
* The first signature id [1-7] to add to the color code
* \param sig_id2
* The second signature id [1-7] to add to the color code
* \param sig_id3
* The third signature id [1-7] to add to the color code
* \param sig_id4
* The fourth signature id [1-7] to add to the color code
* \param sig_id5
* The fifth signature id [1-7] to add to the color code
*
* \return A vision_color_code_t object containing the color code information.
*/
vision_color_code_t create_color_code(const std::uint32_t sig_id1, const std::uint32_t sig_id2,
const std::uint32_t sig_id3 = 0, const std::uint32_t sig_id4 = 0,
const std::uint32_t sig_id5 = 0) const;
/**
* Gets the nth largest object according to size_id.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
*
* \return The vision_object_s_t object corresponding to the given size id, or
* PROS_ERR if an error occurred.
*/
vision_object_s_t get_by_size(const std::uint32_t size_id) const;
/**
* Gets the nth largest object of the given signature according to size_id.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
* EINVAL - sig_id is outside the range [1-8]
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param signature
* The vision_signature_s_t signature for which an object will be
* returned.
*
* \return The vision_object_s_t object corresponding to the given signature
* and size_id, or PROS_ERR if an error occurred.
*/
vision_object_s_t get_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id) const;
/**
* Gets the nth largest object of the given color code according to size_id.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EAGAIN - Reading the Vision Sensor failed for an unknown reason.
*
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param color_code
* The vision_color_code_t for which an object will be returned
*
* \return The vision_object_s_t object corresponding to the given color code
* and size_id, or PROS_ERR if an error occurred.
*/
vision_object_s_t get_by_code(const std::uint32_t size_id, const vision_color_code_t color_code) const;
/**
* Gets the exposure parameter of the Vision Sensor. See
* https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting
* for more detials.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \return The current exposure parameter from [0,150],
* PROS_ERR if an error occurred
*/
std::int32_t get_exposure(void) const;
/**
* Gets the number of objects currently detected by the Vision Sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \return The number of objects detected on the specified vision sensor.
* Returns PROS_ERR if the port was invalid or an error occurred.
*/
std::int32_t get_object_count(void) const;
/**
* Gets the object detection signature with the given id number.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param signature_id
* The signature id to read
*
* \return A vision_signature_s_t containing information about the signature.
*/
vision_signature_s_t get_signature(const std::uint8_t signature_id) const;
/**
* Get the white balance parameter of the Vision Sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \return The current RGB white balance setting of the sensor
*/
std::int32_t get_white_balance(void) const;
/**
* Gets the port number of the Vision Sensor.
*
* \return The vision sensor's port number.
*/
std::uint8_t get_port(void) const;
/**
* Reads up to object_count object descriptors into object_arr.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param object_count
* The number of objects to read
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
std::int32_t read_by_size(const std::uint32_t size_id, const std::uint32_t object_count,
vision_object_s_t* const object_arr) const;
/**
* Reads up to object_count object descriptors into object_arr.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EDOM - size_id is greater than the number of available objects.
* EINVAL - sig_id is outside the range [1-8]
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param object_count
* The number of objects to read
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param signature
* The vision_signature_s_t signature for which an object will be
* returned.
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
std::int32_t read_by_sig(const std::uint32_t size_id, const std::uint32_t sig_id, const std::uint32_t object_count,
vision_object_s_t* const object_arr) const;
/**
* Reads up to object_count object descriptors into object_arr.
*
* This function uses the following values of errno when an error state is
* reached:
* EDOM - size_id is greater than the number of available objects.
* ENODEV - The port cannot be configured as a vision sensor
* EAGAIN - Reading the vision sensor failed for an unknown reason.
*
* \param object_count
* The number of objects to read
* \param size_id
* The object to read from a list roughly ordered by object size
* (0 is the largest item, 1 is the second largest, etc.)
* \param color_code
* The vision_color_code_t for which objects will be returned
* \param[out] object_arr
* A pointer to copy the objects into
*
* \return The number of object signatures copied. This number will be less than
* object_count if there are fewer objects detected by the vision sensor.
* Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
* than size_id were found. All objects in object_arr that were not found are
* given VISION_OBJECT_ERR_SIG as their signature.
*/
int32_t read_by_code(const std::uint32_t size_id, const vision_color_code_t color_code,
const std::uint32_t object_count, vision_object_s_t* const object_arr) const;
/**
* Prints the contents of the signature as an initializer list to the terminal.
*
* \param sig
* The signature for which the contents will be printed
*
* \return 1 if no errors occured, PROS_ERR otherwise
*/
static std::int32_t print_signature(const vision_signature_s_t sig);
/**
* Enables/disables auto white-balancing on the Vision Sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param enabled
* Pass 0 to disable, 1 to enable
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_auto_white_balance(const std::uint8_t enable) const;
/**
* Sets the exposure parameter of the Vision Sensor. See
* https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting
* for more detials.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param percent
* The new exposure setting from [0,150].
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_exposure(const std::uint8_t exposure) const;
/**
* Sets the vision sensor LED color, overriding the automatic behavior.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param rgb
* An RGB code to set the LED to
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_led(const std::int32_t rgb) const;
/**
* Stores the supplied object detection signature onto the vision sensor.
*
* NOTE: This saves the signature in volatile memory, and the signature will be
* lost as soon as the sensor is powered down.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
* EINVAL - sig_id is outside the range [1-8]
*
* \param signature_id
* The signature id to store into
* \param[in] signature_ptr
* A pointer to the signature to save
*
* \return 1 if no errors occured, PROS_ERR otherwise
*/
std::int32_t set_signature(const std::uint8_t signature_id, vision_signature_s_t* const signature_ptr) const;
/**
* Sets the white balance parameter of the Vision Sensor.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param rgb
* The new RGB white balance setting of the sensor
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_white_balance(const std::int32_t rgb) const;
/**
* Sets the (0,0) coordinate for the Field of View.
*
* This will affect the coordinates returned for each request for a
* vision_object_s_t from the sensor, so it is recommended that this function
* only be used to configure the sensor at the beginning of its use.
*
* This function uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param zero_point
* One of vision_zero_e_t to set the (0,0) coordinate for the FOV
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_zero_point(vision_zero_e_t zero_point) const;
/**
* Sets the Wi-Fi mode of the Vision sensor
*
* This functions uses the following values of errno when an error state is
* reached:
* ENODEV - The port cannot be configured as a vision sensor
*
* \param enable
* Disable Wi-Fi on the Vision sensor if 0, enable otherwise (e.g. 1)
*
* \return 1 if the operation was successful or PROS_ERR if the operation
* failed, setting errno.
*/
std::int32_t set_wifi_mode(const std::uint8_t enable) const;
private:
std::uint8_t _port;
};
} // namespace pros
#endif // _PROS_VISION_HPP_