-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_controller.cpp
155 lines (91 loc) · 4.17 KB
/
model_controller.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
#include "model_controller.h"
#include<gazebo-7/gazebo/math/Angle.hh>
namespace gazebo
{
GZ_REGISTER_MODEL_PLUGIN(ModelPush)
ModelPush::ModelPush()
{
this->motorJointName01="base_slider1";
this->motorJointName02="base_slider2";
this->motorJointName03="base_slider3";
}
ModelPush::~ModelPush()
{
event::Events::DisconnectWorldUpdateBegin(this->updateConnection);
}
void ModelPush::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
{
this->model = _parent; // Store the pointer to the model
this->motorJoint01=this->model->GetJoint(this->motorJointName01);
this->motorJoint02=this->model->GetJoint(this->motorJointName02);
this->motorJoint03=this->model->GetJoint(this->motorJointName03);
this->attempt=0;
if (!this->motorJoint01 || !this->motorJoint02 || !this->motorJoint03)
{
gzerr << this->motorJointName01 << std::setw(20) << this->motorJointName02 << std::setw(20) << this->motorJointName03 << std::setw(20) << " some of them weren't found\n";
return;
}
this->activeJoints.push_back(motorJoint01);
this->activeJoints.push_back(motorJoint02);
this->activeJoints.push_back(motorJoint03);
this->vectorofJoints =this->model->GetJoints();
// this->motorJoint01->SetAngle(0,0);
//this->motorJoint02->SetAngle(0,0);
//this->motorJoint03->SetAngle(0,0);
this->updateConnection = event::Events::ConnectWorldUpdateBegin( // Listen to the update event. This event is broadcast every
boost::bind(&ModelPush::OnUpdate, this, _1)); // simulation iteration.
}
// Called by the world update start event
void ModelPush::OnUpdate(const common::UpdateInfo & /*_info*/)
{
this->simTime = this->model->GetWorld()->GetSimTime();
this->realTime = this->model->GetWorld()->GetRealTime();
// std::cout<<"Sim Time: "<< simTime.Double()<<"Real Time: "<<realTime.Double() <<std::endl;
/*
for(physics::Joint_V::iterator jit=vectorofJoints.begin(); jit!=vectorofJoints.end(); ++jit)
{
std::cout<<"name:"<<std::setw(40)<<(*jit)->GetName()<<" "<<(*jit)->GetAngle(0)<<" "<<(*jit)->GetVelocity(0)<<" " << std::endl;
this->model->GetWorld()->GetSimTime();
std::cout << "name: " << std::setw(40) << std::left << (*jit)->GetName() << "angle: " << std::setw(10) << std::left << (*jit)->GetAngle(0)<<"velocity: "<< std::setw(10) << std::left << (*jit)->GetVelocity(0)<<std::endl;// << setw(4) << hourlyRate << endl;
std::cout << "name: ";
std::cout<<(*jit)->GetName();
std::cout.width(40);
std::cout<<(*jit)->GetAngle(0);
std::cout.width(50);
std::cout<<(*jit)->GetVelocity(0)<<std::endl;
jointAngles[(*jit)->GetName()] = (*jit)->GetAngle(0).Radian();
jointVelocity[(*jit)->GetName()] = (*jit)->GetVelocity(0);
jointForce[(*jit)->GetName()] = (*jit)->GetForce(0);
}
this->motorJoint01->SetVelocity(1,0.01);
this->motorJoint02->SetVelocity(1,-0.01);
*/
this->motorJoint01->SetPosition(0,0);
//this->motorJoint01->SetForce(0,(1/20.0)*sin(this->simTime.Double()));
this->motorJoint02->SetVelocity(0,(-1/10.0)*sin(this->simTime.Double()));
this->motorJoint03->SetVelocity(0,(1/10.0)*sin(this->simTime.Double()));
this->motorJoint02->SetForce(0,-10*sin(this->simTime.Double()));
this->motorJoint03->SetForce(0,10*sin(this->simTime.Double()));
//std::cout << (1/10.0)*sin(this->simTime.Double());
/*
if ( this->attempt == 5)
{
std::cout<<"HEHEHE";
}
else
{
std::cout << "INPUT " <<std::endl;
std::cin >> this->attempt;
std::cout <<"value a:" << this->attempt << std::endl;
}
double a;
std::cin >> a;
this->motorJoint01->SetPosition(0,a);
std::cout << "INPUT" << a <<std::endl;
*/
for(physics::Joint_V::iterator jit=activeJoints.begin(); jit!=activeJoints.end(); ++jit)
{
std::cout << "name: " << std::setw(40) << std::left << (*jit)->GetName() << "angle: " << std::setw(15) << std::left << (*jit)->GetAngle(0)<<"velocity: "<< std::setw(15) << std::left << (*jit)->GetVelocity(0)<<std::endl ;//<< setw(4) << hourlyRate << endl;
}
}
}