A C header for recording and playing back VEX V5 robot movements using PROS
- Place
replay.h
in yourinclude
directory - Customize it.
The beginning of the file defines what you're replaying.
values returned by
typedef struct { double wheels[4]; // four wheels int last; // indicate last step (important!!) } ReplayStep;
motor_get_actual_velocity
aredouble
s, but you can put anything in it. Make sure to leaveint last
, as it is used to indicate the last step in your sequence - use it!
To create a replay sequence, first allocate it.
ReplayStep* replay = malloc(sizeof(ReplayStep) * REPLAY_LEN);
int step = 0;
This creates an array* of ReplaySteps
called replay
of length REPLAY_LEN
, and an integer called step
, which you'll likely want in order to keep track of the steps.
Now, when you want to record, simply set replay[step]
's members to whatever you like (such as motor speeds), and increment step
before each delay
call.
When step has reached the desired length, set replay[step].last
to 1
.
Here's a simple example that records the positions of four wheels
void opcontrol() {
ReplayStep* replay = malloc(sizeof(ReplayStep) * REPLAY_LEN); // since you typically delay 2ms every step, 500 steps is typically equivalent to 1 second
int step = 0;
bool recording = false;
/*
insert controller-based movement and trigger for record here
*/
if (recording) {
for (int i = 0; i < 4; i++) {
replay[step].wheels[i] = motor_get_actual_velocity(wheels[i]);
}
step++;
if (step == REPLAY_LEN) {
replay[step - 1].last = 1; // indicate last step
write_replay(replay, "/usd/replay"); // write the replay to the SD card
step = 0;
recording = false;
}
}
delay(2); // make sure the delay is the same during both record and playback
}
Just call write_replay(replay, filename)
. If you're using an SD card in your VEX brain, the filename should start with /usd/
.
If you've allocated more memory than the length of your replay, all you have to do is mark the last step you want as last, and when it is saved, everything after the first step where replay[i].last == 1
will be removed
First, load the replay.
ReplayStep* replay = read_replay(REPLAY_FILENAME);
Now, you can do whatever you want with it. The following example spins four wheels as they were recorded.
for (int i = 0; replay[i-1].last != 1; i++) {
for (int j = 0; j < 4; j++) {
motor_move_velocity(wheel_port[j], replay[i].wheels[j]);
}
delay(2);
}
*don't at me