Cheetah Software  1.0
SimulationBridge Class Reference

#include <SimulationBridge.h>

+ Collaboration diagram for SimulationBridge:

Public Member Functions

 SimulationBridge (RobotType robot, RobotController *robot_ctrl)
 
void run ()
 
void handleControlParameters ()
 
void runRobotControl ()
 
 ~SimulationBridge ()
 
void run_sbus ()
 

Private Attributes

PeriodicTaskManager taskManager
 
bool _firstControllerRun = true
 
PeriodicTaskManager_fakeTaskManager = nullptr
 
RobotType _robot
 
RobotRunner_robotRunner = nullptr
 
SimulatorMode _simMode
 
SharedMemoryObject< SimulatorSyncronizedMessage_sharedMemory
 
RobotControlParameters _robotParams
 
ControlParameters_userParams = nullptr
 
u64 _iterations = 0
 
std::thread * sbus_thread
 

Detailed Description

Definition at line 18 of file SimulationBridge.h.

Constructor & Destructor Documentation

SimulationBridge::SimulationBridge ( RobotType  robot,
RobotController robot_ctrl 
)
inlineexplicit

Definition at line 20 of file SimulationBridge.h.

References _fakeTaskManager, _robotRunner, _userParams, RobotController::getUserControlParameters(), handleControlParameters(), run(), and runRobotControl().

20  :
21  _robot(robot) {
23  _robotRunner = new RobotRunner(robot_ctrl, _fakeTaskManager, 0, "robot-task");
24  _userParams = robot_ctrl->getUserControlParameters();
25 
26  }
ControlParameters * _userParams
virtual ControlParameters * getUserControlParameters()=0
RobotRunner * _robotRunner
PeriodicTaskManager * _fakeTaskManager

+ Here is the call graph for this function:

SimulationBridge::~SimulationBridge ( )
inline

Definition at line 30 of file SimulationBridge.h.

References _fakeTaskManager, _robotRunner, and run_sbus().

30  {
31  delete _fakeTaskManager;
32  delete _robotRunner;
33  }
RobotRunner * _robotRunner
PeriodicTaskManager * _fakeTaskManager

+ Here is the call graph for this function:

Member Function Documentation

void SimulationBridge::handleControlParameters ( )

This function handles a a control parameter message from the simulator

Definition at line 71 of file SimulationBridge.cpp.

References ControlParameter::_kind, ControlParameterCollection::_map, _robotParams, _sharedMemory, _userParams, ControlParameters::collection, controlParameterValueKindToString(), ControlParameter::get(), GET_ROBOT_PARAM_BY_NAME, ControlParameterCollection::lookup(), ControlParameterRequest::name, ControlParameterResponse::name, ControlParameterResponse::nParameters, ControlParameterRequest::parameterKind, ControlParameterResponse::parameterKind, ControlParameterRequest::requestKind, ControlParameterResponse::requestKind, ControlParameterRequest::requestNumber, ControlParameterResponse::requestNumber, ControlParameter::set(), SET_ROBOT_PARAM_BY_NAME, SET_USER_PARAM_BY_NAME, ControlParameterResponse::toString(), ControlParameterRequest::value, and ControlParameterResponse::value.

71  {
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 }
ControlParameterRequestKind requestKind
ControlParameterCollection collection
ControlParameterValueKind _kind
ControlParameterValueKind parameterKind
ControlParameters * _userParams
std::string controlParameterValueKindToString(ControlParameterValueKind valueKind)
std::map< std::string, ControlParameter * > _map
ControlParameterRequestKind requestKind
char name[CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH]
void set(ControlParameterValue value, ControlParameterValueKind kind)
ControlParameterValueKind parameterKind
ControlParameter & lookup(const std::string &name)
ControlParameterValue get(ControlParameterValueKind kind)
char name[CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH]
uint64_t u64
Definition: cTypes.h:17
RobotControlParameters _robotParams
SharedMemoryObject< SimulatorSyncronizedMessage > _sharedMemory

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SimulationBridge::run ( )

Definition at line 13 of file SimulationBridge.cpp.

References _iterations, _robot, _sharedMemory, _simMode, SharedMemoryObject< T >::attach(), DEVELOPMENT_SIMULATOR_SHARED_MEMORY_NAME, DO_NOTHING, EXIT, handleControlParameters(), RUN_CONTROL_PARAMETERS, RUN_CONTROLLER, and runRobotControl().

