/usr/share/openscenegraph/examples/osgrobot/osgrobot.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | /* OpenSceneGraph example, osgrobot.cpp.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
The code below is to show how a heirarchy of objects can be made within a scenegraph.
In other words, how there can be a parent/child relationship between objects such
that when a parent is rotated or translated, the children move is respect to it's
parent movement. A robotic arm is used in this example because this is what I'm
using OSG for.
To rotate the joints use the following table to rotate each respective joint.
joint key rotation
1 q + 1 degree
1 a - 1 degree
2 w + 1 degree
2 s - 1 degree
3 e + 1 degree
3 d - 1 degree
4 r + 1 degree
4 f - 1 degree
5 t + 1 degree
5 g - 1 degree
6 y + 1 degree
6 h - 1 degree
In many robotics projects, the x and z axis are only used to define the positions
of the joints of a robotic arm. By not using the y-axis, the mathematics of
inverse kinematics becomes easier (and in some cases possible). All joint rotations
are rotated about the z-axis. If you toggle the variable 'showAxis' to true, an
x (color red), y (color green), and z (color blue) axis will display. Using the
thumb of the right hand to point in the same direction of the z-axis, use the right
hand rule to define a positive angle of rotation.
The robotic arm in general follows the algorithm
rotate about the x axis, translate along the x-axis, translate along the z axis,
and then rotate about the z-axis. There is 1 exception in the method buildJoint3(...)
where it was necessary to add a 'cheat' to get the joints to align properly. In a
real robotics project such as this, the tube (see buildTube2(...) method) would not
be a part of the mathematics and thus, not have to be drawn and would not require
the cheat. To learn moue about positioning joints using only the x and z axis,
web search 'Denavit-Hartenberg'. wikipedia has comments at the URL...
http://en.wikipedia.org/wiki/Robotics_conventions#Denavit-Hartenberg_link_frame_convention_.28DH.29
Lessons learned with osg...
When starting this work I was very new to OSG. When I first placed a cylinder
in a scene and rotated the cylinder about the z and x axis, I noticed that the rotation
is about the barycenter of the object for both axis. I wanted a rotation about the z-axis
to be along the length of the tube (which OSG gives for free -- not always true with
all scenegraphs) while the rotation about the x and y axis about the
beginning (or start position) of the tube. Knowing what I know about barycenters
I positioned the shape along the -height/2 (see example 1 below)
-- believing the offset -height/2 would place the axis of rotation at the start of
the tube. I was certainly wrong about this and found out the equation needs to
be height/2.
...
example 1
joint->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
...
author: James Moliere
date written: 10/12/2008
*/
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/Texture2D>
#include <osgViewer/Viewer>
#include <iostream>
#include <osgDB/ReadFile>
#include <osg/Math>
#include <osg/Matrixd>
#include <osg/MatrixTransform>
#include <osg/Vec3d>
#include <iostream>
#include <osg/PositionAttitudeTransform>
float jointAngle1;
float jointAngle2;
float jointAngle3;
float jointAngle4;
float jointAngle5;
float jointAngle6;
float EndEffector;
void createAxis(osg::Transform* previousJoint);
osg::MatrixTransform* joint1;
osg::MatrixTransform* joint2;
osg::MatrixTransform* joint3;
osg::MatrixTransform* joint4;
osg::MatrixTransform* joint5;
osg::MatrixTransform* joint6;
osg::MatrixTransform* buildJoint1(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildJoint2(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildTube2(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildJoint3(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildJoint4(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildTube5(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildJoint5(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildJoint6(osg::MatrixTransform* previousJoint);
osg::MatrixTransform* buildEndEffector();
osg::TessellationHints* hints = new osg::TessellationHints;
bool showAxis;
class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
KeyboardEventHandler()
{}
static void rotate(float angle,osg::MatrixTransform *joint)
{
osg::Matrix zRot;
zRot.makeRotate(angle, 0.0,0.0,1.0);
joint->setMatrix(zRot*joint->getMatrix());
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch (ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
switch (ea.getKey())
{
case 'q':
rotate(osg::PI/180, joint1);
return true;
case 'a':
rotate(-osg::PI/180, joint1);
return true;
case 'w':
rotate(osg::PI/180, joint2);
return true;
case 's':
rotate(-osg::PI/180, joint2);
return true;
case 'e':
rotate(osg::PI/180, joint3);
return true;
case 'd':
rotate(-osg::PI/180, joint3);
return true;
case 'r':
rotate(osg::PI/180, joint4);
return true;
case 'f':
rotate(-osg::PI/180, joint4);
return true;
case 't':
rotate(osg::PI/180, joint5);
return true;
case 'g':
rotate(-osg::PI/180, joint5);
return true;
case 'y':
rotate(osg::PI/180, joint6);
return true;
case 'h':
rotate(-osg::PI/180, joint6);
return true;
}
}
default:
break;
}
//return false to allow mouse manipulation
return false;
}
};
osg::Group* createShapes()
{
osg::Group* group = new osg::Group();
osg::MatrixTransform* transform =new osg::MatrixTransform();
group->addChild(transform);
joint1 = buildJoint1(transform);
joint2 = buildJoint2(joint1);
osg::MatrixTransform* tube2= buildTube2(joint2);
joint3 = buildJoint3(tube2);
joint4 = buildJoint4(joint3);
osg::MatrixTransform *tube5 = buildTube5(joint4);
joint5 = buildJoint5(tube5);
joint6= buildJoint6(joint5);
joint6->addChild( buildEndEffector());
return group;
}
osg::MatrixTransform* buildJoint1(osg::MatrixTransform* previousJoint)
{
osg::MatrixTransform* xTransform =new osg::MatrixTransform();
previousJoint->addChild(xTransform);
float radius = 6.7640f;
float height = 45.0f;
osg::Geode* joint = new osg::Geode();
xTransform->addChild(joint);
joint->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform *zTransform = new osg::MatrixTransform();
xTransform->addChild(zTransform);
osg::Matrix zTrans=osg::Matrix::translate(0.0, 0.0, height);
osg::Matrix zRot= osg::Matrix::rotate(jointAngle1, 0.0, 0.0, 1.0);
zTransform->setMatrix(zTrans*zRot);
return zTransform;
}
osg::MatrixTransform* buildJoint2(osg::MatrixTransform* previousJoint)
{
if (showAxis)
{
createAxis(previousJoint);
}
double height = 17.6;
double radius = 4.45633;
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot = osg::Matrix::rotate(osg::PI_2, 1.0, 0.0, 0.0);
xTransform->setMatrix(xRot);
osg::Geode* joint = new osg::Geode();
joint->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
xTransform->addChild(joint);
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
osg::Matrix zTrans = osg::Matrix::translate( 0.0, 0.0, height);
osg::Matrix zRot = osg::Matrix::rotate(osg::PI_2+jointAngle2, 0.0,0.0,1.0);
zTransform->setMatrix(zTrans*zRot);
xTransform->addChild(zTransform);
return zTransform;
}
osg::MatrixTransform* buildTube2(osg::MatrixTransform* previousJoint)
{
if (showAxis)
{
createAxis(previousJoint);
}
double height = 17.6;
double radius = 4.45633;
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot = osg::Matrix::rotate(osg::PI_2, 1.0,0.0,0.0);
xTransform->setMatrix(xRot);
osg::Geode* tube3 = new osg::Geode();
xTransform->addChild(tube3);
tube3->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
xTransform->addChild(zTransform);
osg::Matrix zTrans = osg::Matrix::translate(0,0,height);
zTransform->setMatrix(zTrans);
return zTransform;
}
osg::MatrixTransform* buildJoint3(osg::MatrixTransform* previousJoint)
{
double height =7.5;
double radius = 4.45633;
osg::Geode* joint = new osg::Geode();
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot = osg::Matrix::rotate(-osg::PI_2, 1.0, 0.0, 0.0);
xTransform->setMatrix(xRot);
osg::MatrixTransform* zCheat = new osg::MatrixTransform();
osg::Matrix zTransCheat = osg::Matrix::translate(0.0,0.0,-height);
zCheat->setMatrix(zTransCheat);
xTransform->addChild(zCheat);
osg::ShapeDrawable *shape = new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints);
joint->addDrawable(shape);
zCheat->addChild(joint);
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
zCheat->addChild(zTransform);
osg::Matrix zRot = osg::Matrix::rotate((float)jointAngle3, 0.0f, 0.0f, 1.0f);
osg::Matrix zTrans = osg::Matrix::translate(0,0,0);
zTransform->setMatrix(zTrans*zRot);
if (showAxis)
{
createAxis(zTransform);
}
return zTransform;
}
osg::MatrixTransform* buildJoint4(osg::MatrixTransform* previousJoint)
{
double height = 17.5;
double radius = 2.86479;
osg::Geode* tube4 = new osg::Geode();
tube4->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
//if (showAxis)
//{
//createAxis(xTransform);
//}
previousJoint->addChild(xTransform);
xTransform->addChild(tube4);
osg::Matrix xRot= osg::Matrix::rotate(osg::PI_2, 1,0,0);
xTransform->setMatrix(xRot);
height = 7.5;
osg::Matrix zTrans = osg::Matrix::translate(0,0,17.5);
osg::Matrix zRot = osg::Matrix::rotate(jointAngle4-osg::PI_2, 0,0,1);
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
zTransform->setMatrix(zTrans*zRot);
xTransform->addChild(zTransform);
return zTransform;
}
osg::MatrixTransform* buildTube5(osg::MatrixTransform* previousJoint)
{
if (showAxis)
{
createAxis(previousJoint);
}
double height = 7.5;
double radius = 2.86479;
height = 15.0;
osg::Geode* joint = new osg::Geode();
osg::ShapeDrawable *shape =new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints);
joint->addDrawable(shape);
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
xTransform->addChild(joint);
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
osg::Matrix zTrans = osg::Matrix::translate(0,0,height);
zTransform->setMatrix(zTrans);
xTransform->addChild(zTransform);
return zTransform;
}
osg::MatrixTransform* buildJoint5(osg::MatrixTransform* previousJoint)
{
double radius = 2.86479;
double height = 2.86479*2;
osg::Geode* joint = new osg::Geode();
osg::ShapeDrawable *shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,0.0f),radius,height),hints);
joint->addDrawable(shape);
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
previousJoint->addChild(xTransform);
osg::Matrix xRot= osg::Matrix::rotate(-osg::PI_2, 1.0,0,0);
xTransform->setMatrix(xRot);
xTransform->addChild(joint);
if (showAxis)
{
createAxis(xTransform);
}
osg::MatrixTransform* zTransform = new osg::MatrixTransform();
osg::Matrix zRot= osg::Matrix::rotate(jointAngle5, 0,0,1);
osg::Matrix zTrans = osg::Matrix::translate(0,0,0);
zTransform->setMatrix(zTrans*zRot);
xTransform->addChild(zTransform);
return zTransform;
}
osg::MatrixTransform* buildJoint6(osg::MatrixTransform* previousJoint)
{
double height = 3.0;
double radius = 1.0;
osg::Geode* joint = new osg::Geode();
joint->addDrawable(new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints));
osg::MatrixTransform* xTransform = new osg::MatrixTransform();
osg::Matrix xRot= osg::Matrix::rotate(osg::PI_2, 1.0, 0.0, 0.0);
xTransform->setMatrix(xRot);
xTransform->addChild(joint);
previousJoint->addChild(xTransform);
if (showAxis)
{
createAxis(xTransform);
}
return xTransform;
}
osg::MatrixTransform* buildEndEffector()
{
osg::MatrixTransform* mt = new osg::MatrixTransform();
osg::Matrix m;
double length = 17.0;
m.makeTranslate(0,0,length/2);
mt->setMatrix(m);
osg::Geode *geode_3 = new osg::Geode;
osg::ShapeDrawable *shape1 = new osg::ShapeDrawable(new osg::Box(osg::Vec3(-EndEffector, 0.0f, 0.0f), .5, 1.5f, length), hints);
osg::ShapeDrawable *shape2 = new osg::ShapeDrawable(new osg::Box(osg::Vec3( EndEffector, 0.0f, 0.0f), .5, 1.5f, length), hints);
shape1->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
shape2->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f));
geode_3->addDrawable(shape1);
geode_3->addDrawable(shape2);
mt->addChild(geode_3);
return mt;
}
void createAxis(osg::Transform* previousJoint)
{
double height = 12.0;
double radius = .5;
osg::MatrixTransform* zmt = new osg::MatrixTransform();
previousJoint->addChild(zmt);
osg::ShapeDrawable *zShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints);
osg::ShapeDrawable *zCone = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f,0.0f,1.0),radius+1.0,2.0),hints);
osg::MatrixTransform* zmtCone = new osg::MatrixTransform();
osg::Geode *zgCone = new osg::Geode;
zmtCone->setMatrix( osg::Matrix::translate(0.0,0.0,height));
previousJoint->addChild(zmtCone);
zShape->setColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
zCone->setColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
osg::Geode *z = new osg::Geode;
z->addDrawable(zShape);
zgCone->addDrawable(zCone);
zmtCone->addChild(zgCone);
zmt->addChild(z);
osg::MatrixTransform* mt = new osg::MatrixTransform();
previousJoint->addChild(mt);
osg::Matrix xMatrix = osg::Matrix::rotate(-osg::PI_2, 0.0, 1.0, 0.0);
mt->setMatrix(xMatrix);
osg::ShapeDrawable *xShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints);
xShape->setColor(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
osg::Geode *x = new osg::Geode;
x->addDrawable(xShape);
mt->addChild(x);
osg::MatrixTransform *yMt = new osg::MatrixTransform();
previousJoint->addChild(yMt);
osg::Matrix yMatrix = osg::Matrix::rotate(osg::PI_2, 1.0, 0.0, 0.0);
yMt->setMatrix(yMatrix);
osg::ShapeDrawable *yShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f,0.0f,height/2),radius,height),hints);
yShape->setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
osg::Geode *y = new osg::Geode;
y->addDrawable(yShape);
yMt->addChild(y);
}
int main(int, char **)
{
hints->setDetailRatio(0.5f);
showAxis = true;
jointAngle1=0.0;
jointAngle2=0.0;
jointAngle3=0.0;
jointAngle4=0.0;
jointAngle5=0.0;
jointAngle6=0.0;
EndEffector=1.0;
osgViewer::Viewer viewer;
viewer.addEventHandler(new KeyboardEventHandler());
// add model to viewer.
viewer.setSceneData( createShapes() );
return viewer.run();
}
|