Lesson 17 - Sensor Usage & Commands
NOTE: As more sensors are released, this page may not encompass all types of sensors and how to use them. If you cannot find what you need, the PROS API (documentation on how to use PROS) should always have what you need.
Sensors are an integral part of your robot, as they allow you to do things that simple motors cannot. This lesson will go over the most common sensors used in VEX, and how to use them when programming.
Encoders
Encoders are used to determine how many times an axle has been spun. Encoders can be found built into VEX motors, and using them with PROS is simple.
To get encoder values, you can use this syntax:
double left_motor_encoder = left_motor.get_position();
To tare (reset) the encoder values, use this syntax:
left_motor.tare_position();
IMPORTANT NOTE: Depending on which cartridges you use with your motors, the encoder values will be different compared to the actual RPM of the motor.
IMUs
IMU sensors stand for Inertial Measurement Units, and they can measure various movements of your robot. To set up an IMU, you need the name of the IMU sensor as well as the port it is plugged into. Here is an example:
To calibrate your sensor, you can use this syntax:
imu.reset();
NOTE: The IMU sensor takes ~2 seconds to calibrate. It is important that the IMU fully calibrates at the start of each autonomous period to keep consistency.
There are multiple different ways to get the IMU’s values, but the most common are get_rotation and get_heading. Using rotation values will return the number of degrees spun on the Z-axis, while heading values will return the degrees spun on the X-axis. To reset the values, you can either use tare to reset both at once, or tare_heading and tare_rotation to reset the values individually.
Here is an example using all of the common IMU functions:
pros::Imu imu(19);
imu.reset();
float Heading = imu.get_heading();
float Rotation = imu.get_rotation();
imu.tare();
NOTE: When mounting your IMU, make sure to have it as flat as it can be and away from any heavy vibrations in your robot. The best place to mount an IMU would be on a chassis bar under your robot. Make sure to use rubber links when mounting to reduce vibrations.