-
Notifications
You must be signed in to change notification settings - Fork 937
/
DataReader.cpp
107 lines (90 loc) · 3.23 KB
/
DataReader.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
#include "DataReader.hpp"
#include <Configuration.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
DataReader::DataReader(const RobotType& type, FSM_StateName stateNameIn) : _type(type) {
if (_type == RobotType::MINI_CHEETAH) {
if (stateNameIn == FSM_StateName::BACKFLIP) {
//load_control_plan(THIS_COM "user/WBC_Controller/WBC_States/BackFlip/data/mc_flip.dat");
load_control_plan(THIS_COM "config/mc_flip.dat");
printf("[Backflip DataReader] Setup for mini cheetah\n");
}
else if (stateNameIn == FSM_StateName::FRONTJUMP) {
//load_control_plan(THIS_COM "user/MIT_Controller/Controllers/FrontJump/front_jump_data.dat"); // front_jump_data.dat for succesfull test 1 file
load_control_plan(THIS_COM "config/front_jump_pitchup_v2.dat");
printf("[Front Jump DataReader] Setup for mini cheetah\n");
}
} else {
printf("[Backflip DataReader] Setup for cheetah 3\n");
load_control_plan(THIS_COM "user/WBC_Controller/WBC_States/BackFlip/data/backflip.dat");
}
printf("[Backflip DataReader] Constructed.\n");
}
void DataReader::load_control_plan(const char* filename) {
printf("[Backflip DataReader] Loading control plan %s...\n", filename);
FILE* f = fopen(filename, "rb");
if (!f) {
printf("[Backflip DataReader] Error loading control plan!\n");
return;
}
fseek(f, 0, SEEK_END);
uint64_t file_size = ftell(f);
fseek(f, 0, SEEK_SET);
printf("[Backflip DataReader] Allocating %ld bytes for control plan\n",
file_size);
plan_buffer = (float*)malloc(file_size + 1);
if (!plan_buffer) {
printf("[Backflip DataReader] malloc failed!\n");
return;
}
uint64_t read_success = fread(plan_buffer, file_size, 1, f);
if (!read_success) {
printf("[Backflip DataReader] Error: fread failed.\n");
}
if (file_size % sizeof(float)) {
printf(
"[Backflip DataReader] Error: file size isn't divisible by size of "
"float!\n");
}
fclose(f);
plan_loaded = true;
plan_timesteps = file_size / (sizeof(float) * plan_cols);
printf("[Backflip DataReader] Done loading plan for %d timesteps\n",
plan_timesteps);
}
float* DataReader::get_initial_configuration() {
if (!plan_loaded) {
printf(
"[Backflip DataReader] Error: get_initial_configuration called without "
"a plan!\n");
return nullptr;
}
return plan_buffer + 3;
}
float* DataReader::get_plan_at_time(int timestep) {
if (!plan_loaded) {
printf(
"[Backflip DataReader] Error: get_plan_at_time called without a "
"plan!\n");
return nullptr;
}
if (timestep < 0 || timestep >= plan_timesteps) {
printf(
"[Backflip DataReader] Error: get_plan_at_time called for timestep %d\n"
"\tmust be between 0 and %d\n",
timestep, plan_timesteps - 1);
timestep = plan_timesteps - 1;
// return nullptr; // TODO: this should estop the robot, can't really
// recover from this!
}
// if(timestep < 0) { return plan_buffer + 3; }
// if(timestep >= plan_timesteps){ timestep = plan_timesteps-1; }
return plan_buffer + plan_cols * timestep;
}
void DataReader::unload_control_plan() {
free(plan_buffer);
plan_timesteps = -1;
plan_loaded = false;
printf("[Backflip DataReader] Unloaded plan.\n");
}