13  {
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 }
void attach(const std::string &name)
Definition: SharedMemory.h:191
#define DEVELOPMENT_SIMULATOR_SHARED_MEMORY_NAME
Definition: SharedMemory.h:21
SimulatorMode _simMode
SharedMemoryObject< SimulatorSyncronizedMessage > _sharedMemory

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SimulationBridge::run_sbus ( )

Definition at line 242 of file SimulationBridge.cpp.

References init_sbus(), receive_sbus(), and sbus_packet_complete().

242  {
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 }
void sbus_packet_complete()
Function which handles the completion of an SBUS Packet and overrides the LCM control settings as des...
int init_sbus(int is_simulator)
Definition: rt_sbus.cpp:119
int receive_sbus(int port)
Definition: rt_sbus.cpp:108

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SimulationBridge::runRobotControl ( )

Definition at line 186 of file SimulationBridge.cpp.

References _firstControllerRun, ControlParameterCollection::_map, _robot, RobotRunner::_robot_ctrl, _robotParams, _robotRunner, _sharedMemory, _simMode, RobotRunner::cheaterState, RobotRunner::cheetahMainVisualization, ControlParameters::collection, RobotRunner::controlParameters, RobotRunner::driverCommand, ControlParameters::generateUnitializedList(), RobotController::getUserControlParameters(), RobotRunner::init(), ControlParameters::isFullyInitialized(), RobotRunner::robotType, RobotRunner::run(), RUN_CONTROLLER, run_sbus(), sbus_thread, RobotRunner::spiCommand, RobotRunner::spiData, RobotRunner::tiBoardCommand, RobotRunner::tiBoardData, RobotRunner::vectorNavData, and RobotRunner::visualizationData.

186  {
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 }
SpiCommand * spiCommand
Definition: RobotRunner.h:51
TiBoardData * tiBoardData
Definition: RobotRunner.h:53
SpiData * spiData
Definition: RobotRunner.h:50
ControlParameterCollection collection
std::map< std::string, ControlParameter * > _map
VectorNavData * vectorNavData
Definition: RobotRunner.h:48
std::thread * sbus_thread
void run() override
Definition: RobotRunner.cpp:67
TiBoardCommand * tiBoardCommand
Definition: RobotRunner.h:52
void init() override
Definition: RobotRunner.cpp:28
GamepadCommand * driverCommand
Definition: RobotRunner.h:46
CheetahVisualization * cheetahMainVisualization
Definition: RobotRunner.h:56
RobotControlParameters * controlParameters
Definition: RobotRunner.h:54
CheaterState< double > * cheaterState
Definition: RobotRunner.h:49
virtual ControlParameters * getUserControlParameters()=0
SimulatorMode _simMode
RobotRunner * _robotRunner
RobotControlParameters _robotParams
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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

PeriodicTaskManager* SimulationBridge::_fakeTaskManager = nullptr
private

Definition at line 39 of file SimulationBridge.h.

bool SimulationBridge::_firstControllerRun = true
private

Definition at line 38 of file SimulationBridge.h.

u64 SimulationBridge::_iterations = 0
private

Definition at line 46 of file SimulationBridge.h.

RobotType SimulationBridge::_robot
private

Definition at line 40 of file SimulationBridge.h.

RobotControlParameters SimulationBridge::_robotParams
private

Definition at line 44 of file SimulationBridge.h.

RobotRunner* SimulationBridge::_robotRunner = nullptr
private

Definition at line 41 of file SimulationBridge.h.

SharedMemoryObject<SimulatorSyncronizedMessage> SimulationBridge::_sharedMemory
private

Definition at line 43 of file SimulationBridge.h.

SimulatorMode SimulationBridge::_simMode
private

Definition at line 42 of file SimulationBridge.h.

ControlParameters* SimulationBridge::_userParams = nullptr
private

Definition at line 45 of file SimulationBridge.h.

std::thread* SimulationBridge::sbus_thread
private

Definition at line 48 of file SimulationBridge.h.

PeriodicTaskManager SimulationBridge::taskManager
private

Definition at line 37 of file SimulationBridge.h.


The documentation for this class was generated from the following files: