-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizing_functions_state.ino
256 lines (205 loc) · 6.36 KB
/
randomizing_functions_state.ino
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
#include "randomizing_functions_state.h"
#include <limits>
// Helper functions run at setup.
// Uses stageBehaviors to create times and speeds for stageParameters
void RandomizeBehaviors(void)
{
for (size_t i = 0; i <= ARRAY_SIZE(stageBehaviors) ; i++)
{
BehaviorType behavior = stageBehaviors[i].behaviorType;
switch (behavior)
{
case BehaviorType::Rest:
{
// Speed
stageParameters[i].speed = 0;
// Duration
uint32_t stage_duration = RandomDuration(RestTime[0], RestTime[1]);
stageParameters[i].duration = stage_duration;
break;
}
case BehaviorType::Movement1:
{
// Speed
int number_of_speeds = ARRAY_SIZE(Speeds1);
int j = RandomSpeeds(number_of_speeds);
stageParameters[i].speed = Speeds1[j];
// Duration
uint32_t stage_duration = RandomDuration(MoveTime[0], MoveTime[1]);
stageParameters[i].duration = stage_duration;
break;
}
case BehaviorType::Movement2:
{
// Speed
int number_of_speeds = ARRAY_SIZE(Speeds2);
int j = RandomSpeeds(number_of_speeds);
stageParameters[i].speed = Speeds2[j];
// Duration
uint32_t stage_duration = RandomDuration(MoveTime[0], MoveTime[1]);
stageParameters[i].duration = stage_duration;
break;
}
}
}
// Calculate the speed difference (for warning tone and easier use of probe trials).
for (size_t i = 0; i < ARRAY_SIZE(stageBehaviors) ; i++)
{
// Place in parameter array
stageParameters[i].speed_difference = stageParameters[i + 1].speed - stageParameters[i].speed;
}
}
uint32_t RandomDuration(uint32_t MinTime, uint32_t MaxTime)
{
// Find a random time within the range
uint32_t stage_duration = random(MinTime, MaxTime + 1) * (1000);
return stage_duration;
}
int RandomSpeeds(int number_of_speeds)
{
size_t j = random(0, number_of_speeds);
return j;
}
// Creates a function that randomizes the speed order. Make dependent on speedDiff value to determine what kind of change it is.
static void randomizeAccel(int count)
{
// Only do this if user has said they want to randomize the accelerations.
if (useAccels)
{
// Find the minimum speed
int min_speed = getMinSpeed();
// Don't include index "0" because that is the initial rest period.
for (size_t i = 1; i <= count ; i++)
{
// Use previous stage's speed difference (accel set at start of new stage, when motor transition begins)
// If less than the minimum speed, then assume it's a speed change
if (abs(stageParameters[i - 1].speed_difference) < min_speed)
{
// Pick a random index within the speed array
size_t j = random(0, ARRAY_SIZE(accelsSpeedChange));
// Place in parameter array
stageParameters[i].accel = accelsSpeedChange[j];
}
// Otherwise, is assumed to be a start or stop.
else
{
// Pick a random index within the speed array
size_t j = random(0, ARRAY_SIZE(accelsStartStop));
// Place in parameter array
stageParameters[i].accel = accelsStartStop[j];
}
}
}
}
uint32_t GetTotalTime(int count)
{
uint32_t totalTime = 0;
for (size_t i = 1; i <= count ; i++)
{
totalTime += stageParameters[i].duration;
}
return totalTime;
}
// A function that writes out what goes to the serial monitor.
void Report(float targetSpeed, float accel, int activityTag, String message)
{
CurrentTime=millis()-globalStartTime;
if (useTrialNumber)
{
Serial.print(trial_number);
Serial.print(", ");
}
Serial.print(String(CurrentTime));
Serial.print(", ");
Serial.print(String(targetSpeed));
Serial.print(", ");
if (useAccels){
Serial.print(String(accel));
Serial.print(", ");
}
Serial.print(activityTag);
Serial.print(", ");
Serial.println(message);
}
void HeaderReport(int count, uint32_t totalTime)
{
// Report trial number
if (useTrialNumber)
{
Serial.print("Trial ");
Serial.println(trial_number);
}
// Report total number of stages
Serial.print("Total number of stages: ");
Serial.println(count + 1);
// Report total time of trial
Serial.print("Total time: ");
Serial.println(totalTime);
// report speeds and times HeaderReport(int randomTime.count);
Serial.print("Time, Speed");
if (useAccels)
{
Serial.print(", Acceleration");
}
if (useProbeTrials){
Serial.print(", Probe type");
}
Serial.println();
for (size_t i = 0; i <= count ; i++)
{
Serial.print(stageParameters[i].duration);
Serial.print(", ");
Serial.print(stageParameters[i].speed);
if (useAccels)
{
Serial.print(", ");
Serial.print(stageParameters[i].accel);
}
if (useProbeTrials)
{
if (stageParameters[i].probe == Probe::NoWarning)
{
Serial.print(", ");
Serial.print("Probe: no warning cue");
}
else if (stageParameters[i].probe == Probe::NoChange)
{
Serial.print(", ");
Serial.print("Probe: no change in speed");
}
else
{
Serial.print(", ");
Serial.print("No probe");
}
}
Serial.println();
}
}
int getMinSpeed(void)
{
// A trick to find the maximum possible value for an int, so everything afterwards is always less than that
int min_speed = std::numeric_limits<int>::max();
// Go through first possible list of speeds
for (std::size_t i = 0; i < ARRAY_SIZE(Speeds1); i++)
{
if ((Speeds1[i] > 0) && (Speeds1[i] < min_speed))
{
min_speed = Speeds1[i];
}
}
// Go through second list of speeds.
for (std::size_t i = 0; i < ARRAY_SIZE(Speeds2); i++)
{
if ((Speeds2[i] > 0) && (Speeds2[i] < min_speed))
{
min_speed = Speeds2[i];
}
}
// If our array was broken, make sure we return a reasonable default
if (min_speed == std::numeric_limits<int>::max())
{
min_speed = 2000;
}
return min_speed;
}