UNIVERSAL_ROBOTC : sensor and sensorArray structs


In the introduction to UNIVERSAL_ROBOTC, we learned about the motor struct in which we can store information about a collection of motors with an example on how to implement the motor struct. We know understand the usefulness of creating UNIVERSAL_ROBOTC because it helps save time and allows us to create complex functions with minimal code writing.

Next we will talk about the sensor struct, along with its big brother the sensorArray struct. The sensor struct allows us to store information about a certain sensor. The struct holds the sensor type as well as the ports the sensor uses (up to three ports). This struct supports all sensor types, including gyros and accelerometers. Great! So now we can refer to a sensor throughout the entire program without having to remember the port or sensor type. Here is an example on how to use the sensor struct:

void main
//velocity of the motor system
velocity = 127;

//assign the motor system
motorSystem gadget;
assignMotorSystem(gadget, port0, port1, port2, port3);

//target value for encoder
int target = 500;

//initialize encoder sensor
sensor encoder;

//assign sensor type and sensor ports
assignSensor(encoder, sensorQuadEncoder, dgtl1, dgtl2);

//continue running the motors till the sensor value has been reached
while(getSensorValue(encoder) <= target)
run(velocity, velocity);

//stop motors

The next struct is the sensorArray struct. This is basically an array of sensor structs (up to 20 sensors). Why would this be helpful? One of the most common uses for this would definitely be pneumatic actuators. You now only have to assign the sensors and the sensor array once to control all of the pneumatic actuators. If you have ever worked with any sensors, especially the pneumatic actuators, you will understand how tedious and repetitive it is to write code for them. The beauty of this is that you still have the ability to control the sensors independently as well. Here is an example on how to use the sensorArray struct.

void main
//time in milliseconds to fire the catapult
int timeMS = 500;

//create and assign pneumatics
sensor s0;
sensor s1;
sensor s2;
assignSensor(s0, sensorDigitalOut, dgtl1);
assignSensor(s1, sensorDigitalOut, dgtl2);
assignSensor(s2, sensorDigitalOut, dgtl3);

//link the pneumatics
sensorArray catapult;
assignSensorArray(catapult, s0, s1, s2);

//fire the pneumatics for one second
extendFor(timeMS, catapult);

Remember that this struct can be used for more than pneumatics. It could be used to get the average of sensor values to get a more accurate reading. UNIVERSAL_ROBOTC does not currently have this function in its library, but it is quite straightforward to create. It will most likely be included on the next UNIVERSAL_ROBOTC update! Stay tuned for more tutorials for UNIVERSAL_ROBOTC. Next up will be how to use the required motor ports and required sensors.

Universal ROBOTC

Universal_ROBOTC┬áis a library for the code editor ROBOTC. It uses a combination of variables, structs and functions to accomplish an “OOP-like” programming experience for ROBOTC programmers. Its intention is to be a library of code that works with any type of VEX Competition robot. It currently supports X-Drives, Tank Drives, and H-Drives. It has been tested in competition successfully and is very reliable. If you have any questions or concerns, just comment away!