Cheetah Software  1.0
RobotInterface Class Reference

#include <RobotInterface.h>

+ Inheritance diagram for RobotInterface:
+ Collaboration diagram for RobotInterface:

Public Member Functions

EIGEN_MAKE_ALIGNED_OPERATOR_NEW RobotInterface (RobotType robotType, Graphics3D *gfx, PeriodicTaskManager *tm, ControlParameters &userParameters)
 
RobotControlParametersgetParams ()
 
void startInterface ()
 
void stopInterface ()
 
void lcmHandler ()
 
void sendControlParameter (const std::string &name, ControlParameterValue value, ControlParameterValueKind kind, bool isUser)
 
void handleControlParameter (const lcm::ReceiveBuffer *rbuf, const std::string &chan, const control_parameter_respones_lcmt *msg)
 
void handleVisualizationData (const lcm::ReceiveBuffer *rbuf, const std::string &chan, const cheetah_visualization_lcmt *msg)
 
void init ()
 
void run ()
 
void cleanup ()
 
virtual ~RobotInterface ()
 

Private Attributes

PeriodicTaskManager _taskManager
 
gamepad_lcmt _gamepad_lcmt
 
control_parameter_request_lcmt _parameter_request_lcmt
 
bool _pendingControlParameterSend = false
 
lcm::LCM _lcm
 
uint64_t _robotID
 
std::thread _lcmThread
 
VisualizationData _visualizationData
 
RobotControlParameters _controlParameters
 
ControlParameters_userParameters
 
Graphics3D_gfx
 
RobotType _robotType
 
bool _running = false
 
std::mutex _lcmMutex
 
std::condition_variable _lcmCV
 
bool _waitingForLcmResponse = false
 
bool _lcmResponseBad = true
 
Quadruped< double > _quadruped
 
FloatingBaseModel< double > _model
 
DynamicsSimulator< double > * _simulator = nullptr
 
FBModelState< double > _fwdKinState
 

Additional Inherited Members

- Private Member Functions inherited from PeriodicTask
 PeriodicTask (PeriodicTaskManager *taskManager, float period, std::string name)
 
void start ()
 
void stop ()
 
void printStatus ()
 
void clearMax ()
 
bool isSlow ()
 
virtual ~PeriodicTask ()
 
float getPeriod ()
 
float getRuntime ()
 
float getMaxPeriod ()
 
float getMaxRuntime ()
 

Detailed Description

Definition at line 26 of file RobotInterface.h.

Constructor & Destructor Documentation

RobotInterface::RobotInterface ( RobotType  robotType,
Graphics3D gfx,
PeriodicTaskManager tm,
ControlParameters userParameters 
)

Definition at line 8 of file RobotInterface.cpp.

References _controlParameters, Graphics3D::_drawList, _fwdKinState, _gfx, DrawList::_kinematicXform, _lcm, _model, _parameter_request_lcmt, _quadruped, _robotID, _robotType, _simulator, DrawList::_visualizationData, _visualizationData, DrawList::addCheckerboard(), DrawList::buildDrawList(), Quadruped< T >::buildModel(), CHEETAH_3, CHEETAH_3_DEFAULT_PARAMETERS, ControlParameters::generateUnitializedList(), getConfigDirectoryPath(), handleControlParameter(), handleVisualizationData(), ControlParameters::initializeFromYamlFile(), ControlParameters::isFullyInitialized(), MINI_CHEETAH, MINI_CHEETAH_DEFAULT_PARAMETERS, FBModelState< T >::q, FBModelState< T >::qd, RobotRunner::robotType, Graphics3D::setupCheetah3(), Graphics3D::setupMiniCheetah(), and DrawList::updateCheckerboard().

