forked from dusty-nv/jetson-reinforcement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropPlugin.cpp
150 lines (106 loc) · 3.51 KB
/
PropPlugin.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
/*
* http://github.com/dusty-nv/jetson-reinforcement
*/
#include "PropPlugin.h"
namespace gazebo
{
// Register this plugin with the simulator
GZ_REGISTER_MODEL_PLUGIN(PropPlugin)
//---------------------------------------------------------------------------------------
std::vector<PropPlugin*> props;
size_t GetNumProps()
{
return props.size();
}
PropPlugin* GetProp( size_t index )
{
return props[index];
}
PropPlugin* GetPropByName( const char* name )
{
if( !name )
return NULL;
const size_t numProps = props.size();
for( size_t n=0; n < numProps; n++ )
{
if( strcmp(props[n]->model->GetName().c_str(), name) == 0 )
return props[n];
}
return NULL;
}
void ResetPropDynamics()
{
const size_t numProps = props.size();
for( size_t n=0; n < numProps; n++ )
props[n]->ResetDynamics();
}
void RandomizeProps()
{
const size_t numProps = props.size();
for( size_t n=0; n < numProps; n++ )
props[n]->Randomize();
}
//---------------------------------------------------------------------------------------
// Plugin init
void PropPlugin::Load(physics::ModelPtr _parent, sdf::ElementPtr /*_sdf*/)
{
printf("PropPlugin::Load('%s')\n", _parent->GetName().c_str());
// Store the pointer to the model
this->model = _parent;
// Store the original pose of the model
UpdateResetPose();
// Listen to the update event. This event is broadcast every simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&PropPlugin::OnUpdate, this, _1));
// Track this object in the global Prop registry
props.push_back(this);
}
// UpdateResetPose
void PropPlugin::UpdateResetPose()
{
this->originalPose = model->GetWorldPose();
}
// Called by the world update start event
void PropPlugin::OnUpdate(const common::UpdateInfo & /*_info*/)
{
// Apply a small linear velocity to the model.
//this->model->SetLinearVel(math::Vector3(.03, 0, 0));
/*const math::Pose& pose = model->GetWorldPose();
printf("%s location: %lf %lf %lf\n", model->GetName().c_str(), pose.pos.x, pose.pos.y, pose.pos.z);
const math::Box& bbox = model->GetBoundingBox();
printf("%s bounding: min=%lf %lf %lf max=%lf %lf %lf\n", model->GetName().c_str(), bbox.min.x, bbox.min.y, bbox.min.z, bbox.max.x, bbox.max.y, bbox.max.z);
*/
/*const math::Box& bbox = model->GetBoundingBox();
const math::Vector3 center = bbox.GetCenter();
const math::Vector3 bbSize = bbox.GetSize();
printf("%s bounding: center=%lf %lf %lf size=%lf %lf %lf\n", model->GetName().c_str(), center.x, center.y, center.z, bbSize.x, bbSize.y, bbSize.z);
*/
}
// Reset the model's dynamics and pose to original
void PropPlugin::ResetDynamics()
{
model->SetAngularAccel(math::Vector3(0.0, 0.0, 0.0));
model->SetAngularVel(math::Vector3(0.0, 0.0, 0.0));
model->SetLinearAccel(math::Vector3(0.0, 0.0, 0.0));
model->SetLinearVel(math::Vector3(0.0, 0.0, 0.0));
model->SetWorldPose(originalPose);
}
inline float randf( float rand_min, float rand_max )
{
const float r = float(rand()) / float(RAND_MAX);
return (r * (rand_max - rand_min)) + rand_min;
}
// Randomize
void PropPlugin::Randomize()
{
model->SetAngularAccel(math::Vector3(0.0, 0.0, 0.0));
model->SetAngularVel(math::Vector3(0.0, 0.0, 0.0));
model->SetLinearAccel(math::Vector3(0.0, 0.0, 0.0));
model->SetLinearVel(math::Vector3(0.0, 0.0, 0.0));
math::Pose pose = originalPose;
pose.pos.x = randf(0.35f, 0.45f);
pose.pos.y = randf(-1.5f, 0.2f);
pose.pos.z = 0.0f;
printf("prop random pos: %f %f %f\n", pose.pos.x, pose.pos.y, pose.pos.z);
model->SetWorldPose(pose);
}
}