ROBOTC

Using the MATRIX Controller - Servos

From ROBOTC API Guide
Jump to: navigation, search
1) Using a servo with the MATRIX controller is a fairly simple process. First, make sure the servo is set up appropriately in the Motors and Sensor Setup window; in this example, servo port 1 is set up for a ‘Standard Servo’ type named ‘wristServo’.
MATRIX Servo Move Setup.png
2) Servos work differently than motors. Instead of giving a servo a power and a direction, servos are given a location (when using the MATRIX controller with ROBOTC, these locations range from 0 to 255). Once a servo is given a position, it will need time to move to that position; once it reaches the target position, it will ‘hold’ that position until it is told to move to a different position.
3) In this example, the servo is going to move to 3 different locations (64, 192, and 128). 3 seconds will be given to complete each change in position. In order to assign a servo a position, the ‘servo[]’ command will be used. This command can be found under the ‘Servo’ section of the Function Library.
MATRIX Servo Move Overview.png
MATRIX Servo Library.png
4) The first position the servo will be moved to is 64. The ‘servo[]’ command is used to define which servo is being controlled (in this example, ‘wristServo’ and the value assigned to it is the position the servo must move to (in this example, 64).
MATRIX Servo Position1.png
5) After a servo’s position is set, the program must give the servo enough time to reach its target before a new position is assigned. This can be done with the ‘wait1MSec’ command. In this example, a 3 second wait (3000 milliseconds) will be enough time for the servo to reach its target.
MATRIX Move Forward Wait3.png
6) To assign a new position to the servo, a new ‘servo[]’ command must be used. Remember, each movement will also need its own separate 3 second wait (using the wait1MSec() command) to allow the servo to move to the target location.
MATRIX Servo Position2.png
7) The final movement to position 192 is similar to the previous servo movements, with the value of 192 being assigned to the servo and a 3 second wait to give the servo time to move to the desired position. Unlike the motors, a value of zero should not be assigned to the servos to stop them. A zero value assigned to a servo will instead move the servo to position 0 and cause the servo to ‘hold’ that position until a new value is assigned to it.
MATRIX Servo Position3.png
Let’s take a look at the finished program.This program will move the ‘wristServo’ on the MATRIX controller’s servo port 1 to position 64, 192, and 128, with a three seconds allotted for each movement.
#pragma config(Hubs,  S1, MatrxRbtcs, none,     none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_Matrix_S1_1, motorD,        tmotorMatrix, openLoop)
#pragma config(Motor,  mtr_Matrix_S1_2, motorE,        tmotorMatrix, openLoop)
#pragma config(Motor,  mtr_Matrix_S1_3, motorF,        tmotorMatrix, openLoop)
#pragma config(Motor,  mtr_Matrix_S1_4, motorG,        tmotorMatrix, openLoop)
#pragma config(Servo,  srvo_Matrix_S1_1, wristServo,           tServoStandard)
#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()
{
	//Move the servo to position 64
	//Wait 3 seconds for the servo to move
	servo[wristServo] = 64;
	wait1Msec(3000);
 
	//Move the servo to position 192
	//Wait 3 seconds for the servo to move
	servo[wristServo] = 192;
	wait1Msec(3000);
 
	//Move the servo to position 128
	//Wait 3 seconds for the servo to move
	servo[wristServo] = 128;
	wait1Msec(3000);
}