10  : PeriodicTask(tm, ROBOT_INTERFACE_UPDATE_PERIOD, "robot-interface"),
11  _lcm(getLcmUrl(255)),
12  _userParameters(userParameters) {
13  _parameter_request_lcmt.requestNumber = 0;
14  _gfx = gfx;
15  _robotType = robotType;
16  printf("[RobotInterface] Load parameters...\n");
20  } else if (_robotType == RobotType::CHEETAH_3) {
23  } else {
24  assert(false);
25  }
26 
28  printf("Not all robot control parameters were initialized. Missing:\n%s\n",
30  throw std::runtime_error("not all parameters initialized from ini file");
31  }
32  printf("[RobotInterface] Init LCM\n");
33  printf("[RobotInterface] Init graphics\n");
34  Vec4<float> robotColor;
35  robotColor << 0.2, 0.2, 0.6, 0.6;
36  _robotID = _robotType == RobotType::MINI_CHEETAH ? gfx->setupMiniCheetah(robotColor, true)
37  : gfx->setupCheetah3(robotColor, true);
38  printf("draw list has %lu items\n", _gfx->_drawList._kinematicXform.size());
40  Checkerboard checker(10, 10, 10, 10);
41  uint64_t floorID = _gfx->_drawList.addCheckerboard(checker);
42  _gfx->_drawList.updateCheckerboard(0, floorID);
44 
45  _lcm.subscribe("interface_response", &RobotInterface::handleControlParameter,
46  this);
47  _lcm.subscribe("main_cheetah_visualization",
49 
50  printf("[RobotInterface] Init dynamics\n");
51  _quadruped = robotType == RobotType::MINI_CHEETAH ? buildMiniCheetah<double>()
52  : buildCheetah3<double>();
55  DVec<double> zero12(12);
56  for (u32 i = 0; i < 12; i++) {
57  zero12[i] = 0.;
58  }
59 
60  _fwdKinState.q = zero12;
61  _fwdKinState.qd = zero12;
62 }
RobotType _robotType
VisualizationData * _visualizationData
Definition: DrawList.h:51
FloatingBaseModel< double > _model
#define MINI_CHEETAH_DEFAULT_PARAMETERS
size_t setupMiniCheetah(Vec4< float > color, bool useOld)
Definition: Graphics3D.cpp:122
PeriodicTask(PeriodicTaskManager *taskManager, float period, std::string name)
uint64_t _robotID
std::string getLcmUrl(s64 ttl)
Definition: utilities.cpp:32
size_t setupCheetah3(Vec4< float > color, bool useOld)
Definition: Graphics3D.cpp:117
FBModelState< double > _fwdKinState
std::string getConfigDirectoryPath()
Definition: utilities.cpp:30
void handleControlParameter(const lcm::ReceiveBuffer *rbuf, const std::string &chan, const control_parameter_respones_lcmt *msg)
uint32_t u32
Definition: cTypes.h:18
std::vector< QMatrix4x4 > _kinematicXform
Definition: DrawList.h:279
#define CHEETAH_3_DEFAULT_PARAMETERS
Quadruped< double > _quadruped
void handleVisualizationData(const lcm::ReceiveBuffer *rbuf, const std::string &chan, const cheetah_visualization_lcmt *msg)
VisualizationData _visualizationData
#define ROBOT_INTERFACE_UPDATE_PERIOD
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
control_parameter_request_lcmt _parameter_request_lcmt
Graphics3D * _gfx
void buildDrawList()
Definition: DrawList.cpp:266
void updateCheckerboard(T height, size_t id)
Definition: DrawList.h:229
DynamicsSimulator< double > * _simulator
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition: cppTypes.h:102
RobotControlParameters _controlParameters
void initializeFromYamlFile(const std::string &path)
std::string generateUnitializedList()
size_t addCheckerboard(Checkerboard &checkerBoard)
Definition: DrawList.cpp:215
FloatingBaseModel< T > buildModel()
Definition: Quadruped.cpp:113
ControlParameters & _userParameters
DrawList _drawList
Definition: Graphics3D.h:49

+ Here is the call graph for this function:

