Skip to content

Using the Sensor Struct

Jeffrey Shen edited this page Aug 5, 2017 · 10 revisions

This library provides the sensor structure for VEX sensors which makes monitoring their speed and values much more simple.

Setup

Define and initialize a sensor within your code like the example below:

sensor sensor_name;
initializeSensor(&sensor_name, scaling_factor, sensor_port [, &PID]); //PID is optional.

The ampersand is necessary.

  • sensor_port is what port that this sensor is bound to (i.e. in1, dgtl1, I2C_1).
  • scaling_factor is a float that will be multiplied by the raw sensor value, so if you want to convert the raw value into something more meaningful (i.e. degrees), do that here.
  • PID is an optional parameter which is a pointer to a struct of type pid that defines the constants for a PID control. Include this if you plan on using this sensor within a PID control. If it is not included, all constants will be initialized as 0. Read more about a PID control here.

Usage

Within any loop where you want to access this sensor's value, call updateSensorValue(&sensor_name). This structure will not work without this function call. Then, you can access the speed and value of the sensor using sensor_name.speedand sensor_name.val, respectively. Value is equal to the raw sensor value times the scaling factor, and speed is in terms of units (as defined by the scaling factor) per second.