Tetrapod Project
trajectory_tracking.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include <ros/console.h>
3 
5 
6 int main(int argc, char **argv)
7 {
8  enum CONTROL_MODE {POSITION, VELOCITY, TORQUE};
9 
10  // Choose what kind of controller to use
11  const CONTROL_MODE control_mode = POSITION;
12 
13  const int NUMBER_OF_GAIT_CYCLES = 2;
14 
15  // Set the publish frequency
16  double publish_frequency = 200.0;
17 
18  // Create a SingleLegController instance which controls and interfaces with the motors
19  SingleLegController controller(publish_frequency);
20 
21  // Set up ROS communication
22  controller.initROS();
23 
24  // Set the rate for which we check for new incomming messages
25  ros::Rate check_for_messages_rate(1);
26 
27  // Set the control command rate
28  ros::Rate send_control_command_rate(publish_frequency);
29 
30  // Wait for initial states from the actuators
31  while(!controller.isInitialStateReceived())
32  {
33  // Do a ROS spin to check if we have received any new messages
34  controller.checkForNewMessages();
35 
36  // Wait for a small duration
37  check_for_messages_rate.sleep();
38 
39  // Report that we are waiting for initial states
40  ROS_INFO("WAITING FOR INITIAL STATE");
41  }
42 
43  // Report that we have received the initial states
44  ROS_INFO("Initial state received");
45 
46  // Wait for a confirmation message before moving on
47  while(!controller.readyToProceed())
48  {
49  // Do a ROS spin to check if we have received any new messages
50  controller.checkForNewMessages();
51 
52  // Wait for a small duration
53  check_for_messages_rate.sleep();
54 
55  // Report that we are waiting for start signal
56  ROS_INFO("WAITING FOR START MESSAGE");
57  }
58 
59  // Reset the ready to proceed flag
60  controller.resetReadyToProceed();
61 
62  // Move the joint to the start position
63  controller.moveFootToNominalPosition();
64 
65  int number_of_phase_cycles = 0;
66 
67  while(true)
68  {
69  // Do a ROS spin to check if we have received any new messages
70  controller.checkForNewMessages();
71 
72  // Update the estimated joint states of the robot based on the latest available information
73  if(controller.updateGaitPhase())
74  {
75  number_of_phase_cycles++;
76 
77  if(number_of_phase_cycles >= NUMBER_OF_GAIT_CYCLES * 2)
78  {
79  return 0;
80  }
81  }
82 
83  // Update the setpoint trajectory
84  controller.updateFootTrajectoryReference();
85 
86  // Send the position controller
87  controller.updateJointReferences();
88 
89  if(control_mode == CONTROL_MODE::POSITION)
90  {
91  controller.sendJointPositionCommands();
92  }
93  else if(control_mode == CONTROL_MODE::VELOCITY)
94  {
95  // Update the joint velocity commands
96  controller.updateJointVelocityCommands();
97 
98  // Send the the joint velocity commands to the motors
99  controller.sendJointVelocityCommands();
100  }
101  else if(control_mode == CONTROL_MODE::TORQUE)
102  {
103  // Update the joint torques
104  controller.updateJointTorqueCommands();
105 
106  // Send a command to the motors
107  controller.sendJointTorqueCommands();
108  }
109 
110  // Print all the states to the screen
111  controller.printAllStates();
112 
113  // Write all the states to the logger
114  controller.writeToLog();
115 
116  // Wait a short duration before starting the next loop
117  send_control_command_rate.sleep();
118  }
119 
120  return 0;
121 }
void writeToLog()
Write the joint references and estimated states to the log.
void updateJointVelocityCommands()
Updates the joint velocity control commands based on the joint velocity reference and the joint posit...
void printAllStates()
Print various information about all joints.
void updateFootTrajectoryReference()
Update the foot trajectory based on the leg state.
bool moveFootToNominalPosition()
The function tries to move the foot to the nominal trajectory position.
void resetReadyToProceed()
Set the ready_to_proceed flag to false.
void initROS()
This function initilizes ROS.
void sendJointVelocityCommands()
Updates the joint velocity commands and send them to the motors.
void sendJointPositionCommands()
Sends a joint position commands to the motors.
bool isInitialStateReceived()
Check if the uninitalized joint state of the robot has been overridden by measurements.
void updateJointTorqueCommands(const Eigen::Matrix< double, 3, 1 > &_joint_pos_ref, const Eigen::Matrix< double, 3, 1 > &_joint_vel_ref, const Eigen::Matrix< double, 3, 1 > &_joint_acc_ref, const Eigen::Matrix< double, 3, 1 > &_joint_pos, const Eigen::Matrix< double, 3, 1 > &_joint_vel)
Update the joint torqes references based on the joint trajectories and joint states.
void updateJointReferences()
Update The joint trajectory reference based on the spatial foot trajectory.
bool updateGaitPhase()
Increments the gait cycle iterator and updates the leg state.
void sendJointTorqueCommands()
Updates the joint torque control commands based on the desired foot position.
void checkForNewMessages()
Function used to check if any new ROS messages has been received.
bool readyToProceed()
Check whether or not a ready to proceed message has been received.
int main(int argc, char **argv)