Tetrapod Project
height_control.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 = TORQUE;
12 
13  // Set the publish frequency
14  double publish_frequency = 200.0;
15 
16  // Create a SingleLegController instance which controls and interfaces with the motors
17  SingleLegController controller(publish_frequency);
18 
19  // Set up ROS communication
20  controller.initROS();
21 
22  // Set the rate for which we check for new incomming messages
23  ros::Rate check_for_messages_rate(1);
24 
25  // Set the control command rate
26  ros::Rate send_control_command_rate(publish_frequency);
27 
28  // Wait for initial states from the actuators
29  while(!controller.isInitialStateReceived())
30  {
31  // Do a ROS spin to check if we have received any new messages
32  controller.checkForNewMessages();
33 
34  // Wait for a small duration
35  check_for_messages_rate.sleep();
36 
37  // Report that we are waiting for initial states
38  ROS_INFO("WAITING FOR INITIAL STATE");
39  }
40 
41  // Report that we have received the initial states
42  ROS_INFO("Initial state received");
43 
44  // Wait for a confirmation message before moving on
45  while(!controller.readyToProceed())
46  {
47  // Do a ROS spin to check if we have received any new messages
48  controller.checkForNewMessages();
49 
50  // Wait for a small duration
51  check_for_messages_rate.sleep();
52 
53  // Report that we are waiting for start signal
54  ROS_INFO("WAITING FOR START MESSAGE");
55  }
56 
57  // Reset the ready to proceed flag
58  controller.resetReadyToProceed();
59 
60  Eigen::Matrix<double, 3, 1> foot_goal_position(0.0, 0.4, -0.35);
61 
62  // Move the joint to the start position
63  controller.moveFootToPosition(foot_goal_position);
64 
65  controller.setPoseGoal(foot_goal_position);
66 
67  int setpoint_loop_counter = 0;
68 
69  int setpoint = 0;
70 
71  while(true)
72  {
73  // Do a ROS spin to check if we have received any new messages
74  controller.checkForNewMessages();
75 
77  {
78  if((setpoint == 0) && (setpoint_loop_counter > 1 * publish_frequency))
79  {
80  foot_goal_position(2) = -0.25;
81  setpoint = 1;
82  controller.setPoseGoal(foot_goal_position);
83  ROS_WARN("S1");
84  }
85  else if((setpoint == 1) && (setpoint_loop_counter > 3 * publish_frequency))
86  {
87  foot_goal_position(2) = -0.35;
88  setpoint = 2;
89  controller.setPoseGoal(foot_goal_position);
90  ROS_WARN("S2");
91  }
92  else if((setpoint == 2) && (setpoint_loop_counter > 5 * publish_frequency))
93  {
94  foot_goal_position(2) = -0.20;
95  setpoint = 3;
96  controller.setPoseGoal(foot_goal_position);
97  ROS_WARN("S3");
98  }
99  else if(setpoint_loop_counter > 7 * publish_frequency)
100  {
101  return 0;
102  }
103  }
104 
105  if(control_mode == CONTROL_MODE::POSITION)
106  {
107  controller.sendJointPositionCommands();
108  }
109  else if(control_mode == CONTROL_MODE::VELOCITY)
110  {
111  // Update the joint velocity commands
112  controller.updateJointVelocityCommands();
113 
114  // Send the the joint velocity commands to the motors
115  controller.sendJointVelocityCommands();
116  }
117  else if(control_mode == CONTROL_MODE::TORQUE)
118  {
119  // Update the joint torques
120  //controller.updateJointTorqueCommands();
121  controller.updateClosedLoopTorqueCommands();
122 
123  // Send a command to the motors
124  controller.sendJointTorqueCommands();
125  }
126 
127  ROS_INFO("SP: %d\t", setpoint);
128 
129  // Print all the states to the screen
130  controller.printAllStates();
131 
132  // Write all the states to the logger
133  controller.writeToLog();
134 
135  // Wait a short duration before starting the next loop
136  send_control_command_rate.sleep();
137 
138  // Increment the loop counter
139  setpoint_loop_counter++;
140  }
141 
142  return 0;
143 }
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 updateClosedLoopTorqueCommands()
A non model based torque controller.
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 updatePoseControlJointTrajectoryReference()
Update the pose control trajectory reference for the single leg.
bool isInitialStateReceived()
Check if the uninitalized joint state of the robot has been overridden by measurements.
void setPoseGoal(Eigen::Matrix< double, 3, 1 > _foot_pos)
Set the final and initial states for pose control trajectories.
bool moveFootToPosition(Eigen::Matrix< double, 3, 1 > _foot_goal_pos)
Creates a trajectory from the current foot position to the goal foot position and moves the foot to t...
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)