Cheetah Software  1.0
RobotRunner.cpp
Go to the documentation of this file.
1 #include "RobotRunner.h"
4 #include "Dynamics/Cheetah3.h"
5 #include "Dynamics/MiniCheetah.h"
6 
8 #include <ParamHandler.hpp>
9 #include <Utilities/Timer.h>
10 #include <unistd.h>
11 
13 #include "rt/rt_interface_lcm.h"
14 
16  PeriodicTaskManager* manager,
17  float period, std::string name):
18  PeriodicTask(manager, period, name),
19  _lcm(getLcmUrl(255)) {
20 
21  _robot_ctrl = robot_ctrl;
22 }
23 
29  printf("[RobotRunner] initialize\n");
30 
31  // Build the appropriate Quadruped object
33  _quadruped = buildMiniCheetah<float>();
34  } else {
35  _quadruped = buildCheetah3<float>();
36  }
37 
38  // Initialize the model and robot data
41 
42  // Always initialize the leg controller and state entimator
48 
49  // Controller initializations
59 
61 }
62 
68  // Run the state estimator step
72 
73  // Update the data from the robot
74  setupStep();
75 
76  static int count_ini(0);
77  ++count_ini;
78  if (count_ini < 10) {
79  _legController->setEnabled(false);
80  } else if (20 < count_ini && count_ini < 30) {
81  _legController->setEnabled(false);
82  } else if (40 < count_ini && count_ini < 50) {
83  _legController->setEnabled(false);
84  } else {
86 
87  // Controller
89  Mat3<float> kpMat;
90  Mat3<float> kdMat;
91  kpMat << 5, 0, 0, 0, 5, 0, 0, 0, 5;
92  kdMat << 0.1, 0, 0, 0, 0.1, 0, 0, 0, 0.1;
93 
94  for (int leg = 0; leg < 4; leg++) {
95  _legController->commands[leg].kpJoint = kpMat;
96  _legController->commands[leg].kdJoint = kdMat;
97  }
98  } else {
99  // Run Control
101 
102  // Update Visualization
104  }
105  }
106 
107 
108 
109  // Visualization (will make this into a separate function later)
110  for (int leg = 0; leg < 4; leg++) {
111  for (int joint = 0; joint < 3; joint++) {
112  cheetahMainVisualization->q[leg * 3 + joint] =
113  _legController->datas[leg].q[joint];
114  }
115  }
116  cheetahMainVisualization->p.setZero();
118 
119  // Sets the leg controller commands for the robot appropriate commands
120  finalizeStep();
121 }
122 
124  // Update the leg data
127  } else if (robotType == RobotType::CHEETAH_3) {
129  } else {
130  assert(false);
131  }
132 
133  // Setup the leg controller for a new iteration
135  _legController->setEnabled(true);
137 
138  // state estimator
139  // check transition to cheater mode:
140  if (!_cheaterModeEnabled && controlParameters->cheater_mode) {
141  printf("[RobotRunner] Transitioning to Cheater Mode...\n");
143  // todo any configuration
144  _cheaterModeEnabled = true;
145  }
146 
147  // check transition from cheater mode:
148  if (_cheaterModeEnabled && !controlParameters->cheater_mode) {
149  printf("[RobotRunner] Transitioning from Cheater Mode...\n");
151  // todo any configuration
152  _cheaterModeEnabled = false;
153  }
154 
155  // todo safety checks, sanity checks, etc...
156 }
157 
161  } else if (robotType == RobotType::CHEETAH_3) {
163  } else {
164  assert(false);
165  }
168  _lcm.publish("leg_control_command", &leg_control_command_lcm);
169  _lcm.publish("leg_control_data", &leg_control_data_lcm);
170  _lcm.publish("state_estimator", &state_estimator_lcm);
171  _iterations++;
172 }
173 
177  Vec4<float> contactDefault;
178  contactDefault << 0.5, 0.5, 0.5, 0.5;
179  _stateEstimator->setContactPhase(contactDefault);
180  if (cheaterMode) {
183  } else {
186  }
187 }
188 
190  delete _legController;
191  delete _stateEstimator;
192  delete _jpos_initializer;
193 }
194 
void finalizeStep()
SpiCommand * spiCommand
Definition: RobotRunner.h:51
TiBoardData * tiBoardData
Definition: RobotRunner.h:53
SpiData * spiData
Definition: RobotRunner.h:50
bool IsInitialized(LegController< T > *)
LegController< float > * _legController
Definition: RobotRunner.h:68
leg_control_data_lcmt leg_control_data_lcm
Definition: RobotRunner.h:76
VisualizationData * _visualizationData
LegController< float > * _legController
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
VectorNavData * vectorNavData
Definition: RobotRunner.h:48
void setLcm(leg_control_data_lcmt *data, leg_control_command_lcmt *command)
Utility function to build a Cheetah 3 Quadruped object.
void initializeStateEstimator(bool cheaterMode=false)
Utility function to build a Mini Cheetah Quadruped object.
void run() override
Definition: RobotRunner.cpp:67
EIGEN_MAKE_ALIGNED_OPERATOR_NEW RobotRunner(RobotController *, PeriodicTaskManager *, float, std::string)
Definition: RobotRunner.cpp:15
TiBoardCommand * tiBoardCommand
Definition: RobotRunner.h:52
Common framework for running robot controllers. This code is a common interface between control code ...
LegControllerData< T > datas[4]
Definition: LegController.h:70
std::string getLcmUrl(s64 ttl)
Definition: utilities.cpp:32
void init() override
Definition: RobotRunner.cpp:28
void setMaxTorqueCheetah3(T tau)
Definition: LegController.h:67
virtual void runController()=0
GamepadCommand * driverCommand
Definition: RobotRunner.h:46
void updateCommand(SpiCommand *spiCommand)
CheetahVisualization * cheetahMainVisualization
Definition: RobotRunner.h:56
bool _cheaterModeEnabled
Definition: RobotRunner.h:71
Timer for measuring how long things take.
StateEstimate< float > * _stateEstimate
RobotControlParameters * controlParameters
Definition: RobotRunner.h:54
void setEnabled(bool enabled)
Definition: LegController.h:61
GamepadCommand * _driverCommand
RobotControlParameters * _controlParameters
void updateData(const SpiData *spiData)
void cleanup() override
JPosInitializer< float > * _jpos_initializer
Definition: RobotRunner.h:66
virtual void updateVisualization()=0
void run(CheetahVisualization *visualization=nullptr)
CheaterState< double > * cheaterState
Definition: RobotRunner.h:49
StateEstimatorContainer< float > * _stateEstimator
StateEstimate< float > _stateEstimate
Definition: RobotRunner.h:69
FloatingBaseModel< float > * _model
RobotType _robotType
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
Quadruped< float > _quadruped
Definition: RobotRunner.h:67
All State Estimation Algorithms.
FloatingBaseModel< float > _model
Definition: RobotRunner.h:79
lcm::LCM _lcm
Definition: RobotRunner.h:73
leg_control_command_lcmt leg_control_command_lcm
Definition: RobotRunner.h:74
void setLcm(state_estimator_lcmt &lcm_data)
void setupStep()
virtual void initializeController()=0
void setContactPhase(Vec4< T > &phase)
StateEstimatorContainer< float > * _stateEstimator
Definition: RobotRunner.h:70
virtual ~RobotRunner()
VisualizationData * visualizationData
Definition: RobotRunner.h:55
u64 _iterations
Definition: RobotRunner.h:80
All Contact Estimation Algorithms.
state_estimator_lcmt state_estimator_lcm
Definition: RobotRunner.h:75
Quadruped< float > * _quadruped
FloatingBaseModel< T > buildModel()
Definition: Quadruped.cpp:113
RobotController * _robot_ctrl
Definition: RobotRunner.h:44
RobotType robotType
Definition: RobotRunner.h:47
All Orientation Estimation Algorithms.
LegControllerCommand< T > commands[4]
Definition: LegController.h:69