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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s