Cheetah Software  1.0
DesiredStateCommand.cpp
Go to the documentation of this file.
1 /*========================= Gamepad Control ==========================*/
7 
8 /*=========================== Gait Data ===============================*/
12 template <typename T>
14  // Overall desired state
15  stateDes = Vec12<T>::Zero();
16  stateTrajDes = Eigen::Matrix<T, 12, 10>::Zero();
17 }
18 
19 template struct DesiredStateData<double>;
20 template struct DesiredStateData<float>;
21 
25 template <typename T>
27  data.zero();
28 
29  // Forward linear velocity
30  data.stateDes(6) =
31  deadband(gamepadCommand->leftStickAnalog[1], minVelX, maxVelX);
32 
33  // Lateral linear velocity
34  data.stateDes(7) =
35  deadband(gamepadCommand->leftStickAnalog[0], minVelY, maxVelY);
36 
37  // VErtical linear velocity
38  data.stateDes(8) = 0.0;
39 
40  // X position
41  data.stateDes(0) = stateEstimate->position(0) + dt * data.stateDes(6);
42 
43  // Y position
44  data.stateDes(1) = stateEstimate->position(1) + dt * data.stateDes(7);
45 
46  // Z position height
47  data.stateDes(2) = 0.45;
48 
49  // Roll rate
50  data.stateDes(9) = 0.0;
51 
52  // Pitch rate
53  data.stateDes(10) = 0.0;
54 
55  // Yaw turn rate
56  data.stateDes(11) =
57  deadband(gamepadCommand->rightStickAnalog[0], minTurnRate, maxTurnRate);
58 
59  // Roll
60  data.stateDes(3) = 0.0;
61 
62  // Pitch
63  data.stateDes(4) =
64  deadband(gamepadCommand->rightStickAnalog[1], minPitch, maxPitch);
65 
66  // Yaw
67  data.stateDes(5) = stateEstimate->rpy(2) + dt * data.stateDes(11);
68 }
69 
73 template <typename T>
75  A = Mat12<T>::Zero();
76  A(0, 0) = 1;
77  A(1, 1) = 1;
78  A(2, 2) = 1;
79  A(3, 3) = 1;
80  A(4, 4) = 1;
81  A(5, 5) = 1;
82  A(6, 6) = 1;
83  A(7, 7) = 1;
84  A(8, 8) = 1;
85  A(9, 9) = 1;
86  A(10, 10) = 1;
87  A(11, 11) = 1;
88  data.stateTrajDes.col(0) = data.stateDes;
89 
90  for (int k = 1; k < N; k++) {
91  A(0, 6) = dtVec(k - 1);
92  A(1, 7) = dtVec(k - 1);
93  A(2, 8) = dtVec(k - 1);
94  A(3, 9) = dtVec(k - 1);
95  A(4, 10) = dtVec(k - 1);
96  A(5, 11) = dtVec(k - 1);
97  data.stateTrajDes.col(k) = A * data.stateTrajDes.col(k - 1);
98  for (int i = 0; i < 12; i++) {
99  // std::cout << data.stateTrajDes(i, k) << " ";
100  }
101  // std::cout << std::endl;
102  }
103  // std::cout << std::endl;
104 }
105 
109 template <typename T>
110 float DesiredStateCommand<T>::deadband(float command, T minVal, T maxVal) {
111  if (command < deadbandRegion && command > -deadbandRegion) {
112  return 0.0;
113  } else {
114  return (command / (2)) * (maxVal - minVal);
115  }
116 }
117 
121 template <typename T>
123  // Increment printing iteration
124  printIter++;
125 
126  // Print at requested frequency
127  if (printIter == printNum) {
128  std::cout << "[DESIRED STATE COMMAND] Printing Raw Gamepad Info...\n";
129  std::cout << "---------------------------------------------------------\n";
130  std::cout << "Button Start: " << gamepadCommand->start
131  << " | Back: " << gamepadCommand->back << "\n";
132  std::cout << "Button A: " << gamepadCommand->a
133  << " | B: " << gamepadCommand->b << " | X: " << gamepadCommand->x
134  << " | Y: " << gamepadCommand->y << "\n";
135  std::cout << "Left Stick Button: " << gamepadCommand->leftStickButton
136  << " | X: " << gamepadCommand->leftStickAnalog[0]
137  << " | Y: " << gamepadCommand->leftStickAnalog[1] << "\n";
138  std::cout << "Right Analog Button: " << gamepadCommand->rightStickButton
139  << " | X: " << gamepadCommand->rightStickAnalog[0]
140  << " | Y: " << gamepadCommand->rightStickAnalog[1] << "\n";
141  std::cout << "Left Bumper: " << gamepadCommand->leftBumper
142  << " | Trigger Switch: " << gamepadCommand->leftTriggerButton
143  << " | Trigger Value: " << gamepadCommand->leftTriggerAnalog
144  << "\n";
145  std::cout << "Right Bumper: " << gamepadCommand->rightBumper
146  << " | Trigger Switch: " << gamepadCommand->rightTriggerButton
147  << " | Trigger Value: " << gamepadCommand->rightTriggerAnalog
148  << "\n\n";
149  std::cout << std::endl;
150 
151  // Reset iteration counter
152  printIter = 0;
153  }
154 }
155 
159 template <typename T>
161  // Increment printing iteration
162  printIter++;
163 
164  // Print at requested frequency
165  if (printIter == printNum) {
166  std::cout << "[DESIRED STATE COMMAND] Printing State Command Info...\n";
167  std::cout << "---------------------------------------------------------\n";
168  std::cout << "Position X: " << data.stateDes(0)
169  << " | Y: " << data.stateDes(1) << " | Z: " << data.stateDes(2)
170  << "\n";
171  std::cout << "Orientation Roll: " << data.stateDes(3)
172  << " | Pitch: " << data.stateDes(4)
173  << " | Yaw: " << data.stateDes(5) << "\n";
174  std::cout << "Velocity X: " << data.stateDes(6)
175  << " | Y: " << data.stateDes(7) << " | Z: " << data.stateDes(8)
176  << "\n";
177  std::cout << "Angular Velocity X: " << data.stateDes(9)
178  << " | Y: " << data.stateDes(10) << " | Z: " << data.stateDes(11)
179  << "\n";
180  std::cout << std::endl;
181  std::cout << std::endl;
182 
183  // Reset iteration counter
184  printIter = 0;
185  }
186 }
187 
188 template class DesiredStateCommand<double>;
189 template class DesiredStateCommand<float>;
typename Eigen::Matrix< T, 12, 12 > Mat12
Definition: cppTypes.h:74
T deadband(T x, T range)
Definition: utilities.h:46
Eigen::Matrix< T, 10, 1 > Vec10
Definition: cppTypes.h:38
Eigen::Matrix< T, 12, 1 > Vec12
Definition: cppTypes.h:42
float deadband(float command, T minVal, T maxVal)
void desiredStateTrajectory(int N, Vec10< T > dtVec)