-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.cpp
168 lines (143 loc) · 5.12 KB
/
env.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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <iostream>
#include "mujoco.h"
#include "glfw3.h"
#include <vector>
#include "env.h"
using namespace std;
Env* Env_helper_pointer;
bool button_left = false;
bool button_middle = false;
bool button_right = false;
double lastx = 0;
double lasty = 0;
//callback functions for events, working on making them member functions somehow
void scroll(GLFWwindow* window, double xoffset, double yoffset)
{
// emulate vertical mouse motion = 5% of window height
mjv_moveCamera(Env_helper_pointer->m, mjMOUSE_ZOOM, 0, -0.05*yoffset, &(Env_helper_pointer->scn), &(Env_helper_pointer->cam));
}
void mouse_button(GLFWwindow* window, int button, int act, int mods)
{
// update button state
button_left = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)==GLFW_PRESS);
button_middle = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE)==GLFW_PRESS);
button_right = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)==GLFW_PRESS);
// update mouse position
glfwGetCursorPos(window, &lastx, &lasty);
}
void mouse_move(GLFWwindow* window, double xpos, double ypos)
{
// no buttons down: nothing to do
if( !button_left && !button_middle && !button_right )
return;
// compute mouse displacement, save
double dx = xpos - lastx;
double dy = ypos - lasty;
lastx = xpos;
lasty = ypos;
// get current window size
int width, height;
glfwGetWindowSize(window, &width, &height);
// get shift key state
bool mod_shift = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)==GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT)==GLFW_PRESS);
// determine action based on mouse button
mjtMouse action;
if( button_right )
action = mod_shift ? mjMOUSE_MOVE_H : mjMOUSE_MOVE_V;
else if( button_left )
action = mod_shift ? mjMOUSE_ROTATE_H : mjMOUSE_ROTATE_V;
else
action = mjMOUSE_ZOOM;
// move camera
mjv_moveCamera(Env_helper_pointer->m, action, dx/height, dy/height, &(Env_helper_pointer->scn), &(Env_helper_pointer->cam));
}
Env::Env(const char** argv)
{
Env_helper_pointer = this;
mj_activate("mjkey.txt");
char error[1000] = "Could not load binary model";
if( strlen(argv[1])>4 && !strcmp(argv[1]+strlen(argv[1])-4, ".mjb") )
Env::m = mj_loadModel(argv[1], 0);
else
Env::m = mj_loadXML(argv[1], 0, error, 1000);
if( !Env::m )
mju_error_s("Load model error: %s", error);
Env::d = mj_makeData(Env::m);
if( !glfwInit() )
mju_error("Could not initialize GLFW");
// create window, make OpenGL context current, request v-sync
Env::window = glfwCreateWindow(1200, 900, "Demo", NULL, NULL);
glfwMakeContextCurrent(Env::window);
glfwSwapInterval(1);
// initialize visualization data structures
mjv_defaultCamera(&(Env::cam));
mjv_defaultOption(&(Env::opt));
mjv_defaultScene(&(Env::scn));
mjr_defaultContext(&(Env::con));
// create scene and context
mjv_makeScene(Env::m, &(Env::scn), 2000);
mjr_makeContext(Env::m, &(Env::con), mjFONTSCALE_150);
glfwSetScrollCallback(Env::window, scroll);
}
void Env::reset()
{
mj_resetData(Env::m,Env::d);
//get random positions and velocities
for(short i=0; i<Env::m->nq; i++)
d->qpos[i] = 3.142*(((double) rand() / (RAND_MAX))-0.5);
for(short i=0; i<Env::m->nv; i++)
d->qvel[i] = 5*(((double) rand() / (RAND_MAX))-0.5);
for(short i=0; i<Env::m->nu; i++)
d->ctrl[i] = 0.0;
}
void Env::get_state(vector<float> &sensordata)
{
//change this block according to what is your env state
mj_step1(Env::m,Env::d);
sensordata.push_back(sin(Env::d->sensordata[0]));
sensordata.push_back(cos(Env::d->sensordata[0]));
sensordata.push_back(Env::d->sensordata[1]);
sensordata.push_back(Env::d->ctrl[0]);
sensordata.push_back(Env::d->time-Env::epoch_start_time);
//std::this_thread::sleep_for(std::chrono::microseconds(1));
}
void Env::take_action(vector<double> &action)
{
d->ctrl = &action[0];
mj_step2(Env::m,Env::d);
}
void Env::render(/*mjtNum &render_time*/)
{
mjrRect viewport = {0, 0, 0, 0};
glfwGetFramebufferSize(Env::window, &viewport.width, &viewport.height);
// update scene and render
mjv_updateScene(Env::m, Env::d, &(Env::opt), NULL, &(Env::cam), mjCAT_ALL, &(Env::scn));
mjr_render(viewport, &(Env::scn), &(Env::con));
// swap OpenGL buffers (blocking call due to v-sync)
glfwSwapBuffers(Env::window);
// process pending GUI events, call GLFW callbacks
glfwPollEvents();
//render_time = Env::d->time;
}
void Env::set_epoch_time()
{
Env::epoch_start_time = Env::d->time;
}
void Env::set_render_time()
{
Env::render_time = Env::d->time;
}
Env::~Env()
{
mj_deleteData(Env::d);
mj_deleteModel(Env::m);
mj_deactivate();
#if defined(__APPLE__) || defined(_WIN32)
glfwTerminate();
#endif
cout<<"destructed"<<endl;
}