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:
//velocity of the motor system
velocity = 127;
//assign the motor system
assignMotorSystem(gadget, port0, port1, port2, port3);
//target value for encoder
int target = 500;
//initialize encoder sensor
//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)
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.
//time in milliseconds to fire the catapult
int timeMS = 500;
//create and assign pneumatics
assignSensor(s0, sensorDigitalOut, dgtl1);
assignSensor(s1, sensorDigitalOut, dgtl2);
assignSensor(s2, sensorDigitalOut, dgtl3);
//link the pneumatics
assignSensorArray(catapult, s0, s1, s2);
//fire the pneumatics for one second
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.