virtual RobotInterface::~RobotInterface ( )
inlinevirtual

Definition at line 50 of file RobotInterface.h.

References _simulator, and PeriodicTask::stop().

50  {
51  delete _simulator;
52  stop();
53  }
DynamicsSimulator< double > * _simulator

+ Here is the call graph for this function:

Member Function Documentation

void RobotInterface::cleanup ( )
inlinevirtual

Implements PeriodicTask.

Definition at line 49 of file RobotInterface.h.

49 {}
RobotControlParameters& RobotInterface::getParams ( )
inline

Definition at line 31 of file RobotInterface.h.

References _controlParameters, handleControlParameter(), handleVisualizationData(), lcmHandler(), sendControlParameter(), startInterface(), and stopInterface().

31 { return _controlParameters; }
RobotControlParameters _controlParameters

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RobotInterface::handleControlParameter ( const lcm::ReceiveBuffer *  rbuf,
const std::string &  chan,
const control_parameter_respones_lcmt *  msg 
)

Definition at line 152 of file RobotInterface.cpp.

References _lcmCV, _lcmMutex, _lcmResponseBad, _parameter_request_lcmt, and _waitingForLcmResponse.

154  {
155  (void)rbuf;
156  (void)chan;
157  if (!_waitingForLcmResponse) {
158  printf(
159  "[RobotInterface] Got a control parameter response when we weren't "
160  "expecting one, ignoring it!\n");
161  _lcmResponseBad = true;
162  return;
163  }
164 
165  // got a real response, let's check it
166  if (msg->requestNumber == _parameter_request_lcmt.requestNumber &&
167  msg->parameterKind == _parameter_request_lcmt.parameterKind &&
168  std::string((char *)msg->name) == (char *)_parameter_request_lcmt.name) {
169  _lcmResponseBad = false;
170  std::unique_lock<std::mutex> lock(_lcmMutex);
171  _waitingForLcmResponse = false;
172  _lcmCV.notify_all();
173  }
174 }
control_parameter_request_lcmt _parameter_request_lcmt
std::mutex _lcmMutex
bool _waitingForLcmResponse
std::condition_variable _lcmCV

+ Here is the caller graph for this function:

void RobotInterface::handleVisualizationData ( const lcm::ReceiveBuffer *  rbuf,
const std::string &  chan,
const cheetah_visualization_lcmt *  msg 
)

Definition at line 64 of file RobotInterface.cpp.

References _fwdKinState, _simulator, FBModelState< T >::bodyOrientation, FBModelState< T >::bodyPosition, DynamicsSimulator< T >::forwardKinematics(), FBModelState< T >::q, and DynamicsSimulator< T >::setState().

66  {
67  (void)rbuf;
68  (void)chan;
69  for (int i = 0; i < 3; i++) {
70  _fwdKinState.bodyPosition[i] = msg->x[i];
71  }
72 
73  for (int i = 0; i < 4; i++) {
74  _fwdKinState.bodyOrientation[i] = msg->quat[i];
75  }
76 
77  for (int i = 0; i < 12; i++) {
78  _fwdKinState.q[i] = msg->q[i];
79  }
80 
83 }
Vec3< T > bodyPosition
void forwardKinematics()
Do forward kinematics for feet.
void setState(const FBModelState< T > &state)
FBModelState< double > _fwdKinState
DynamicsSimulator< double > * _simulator
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Quat< T > bodyOrientation

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RobotInterface::init ( )
inlinevirtual

Implements PeriodicTask.

Definition at line 47 of file RobotInterface.h.

References run().

47 {}

+ Here is the call graph for this function:

void RobotInterface::lcmHandler ( )

Definition at line 202 of file RobotInterface.cpp.

References _lcm, and _running.

202  {
203  while (_running) {
204  _lcm.handleTimeout(1000);
205  }
206 }

+ Here is the caller graph for this function:

