driveForward(); // Just start driving forward driveForward(12); // Just drive for 12 inches driveForward(12,25); // Drive for 12 inches at 25% powerThe types of function overloading we use are classified as a form of static polymorphism in which a function call is resolved using the 'best match technique', i.e., the function is resolved depending upon the argument list.
task main() { }Next, insert a function to draw a circle.
task main() { drawCircle(100, 50, 20); }Press the F5 key to run your program. Before the program runs, the computer will ask you to save your program and it will suggest a filename. You may change the name of the file as well as its location if you want. Press the F5 key and then press the Start button once it is highlighted.
Did you see the circle?
Probably not. The robot drew the circle and ended the program probably faster than you could see it. Let's make the robot wait a little bit.
task main() { drawCircle(100, 50, 20); wait(2); }Run the program now. You should see the circle. Ok.
task main() { drawCircle(random(100), random(100), random(20)); wait(2); }Run it several times.
The reason is NOT intuitive and is probably just a bug in the RobotC compiler. There is a simple work around: just make sure you pass integers to the drawCircle function.
An integer is a whole number (not a fraction or a number with a decimal). You can declare variables to contain only certain types of numbers (for example: integers). To declare a variable as an integer, we use the int keyword prior to the variable name in its declaration statement (see example below).
task main() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); wait(2); }Fix your program so that it looks like the one above and run it. Feel free to copy-and-paste the code directly into your program. If you use the copy and paste method, make sure you are overwriting the old statements in your program that you no longer need.
void randomCircle() { } task main() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); wait(2); }Now block and move the statements which drew the circle into the newly created function as shown below:
void randomCircle() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); } task main() { wait(2); }Now we just need our program to use the new function we created. So we will add a randomCircle(); within the task main() as shown below:
void randomCircle() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); } task main() { randomCircle(); wait(2); }We can "call" our function as many times as we want as in:
void randomCircle() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); } task main() { randomCircle(); randomCircle(); randomCircle(); randomCircle(); randomCircle(); wait(2); }
void randomCircle() { int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); } task main() { while(true) { randomCircle(); randomCircle(); randomCircle(); randomCircle(); randomCircle(); wait(2); } }Adding the while(true) loop allow the robot to continue executing the functions forever.
Try adding the functions below to your program:
void randomCircle() { // Not specifying any parameters here. All will be created with random numbers. int x = random(100); int y = random(100); int r = random(20); drawCircle(x, y, r); } void randomCircle(int r) { // Here, I am specifying r but not x or y. int x = random(100); int y = random(100); drawCircle(x, y, r); } void randomCircle(int x, int y) { // Here, I am specifying x and y but not r. int r = random(100); drawCircle(x, y, r); } task main() { while(true) { //randomCircle(50, 50); randomCircle(100); wait(0.2); } }It's the same function name: randomCircle(). But there are three different ways it can be referenced -- each with a different combination of parameters. This makes your program more flexible in that it can handle different combinations of functions and parameters.
task main() { randomCircle( 50, 50, 30); randomCircle(150, 120); randomCircle(100); wait(10); }
void randomSquare(int s) { int x = random(100); int y = random(100); drawRect(x, y, x+s, y+s); }Your task main() could use the function as:
task main() { int squareSize=50; while(true) { randomSquare(squareSize); wait(0.2); } }
driveForward(); | Just make the robot start driving forward (for infinity). |
driveForward(12); | Make the robot drive forward for 12 inches. |
driveForward(12, 50); | Make the robot drive forward for 12 inches at 50% power. |
task main() { }Add a basic drive forward function.
void driveForward() { setMotorSync(B, C, 0, 20); } task main() { }Let's add a function for driving a specific distance. In order to implement this, we will need to include a function that lets the robot know how far it has traveled.
#define PI 3.14159 typedef struct genericRobotStruct { // Create a generic structure to store things such as powerLevel, wheelDiameter, gearRatio, etc. int powerLevel; int wheelDiameterMM; int gearRatio; int distanceBetweenWheelsMM; bool criteriaNotMetYet; } robotObj; robotObj robot; void untilDistance(int moveIN) { resetMotorEncoder(B); resetMotorEncoder(C); float targetDegree; targetDegree=(moveIN * 25.4)/(robot.WheelDiameterMM*PI)*360; repeatUntil(abs(getMotorEncoder(B)) > targetDegree) { sleep(5); } } void driveForward() { setMotorSync(B, C, 0, robot.powerLevel); } // Overloaded function void driveForward(int distance) { driveForward(); untilDistance(distance); stopAllMotors(); } // Another Overloaded function void driveForward(int distance, int pwr) { setMotorSync(B, C, 0, pwr); untilDistance(distance); stopAllMotors(); } task main() { robot.wheelDiameterMM = 43.2; robot.distanceBetweenWheelsMM = 130; robot.powerLevel=40; driveForward(12); }If your robot doesn't move exactly the distance you specified, then you may need to increase or decrease the wheel diameter property of the robot object shown in the main task.
Now you should be able to use functions such as:
task main() { robot.wheelDiameterMM = 43.2; robot.distanceBetweenWheelsMM = 130; robot.powerLevel=40; driveForward(12); driveForward(12,100); driveForward(); untilDistance(48); }