In: Computer Science
#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2, rightEncoder, sensorQuadEncoder)
#pragma config(Sensor, dgtl7, leftEncoder, sensorQuadEncoder)
#pragma config(Motor, servo_10, rightServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl10, None)
#pragma config(Motor, motor_11, leftServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl11, None)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
void turnLeftDeg(int degrees, int power)
{
//Reset encoders
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
//Determine tickGoal
int tickGoal = (23 * degrees) / 10;
//Start the motors in a left point turn.
motor[leftServo] = -1 * power;
motor[rightServo] = power;
//Since the wheels may go at slightly different speeds due to manufacturing tolerances, etc.,
//we need to test both encoders and control both motors separately. This may result in one motor
//going for longer than another but it will ultimately result in a much more accurate turn.
while(SensorValue[rightEncoder] < tickGoal || SensorValue[leftEncoder] > -1 * tickGoal) {
if(SensorValue[rightEncoder] > tickGoal) {motor[rightServo] = 0;}
if(SensorValue[leftEncoder] < -1 * tickGoal) {motor[leftServo] = 0;}
}
//Make sure both motors stop at the end of the turn.
motor[leftServo] = 0;
motor[rightServo] = 0;
}
void turnRightDeg(int degrees, int power)
{
//Reset encoders
SensorValue[leftEncoder] = 0;
SensorValue[rightEncoder] = 0;
//Determine tickGoal
int tickGoal = (23 * degrees) / 10;
//Start the motors in a left point turn.
motor[leftServo] = power;
motor[rightServo] = -1 * power;
//Since the wheels may go at slightly different speeds due to manufacturing tolerances, etc.,
//we need to test both encoders and control both motors separately. This may result in one motor
//going for longer than another but it will ultimately result in a much more accurate turn.
while(SensorValue[leftEncoder] < tickGoal || SensorValue[rightEncoder] > -1 * tickGoal) {
if(SensorValue[leftEncoder] > tickGoal) {motor[leftServo] = 0;}
if(SensorValue[rightEncoder] < -1 * tickGoal) {motor[rightServo] = 0;}
}
//Make sure both motors stop at the end of the turn.
motor[leftServo] = 0;
motor[rightServo] = 0;
}
task main()
{
//Turn right 90 degrees with power 30
turnRightDeg(90,30);
//Wait a bit so the robot's momentum does not effect the next turn
wait1Msec(500);
turnLeftDeg(50,40);
wait1Msec(500);
turnRightDeg(160,20);
wait1Msec(500);
turnLeftDeg(200,30);
}