This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompCodeDrive.c
151 lines (131 loc) · 5.59 KB
/
compCodeDrive.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, in1, gyro, sensorGyro)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_3, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port2, leftD, tmotorVex393HighSpeed_MC29, PIDControl, encoderPort, I2C_1)
#pragma config(Motor, port3, rightD, tmotorVex393HighSpeed_MC29, PIDControl, reversed, encoderPort, I2C_2)
#pragma config(Motor, port4, lift, tmotorVex393_MC29, openLoop, reversed, encoderPort, I2C_3)
/*---------------------------------------------------------------------------*/
/* */
/* Description: Competition template for VEX EDR */
/* */
/*---------------------------------------------------------------------------*/
// This code is for the VEX cortex platform
#pragma platform(VEX2)
// Select Download method as "competition"
#pragma competitionControl(Competition)
//Main competition background code...do not modify!
#include "Vex_Competition_Includes.c"
/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the cortex has been powered on and */
/* not every time that the robot is disabled. */
/*---------------------------------------------------------------------------*/
int threshold=15;
void liftPID(float target){
float kpLift=0.5;
float kiLift=0;
float kdLift=2;
float errorPLift;
float errorILift;
float errorDLift;
float proportionalLift;
float integralLift;
float integralLimitLift=50;
float integralActiveZoneLift=100;
float derivativeLift;
float powerLift;
clearTimer(T2);
while(time1(T2)<1000){
errorPLift=target-nMotorEncoder[lift];
proportionalLift=errorPLift*kpLift;
//INTEGRAL
if(abs(errorPLift)<integralActiveZoneLift){
errorILift+=errorPLift;
}
else{
errorILift=0;
}
integralLift=errorILift*kiLift;
if(integralLift>integralLimitLift){
integralLift=integralLimitLift;
}
//DERIVATIVE
derivativeLift=(errorPLift-errorDLift)*kdLift;
errorDLift=errorPLift;
powerLift=proportionalLift+integralLift+derivativeLift;
motor[lift]=powerLift;
}
}
void pre_auton()
{
// Set bStopTasksBetweenModes to false if you want to keep user created tasks
// running between Autonomous and Driver controlled modes. You will need to
// manage all user created tasks if set to false.
bStopTasksBetweenModes = true;
// Set bDisplayCompetitionStatusOnLcd to false if you don't want the LCD
// used by the competition include file, for example, you might want
// to display your team name on the LCD in this function.
// bDisplayCompetitionStatusOnLcd = false;
// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
task autonomous()
{
// ..........................................................................
// Insert user code here.
// ..........................................................................
// Remove this function call once you have "real" code.
AutonomousCodePlaceholderForTesting();
}
/*---------------------------------------------------------------------------*/
/* */
/* User Control Task */
/* */
/* This task is used to control your robot during the user control phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/
task usercontrol()
{
while(1){
if(abs(vexRT[Ch3])>threshold){
motor[leftD]=vexRT[Ch3];
}
else{
motor[leftD]=0;
}
if(abs(vexRT[Ch2])>threshold){
motor[rightD]=vexRT[Ch2];
}
else{
motor[rightD]=0;
}
if(vexRT[Btn6U]==1){
motor[lift]=127;
//liftPID(-40);
}
else if(vexRT[Btn6D]==1){
motor[lift]=-127;
//liftPID(630);
}
else{
motor[lift]=0;
}
}
}