ROBOTC

Using the MATRIX Controller - Motors

From ROBOTC API Guide
Jump to: navigation, search
1) Controlling a robot's movement with the MATRIX controller is an easy process with ROBOTC. First, make sure the motors are set up appropriately in the Motors and Sensor Setup window; in this example, motor port 1 is named named ‘frontLeftDrive’, port 2 is named 'frontRightDrive', port 3 is named 'rearLeftDrive', and port 4 is named 'rearRightDrive'. Note that the left motors ('frontLeftDrive' and 'rearLeftDrive') are reversed to ensure that positive motor values will turn them in the correct direction.
MATRIX Move Forward Setup.png
2) Now that the MATRIX Controller is set up in ROBOTC, the robot can be programmed to move forward for a specific amount of time. In this example, the robot will first wait for 2 seconds, move forward for 3 seconds, then stop before the program ends.
MATRIX Move Forward Overview.png
3) ROBOTC executes programs from the top of the code to the bottom of the code, left to right (similar to reading a book). Because of this, our first command should be a 2000 millisecond ‘wait1MSec’ command.
MATRIX Move Forward Wait2.png
4)The wait1MSec command tells the program to halt at the specified line of code for a specific amount of time. Remember that the Function Library on the left side of ROBOTC (next to the code section of the IDE) can be used to quickly reference and drag-and-drop different commands. The wait1MSec command can be found under the ‘Timing’ section of the Function library.
MATRIX Move Forward Library.png
5) Next, all of the motors will need to be turned on in the correct direction. Assuming that the motors are setup correctly in the Motors and Sensors Setup window, a positive value should move the motors in the forward direction. In this example, the half-speed forward value of '50' is used for each motor. Note that all of the motor power levels should be set to the same value to move the robot straight forward.
MATRIX Move Forward Halfspeed.png
6) The next line of code is another ‘wait1MSec’ command which will tell the robot how long it should move forward for. Since the robot must move forward for 3 seconds, a 3000 millisecond wait command is used.
MATRIX Move Forward Wait3.png
7) Once the robot has moved for the desired amount of time, the motors must be stopped in order to prevent the robot from moving beyond its target. This is done by assigning a motor power level of zero to all of the drive motors.
MATRIX Move Forward Stop.png
Let's take a look at the finished program. This program will wait for 2 seconds, move the robot forward at half speed for 3 seconds, then stop the robot.
#pragma config(Hubs,  S1, MatrxRbtcs, none,     none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_Matrix_S1_1, frontLeftDrive, tmotorMatrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_Matrix_S1_2, frontRightDrive, tmotorMatrix, PIDControl, encoder)
#pragma config(Motor,  mtr_Matrix_S1_3, rearLeftDrive, tmotorMatrix, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_Matrix_S1_4, rearRightDrive, tmotorMatrix, PIDControl, encoder)
#pragma config(Servo,  srvo_Matrix_S1_1, servo1,               tServoNone)
#pragma config(Servo,  srvo_Matrix_S1_2, servo2,               tServoNone)
#pragma config(Servo,  srvo_Matrix_S1_3, servo3,               tServoNone)
#pragma config(Servo,  srvo_Matrix_S1_4, servo4,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
        //Wait for two seconds
	wait1Msec(2000);
 
	//Move forward for three seconds
	motor[frontLeftDrive]  = 50;
	motor[frontRightDrive] = 50;
	motor[rearLeftDrive]   = 50;
	motor[rearRightDrive]  = 50;
 
	wait1Msec(3000);
 
	//Stop
	motor[frontLeftDrive]  = 0;
	motor[frontRightDrive] = 0;
	motor[rearLeftDrive]   = 0;
	motor[rearRightDrive]  = 0;
}