Cheetah Software  1.0
Simulation.h
Go to the documentation of this file.
1 
6 #ifndef PROJECT_SIMULATION_H
7 #define PROJECT_SIMULATION_H
8 
12 #include "Dynamics/Cheetah3.h"
13 #include "Dynamics/MiniCheetah.h"
14 #include "Dynamics/Quadruped.h"
15 #include "Graphics3D.h"
20 #include "Utilities/SharedMemory.h"
21 #include "Utilities/Timer.h"
22 
23 #include <mutex>
24 #include <queue>
25 #include <utility>
26 #include <vector>
27 
28 #include <lcm/lcm-cpp.hpp>
29 #include "simulator_lcmt.hpp"
30 
31 #define SIM_LCM_NAME "simulator_state"
32 
39 class Simulation {
40  public:
41  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42  explicit Simulation(RobotType robot, Graphics3D* window,
43  SimulatorControlParameters& params, ControlParameters& userParams);
44 
49  _simulator->setState(state);
50  }
51 
52  void step(double dt, double dtLowLevelControl, double dtHighLevelControl);
53 
54  void addCollisionPlane(double mu, double resti, double height,
55  double sizeX = 20, double sizeY = 20,
56  double checkerX = 40, double checkerY = 40,
57  bool addToWindow = true);
58  void addCollisionBox(double mu, double resti, double depth, double width,
59  double height, const Vec3<double>& pos,
60  const Mat3<double>& ori, bool addToWindow = true,
61  bool transparent = true);
62  void addCollisionMesh(double mu, double resti, double grid_size,
63  const Vec3<double>& left_corner_loc,
64  const DMat<double>& height_map, bool addToWindow = true,
65  bool transparent = true);
66 
67  void lowLevelControl();
68  void highLevelControl();
69 
73  void updateGraphics();
74 
75  void runAtSpeed(bool graphics = true);
76  void sendControlParameter(const std::string& name,
79  bool isUser);
80 
81  void resetSimTime() {
82  _currentSimTime = 0.;
85  }
86 
88  delete _simulator;
89  delete _robotDataSimulator;
90  delete _imuSimulator;
91  delete _lcm;
92  }
93 
95 
96  void stop() {
97  _running = false; // kill simulation loop
98  _wantStop = true; // if we're still trying to connect, this will kill us
99 
100  if (_connected) {
101  _sharedMemory().simToRobot.mode = SimulatorMode::EXIT;
102  _sharedMemory().simulatorIsDone();
103  }
104  }
105 
107 
110 
111  bool isRobotConnected() { return _connected; }
112 
113  void firstRun();
114  void buildLcmMessage();
115  void loadTerrainFile(const std::string& terrainFileName,
116  bool addGraphics = true);
117 
118  private:
119  std::mutex _robotMutex;
125 
127  Graphics3D* _window = nullptr;
135  std::vector<ActuatorModel<double>> _actuatorModels;
141  lcm::LCM* _lcm = nullptr;
142  bool _running = false;
143  bool _connected = false;
144  bool _wantStop = false;
145  double _desiredSimSpeed = 1.;
146  double _currentSimTime = 0.;
150  simulator_lcmt _simLCM;
151 };
152 
153 #endif // PROJECT_SIMULATION_H
void stop()
Definition: Simulation.h:96
void addCollisionBox(double mu, double resti, double depth, double width, double height, const Vec3< double > &pos, const Mat3< double > &ori, bool addToWindow=true, bool transparent=true)
Definition: Simulation.cpp:546
const FBModelState< double > & getRobotState()
Definition: Simulation.h:94
void runAtSpeed(bool graphics=true)
Definition: Simulation.cpp:579
Shared memory utilities for connecting the simulator program to the robot program.
void step(double dt, double dtLowLevelControl, double dtHighLevelControl)
Definition: Simulation.cpp:305
ControlParameters & getUserParams()
Definition: Simulation.h:109
Simulated IMU.
ControlParameterValueKind
void addCollisionMesh(double mu, double resti, double grid_size, const Vec3< double > &left_corner_loc, const DMat< double > &height_map, bool addToWindow=true, bool transparent=true)
Definition: Simulation.cpp:559
std::vector< ActuatorModel< double > > _actuatorModels
Definition: Simulation.h:135
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
void highLevelControl()
Definition: Simulation.cpp:398
Data structure containing parameters for quadruped robot.
bool _connected
Definition: Simulation.h:143
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
void updateGraphics()
Definition: Simulation.cpp:805
Utility function to build a Cheetah 3 Quadruped object.
std::mutex _robotMutex
Definition: Simulation.h:119
void lowLevelControl()
Definition: Simulation.cpp:360
Utility function to build a Mini Cheetah Quadruped object.
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
TI_BoardControl _tiBoards[4]
Definition: Simulation.h:139
Graphics3D * _window
Definition: Simulation.h:127
SharedMemoryObject< SimulatorSyncronizedMessage > _sharedMemory
Definition: Simulation.h:120
DynamicsSimulator< double > * _robotDataSimulator
Definition: Simulation.h:134
void firstRun()
Definition: Simulation.cpp:268
int64_t s64
Definition: cTypes.h:24
void loadTerrainFile(const std::string &terrainFileName, bool addGraphics=true)
Definition: Simulation.cpp:638
DVec< double > _tau
Definition: Simulation.h:132
void setState(const FBModelState< T > &state)
RobotControlParameters _robotParams
Definition: Simulation.h:124
size_t _simRobotID
Definition: Simulation.h:126
bool _running
Definition: Simulation.h:142
void addCollisionPlane(double mu, double resti, double height, double sizeX=20, double sizeY=20, double checkerX=40, double checkerY=40, bool addToWindow=true)
Definition: Simulation.cpp:520
SpiData _spiData
Definition: Simulation.h:137
Quadruped< double > _quadruped
Definition: Simulation.h:128
void buildLcmMessage()
Definition: Simulation.cpp:472
void setRobotState(FBModelState< double > &state)
Definition: Simulation.h:48
Spine Board Code, used to simulate the SpineBoard.
double _timeOfNextLowLevelControl
Definition: Simulation.h:147
s64 _highLevelIterations
Definition: Simulation.h:149
Timer for measuring how long things take.
SpiCommand _spiCommand
Definition: Simulation.h:136
SpineBoard _spineBoards[4]
Definition: Simulation.h:138
RobotType _robot
Definition: Simulation.h:140
RobotControlParameters & getRobotParams()
Definition: Simulation.h:108
ControlParameters & _userParams
Definition: Simulation.h:123
simulator_lcmt _simLCM
Definition: Simulation.h:150
SimulatorControlParameters & _simParams
Definition: Simulation.h:122
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Simulation(RobotType robot, Graphics3D *window, SimulatorControlParameters &params, ControlParameters &userParams)
Definition: Simulation.cpp:18
FloatingBaseModel< double > _model
Definition: Simulation.h:130
const FBModelState< T > & getState() const
size_t _controllerRobotID
Definition: Simulation.h:126
void resetSimTime()
Definition: Simulation.h:81
double _desiredSimSpeed
Definition: Simulation.h:145
DynamicsSimulator< double > * _simulator
Definition: Simulation.h:133
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
bool _wantStop
Definition: Simulation.h:144
Types to allow remote access of control parameters, for use with LCM/Shared memory.
Messages sent to/from the development simulator.
RobotType
Definition: cppTypes.h:120
double _currentSimTime
Definition: Simulation.h:146
ImuSimulator< double > * _imuSimulator
Definition: Simulation.h:121
FBModelState< double > _robotControllerState
Definition: Simulation.h:129
SimulatorControlParameters & getSimParams()
Definition: Simulation.h:106
double _timeOfNextHighLevelControl
Definition: Simulation.h:148
void sendControlParameter(const std::string &name, ControlParameterValue value, ControlParameterValueKind kind, bool isUser)
Definition: Simulation.cpp:216
Visualizer window for simulator.
FloatingBaseModel< double > _robotDataModel
Definition: Simulation.h:131
bool isRobotConnected()
Definition: Simulation.h:111
lcm::LCM * _lcm
Definition: Simulation.h:141