-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWorlds7_tbh.c
427 lines (352 loc) · 13.2 KB
/
Worlds7_tbh.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, in7, indexLow, sensorAnalog)
#pragma config(Sensor, in8, powerExpander, sensorAnalog)
#pragma config(Sensor, dgtl1, flywheelEncoder, sensorRotation)
#pragma config(Sensor, dgtl3, indexHigh, sensorTouch)
#pragma config(Sensor, dgtl9, upToSpeed, sensorLEDtoVCC)
#pragma config(Sensor, dgtl10, encoderTest, sensorTouch)
#pragma config(Sensor, dgtl11, tune, sensorTouch)
#pragma config(Sensor, dgtl12, debug, sensorTouch)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, flywheel3, tmotorVex393TurboSpeed_HBridge, openLoop)
#pragma config(Motor, port2, indexer, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port3, flywheel2, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port4, flywheel1, tmotorVex393HighSpeed_MC29, openLoop)
#pragma config(Motor, port5, rightWheel2, tmotorVex393TurboSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port6, leftWheel13, tmotorVex393HighSpeed_MC29, openLoop, encoderPort, I2C_2)
#pragma config(Motor, port7, flywheel4, tmotorVex393HighSpeed_MC29, openLoop, reversed)
#pragma config(Motor, port8, leftWheel2, tmotorVex393TurboSpeed_MC29, openLoop)
#pragma config(Motor, port9, rightWheel13, tmotorVex393TurboSpeed_MC29, openLoop, reversed, encoderPort, I2C_1)
#pragma config(Motor, port10, intake, tmotorVex393HighSpeed_HBridge, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#pragma platform(VEX)
//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(0)
#pragma userControlDuration(60)
#pragma systemFile // eliminates warning for "unreferenced" functions
#include "Vex_Competition_Includes.c" //Main competition background code...do not modify!
bool debugMode = true;
bool debugDrivebaseActive = false;
bool debugFlywheelActive = false;
int flywheelVelocity;
int flywheelVelocityUpdateFrequency = 25;
int flywheelReverseStartThreshold = 10;
int flywheelSlowDownVelocity = 4000;
int flywheelControlUpdateFrequency = flywheelVelocityUpdateFrequency;
int intakeMoveUpTime = 200;
int intakeMoveDownTime = 250;
int intakeShootVelocityThreshold = 50;
int intakeLightThreshold = 2500; //higher is more sensitive
bool intakeLongShot = false;
bool intakeAutonomousIntake;
bool intakeAutonomousIndexer;
bool intakeAutonomousShoot;
int autonomousChoice = -1;
#include "JonLib/Drivebase.h"
#include "JonLib/PID.h"
#include "autonomous.h"
#warning "setLeftWheelSpeed"
void setLeftWheelSpeed ( int speed ) {
motor[leftWheel13] = speed;
motor[leftWheel2] = speed;
}
//Sets the speed of the wheels on the right side of the robot
#warning "setRightWheelSpeed"
void setRightWheelSpeed ( int speed ) {
motor[rightWheel13] = speed;
motor[rightWheel2] = speed;
}
void setFlywheel (int speed) {
motor[flywheel1] = speed;
motor[flywheel2] = speed;
motor[flywheel3] = speed;
motor[flywheel4] = speed;
}
//Logarithmic drivebase control
#warning "logDrive"
void logDrive () {
setWheelSpeed(
abs(vexRT(Ch3))*vexRT(Ch3)/127,
(abs(vexRT(Ch2))*vexRT(Ch2)/127)>100?100:abs(vexRT(Ch2))*vexRT(Ch2)/127);
}
// Update inteval (in mS) for the flywheel control loop
#define FW_LOOP_SPEED 25
// Maximum power we want to send to the flywheel motors
#define FW_MAX_POWER 100
// encoder counts per revolution depending on motor
#define MOTOR_TPR_269 240.448
#define MOTOR_TPR_393R 261.333
#define MOTOR_TPR_393S 392
#define MOTOR_TPR_393T 627.2
#define MOTOR_TPR_QUAD 360.0
// Structure to gather all the flywheel ralated data
typedef struct _fw_controller {
long counter; ///< loop counter used for debug
// encoder tick per revolution
float ticks_per_rev; ///< encoder ticks per revolution
// Encoder
long e_current; ///< current encoder count
long e_last; ///< current encoder count
// velocity measurement
float v_current; ///< current velocity in rpm
long v_time; ///< Time of last velocity calculation
// TBH control algorithm variables
long target; ///< target velocity
long current; ///< current velocity
long last; ///< last velocity
float error; ///< error between actual and target velocities
float last_error; ///< error last time update called
float gain; ///< gain
float drive; ///< final drive out of TBH (0.0 to 1.0)
float drive_at_zero; ///< drive at last zero crossing
long first_cross; ///< flag indicating first zero crossing
float drive_approx; ///< estimated open loop drive
// final motor drive
long motor_drive; ///< final motor control value
} fw_controller;
// Make the controller global for easy debugging
static fw_controller flywheel;
/*-----------------------------------------------------------------------------*/
/** @brief Set the controller position */
/** @param[in] fw pointer to flywheel controller structure */
/** @param[in] desired velocity */
/** @param[in] predicted_drive estimated open loop motor drive */
/*-----------------------------------------------------------------------------*/
void
FwVelocitySet( fw_controller *fw, int velocity, float predicted_drive )
{
// set target velocity (motor rpm)
fw->target = velocity;
// Set error so zero crossing is correctly detected
fw->error = fw->target - fw->current;
fw->last_error = fw->error;
// Set predicted open loop drive value
fw->drive_approx = predicted_drive;
// Set flag to detect first zero crossing
fw->first_cross = 1;
// clear tbh variable
fw->drive_at_zero = 0;
}
/*-----------------------------------------------------------------------------*/
/** @brief Calculate the current flywheel motor velocity */
/** @param[in] fw pointer to flywheel controller structure */
/*-----------------------------------------------------------------------------*/
void
FwCalculateSpeed( fw_controller *fw )
{
int delta_ms;
int delta_enc;
// Get current encoder value
fw->e_current = SensorValue[flywheelEncoder];
// This is just used so we don't need to know how often we are called
// how many mS since we were last here
delta_ms = nSysTime - fw->v_time;
fw->v_time = nSysTime;
// Change in encoder count
delta_enc = (fw->e_current - fw->e_last);
// save last position
fw->e_last = fw->e_current;
// Calculate velocity in rpm
fw->v_current = (1000.0 / delta_ms) * delta_enc * 60.0 / fw->ticks_per_rev;
}
/*-----------------------------------------------------------------------------*/
/** @brief Update the velocity tbh controller variables */
/** @param[in] fw pointer to flywheel controller structure */
/*-----------------------------------------------------------------------------*/
void
FwControlUpdateVelocityTbh( fw_controller *fw )
{
// calculate error in velocity
// target is desired velocity
// current is measured velocity
fw->error = fw->target - fw->current;
// Use Kp as gain
fw->drive = fw->drive + (fw->error * fw->gain);
// Clip - we are only going forwards
if( fw->drive > 1 )
fw->drive = 1;
if( fw->drive < 0 )
fw->drive = 0;
// Check for zero crossing
if( sgn(fw->error) != sgn(fw->last_error) ) {
// First zero crossing after a new set velocity command
if( fw->first_cross ) {
// Set drive to the open loop approximation
fw->drive = fw->drive_approx;
fw->first_cross = 0;
}
else
fw->drive = 0.5 * ( fw->drive + fw->drive_at_zero );
// Save this drive value in the "tbh" variable
fw->drive_at_zero = fw->drive;
}
// Save last error
fw->last_error = fw->error;
}
/*-----------------------------------------------------------------------------*/
/** @brief Task to control the velocity of the flywheel */
/*-----------------------------------------------------------------------------*/
task
FwControlTask()
{
fw_controller *fw = &flywheel;
// Set the gain
fw->gain = 0.05;
// We are using Speed geared motors
// Set the encoder ticks per revolution
fw->ticks_per_rev = MOTOR_TPR_393S;
while(1)
{
// debug counter
fw->counter++;
// Calculate velocity
FwCalculateSpeed( fw );
// Set current speed for the tbh calculation code
fw->current = fw->v_current;
// Do the velocity TBH calculations
FwControlUpdateVelocityTbh( fw ) ;
// Scale drive into the range the motors need
fw->motor_drive = (fw->drive * FW_MAX_POWER) + 0.5;
// Final Limit of motor values - don't really need this
if( fw->motor_drive > 127 ) fw->motor_drive = 127;
if( fw->motor_drive < -127 ) fw->motor_drive = -127;
// and finally set the motor control value
setFlywheel( fw->motor_drive );
// Run at somewhere between 20 and 50mS
wait1Msec( FW_LOOP_SPEED );
}
}
#warning "stopFlywheel"
void stopFlywheel () {
stopTask(FWControlTask);
setFlywheel(0);
}
#warning "intakeControl"
task intakeControl () {
while(true) {
motor[intake] = ((vexRT(Btn5U)||intakeAutonomousIntake)-vexRT(Btn5D))*127;
//Move ball from high limit switch to low limit switch
if(vexRT(Btn6D) && SensorValue[indexHigh]) {
motor[indexer] = -127;
delay(intakeMoveDownTime);
}
//Shooting control
if (vexRT(Btn6U) || intakeAutonomousShoot) {
//if(intakeLongShot?(abs(flywheel.target-flywheel.current)<1):true) {
if(time1[T1]>400 || !intakeLongShot) {
writeDebugStreamLine("%d", flywheelVelocity);
motor[indexer] = 127;
wait1Msec(130);
clearTimer(T1);
}
else {
motor[indexer] = (SensorValue[indexHigh])?-5:80;
}
}
//Move ball down even if there is a sensor we want
else if (vexRT(Btn5D))
motor[indexer] = -127;
//Stop ball if ball is at a sensor
else if(SensorValue[indexLow]<intakeLightThreshold && !SensorValue[indexHigh]) {
motor[indexer] = 70;
clearTimer(T2);
while(time1[T2] < intakeMoveUpTime && !SensorValue[indexHigh]) { delay(20); }
motor[indexer] = 0;
}
else
motor[indexer] = 0;
delay(25);
}
}
task reverseFlywheel () {
while(true) {
if(vexRT(Btn7L) && flywheelVelocity > flywheelReverseStartThreshold) {
stopFlywheel();
clearLCDLine(1);
while(flywheelVelocity>0) {
setFlywheel(flywheelVelocity>flywheelSlowDownVelocity?0:-pow(abs((flywheelVelocity/1000)-flywheelSlowDownVelocity/1000),1.3));
clearLCDLine(0);
displayLCDNumber(0,0,flywheelVelocity);
displayLCDNumber(0,10,motor[flywheel1]);
delay(25);
}
}
else {
while(vexRT(Btn7L)) {
stopFlywheel();
setFlywheel(-127);
delay(25);
}
if(flywheelVelocity < 0 && !debugFlywheelActive) {
setFlywheel(0);
}
}
delay(25);
}
}
void startFlywheel (int velocity, float predicted) {
startTask(FwControlTask);
FwVelocitySet( &flywheel, velocity, predicted );
}
#warning "init"
void init() {
playTone(700,10);
clearDebugStream();
intakeAutonomousIndexer = false;
intakeAutonomousIntake = false;
intakeAutonomousShoot = false;
//Startup modes
if(!debugMode)
debugMode = (bool) SensorValue[debug];
intakeAutonomousIndexer = false;
intakeAutonomousIntake = false;
intakeAutonomousIndexer = false;
startTask(intakeControl);
startTask(reverseFlywheel);
}
void pre_auton() {
init();
bStopTasksBetweenModes = true;
}
//#include "autonomousPrograms.h"
task autonomous() {
/*switch (autonomousChoice) {
case 0: fourBalls(); break;
case 1: rSCurveAuto(); break;
case 2: rAngleShotAuto(); break;
case 3: rFourCross(); break;
case 4: lSCurveAuto(); break;
case 5: lAngleShotAuto(); break;
case 6: lFourCross(); break;
}
*/
}
task usercontrol() {
init();
while (true) {
if(!debugDrivebaseActive)
logDrive();
if(vexRT(Btn8U)){
startFlywheel(310, 0.0 );
intakeLongShot = false;
while(vexRT(Btn8U)) { delay(10); }
}
else if(vexRT(Btn8R)) {
startFlywheel( 370, 0.0 );
intakeLongShot = false;
while(vexRT(Btn8R)) { delay(10); }
}
else if(vexRT(Btn8L)) {
startFlywheel( 420, 0.0 );
intakeLongShot = true;
while(vexRT(Btn8L)) { delay(10); }
}
// for debugging - consider removing
else if(vexRT(Btn7U))
setFlywheel(60);
else if(vexRT(Btn8D))
stopFlywheel();
}
}