void RobotInterface::run ( )
virtual

Implements PeriodicTask.

Definition at line 85 of file RobotInterface.cpp.

References Graphics3D::_drawList, _gamepad_lcmt, _gfx, _lcm, _robotID, _simulator, GamepadCommand::get(), Graphics3D::getDriverCommand(), INTERFACE_LCM_NAME, and DrawList::updateRobotFromModel().

85  {
86  if (_gfx) {
88  _gfx->update();
91  }
92 }
void get(gamepad_lcmt *lcmt)
void updateRobotFromModel(DynamicsSimulator< T > &model, size_t id, bool updateOrigin=false)
Definition: DrawList.h:170
uint64_t _robotID
#define INTERFACE_LCM_NAME
Graphics3D * _gfx
GamepadCommand & getDriverCommand()
Definition: Graphics3D.h:52
DynamicsSimulator< double > * _simulator
gamepad_lcmt _gamepad_lcmt
DrawList _drawList
Definition: Graphics3D.h:49

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RobotInterface::sendControlParameter ( const std::string &  name,
ControlParameterValue  value,
ControlParameterValueKind  kind,
bool  isUser 
)

Definition at line 96 of file RobotInterface.cpp.

References _lcm, _lcmCV, _lcmMutex, _lcmResponseBad, _parameter_request_lcmt, _pendingControlParameterSend, _waitingForLcmResponse, controlParameterValueToString(), SET_ROBOT_PARAM_BY_NAME, SET_USER_PARAM_BY_NAME, and TIMES_TO_RESEND_CONTROL_PARAM.

98  {
100  printf(
101  "[ERROR] trying to send control parameter while a send is in progress, "
102  "ignoring!\n");
103  return;
104  }
106  for (int iteration = 0; iteration < TIMES_TO_RESEND_CONTROL_PARAM;) {
107  // new message
108  _parameter_request_lcmt.requestNumber++;
109 
110  // message data
111  _parameter_request_lcmt.requestKind = isUser ?
113  strcpy((char *)_parameter_request_lcmt.name, name.c_str());
114  memcpy(_parameter_request_lcmt.value, &value, sizeof(value));
115  _parameter_request_lcmt.parameterKind = (s8)kind;
116  printf("set %s to %s (%d)\n", name.c_str(),
117  controlParameterValueToString(value, kind).c_str(), iteration);
118 
119  // send
120  _lcm.publish("interface_request", &_parameter_request_lcmt);
121 
122  // wait for response with timeout
123  _waitingForLcmResponse = true;
124  _lcmResponseBad = true;
125  std::unique_lock<std::mutex> lock(_lcmMutex);
126 
127  if (_lcmCV.wait_for(lock, 100ms) == std::cv_status::no_timeout) {
128  _waitingForLcmResponse = false;
129  // check it
131  printf(
132  "[RobotInterface] Failed to send parameter %s (iter %d) "
133  "wakeup %d bad? %d\n",
134  name.c_str(), iteration, _waitingForLcmResponse, _lcmResponseBad);
135  usleep(100000); // sleep a bit to let other bad sends happen
136  } else {
137  iteration++;
138  }
139  } else {
140  _waitingForLcmResponse = false;
141  // fail!
142  printf(
143  "[RobotInterface] Failed to send parameter %s (iter %d timed out), "
144  "trying again...\n",
145  name.c_str(), iteration);
146  usleep(100000); // sleep a bit to let other bad sends happen
147  }
148  }
150 }
int8_t s8
Definition: cTypes.h:21
ControlParameterRequestKind
std::string controlParameterValueToString(ControlParameterValue v, ControlParameterValueKind kind)
#define TIMES_TO_RESEND_CONTROL_PARAM
control_parameter_request_lcmt _parameter_request_lcmt
bool _pendingControlParameterSend
std::mutex _lcmMutex
bool _waitingForLcmResponse
std::condition_variable _lcmCV

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RobotInterface::startInterface ( )

