-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntropyDrive.cpp
More file actions
530 lines (426 loc) · 10.7 KB
/
EntropyDrive.cpp
File metadata and controls
530 lines (426 loc) · 10.7 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include "IODefinitions.h"
#include "WPILib.h"
#include "EntropySubsystemTemplate.h"
#include "EntropyDrive.h"
#include "EntropyDriveTable.h"
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#define DEADZONE 1
double DEAD_ZONE_MAX = .15;
float CompMoveValuePlus=0.99;
float CompMoveValueMinus=0.60;
float CompRotateValuePlus=0.99;
float CompRotateValueMinus=0.99;
double dampValue=0.05;
bool EntropyDrive::Initialize ()
{
MotorDriveLeft1 = new CANJaguar(IODefinitions::MOTOR_DRIVE_LEFT_1);
MotorDriveLeft2 = new CANJaguar(IODefinitions::MOTOR_DRIVE_LEFT_2);
MotorDriveRight1 = new CANJaguar(IODefinitions::MOTOR_DRIVE_RIGHT_1);
MotorDriveRight2 = new CANJaguar(IODefinitions::MOTOR_DRIVE_RIGHT_2);
wpiDrive = new RobotDrive( MotorDriveLeft1,
MotorDriveLeft2,
MotorDriveRight1,
MotorDriveRight2 );
typedef struct
{
float value;
string name;
}valStruct;
valStruct valList[30];
string line;
ifstream file ("EntropyDriveINI.txt", std::ifstream::in);
int index = 0;
while ( getline (file,line) )
{
char *ptr = strtok((char*)(line.data()), (" ="));
while (ptr != NULL && index <= 30)
{
valList[index].name.assign(ptr);
ptr = strtok (NULL, " =");
valList[index].value = atof(ptr);
index++;
}
}
file.close();
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("DEAD_ZONE_MAX")== 0)
{
DEAD_ZONE_MAX = valList[x].value;
break;
}
}
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("CompMoveValuePlus")== 0)
{
CompMoveValuePlus = valList[x].value;
break;
}
}
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("CompMoveValueMinus")== 0)
{
CompMoveValueMinus = valList[x].value;
break;
}
}
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("CompRotateValuePlus")== 0)
{
CompRotateValuePlus = valList[x].value;
break;
}
}
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("CompRotateValueMinus")== 0)
{
CompRotateValueMinus = valList[x].value;
break;
}
}
for(int x = 0;x<30;x++)
{
if (valList[x].name.compare("dampValue")== 0)
{
dampValue = valList[x].value;
break;
}
}
return true;
}
void EntropyDrive::Cleanup ()
{
MotorDriveLeft1->Disable();
MotorDriveLeft2->Disable();
MotorDriveRight1->Disable();
MotorDriveRight2->Disable();
}
bool EntropyDrive::DriveRobot(float MoveValue, float RotateValue){
float LeftMotors = 0;
float RightMotors = 0;
MoveValue = Limit(MoveValue);
RotateValue = Limit(RotateValue);
MoveValue = addDeadZone(MoveValue);
//MoveValue = moveValueDampen(MoveValue);
LeftMotors = left_scale(RotateValue, MoveValue, Rotate);
RightMotors = right_scale(RotateValue, MoveValue, Rotate);
//DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line5, "Drive L: %f", LeftMotors);
//DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line6, "Drive R: %f", RightMotors);
//DriverStationLCD::GetInstance()->UpdateLCD();
//Command motors
wpiDrive->SetLeftRightMotorOutputs( LeftMotors, RightMotors );
return true;
}
bool EntropyDrive::DriveRobotTrig(float MoveValue, float RotateValue){
float LeftMotors = 0;
float RightMotors = 0;
float OutsideWheels = 0;
float InsideWheels = 0;
float Hypot = 0;
float AbsMoveValue = 0;
float AbsRotateValue = 0;
//Normalize Joystick inputs
if (MoveValue >= 0.0)
{
MoveValue=MoveValue/CompMoveValuePlus;
}
else
{
MoveValue=MoveValue/CompMoveValueMinus;
}
if (RotateValue>=0.0)
{
RotateValue=RotateValue/CompRotateValuePlus;
}
else
{
RotateValue=RotateValue/CompRotateValueMinus;
}
MoveValue=Limit(MoveValue);
RotateValue=Limit(RotateValue);
AbsMoveValue=absolutevalue(MoveValue);
AbsRotateValue= absolutevalue(RotateValue);
//Theta = atanf(AbsMoveValue/(AbsRotateValue+0.000001));
//Theta = asinf(0.2);
Hypot = sqrt(AbsMoveValue*AbsMoveValue+AbsRotateValue*AbsRotateValue);
OutsideWheels = AbsMoveValue*(AbsMoveValue/Hypot);
InsideWheels = AbsMoveValue*( 1- (AbsRotateValue/Hypot));
//Scale Motor inputs
if (RotateValue<=0.0)
{
LeftMotors = InsideWheels * MoveValue/AbsMoveValue;
RightMotors = OutsideWheels * MoveValue/AbsMoveValue;
}
else
{
LeftMotors = OutsideWheels * MoveValue/AbsMoveValue;
RightMotors = InsideWheels * MoveValue/AbsMoveValue;
}
//Command motors
wpiDrive->SetLeftRightMotorOutputs( -1*LeftMotors,-1*RightMotors );
return true;
}
/* search left drive table using binary search */
/* Input: x_value (rotate)*/
/* y_value (move -forward/backward)*/
/* slow_mo if true, scale output - not being done */
/* return: left scale_value */
float EntropyDrive::left_scale(float rotateValue, float moveValue, DriveMode mode)
{
int x_index = 0;
int y_index = 0;
float temp_drive = 0;
int x_idx = 0;
float absRotate = rotateValue;
if(mode == Radius)
{
absRotate = fabs(rotateValue);
}
get_index(x_index, y_index, moveValue, absRotate, mode);
if(mode == Rotate)
{
x_idx = x_index;
}
else
{
if(rotateValue < 0)
{
x_idx = 32-x_index;
}
else
{
x_idx = x_index;
}
}
temp_drive = left_fast_njxy[y_index][x_idx];
return temp_drive;
}
// Code is replicated from RobotDrive class
float EntropyDrive::Limit(float num)
{
if (num > 1.0)
{
return 1.0;
}
if (num < -1.0)
{
return -1.0;
}
return num;
}
/* search right drive table using binary search if axis index tables */
/* Input: x_value (rotate)*/
/* y_value (move -forward/backward)*/
/* slow_mo if true, scale output - not being done */
/* return: left scale_value */
float EntropyDrive::right_scale(float rotateValue, float moveValue, DriveMode mode)
{
int x_index = 0;
int y_index = 0;
float temp_drive = 0;
int x_idx = 0;
float absRotate = rotateValue;
if(mode == Radius)
{
absRotate = fabs(rotateValue);
}
get_index(x_index, y_index, moveValue, absRotate, mode);
temp_drive = left_fast_njxy[y_index][32-x_index];
if(mode == Rotate)
{
x_idx = 32-x_index;
}
else
{
if(rotateValue < 0)
{
x_idx = x_index;
}
else
{
x_idx = 32-x_index;
}
}
temp_drive = left_fast_njxy[y_index][x_idx];
return temp_drive;
}
float EntropyDrive::absolutevalue(float x)
{
if (x < 0.0)
{
x = x*-1.0;
}
return x;
}
bool EntropyDrive::range(float x, float y, float z)
{
return (((y <= x) && (x <= z)) || ((y >= x) && (x >= z)));
}
float EntropyDrive::drive_table_limit(float x, float max, float min)
{
if(x > max)
{
return max;
}
else if(x < min)
{
return min;
}
else
{
return x;
}
}
void EntropyDrive::get_index(int &x_index, int &y_index, float moveValue, float rotateValue, DriveMode mode)
{
float rotate = 0;
float move = 0;
float minRotate = 0;
float maxRotate = 0;
const float *arrayPtr = 0;
unsigned int arrayLength = 0;
float diff1 = 0;
float diff2 = 0;
if(mode == Radius)
{
arrayPtr = left_lookup_radius;
arrayLength = 18;//sizeof(left_lookup_radius)/sizeof(float);
minRotate = arrayPtr[16];
maxRotate = arrayPtr[15];
}
else /*Rotate*/
{
arrayPtr = left_lookupx;
arrayLength = sizeof(left_lookupx)/sizeof(float);
minRotate = arrayPtr[0];
maxRotate = arrayPtr[arrayLength-1];
}
rotate = drive_table_limit(rotateValue, maxRotate, minRotate);
for(unsigned int i = 0; i < arrayLength; i++)
{
if(i+1 >= arrayLength || range(rotate, arrayPtr[i], arrayPtr[i+1]))
{
//Assume match found
if((i + 1) >= arrayLength)
{
x_index = i;
}
else
{
diff1 = fabs(rotate - arrayPtr[i]);
diff2 = fabs(rotate - arrayPtr[i+1]);
if(diff1 < diff2)
{
x_index = i;
}
else
{
x_index = i + 1;
}
}
break;
}
}
arrayLength = (sizeof(left_lookupy)/sizeof(float));
move = drive_table_limit(moveValue, left_lookupy[32], left_lookupy[0]);
for(unsigned int i = 0; i < arrayLength; i++)
{
if(i+1 >= arrayLength || range(move, left_lookupy[i], left_lookupy[i+1]))
{
//Assume match found
if((i + 1) >= arrayLength)
{
y_index = i;
}
else
{
diff1 = fabs(move - left_lookupy[i]);
diff2 = fabs(move - left_lookupy[i+1]);
if(diff1 < diff2)
{
y_index = i;
}
else
{
y_index = i + 1;
}
}
break;
}
}
}
double EntropyDrive::addDeadZone (double Value)
{
#ifdef DEADZONE
if (Value<DEAD_ZONE_MAX){
if (Value>-DEAD_ZONE_MAX){
Value=0;
}
}
#endif
return Value;
}
float EntropyDrive::moveValueDampen (float moveValue)
{
if ((moveValue - previousValue > -0.1 and moveValue - previousValue < 0.1) and moveValue == 0){
previousValue = 0;
moveValue = 0;
}
else if(moveValue > previousValue){
moveValue = previousValue + dampValue;
}
else if (moveValue < previousValue){
moveValue = previousValue - dampValue;
}
previousValue=moveValue;
return moveValue;
}
void EntropyDrive::DriveTrainTest( )
{
CANJaguar * Motors[4] = {MotorDriveLeft1, MotorDriveLeft2, MotorDriveRight1, MotorDriveRight2 };
char * Motors_Names[4] = {"MotorLeft1", "MotorLeft2", "MotorRight1", "MotorRight2" };
int Motors_Values[4] = { IODefinitions::MOTOR_DRIVE_LEFT_1,
IODefinitions::MOTOR_DRIVE_LEFT_2,
IODefinitions::MOTOR_DRIVE_RIGHT_1,
IODefinitions::MOTOR_DRIVE_RIGHT_2
};
UINT8 syncGroup = 0x80;
for ( int i = 0; i < 4; i++ )
{
DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line1,"Motor: %s",
Motors_Names[i] );
DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line2,"Jaguar: %d", Motors_Values[i]);
DriverStationLCD::GetInstance()->UpdateLCD();
Motors[i]->Set( 0.6 ,syncGroup);
CANJaguar::UpdateSyncGroup(syncGroup);
Wait( 5 );
Motors[i]->Set( 0.0 , syncGroup);
CANJaguar::UpdateSyncGroup(syncGroup);
Wait( 2 );
DisplayEncodersTestDSLine5Line6();
}
}
void EntropyDrive::InitEncoderTest()
{
m_leftEncoderTest = new Encoder(1, 1, 1, 2, false, Encoder::k4X);
m_rightEncoderTest = new Encoder(1, 3, 1, 4, true, Encoder::k4X);
m_leftEncoderTest->SetDistancePerPulse(PULSE_RATIO / 12.0);
m_leftEncoderTest->SetPIDSourceParameter(Encoder::kRate);
m_rightEncoderTest->SetDistancePerPulse(PULSE_RATIO / 12.0);
m_rightEncoderTest->SetPIDSourceParameter(Encoder::kRate);
m_leftEncoderTest->Start();
m_rightEncoderTest->Start();
}
void EntropyDrive::DisplayEncodersTestDSLine5Line6()
{
DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line5, "Left Dist: %f", m_leftEncoderTest->GetDistance());
DriverStationLCD::GetInstance()->PrintfLine(DriverStationLCD::kUser_Line6, "Right Dist: %f", m_rightEncoderTest->GetDistance());
DriverStationLCD::GetInstance()->UpdateLCD();
}