UNIVERSAL_ROBOTC : sensor and sensorArray structs

github

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
halt(gadget);
}

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.

Advertisements

UNIVERSAL_ROBOTC : Intro

github

Universal_ROBOTC was developed in order to make programming in the RobotC environment less complicated and straightforward for beginner and expert programmers alike. The cool part about the Universal ROBOTC library is that it gives the illusion of using classes. Why is this interesting? The C language does not support classes. So how is this possible? In RobotC, there is a data type known as a struct. It is basically a combination of variables. To understand this think of it like this: The data type is a dog. A dog has a head, four legs, a body and a tail. The different parts of the body is what makes up a dog, but these parts can be seen as different from eachother. That is how a struct works. A class is similar except that our dog could do actions such as fetch, run and eat. What is cool about these data types are that they can be user defined. Therefore you can have multiple instances of each. Remember C does not support classes. So we have a very lame dog that can only store data, not very helpful since we are trying to program a robot to do something! The answer is quite simple, just create a user defined library that relies on these struct structures, and boom you have yourself a fake class. However, it truly isn’t a class nor is it real OOP programming, it’s just simulated and seems that way.

Now that we have a rudimentary understanding on how the Universal_ROBOTC library works, let’s run through an example. Let’s say we want to create just a simple program to run 4 motors for five seconds. Traditionally we were forced to do this:

void main()
{
//motor speed
int velocity = 0;

//time in ms to run motors
int timeMS = 0;

//run motors at full speed forward
velocity = 127;
motor[port0] = velocity;
motor[port1] = velocity;
motor[port2] = velocity;
motor[port3] = velocity;

//run for 5 seconds
timeMS = 5000;
wait1Msec(timeMS);

//stop motors
velocity = 0;
motor[port0] = velocity;
motor[port1] = velocity;
motor[port2] = velocity;
motor[port3] = velocity;
}

Of course you could create a nice little function for recurrences of this scenario, however you would only be limited to using four motors and those motors would have to always be the same motors. The solution using the Universal_ROBOTC library is less messy. Here is an example:

int main()
{
//motor speed
int velocity = 0;

//time in ms to run motors
int timeMS = 0;

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

//run gadget for 5 seconds
runFor(velocity, timeMS, gadget);
}

The advantage is that the program stores the motors you’re using and has functions to do things with the motors and you are not limited to just four motors. You can have no motors or you can have up to ten motors. I am not going to go into detail about how this is possible, but function overloading is definitely used along with a for loop.

Stay tuned for more tips and tutorial on how to use this library! The next one will be on using sensors with Universal_ROBOTC library.

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!