pros_sys/
vision.rs

1use core::ffi::c_uint;
2
3pub const VISION_OBJECT_ERR_SIG: u16 = 255;
4pub const VISION_FOV_WIDTH: c_uint = 316;
5pub const VISION_FOV_HEIGHT: c_uint = 212;
6
7pub const E_VISION_OBJECT_NORMAL: c_uint = 0;
8pub const E_VISION_OBJECT_COLOR_CODE: c_uint = 1;
9pub const E_VISION_OBJECT_LINE: c_uint = 2;
10/**
11 * This enumeration defines the different types of objects
12 * that can be detected by the Vision Sensor
13 */
14pub type vision_object_type_e_t = c_uint;
15
16/**
17 * This structure contains the parameters used by the Vision Sensor
18 * to detect objects.
19 */
20#[repr(packed, C)]
21pub struct vision_signature_s_t {
22    pub id: u8,
23    pub _pad: [u8; 3],
24    pub range: f32,
25    pub u_min: i32,
26    pub u_max: i32,
27    pub u_mean: i32,
28    pub v_min: i32,
29    pub v_max: i32,
30    pub v_mean: i32,
31    pub rgb: u32,
32    pub r#type: u32,
33}
34
35/**
36 * Color codes are just signatures with multiple IDs and a different type.
37 */
38pub type vision_color_code_t = u16;
39
40/**
41 * This structure contains a descriptor of an object detected
42 * by the Vision Sensor
43 */
44#[repr(packed, C)]
45pub struct vision_object_s_t {
46    pub signature: u16,
47    pub r#type: vision_object_type_e_t,
48    pub left_coord: i16,
49    pub top_coord: i16,
50    pub width: i16,
51    pub height: i16,
52    pub angle: i16,
53    pub x_middle_coord: i16,
54    pub y_middle_coord: i16,
55}
56
57pub const E_VISION_ZERO_TOPLEFT: c_uint = 0;
58pub const E_VISION_ZERO_CENTER: c_uint = 1;
59pub type vision_zero_e_t = c_uint;
60
61extern "C" {
62    /**
63    Clears the vision sensor LED color, resetting it back to its default behavior,
64    displaying the most prominent object signature color.
65
66    This function uses the following values of errno when an error state is
67    reached:
68    ENXIO - The given value is not within the range of V5 ports (1-21).
69    ENODEV - The port cannot be configured as a vision sensor
70
71    \param port
72           The V5 port number from 1-21
73
74    \return 1 if the operation was successful or PROS_ERR if the operation
75    failed, setting errno.
76    */
77    pub fn vision_clear_led(port: u8) -> i32;
78    /**
79    Creates a signature from the vision sensor utility
80
81    \param id
82           The signature ID
83    \param u_min
84           Minimum value on U axis
85    \param u_max
86           Maximum value on U axis
87    \param u_mean
88           Mean value on U axis
89    \param v_min
90           Minimum value on V axis
91    \param v_max
92           Maximum value on V axis
93    \param v_mean
94           Mean value on V axis
95    \param range
96           Scale factor
97    \param type
98           Signature type
99
100    \return A vision_signature_s_t that can be set using vision_set_signature
101    */
102    pub fn vision_signature_from_utility(
103        id: i32,
104        u_min: i32,
105        u_max: i32,
106        u_mean: i32,
107        v_min: i32,
108        v_max: i32,
109        v_mean: i32,
110        range: f32,
111        r#type: i32,
112    ) -> vision_signature_s_t;
113    /**
114    Creates a color code that represents a combination of the given signature
115    IDs. If fewer than 5 signatures are to be a part of the color code, pass 0
116    for the additional function parameters.
117
118    This function uses the following values of errno when an error state is
119    reached:
120    EINVAL - Fewer than two signatures have been provided or one of the
121             signatures is out of its [1-7] range (or 0 when omitted).
122
123    \param port
124           The V5 port number from 1-21
125    \param sig_id1
126           The first signature id [1-7] to add to the color code
127    \param sig_id2
128           The second signature id [1-7] to add to the color code
129    \param sig_id3
130           The third signature id [1-7] to add to the color code
131    \param sig_id4
132           The fourth signature id [1-7] to add to the color code
133    \param sig_id5
134           The fifth signature id [1-7] to add to the color code
135
136    \return A vision_color_code_t object containing the color code information.
137    */
138    pub fn vision_create_color_code(
139        port: u8,
140        sig_id1: u32,
141        sig_id2: u32,
142        sig_id3: u32,
143        sig_id4: u32,
144        sig_id5: u32,
145    ) -> vision_color_code_t;
146    /**
147    Gets the nth largest object according to size_id.
148
149    This function uses the following values of errno when an error state is
150    reached:
151    ENXIO - The given value is not within the range of V5 ports (1-21).
152    ENODEV - The port cannot be configured as a vision sensor
153    EDOM - size_id is greater than the number of available objects.
154    EHOSTDOWN - Reading the vision sensor failed for an unknown reason.
155
156    \param port
157           The V5 port number from 1-21
158    \param size_id
159           The object to read from a list roughly ordered by object size
160           (0 is the largest item, 1 is the second largest, etc.)
161
162    \return The vision_object_s_t object corresponding to the given size id, or
163    PROS_ERR if an error occurred.
164    */
165    pub fn vision_get_by_size(port: u8, size_id: u32) -> vision_object_s_t;
166    /**
167    Gets the nth largest object of the given signature according to size_id.
168
169    This function uses the following values of errno when an error state is
170    reached:
171    ENXIO - The given value is not within the range of V5 ports (1-21).
172    ENODEV - The port cannot be configured as a vision sensor
173    EINVAL - sig_id is outside the range [1-8]
174    EDOM - size_id is greater than the number of available objects.
175    EAGAIN - Reading the vision sensor failed for an unknown reason.
176
177    \param port
178           The V5 port number from 1-21
179    \param size_id
180           The object to read from a list roughly ordered by object size
181           (0 is the largest item, 1 is the second largest, etc.)
182    \param signature
183           The signature ID [1-7] for which an object will be returned.
184
185    \return The vision_object_s_t object corresponding to the given signature and
186    size_id, or PROS_ERR if an error occurred.
187    */
188    pub fn vision_get_by_sig(port: u8, size_id: u32, sig_id: u32) -> vision_object_s_t;
189    /**
190    Gets the nth largest object of the given color code according to size_id.
191
192    This function uses the following values of errno when an error state is
193    reached:
194    ENXIO - The given value is not within the range of V5 ports (1-21).
195    ENODEV - The port cannot be configured as a vision sensor
196    EAGAIN - Reading the vision sensor failed for an unknown reason.
197
198    \param port
199           The V5 port number from 1-21
200    \param size_id
201           The object to read from a list roughly ordered by object size
202           (0 is the largest item, 1 is the second largest, etc.)
203    \param color_code
204           The vision_color_code_t for which an object will be returned
205
206    \return The vision_object_s_t object corresponding to the given color code
207    and size_id, or PROS_ERR if an error occurred.
208    */
209    pub fn vision_get_by_code(
210        port: u8,
211        size_id: u32,
212        code: vision_color_code_t,
213    ) -> vision_object_s_t;
214    /**
215    Gets the exposure parameter of the Vision Sensor. See
216    <https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting>
217    for more details.
218
219    This function uses the following values of errno when an error state is
220    reached:
221    ENXIO - The given value is not within the range of V5 ports (1-21).
222    ENODEV - The port cannot be configured as a vision sensor
223
224    \param port
225           The V5 port number from 1-21
226
227    \return The current exposure setting from \[0,150], PROS_ERR if an error
228    occurred
229    */
230    pub fn vision_get_exposure(port: u8) -> i32;
231    /**
232    Gets the number of objects currently detected by the Vision Sensor.
233
234    This function uses the following values of errno when an error state is
235    reached:
236    ENXIO - The given value is not within the range of V5 ports (1-21).
237    ENODEV - The port cannot be configured as a vision sensor
238
239    \param port
240           The V5 port number from 1-21
241
242    \return The number of objects detected on the specified vision sensor.
243    Returns PROS_ERR if the port was invalid or an error occurred.
244    */
245    pub fn vision_get_object_count(port: u8) -> i32;
246    /**
247    Get the white balance parameter of the Vision Sensor.
248
249    This function uses the following values of errno when an error state is
250    reached:
251    ENXIO - The given value is not within the range of V5 ports (1-21).
252    ENODEV - The port cannot be configured as a vision sensor
253
254    \param port
255                The V5 port number from 1-21
256
257    \return The current RGB white balance setting of the sensor
258    */
259    pub fn vision_get_white_balance(port: u8) -> i32;
260    /**
261    Prints the contents of the signature as an initializer list to the terminal.
262
263    \param sig
264           The signature for which the contents will be printed
265
266    \return 1 if no errors occurred, PROS_ERR otherwise
267    */
268    pub fn vision_print_signature(sig: vision_signature_s_t) -> i32;
269    /**
270    Reads up to object_count object descriptors into object_arr.
271
272    This function uses the following values of errno when an error state is
273    reached:
274    ENXIO - The given value is not within the range of V5 ports (1-21), or
275             fewer than object_count number of objects were found.
276    ENODEV - The port cannot be configured as a vision sensor
277    EDOM - size_id is greater than the number of available objects.
278
279    \param port
280           The V5 port number from 1-21
281    \param size_id
282           The object to read from a list roughly ordered by object size
283           (0 is the largest item, 1 is the second largest, etc.)
284    \param object_count
285           The number of objects to read
286    \param\[out] object_arr
287                A pointer to copy the objects into
288
289    \return The number of object signatures copied. This number will be less than
290    object_count if there are fewer objects detected by the vision sensor.
291    Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
292    than size_id were found. All objects in object_arr that were not found are
293    given VISION_OBJECT_ERR_SIG as their signature.
294    */
295    pub fn vision_read_by_size(
296        port: u8,
297        size_id: u32,
298        object_count: u32,
299        object_arr: *mut vision_object_s_t,
300    ) -> i32;
301    /**
302    Reads up to object_count object descriptors into object_arr.
303
304    This function uses the following values of errno when an error state is
305    reached:
306    ENXIO - The given value is not within the range of V5 ports (1-21), or
307             fewer than object_count number of objects were found.
308    ENODEV - The port cannot be configured as a vision sensor
309    EDOM - size_id is greater than the number of available objects.
310
311    \param port
312           The V5 port number from 1-21
313    \param object_count
314           The number of objects to read
315    \param size_id
316           The object to read from a list roughly ordered by object size
317           (0 is the largest item, 1 is the second largest, etc.)
318    \param signature
319           The signature ID [1-7] for which objects will be returned.
320    \param\[out] object_arr
321                A pointer to copy the objects into
322
323    \return The number of object signatures copied. This number will be less than
324    object_count if there are fewer objects detected by the vision sensor.
325    Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
326    than size_id were found. All objects in object_arr that were not found are
327    given VISION_OBJECT_ERR_SIG as their signature.
328    */
329    pub fn vision_read_by_sig(
330        port: u8,
331        size_id: u32,
332        sig_id: u32,
333        object_count: u32,
334        object_arr: *mut vision_object_s_t,
335    ) -> i32;
336    /**
337    Reads up to object_count object descriptors into object_arr.
338
339    This function uses the following values of errno when an error state is
340    reached:
341    ENXIO - The given value is not within the range of V5 ports (1-21), or
342             fewer than object_count number of objects were found.
343    ENODEV - The port cannot be configured as a vision sensor
344
345    \param port
346           The V5 port number from 1-21
347    \param object_count
348           The number of objects to read
349    \param size_id
350           The object to read from a list roughly ordered by object size
351           (0 is the largest item, 1 is the second largest, etc.)
352    \param color_code
353           The vision_color_code_t for which objects will be returned
354    \param\[out] object_arr
355                A pointer to copy the objects into
356
357    \return The number of object signatures copied. This number will be less than
358    object_count if there are fewer objects detected by the vision sensor.
359    Returns PROS_ERR if the port was invalid, an error occurred, or fewer objects
360    than size_id were found. All objects in object_arr that were not found are
361    given VISION_OBJECT_ERR_SIG as their signature.
362    */
363    pub fn vision_read_by_code(
364        port: u8,
365        size_id: u32,
366        code: vision_color_code_t,
367        object_count: u32,
368        object_arr: *mut vision_object_s_t,
369    ) -> i32;
370    /**
371    Gets the object detection signature with the given id number.
372
373    \param port
374           The V5 port number from 1-21
375    \param signature_id
376           The signature id to read
377
378    \return A vision_signature_s_t containing information about the signature.
379    */
380    pub fn vision_get_signature(port: u8, signature_id: u8) -> vision_signature_s_t;
381    /**
382    Stores the supplied object detection signature onto the vision sensor.
383
384    NOTE: This saves the signature in volatile memory, and the signature will be
385    lost as soon as the sensor is powered down.
386
387    \param port
388           The V5 port number from 1-21
389    \param signature_id
390           The signature id to store into
391    \param\[in] signature_ptr
392               A pointer to the signature to save
393
394    \return 1 if no errors occurred, PROS_ERR otherwise
395    */
396    pub fn vision_set_signature(
397        port: u8,
398        signature_id: u8,
399        signature_ptr: *const vision_signature_s_t,
400    ) -> i32;
401    /**
402    Enables/disables auto white-balancing on the Vision Sensor.
403
404    This function uses the following values of errno when an error state is
405    reached:
406    ENXIO - The given value is not within the range of V5 ports (1-21).
407    ENODEV - The port cannot be configured as a vision sensor
408    EINVAL - enable was not 0 or 1
409
410    \param port
411                The V5 port number from 1-21
412    \param enabled
413                Pass 0 to disable, 1 to enable
414
415    \return 1 if the operation was successful or PROS_ERR if the operation
416    failed, setting errno.
417    */
418    pub fn vision_set_auto_white_balance(port: u8, enable: u8) -> i32;
419    /**
420    Sets the exposure parameter of the Vision Sensor. See
421    <https://pros.cs.purdue.edu/v5/tutorials/topical/vision.html#exposure-setting>
422    for more details.
423
424    This function uses the following values of errno when an error state is
425    reached:
426    ENXIO - The given value is not within the range of V5 ports (1-21).
427    ENODEV - The port cannot be configured as a vision sensor
428
429    \param port
430           The V5 port number from 1-21
431    \param percent
432           The new exposure setting from \[0,150]
433
434    \return 1 if the operation was successful or PROS_ERR if the operation
435    failed, setting errno.
436    */
437    pub fn vision_set_exposure(port: u8, exposure: u8) -> i32;
438    /**
439    Sets the vision sensor LED color, overriding the automatic behavior.
440
441    This function uses the following values of errno when an error state is
442    reached:
443    ENXIO - The given value is not within the range of V5 ports (1-21).
444    ENODEV - The port cannot be configured as a vision sensor
445
446    \param port
447           The V5 port number from 1-21
448    \param rgb
449           An RGB code to set the LED to
450
451    \return 1 if the operation was successful or PROS_ERR if the operation
452    failed, setting errno.
453    */
454    pub fn vision_set_led(port: u8, rgb: i32) -> i32;
455    /**
456    Sets the white balance parameter of the Vision Sensor.
457
458    This function uses the following values of errno when an error state is
459    reached:
460    ENXIO - The given value is not within the range of V5 ports (1-21).
461    ENODEV - The port cannot be configured as a vision sensor
462
463    \param port
464                The V5 port number from 1-21
465    \param rgb
466           The new RGB white balance setting of the sensor
467
468    \return 1 if the operation was successful or PROS_ERR if the operation
469    failed, setting errno.
470    */
471    pub fn vision_set_white_balance(port: u8, rgb: i32) -> i32;
472    /**
473    Sets the (0,0) coordinate for the Field of View.
474
475    This will affect the coordinates returned for each request for a
476    vision_object_s_t from the sensor, so it is recommended that this function
477    only be used to configure the sensor at the beginning of its use.
478
479    This function uses the following values of errno when an error state is
480    reached:
481    ENXIO - The given value is not within the range of V5 ports (1-21).
482    ENODEV - The port cannot be configured as a vision sensor
483
484    \param port
485                The V5 port number from 1-21
486    \param zero_point
487           One of vision_zero_e_t to set the (0,0) coordinate for the FOV
488
489    \return 1 if the operation was successful or PROS_ERR if the operation
490    failed, setting errno.
491    */
492    pub fn vision_set_zero_point(port: u8, zero_point: vision_zero_e_t) -> i32;
493    /**
494    Sets the Wi-Fi mode of the Vision sensor
495
496    This functions uses the following values of errno when an error state is
497    reached:
498    ENXIO - The given port is not within the range of V5 ports (1-21)
499    EACCESS - Another resource is currently trying to access the port
500
501    \param port
502           The V5 port number from 1-21
503    \param enable
504           Disable Wi-Fi on the Vision sensor if 0, enable otherwise (e.g. 1)
505
506    \return 1 if the operation was successful or PROS_ERR if the operation
507    failed, setting errno.
508    */
509    pub fn vision_set_wifi_mode(port: u8, mode: u8) -> i32;
510}