forked from AlexMaxseiner/FTC-Team4592-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefenceauto.c
114 lines (96 loc) · 3.39 KB
/
defenceauto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma config(Hubs, S3, HTMotor, HTMotor, HTServo, none)
#pragma config(Sensor, S1, irSeek, sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S2, light, sensorLightActive)
#pragma config(Sensor, S4, liftSafetyTouch, sensorTouch)
#pragma config(Motor, mtr_S3_C1_1, leftDrive, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S3_C1_2, rightDrive, tmotorTetrix, openLoop, encoder)
#pragma config(Motor, mtr_S3_C2_1, lift, tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S3_C2_2, slide, tmotorTetrix, openLoop, encoder)
#pragma config(Servo, srvo_S3_C3_1, claw, tServoStandard)
#pragma config(Servo, srvo_S3_C3_2, clawRelease, tServoStandard)
#pragma config(Servo, srvo_S3_C3_3, servo3, tServoNone)
#pragma config(Servo, srvo_S3_C3_4, servo4, tServoNone)
#pragma config(Servo, srvo_S3_C3_5, leftgrab, tServoNone)
#pragma config(Servo, srvo_S3_C3_6, rightgrab, tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
#include "globalVariables.h"
#include "globalFunctions.c"
void initializeRobot();
void stopDrive();
void forward(float distance);
task main()
{
initializeRobot();
forward(30);
wait1Msec(750);
motor[leftDrive] = 30;
motor[rightDrive] = 0;
wait1Msec(750);
forward(10);
stopDrive();
while (true) {}
}
void saftey(){
}
void initializeRobot()
{
ClearTimer(T1);
ClearTimer(T2);
ClearTimer(T3);
ClearTimer(T4);
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
batteryTest();
return;
}
void stopDrive()
{
motor[leftDrive] = 0;
motor[rightDrive] = 0;
}
void forward(float distance) {//forward function to pass in a length in inches and then it goes that for
const float CHANGE = 2;
float totalTraveled = 0;
float encoderTarget = (169.92*distance) - 55.875; //calculate the encoder target
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
float leftEncoder;
float rightEncoder;
float leftPower = 50;
float rightPower = 50;
while(abs(totalTraveled) < abs(encoderTarget))
{
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(leftEncoder > rightEncoder)//changes based on which one has traveled farther
{
leftPower -= CHANGE;
rightPower += CHANGE;
}
else if(leftEncoder < rightEncoder)//same thing
{
leftPower += CHANGE;
rightPower -= CHANGE;
}
motor[leftDrive] = leftPower;
motor[rightDrive] = rightPower;
totalTraveled += (leftEncoder + rightEncoder)/ 2.0;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
ClearTimer(T1);
while(time1[T1] < 200) {
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(!((abs(totalTraveled + (leftEncoder + rightEncoder)/ 2.0)/* average*/)/*adds the distance traveled*/ < abs(encoderTarget)))
{//while all that is not less than the encoder target
totalTraveled += (nMotorEncoder[leftDrive] + nMotorEncoder[rightDrive])/ 2.0;// add the aveage of the encoders
break;//exit
}
}
}
motor[leftDrive] = 0;
motor[rightDrive] = 0;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
}