-
Notifications
You must be signed in to change notification settings - Fork 0
/
polaris_simple_control_plugin.cpp
436 lines (313 loc) · 12.2 KB
/
polaris_simple_control_plugin.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Gazebo - Outdoor Multi-Robot Simulator
* Copyright (C) 2003
* Nate Koenig & Andrew Howard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* Desc: ROS interface to a Position2d controller for an Ackermann drive.
* Author: Daniel Hewlett (adapted from Nathan Koenig)
*/
/*
* Adapted as a ROS gazebo plugin for car-like vehicles using
* Ackermann steering.
*
* Copyright (C) 2011, Nicu Stiurca, Jack O'Quin
*/
/*
* Adapted for Lego NXT
* Copyright (C) 2011, Alexandr Buyval
*/
#include <algorithm>
#include <assert.h>
#include <polaris_simple_control_plugin.h>
using namespace gazebo;
enum
{
RIGHT, LEFT, LSTEER, RSTEER
};
const double TAU = 6.28318530717958647693; // 2 * pi
// Constructor
AckermannPlugin::AckermannPlugin()
{
}
// Destructor
AckermannPlugin::~AckermannPlugin()
{
event::Events::DisconnectWorldUpdateBegin(updateConnection);
delete transform_broadcaster_;
rosnode_->shutdown();
callback_queue_thread_.join();
delete rosnode_;
}
// Load the controller
void AckermannPlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
ROS_INFO("AckermannPlugin::Load");
world = _model->GetWorld();
// load parameters
if (!_sdf->HasElement("robotNamespace"))
robotNamespace.clear();
else
//getSdfParam<std::string>(_sdf, "robotNamespace", robotNamespace,robotNamespace);
//param = sdf->GetElement(name)->Get<T>();
robotNamespace = _sdf->GetElement("robotNamespace")->Get<std::string>() + "/";
if (!_sdf->HasElement("topicName"))
topicName = "cmd_vel";
else
//getSdfParam<std::string>(_sdf, "topicName", topicName,topicName);
topicName = _sdf->GetElement("topicName")->Get<std::string>();
if (!_sdf->HasElement("wheelDistance"))
wheelDistance = 1.2;
else
wheelDistance = _sdf->GetElement("wheelDistance")->Get<double>();
if (!_sdf->HasElement("CarLength"))
CarLength = 1.88;
else
CarLength = _sdf->GetElement("CarLength")->Get<double>();
if (!_sdf->HasElement("wheelDiameter"))
wheelDiam = 0.32;
else
wheelDiam = _sdf->GetElement("wheelDiameter")->Get<double>();
if (!_sdf->HasElement("driveTorque"))
driveTorque = 10.0;
else
driveTorque = _sdf->GetElement("driveTorque")->Get<double>();
if (!_sdf->HasElement("steerTorque"))
steerTorque = 10.0;
else
steerTorque = _sdf->GetElement("steerTorque")->Get<double>();
if (!_sdf->HasElement("bodyName"))
{
link = _model->GetLink();
linkName = link->GetName();
}
else {
//getSdfParam<std::string>(_sdf, "bodyName", linkName,linkName);
linkName = _sdf->GetElement("bodyName")->Get<std::string>();
link = _model->GetLink(linkName);
}
// assert that the body by linkName exists
if (!link)
{
ROS_FATAL("AckermannPlugin error: bodyName: %s does not exist\n", linkName.c_str());
return;
}
if (_sdf->HasElement("leftJoint")) joints[LEFT] = _model->GetJoint(_sdf->GetElement("leftJoint")->Get<std::string>());
if (_sdf->HasElement("rightJoint")) joints[RIGHT] = _model->GetJoint(_sdf->GetElement("rightJoint")->Get<std::string>());
if (_sdf->HasElement("leftsteerJoint")) joints[LSTEER] = _model->GetJoint(_sdf->GetElement("leftsteerJoint")->Get<std::string>());
if (_sdf->HasElement("rightsteerJoint")) joints[RSTEER] = _model->GetJoint(_sdf->GetElement("rightsteerJoint")->Get<std::string>());
//ROS_INFO("Info --- Load parametres");
if (!joints[LEFT]) ROS_FATAL("Plugin error: The controller couldn't get left joint");
if (!joints[RIGHT]) ROS_FATAL("Plugin error: The controller couldn't get right joint");
if (!joints[LSTEER]) ROS_FATAL("Plugin error: The controller couldn't get leftsteer joint");
if (!joints[RSTEER]) ROS_FATAL("Plugin error: The controller couldn't get rightsteer joint");
enableMotors = true;
wheelSpeed[RIGHT] = 0;
wheelSpeed[LEFT] = 0;
steerAngle = 0;
prevUpdateTime = world->GetSimTime();
// Initialize the ROS node and subscribe to car_drive
joints[LEFT]->SetParam ( "fmax", 0, driveTorque );
joints[RIGHT]->SetParam ( "fmax", 0, driveTorque );
joints[LSTEER]->SetParam ( "fmax", 0, steerTorque );
joints[RSTEER]->SetParam ( "fmax", 0, steerTorque );
// joints[STEER]->SetHighStop(0, 3.14);
// joints[STEER]->SetLowStop(0, -3,14);
if (!ros::isInitialized())
{
int argc = 0;
char** argv = NULL;
ros::init(argc,argv,"ackermann_plugin",ros::init_options::NoSigintHandler|ros::init_options::AnonymousName);
}
rosnode_ = new ros::NodeHandle(robotNamespace);
tf_prefix_ = tf::getPrefixParam(*rosnode_);
transform_broadcaster_ = new tf::TransformBroadcaster();
// ROS: Subscribe to the CarDrive command topic (usually "car_drive")
//ROS_INFO("%s: Try to subscribe to %s!", rosnode_->info(), topicName.c_str());
ros::SubscribeOptions so =
ros::SubscribeOptions::create<geometry_msgs::Twist>(topicName, 1,
boost::bind(&AckermannPlugin::carDriveCallback, this, _1),
ros::VoidPtr(), &queue_);
sub_ = rosnode_->subscribe(so);
pub_ = rosnode_->advertise<nav_msgs::Odometry>("odom", 1);
callback_queue_thread_ = boost::thread(boost::bind(&AckermannPlugin::QueueThread, this));
Reset();
// New Mechanism for Updating every World Cycle
// Listen to the update event. This event is broadcast every
// simulation iteration.
updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&AckermannPlugin::Update, this));
}
// Initialize the controller
void AckermannPlugin::Init()
{
callback_queue_thread_ = boost::thread(boost::bind(&AckermannPlugin::QueueThread, this));
}
// Reset
void AckermannPlugin::Reset()
{
prevUpdateTime = world->GetSimTime();
alive_ = true;
joints[LEFT]->SetParam ( "fmax", 0, driveTorque );
joints[RIGHT]->SetParam ( "fmax", 0, driveTorque );
joints[LSTEER]->SetParam ( "fmax", 0, steerTorque );
joints[RSTEER]->SetParam ( "fmax", 0, steerTorque );
}
// Update the controller
void AckermannPlugin::Update()
{
double a,b;
double w;
common::Time stepTime;
for ( int i = 0; i < 2; i++ )
{
if ( fabs(driveTorque -joints[i]->GetParam ( "fmax", 0 )) > 1e-6 )
joints[i]->SetParam ( "fmax", 0, driveTorque );
}
GetPositionCmd();
stepTime = world->GetSimTime() - prevUpdateTime;
prevUpdateTime = world->GetSimTime();
// Get steering angle
a = joints[LSTEER]->GetAngle(0).Radian();
b = joints[RSTEER]->GetAngle(0).Radian();
//ROS_INFO("Current anlge:%f Command angle:%f", a, steerAngle);
double left_ang_vel = ((steerAngle - a) / stepTime.Double())/10;
double right_ang_vel = ((steerAngle - b) / stepTime.Double())/10;
//ROS_INFO("Current anlge:%f Command angle:%f Output left_ang_vel:%f Output right_ang_vel:%f", a, steerAngle,left_ang_vel,right_ang_vel);
if (left_ang_vel>10) left_ang_vel=10;
if (left_ang_vel<-10) left_ang_vel=-10;
if (right_ang_vel>10) right_ang_vel=10;
if (right_ang_vel<-10) right_ang_vel=-10;
//ROS_INFO("Angular speed:%f", ang_vel);
//Differential velocity of rear wheel
/*
if(fabs(steerAngle)>0.1)
{
w=wheelSpeed[LEFT]/(CarLength/2/sin(steerAngle));
wheelSpeed[LEFT]=w*(CarLength/2/tan(steerAngle)-wheelDistance/2);
wheelSpeed[RIGHT]=w*(CarLength/2/tan(steerAngle)+wheelDistance/2);
}
*/
//ROS_INFO("w:%f steerAngle:%f wheelSpeed[LEFT]:%f wheelSpeed[RIGHT]:%f", w,steerAngle,wheelSpeed[LEFT], wheelSpeed[RIGHT]);
if (enableMotors)
{
if(wheelSpeed[LEFT]>0)
{
joints[LEFT]->SetForce(0, driveTorque);
joints[RIGHT]->SetForce(0,driveTorque);
}
else
{
joints[LEFT]->SetForce(0, -driveTorque);
joints[RIGHT]->SetForce(0,-driveTorque);
}
if(left_ang_vel>0)
{
joints[LSTEER]->SetForce(0,steerTorque);
joints[RSTEER]->SetForce(0,steerTorque);
}
else
{
joints[LSTEER]->SetForce(0,-steerTorque);
joints[RSTEER]->SetForce(0,-steerTorque);
}
joints[LEFT]->SetParam ( "vel", 0, wheelSpeed[LEFT] / (wheelDiam / 2.0));
joints[RIGHT]->SetParam ( "vel", 0, wheelSpeed[RIGHT] / (wheelDiam / 2.0));
// joints[STEER]->SetForce(0, ang_vel);
joints[LSTEER]->SetParam ( "vel", 0, left_ang_vel);
joints[RSTEER]->SetParam ( "vel", 0, right_ang_vel);
/* joints[LEFT]->SetVelocity(0, wheelSpeed[LEFT] / (wheelDiam / 2.0));
joints[RIGHT]->SetVelocity(0, wheelSpeed[RIGHT] / (wheelDiam / 2.0));
// FIXME: come up with something nicer for doing position control
// than this simple proportional controller
joints[STEER]->SetVelocity(0, ang_vel);
joints[LEFT]->SetMaxForce(0, driveTorque);
joints[RIGHT]->SetMaxForce(0,driveTorque);
joints[STEER]->SetMaxForce(0,steerTorque); */
/*#if GAZEBO_MAJOR_VERSION > 2
joints_[LEFT]->SetParam ( "vel", 0, wheel_speed_instr_[LEFT] / ( wheel_diameter_ / 2.0 ) );
joints_[RIGHT]->SetParam ( "vel", 0, wheel_speed_instr_[RIGHT] / ( wheel_diameter_ / 2.0 ) );
#else
joints_[LEFT]->SetVelocity ( 0,wheel_speed_instr_[LEFT] / ( wheel_diameter_ / 2.0 ) );
joints_[RIGHT]->SetVelocity ( 0,wheel_speed_instr_[RIGHT] / ( wheel_diameter_ / 2.0 ) );
#endif*/
}
publish_odometry();
}
// NEW: Now uses the target velocities from the ROS message, not the Iface
void AckermannPlugin::GetPositionCmd()
{
lock.lock();
// Changed motors to be always on, which is probably what we want anyway
enableMotors = true;
wheelSpeed[LEFT] = speed_;
wheelSpeed[RIGHT] = speed_;
steerAngle = angle_;
lock.unlock();
}
// NEW: Store the velocities from the ROS message
void AckermannPlugin::carDriveCallback(const geometry_msgs::Twist::ConstPtr& cmd_msg)
{
lock.lock();
speed_ = cmd_msg->linear.x;
angle_ = cmd_msg->angular.z;
// FIXME: take acceleration and jerk into account
lock.unlock();
}
// NEW: custom callback queue thread
void AckermannPlugin::QueueThread()
{
static const double timeout = 0.01;
while (alive_ && rosnode_->ok())
{
// std::cout << "CALLING STUFF\n";
queue_.callAvailable(ros::WallDuration(timeout));
}
}
// NEW: Update this to publish odometry topic
void AckermannPlugin::publish_odometry()
{
// get current time
ros::Time current_time_((world->GetSimTime()).sec, (world->GetSimTime()).nsec);
// getting data for base_footprint to odom transform
math::Pose pose = link->GetWorldPose();
math::Vector3 velocity = link->GetWorldLinearVel();
math::Vector3 angular_velocity = link->GetWorldAngularVel();
tf::Quaternion qt(pose.rot.x, pose.rot.y, pose.rot.z, pose.rot.w);
tf::Vector3 vt(pose.pos.x, pose.pos.y, pose.pos.z);
tf::Transform base_footprint_to_odom(qt, vt);
transform_broadcaster_->sendTransform(tf::StampedTransform(base_footprint_to_odom,
current_time_,
"odom",
"base_footprint"));
// publish odom topic
odom_.pose.pose.position.x = pose.pos.x;
odom_.pose.pose.position.y = pose.pos.y;
odom_.pose.pose.orientation.x = pose.rot.x;
odom_.pose.pose.orientation.y = pose.rot.y;
odom_.pose.pose.orientation.z = pose.rot.z;
odom_.pose.pose.orientation.w = pose.rot.w;
odom_.twist.twist.linear.x = velocity.x;
odom_.twist.twist.linear.y = velocity.y;
odom_.twist.twist.angular.z = angular_velocity.z;
odom_.header.frame_id = tf::resolve(tf_prefix_, "odom");
odom_.child_frame_id = "base_footprint";
odom_.header.stamp = current_time_;
pub_.publish(odom_);
}
GZ_REGISTER_MODEL_PLUGIN(AckermannPlugin)