FSHS Robotics

Back to C Course

FSHS Robotics C Course

2. Basics of RobotC

2.1 RobotC Introduction

Now that we've learnt a tiny bit of C, let's quickly look at RobotC. Everything in the table from 1.2 is the same, except:

Code RobotC Code Comment
#include <stdio.h> - Not needed! Instead, some automatically generated code replaces this.
int main() { ... } task main() { ... } Instead of returning an int, we call them 'tasks'.
return 0 - Tasks do not require return values.

2.2 Moving motors in RobotC

Let's look at a program that runs Motor A for one second, then stops.

#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main() {
    motor[motorL] = 100; // SETS the motor speed
    wait1Msec(1000); // Keeps running at 100 speed for 1 second
    motor[motorL] = 0; // sets speed to 0
}

Key things to note:

  • C reads top to bottom. So, the motor is set to 100, nothing happens for 1 second, then it stops.
  • The = makes a change to a particular variable. Here, it's changing the motor speed variable.
  • The 1000 in wait1Msec(1000) is an argument to the wait function. We can change this number if we want it to be longer or shorter.
  • The motor speed is set. It isn't 'pinged' - from the point you call motor[motorL] = 100, it will run at 100 speed forever until it is stopped or the program terminates.

2.3 Exercises

Given the following starting code, copy the code into a new file, call it simplemotor.c and save it in your Robotics folder.

#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorB, motorR, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main() {
    motor[motorL] = 30;  // SETS the left motor speed
    motor[motorR] = -30; // SETS the right motor speed to go backwards
    wait1Msec(1000);     // Keeps running for 1 second
    motor[motorL] = 0;   // sets speed to 0
    motor[motorR] = 0;   // sets speed to 0
}
  1. What do you think the code does? If it was hooked to a robot with two wheels, what would the robot do?
  2. Change the code so that the robot goes forward for 1 second, spin clockwise for 3 seconds, moves backwards for 1 second and then stops.

Ask for the code to be uploaded to a demo robot and see if it works!

© Fort Street High School Robotics