-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobotConfiguration.cpp
335 lines (272 loc) · 9.13 KB
/
RobotConfiguration.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstring>
#include "WPILib.h"
#include "RobotConfiguration.h"
#include "DebugUtil.h"
std::map<std::string, float> * RobotConfiguration::m_map = NULL;
bool RobotConfiguration::m_updated = false;
void RobotConfiguration::Init()
{
// Initialize the configuration.
// Defaults should be referenced here
// in case the robot's file is gone.
printf("Initializing configuration.\n");
m_map = new std::map<std::string, float>();
SetConfigurationVariable("DRIVE_TRAIN_P", 1);
SetConfigurationVariable("DRIVE_TRAIN_I", 0);
SetConfigurationVariable("DRIVE_TRAIN_D", 0);
//1,.02,0.0
SetConfigurationVariable("ARM_P", 1);
SetConfigurationVariable("ARM_I", .02);
SetConfigurationVariable("ARM_D", 0);
SetConfigurationVariable("ARM_PID_TOLERANCE", 10);
SetConfigurationVariable("ARM_INPUT_RANGE_MIN", -165);
SetConfigurationVariable("ARM_INPUT_RANGE_MAX", 165);
//4,.02,0
SetConfigurationVariable("SHUTTLE_P", 4);
SetConfigurationVariable("SHUTTLE_I", .02);
SetConfigurationVariable("SHUTTLE_D", 0);
SetConfigurationVariable("SHUTTLE_PID_TOLERANCE", 10);
SetConfigurationVariable("SHUTTLE_INPUT_RANGE_MIN", -18);
SetConfigurationVariable("SHUTTLE_INPUT_RANGE_MAX", 10);
SetConfigurationVariable("DRIVE_MAX_OUTPUT", 150);
SetConfigurationVariable("NORMAL_AUTO_SPEED", .4);
SetConfigurationVariable("NORMAL_INSIDE_SPEED", .2);
SetConfigurationVariable("NORMAL_OUTSIDE_SPEED", .6);
SetConfigurationVariable("NORMAL_EXTRA_SPEED", .8);
SetConfigurationVariable("GRABBER_FASTER_SPEED", .6);
SetConfigurationVariable("GRABBER_SLOWER_SPEED", .4);
SetConfigurationVariable("GRABBER_MIDDLE_SPEED", .5);
SetConfigurationVariable("SHUTTLE_MOVE_TIME", 1);
SetConfigurationVariable("SHUTTLE_UP_SPEED", .55);
// Arm setpoints
SetConfigurationVariable("POSE_SHUTTLE_LOW", -18);
SetConfigurationVariable("POSE_SHUTTLE_HIGH", 8);
SetConfigurationVariable("POSE_SHUTTLE_MID", 4);
SetConfigurationVariable("POSE_ARM_1", -141);
SetConfigurationVariable("POSE_ARM_2", -45);
SetConfigurationVariable("POSE_ARM_3", -108);
SetConfigurationVariable("POSE_ARM_4", -97);
SetConfigurationVariable("POSE_ARM_5", -90);
SetConfigurationVariable("POSE_ARM_6", -80);
SetConfigurationVariable("POSE_ARM_7", -44);
SetConfigurationVariable("POSE_ARM_8", -39);
SetConfigurationVariable("POSE_ARM_9", 0);
SetConfigurationVariable("POSE_ARM_18", -156);
SetConfigurationVariable("POSE_ARM_0", -34);
SetConfigurationVariable("AUTO_SCORE_DISTANCE", 15);
SetConfigurationVariable("DEPLOY_SPEED", .6);
SetConfigurationVariable("AUTO_BACKUP_SPEED", -.55);
SetConfigurationVariable("AUTO_BACKUP_DISTANCE", -15);
SetConfigurationVariable("AUTO_FINAL_BACKUP_DISTANCE", -30);
SetConfigurationVariable("AUTO_HANGING_POSE", 1);
SetConfigurationVariable("AUTO_EJECT_TIME", 5);
SetConfigurationVariable("AUTO_ROTATE_TIME", 3);
SetConfigurationVariable("ARM_OUTPUT_MIN", -.4);
SetConfigurationVariable("ARM_OUTPUT_MAX", .4);
SetConfigurationVariable("SHUTTLE_OUTPUT_MIN", -1);
SetConfigurationVariable("SHUTTLE_OUTPUT_MAX", 1);
SetConfigurationVariable("AUTO_DRIVE_DISTANCE", 174);
SetConfigurationVariable("ENABLE_LINE_FOLLOWING", 1);
SetConfigurationVariable("DRIVE_STRAIGHT_P", .1);
printf("Opening file.\n");
bool success = OpenAndParseFile("config.txt");
if (success) {
printf("File read!\n");
DumpConfiguration();
} else {
printf("File failed.\n");
printf("Creating file...");
RobotConfiguration::SaveSettings();
printf("done.\n");
}
printf("Configuration initialization complete.\n");
m_updated = true;
}
void RobotConfiguration::Process()
{
// Nothing right now.
}
void RobotConfiguration::SaveSettings()
{
std::ofstream config("config.txt");
if (!config.is_open()) {
printf("Config error: cannot save");
return;
}
// Write out initial header.
config << "# config.txt -- Configuration file for cRIO\n";
config << "# Team 1741\n";
config << std::endl;
// Write out settings.
std::map<std::string, float>::const_iterator itr;
for (itr = m_map->begin(); itr != m_map->end(); ++itr) {
std::string key = itr->first;
float value = itr->second;
// These are stored in some order to minimize the access
// time of retrieval, so that might not make sense in the output.
config << key << " " << value << std::endl;
}
config << std::endl;
config.close();
return;
}
void RobotConfiguration::DumpConfiguration()
{
std::map<std::string, float>::const_iterator itr;
printf("Configuration Variables:\n");
for (itr = m_map->begin(); itr != m_map->end(); ++itr) {
std::string key = itr->first;
float value = itr->second;
// These are stored in some order to minimize the access
// time of retrieval, so that might not make sense in the output.
printf("\t%s = %2.2f\n",key.c_str(), value);
}
printf("Dump complete.\n");
}
void RobotConfiguration::SetDefaults()
{
RobotConfiguration::SetDriveTrainPID(1.0, 0.2, 0.0);
}
void RobotConfiguration::SetDriveTrainPID(float p, float i, float d)
{
SetConfigurationVariable("DRIVE_TRAIN_P", p);
SetConfigurationVariable("DRIVE_TRAIN_I", i);
SetConfigurationVariable("DRIVE_TRAIN_D", d);
}
float RobotConfiguration::GetDriveTrainD()
{
return GetConfigurationVariable("DRIVE_TRAIN_D");
}
float RobotConfiguration::GetDriveTrainI()
{
return GetConfigurationVariable("DRIVE_TRAIN_I");
}
float RobotConfiguration::GetDriveTrainP()
{
return GetConfigurationVariable("DRIVE_TRAIN_P");
}
bool RobotConfiguration::StringsEqual(char *a, char *b)
{
while (*a != '\0' && *b != '\0') {
if (toupper(*a) != toupper(*b))
{
return false;
}
a++; b++;
}
if (*a != '\0' || *b != '\0') {
return false;
}
return true;
}
bool RobotConfiguration::OpenAndParseFile(char * filename)
{
std::ifstream config(filename);
if (!config.is_open()) {
printf("Config error: cannot open file '%s'\n",
filename);
return false;
} else {
int line_number = 0;
while (!config.eof()) {
++line_number;
std::string entire_line = "";
bool is_comment = false;
bool is_blank = true;
getline(config, entire_line);
//printf("Read line: '%s'\n", entire_line.c_str());
// Check to see if the line is a comment.
for (unsigned int i = 0; i < entire_line.length(); ++i) {
if (isspace(entire_line[i])) {
continue; // Ignore whitespace
} else if (entire_line[i] == '#') {
// If we have a comment character
is_comment = true;
is_blank = false;
break;
} else {
// Line is NOT a comment.
is_comment = false;
is_blank = false;
break;
}
} // End for loop
if (is_blank) {
//printf("Line is blank.\n");
continue;
}
if (is_comment) {
//printf("Line is comment.\n");
continue; // Move to next line.
// Continue means the rest of the loop
// is skipped, we go back up to the
// top where we read in another line.
}
// Ok, at this point we know that the line
// contains a field name and value. Let's find
// out which one.
// I could use string.find, but I'm going
// to use a custom loop so I can use
// tabs and other stuff in the config file.
std::string field = "";
std::string value = "";
int split_point = -1;
for (unsigned int i = 0; i < entire_line.length(); ++i)
{
if (isspace(entire_line[i])) {
split_point = i;
break;
}
}
if (split_point == -1) {
printf( "Config Error: invalid syntax at line %d in '%s': '%s'\n",
line_number, filename,
entire_line.c_str());
// Skip this for now.
// Later we might want to stop parsing here.
continue;
} else {
field = entire_line.substr(0, split_point);
value = entire_line.substr(split_point+1,
entire_line.length() - split_point);
printf("Config: '%s' = '%s'\n", field.c_str(), value.c_str());
}
std::remove(field.begin(), field.end(), ' ');
std::remove(value.begin(), value.end(), ' ');
// Ok, let's actually do the work of getting the values.
if (!HasConfigurationVariable(field)) {
printf("Config: Unknown field '%s'\n", field.c_str());
} else {
SetConfigurationVariable(field, atof(value.c_str()));
}
}
config.close();
return true;
}
}
float RobotConfiguration::GetConfigurationVariable( std::string key )
{
float value = 0;
if (!HasConfigurationVariable(key)) {
// element was not found. Complain.
DPRINTF("Hey! Configuration variable '%s' was not found. Returning 0.", key.c_str() );
value = 0;
} else {
value = (*m_map)[key];
}
return value;
}
void RobotConfiguration::SetConfigurationVariable(std::string key, float value)
{
// According to std library, if key doesn't exist, it's created.
(*m_map)[key] = value;
}
bool RobotConfiguration::HasConfigurationVariable(std::string key)
{
std::map<std::string, float>::const_iterator itr = m_map->find( key );
return (itr != m_map->end() );
}