NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
AppEscape.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17  */
18 
26 // This application
27 #include "EscapeModel.h"
28 #include "EscapeController.h"
29 
30 // This library
34 #include "core/tgModel.h"
35 #include "core/tgSimViewGraphics.h"
36 #include "core/tgSimulation.h"
37 #include "core/tgWorld.h"
38 
39 // Bullet Physics
40 #include "LinearMath/btVector3.h"
41 
42 // The C++ Standard Library
43 #include <iostream>
44 
45 tgBoxGround *createGround();
46 tgWorld *createWorld();
49 void simulate(tgSimulation *simulation);
50 
59 int main(int argc, char** argv)
60 {
61  std::cout << "AppEscapeCrater" << std::endl;
62 
63  // First create the world
64  tgWorld *world = createWorld();
65 
66  // Second create the view
67  //tgSimViewGraphics *view = createGraphicsView(world); // For visual experimenting on one tensegrity
68  tgSimView *view = createView(world); // For running multiple episodes
69 
70  // Third create the simulation
71  tgSimulation *simulation = new tgSimulation(*view);
72 
73  // Fourth create the models with their controllers and add the models to the simulation
74  EscapeModel* const model = new EscapeModel();
75 
76  /* Required for setting up learning file input/output. */
77  const std::string suffix((argc > 1) ? argv[1] : "default");
78 
79 
80 
81  // Fifth create controller and attach it to the model
82  double initialLength = 9.0; // decimeters
83  EscapeController* const controller = new EscapeController(initialLength,
84  suffix,
85  "craterEscape/",
86  "Config.ini");
87  model->attach(controller);
88 
89  //Sixth add model (with controller) to simulation
90  simulation->addModel(model);
91 
92  //Seventh add crater to simulation
93  btVector3 originCrater = btVector3(0,0,0);
94  //tgCraterShallow* crater = new tgCraterShallow(originCrater);
95  tgCraterDeep* crater = new tgCraterDeep(originCrater);
96  simulation->addModel(crater);
97 
98  simulate(simulation);
99 
100  delete controller;
101  //Teardown is handled by delete, so that should be automatic
102  return 0;
103 }
104 
105 tgBoxGround *createGround() {
106  // Determine the angle of the ground in radians. All 0 is flat
107  const double yaw = 0.0;
108  const double pitch = 0.0;
109  const double roll = 0.0;
110  const btVector3 eulerAngles = btVector3(yaw, pitch, roll); // Default: (0.0, 0.0, 0.0)
111  const double friction = 0.5; // Default: 0.5
112  const double restitution = 0.0; // Default: 0.0
113  const btVector3 size = btVector3(10000.0, 2, 10000.0); // Default: (500.0, 1.5, 500.0)
114  const btVector3 origin = btVector3(0.0, 0.0, 0.0); // Default: (0.0, 0.0, 0.0)
115  const tgBoxGround::Config groundConfig(eulerAngles, friction, restitution,
116  size, origin);
117  // the world will delete this
118  return new tgBoxGround(groundConfig);
119 }
120 
121 tgWorld *createWorld() {
122  const tgWorld::Config config(98.1); // gravity, cm/sec^2 Use this to adjust length scale of world.
123  // NB: by changing the setting below from 981 to 98.1, we've
124  // scaled the world length scale to decimeters not cm.
125 
126  tgBoxGround* ground = createGround();
127  return new tgWorld(config, ground);
128 }
129 
132  const double timestep_physics = 1.0 / 60.0 / 10.0; // Seconds
133  const double timestep_graphics = 1.f /60.f; // Seconds, AKA render rate. Leave at 1/60 for real-time viewing
134  return new tgSimViewGraphics(*world, timestep_physics, timestep_graphics);
135 }
136 
139  const double timestep_physics = 1.0 / 60.0 / 10.0; // Seconds
140  const double timestep_graphics = 1.f /60.f; // Seconds, AKA render rate. Leave at 1/60 for real-time viewing
141  return new tgSimView(*world, timestep_physics, timestep_graphics);
142 }
143 
145 void simulate(tgSimulation *simulation) {
146  int nEpisodes = 1; // Number of episodes ("trial runs")
147  int nSteps = 60000; // Number of steps in each episode, 60k is 100 seconds (timestep_physics*nSteps)
148  for (int i=0; i<nEpisodes; i++) {
149  simulation->run(nSteps);
150  simulation->reset();
151  }
152 }
153 
Contains the definition of class EscapeModel. $Id$.
Contains the definition of class tgCraterShallow. Specifically, a crater is defined as a series of bo...
Contains the definition of class tgCraterDeep. Specifically, this crater is defined as a series of bo...
void addModel(tgModel *pModel)
int main(int argc, char **argv)
Definition: AppEscape.cpp:59
Contains the definition of class tgSimulation.
Contains the definition of class tgModel.
Contains the definition of class tgSimViewGraphics.
tgSimView * createView(tgWorld *world)
Definition: AppEscape.cpp:138
Contains the definition of class tgWorld $Id$.
tgSimViewGraphics * createGraphicsView(tgWorld *world)
Definition: AppEscape.cpp:131
void run() const
Contains the definition of class tgBoxGround.
void simulate(tgSimulation *simulation)
Definition: AppEscape.cpp:145
Contains the definition of class EscapeController.
void attach(tgObserver< T > *pObserver)
Definition: tgSubject.h:91