Cheetah Software  1.0
SimulationBridge.cpp
Go to the documentation of this file.
1 
7 #include "SimulationBridge.h"
8 #include <gui_main_control_settings_t.hpp>
10 #include "rt/rt_interface_lcm.h"
11 #include "rt/rt_sbus.h"
12 
14  // init shared memory:
16  _sharedMemory().init();
17 
18  // init Quadruped Controller
19 
20  printf("[Simulation Driver] Starting main loop...\n");
21  bool firstRun = true;
22  for (;;) {
23  // wait for our turn to access the shared memory
24  // on the first loop, this gives the simulator a chance to put stuff in
25  // shared memory before we start
26  _sharedMemory().waitForSimulator();
27 
28  if (firstRun) {
29  firstRun = false;
30  // check that the robot type is correct:
31  if (_robot != _sharedMemory().simToRobot.robotType) {
32  printf(
33  "simulator and simulatorDriver don't agree on which robot we are "
34  "simulating (robot %d, sim %d)\n",
35  (int)_robot, (int)_sharedMemory().simToRobot.robotType);
36  throw std::runtime_error("robot mismatch!");
37  }
38  }
39 
40  // the simulator tells us which mode to run in
41  _simMode = _sharedMemory().simToRobot.mode;
42  switch (_simMode) {
43  case SimulatorMode::RUN_CONTROL_PARAMETERS: // there is a new control
44  // parameter request
46  break;
47  case SimulatorMode::RUN_CONTROLLER: // the simulator is ready for the
48  // next robot controller run
49  _iterations++;
51  break;
52  case SimulatorMode::DO_NOTHING: // the simulator is just checking to see
53  // if we are alive yet
54  break;
55  case SimulatorMode::EXIT: // the simulator is done with us
56  printf("[Simulation Driver] Transitioned to exit mode\n");
57  return;
58  break;
59  default:
60  throw std::runtime_error("unknown simulator mode");
61  }
62 
63  // tell the simulator we are done
64  _sharedMemory().robotIsDone();
65  }
66 }
67 
72  ControlParameterRequest& request =
73  _sharedMemory().simToRobot.controlParameterRequest;
74  ControlParameterResponse& response =
75  _sharedMemory().robotToSim.controlParameterResponse;
76  if (request.requestNumber <= response.requestNumber) {
77  // nothing to do!
78  printf(
79  "[SimulationBridge] Warning: the simulator has run a ControlParameter "
80  "iteration, but there is no new request!\n");
81  return;
82  }
83 
84  // sanity check
85  u64 nRequests = request.requestNumber - response.requestNumber;
86  assert(nRequests == 1);
87 
89  .size(); // todo don't do this every single time?
90 
91  switch (request.requestKind) {
93  std::string name(request.name);
95 
96  // type check
97  if (param._kind != request.parameterKind) {
98  throw std::runtime_error(
99  "type mismatch for parameter " + name + ", robot thinks it is " +
101  " but received a command to set it to " +
103  }
104 
105  // do the actual set
106  param.set(request.value, request.parameterKind);
107 
108  // respond:
109  response.requestNumber =
110  request.requestNumber; // acknowledge that the set has happened
111  response.parameterKind =
112  request.parameterKind; // just for debugging print statements
113  response.value = request.value; // just for debugging print statements
114  strcpy(response.name,
115  name.c_str()); // just for debugging print statements
116  response.requestKind = request.requestKind;
117 
118  printf("%s\n", response.toString().c_str());
119 
120  } break;
121 
123  std::string name(request.name);
124  if(!_userParams) {
125  printf("[Simulation Bridge] Warning: tried to set user parameter, but the robot does not have any!\n");
126  } else {
128 
129  // type check
130  if (param._kind != request.parameterKind) {
131  throw std::runtime_error(
132  "type mismatch for parameter " + name + ", robot thinks it is " +
134  " but received a command to set it to " +
136  }
137 
138  // do the actual set
139  param.set(request.value, request.parameterKind);
140  }
141 
142  // respond:
143  response.requestNumber =
144  request.requestNumber; // acknowledge that the set has happened
145  response.parameterKind =
146  request.parameterKind; // just for debugging print statements
147  response.value = request.value; // just for debugging print statements
148  strcpy(response.name,
149  name.c_str()); // just for debugging print statements
150  response.requestKind = request.requestKind;
151 
152  printf("%s\n", response.toString().c_str());
153 
154  } break;
155 
157  std::string name(request.name);
159 
160  // type check
161  if (param._kind != request.parameterKind) {
162  throw std::runtime_error(
163  "type mismatch for parameter " + name + ", robot thinks it is " +
165  " but received a command to set it to " +
167  }
168 
169  // respond
170  response.value = param.get(request.parameterKind);
171  response.requestNumber = request.requestNumber; // acknowledge
172  response.parameterKind =
173  request.parameterKind; // just for debugging print statements
174  strcpy(response.name,
175  name.c_str()); // just for debugging print statements
176  response.requestKind =
177  request.requestKind; // just for debugging print statements
178 
179  printf("%s\n", response.toString().c_str());
180  } break;
181  default:
182  throw std::runtime_error("unhandled get/set");
183  }
184 }
185 
187  if (_firstControllerRun) {
188  printf("[Simulator Driver] First run of robot controller...\n");
190  printf("\tAll %ld control parameters are initialized\n",
191  _robotParams.collection._map.size());
192  } else {
193  printf(
194  "\tbut not all control parameters were initialized. Missing:\n%s\n",
196  throw std::runtime_error(
197  "not all parameters initialized when going into RUN_CONTROLLER");
198  }
199 
200  auto* userControlParameters = _robotRunner->_robot_ctrl->getUserControlParameters();
201  if(userControlParameters) {
202  if (userControlParameters->isFullyInitialized()) {
203  printf("\tAll %ld user parameters are initialized\n",
204  userControlParameters->collection._map.size());
206  } else {
207  printf(
208  "\tbut not all control parameters were initialized. Missing:\n%s\n",
209  userControlParameters->generateUnitializedList().c_str());
210  throw std::runtime_error(
211  "not all parameters initialized when going into RUN_CONTROLLER");
212  }
213  } else {
215  }
216 
217 
219  &_sharedMemory().simToRobot.gamepadCommand;
220  _robotRunner->spiData = &_sharedMemory().simToRobot.spiData;
221  _robotRunner->tiBoardData = _sharedMemory().simToRobot.tiBoardData;
223  _robotRunner->vectorNavData = &_sharedMemory().simToRobot.vectorNav;
224  _robotRunner->cheaterState = &_sharedMemory().simToRobot.cheaterState;
225  _robotRunner->spiCommand = &_sharedMemory().robotToSim.spiCommand;
227  _sharedMemory().robotToSim.tiBoardCommand;
230  &_sharedMemory().robotToSim.visualizationData;
232  &_sharedMemory().robotToSim.mainCheetahVisualization;
233 
234  _robotRunner->init();
235  _firstControllerRun = false;
236 
237  sbus_thread = new std::thread(&SimulationBridge::run_sbus, this);
238  }
239  _robotRunner->run();
240 }
241 
243  printf("[run_sbus] starting...\n");
244  int port = init_sbus(true); // Simulation
245  while (true) {
246  if (port > 0) {
247  int x = receive_sbus(port);
248  if (x) {
250  }
251  }
252  usleep(5000);
253  }
254 }
ControlParameterRequestKind requestKind
SpiCommand * spiCommand
Definition: RobotRunner.h:51
TiBoardData * tiBoardData
Definition: RobotRunner.h:53
SpiData * spiData
Definition: RobotRunner.h:50
ControlParameterCollection collection
ControlParameterValueKind _kind
ControlParameterValueKind parameterKind
ControlParameters * _userParams
std::string controlParameterValueKindToString(ControlParameterValueKind valueKind)
std::map< std::string, ControlParameter * > _map
VectorNavData * vectorNavData
Definition: RobotRunner.h:48
std::thread * sbus_thread
void run() override
Definition: RobotRunner.cpp:67
ControlParameterRequestKind requestKind
TiBoardCommand * tiBoardCommand
Definition: RobotRunner.h:52
void attach(const std::string &name)
Definition: SharedMemory.h:191
char name[CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH]
void init() override
Definition: RobotRunner.cpp:28
void set(ControlParameterValue value, ControlParameterValueKind kind)
ControlParameterValueKind parameterKind
#define DEVELOPMENT_SIMULATOR_SHARED_MEMORY_NAME
Definition: SharedMemory.h:21
void sbus_packet_complete()
Function which handles the completion of an SBUS Packet and overrides the LCM control settings as des...
GamepadCommand * driverCommand
Definition: RobotRunner.h:46
CheetahVisualization * cheetahMainVisualization
Definition: RobotRunner.h:56
int init_sbus(int is_simulator)
Definition: rt_sbus.cpp:119
ControlParameter & lookup(const std::string &name)
ControlParameterValue get(ControlParameterValueKind kind)
RobotControlParameters * controlParameters
Definition: RobotRunner.h:54
CheaterState< double > * cheaterState
Definition: RobotRunner.h:49
Common Leg Control Interface and Leg Control Algorithms.
char name[CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH]
virtual ControlParameters * getUserControlParameters()=0
SimulatorMode _simMode
RobotRunner * _robotRunner
uint64_t u64
Definition: cTypes.h:17
RobotControlParameters _robotParams
int receive_sbus(int port)
Definition: rt_sbus.cpp:108
VisualizationData * visualizationData
Definition: RobotRunner.h:55
SharedMemoryObject< SimulatorSyncronizedMessage > _sharedMemory
std::string generateUnitializedList()
RobotController * _robot_ctrl
Definition: RobotRunner.h:44
RobotType robotType
Definition: RobotRunner.h:47