Definition at line 176 of file RobotInterface.cpp.

References _controlParameters, _lcmThread, ControlParameterCollection::_map, _running, _userParameters, ControlParameters::collection, lcmHandler(), sendControlParameter(), and PeriodicTask::start().

176  {
177  _running = true;
178  this->start();
179  _lcmThread = std::thread(&RobotInterface::lcmHandler, this);
180  printf("[RobotInterface] Send parameters to robot...\n");
181  for (auto &kv : _controlParameters.collection._map) {
182  sendControlParameter(kv.first, kv.second->get(kv.second->_kind),
183  kv.second->_kind, false);
184  }
185 
186  for (auto &kv : _userParameters.collection._map) {
187  sendControlParameter(kv.first, kv.second->get(kv.second->_kind),
188  kv.second->_kind, true);
189  }
190 
191 }
ControlParameterCollection collection
void sendControlParameter(const std::string &name, ControlParameterValue value, ControlParameterValueKind kind, bool isUser)
std::map< std::string, ControlParameter * > _map
std::thread _lcmThread
RobotControlParameters _controlParameters
ControlParameters & _userParameters

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void RobotInterface::stopInterface ( )

Definition at line 193 of file RobotInterface.cpp.

References _lcmThread, _running, _taskManager, and PeriodicTaskManager::stopAll().

193  {
194  printf("stopInterface\n");
195  _running = false;
197  printf("stopall done\n");
198  _lcmThread.join();
199  printf("lcmthread joined\n");
200 }
PeriodicTaskManager _taskManager
std::thread _lcmThread

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

RobotControlParameters RobotInterface::_controlParameters
private

Definition at line 64 of file RobotInterface.h.

FBModelState<double> RobotInterface::_fwdKinState
private

Definition at line 79 of file RobotInterface.h.

gamepad_lcmt RobotInterface::_gamepad_lcmt
private

Definition at line 57 of file RobotInterface.h.

Graphics3D* RobotInterface::_gfx
private

Definition at line 66 of file RobotInterface.h.

lcm::LCM RobotInterface::_lcm
private

Definition at line 60 of file RobotInterface.h.

std::condition_variable RobotInterface::_lcmCV
private

Definition at line 71 of file RobotInterface.h.

std::mutex RobotInterface::_lcmMutex
private

Definition at line 70 of file RobotInterface.h.

bool RobotInterface::_lcmResponseBad = true
private

Definition at line 73 of file RobotInterface.h.

std::thread RobotInterface::_lcmThread
private

Definition at line 62 of file RobotInterface.h.

FloatingBaseModel<double> RobotInterface::_model
private

Definition at line 77 of file RobotInterface.h.

control_parameter_request_lcmt RobotInterface::_parameter_request_lcmt
private

Definition at line 58 of file RobotInterface.h.

bool RobotInterface::_pendingControlParameterSend = false
private

Definition at line 59 of file RobotInterface.h.

Quadruped<double> RobotInterface::_quadruped
private

Definition at line 76 of file RobotInterface.h.

uint64_t RobotInterface::_robotID
private

Definition at line 61 of file RobotInterface.h.

RobotType RobotInterface::_robotType
private

Definition at line 67 of file RobotInterface.h.

bool RobotInterface::_running = false
private

Definition at line 68 of file RobotInterface.h.

DynamicsSimulator<double>* RobotInterface::_simulator = nullptr
private

Definition at line 78 of file RobotInterface.h.

PeriodicTaskManager RobotInterface::_taskManager
private

Definition at line 56 of file RobotInterface.h.

ControlParameters& RobotInterface::_userParameters
private

Definition at line 65 of file RobotInterface.h.

VisualizationData RobotInterface::_visualizationData
private

Definition at line 63 of file RobotInterface.h.

bool RobotInterface::_waitingForLcmResponse = false
private

Definition at line 72 of file RobotInterface.h.


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