Tetrapod Project
log_utils.cpp
Go to the documentation of this file.
1 /*******************************************************************/
2 /* AUTHOR: Paal Arthur S. Thorseth */
3 /* ORGN: Dept of Eng Cybernetics, NTNU Trondheim */
4 /* FILE: log_utils.cpp */
5 /* DATE: Jun 12, 2021 */
6 /* */
7 /* Copyright (C) 2021 Paal Arthur S. Thorseth, */
8 /* Adrian B. Ghansah */
9 /* */
10 /* This program is free software: you can redistribute it */
11 /* and/or modify it under the terms of the GNU General */
12 /* Public License as published by the Free Software Foundation, */
13 /* either version 3 of the License, or (at your option) any */
14 /* later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty */
18 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19 /* See the GNU General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU General Public */
22 /* License along with this program. If not, see */
23 /* <https://www.gnu.org/licenses/>. */
24 /* */
25 /*******************************************************************/
26 
27 #include <log_utils/log_utils.h>
28 
29 // Constructor
31 {
32  this->InitRos();
33  this->InitRosQueueThreads();
34 
35  this->genCoord.setZero();
36  this->genVel.setZero();
37  this->fPos.setZero();
38 
39 
40 }
41 
42 // Destructor
44 {
45  this->rosNode->shutdown();
46 
47  this->rosProcessQueue.clear();
48  this->rosProcessQueue.disable();
49  this->rosProcessQueueThread.join();
50 
51  this->rosPublishQueue.clear();
52  this->rosPublishQueue.disable();
53  this->rosPublishQueueThread.join();
54 }
55 
56 // Write to log
58 {
59  // Update time stamps
60  this->jointState.header.stamp = ros::Time::now();
61  this->jointStateCmd.header.stamp = ros::Time::now();
62 
63  // Publish
64  this->jointStatePub.publish(this->jointState);
65  this->jointStateCmdPub.publish(this->jointStateCmd);
66 }
67 
68 // Callback for ROS Generalized Coordinates messages
69 void LogUtils::OnGenCoordMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
70 {
71  std::vector<double> data = _msg->data;
72 
73  this->genCoord = Eigen::Map<Eigen::MatrixXd>(data.data(), 18, 1);
74 
75  //debug_utils::printGeneralizedCoordinates(this->genCoord);
76 
78  {
79  ROS_ERROR("[LogUtils::OnGenCoordMsg] Could not solve forward kinematics!");
80  }
81 
82  //debug_utils::printFootstepPositions(this->fPos);
83 
84 }
85 
86 // Callback for ROS Generalized Velocities messages
87 void LogUtils::OnGenVelMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
88 {
89  std::vector<double> data = _msg->data;
90 
91  this->genVel = Eigen::Map<Eigen::MatrixXd>(data.data(), 18, 1);
92 
93 }
94 
95 // Callback for ROS Joint State messages
96 void LogUtils::OnJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg)
97 {
98  this->jointState.name = _msg->name;
99  this->jointState.position = _msg->position;
100  this->jointState.velocity = _msg->velocity;
101  this->jointState.effort = _msg->effort;
102 }
103 
104 // Callback for ROS Joint State Command messages
105 void LogUtils::OnJointStateCmdMsg(const sensor_msgs::JointStateConstPtr &_msg)
106 {
107  this->jointStateCmd.name = _msg->name;
108  this->jointStateCmd.position = _msg->position;
109  this->jointStateCmd.velocity = _msg->velocity;
110  this->jointStateCmd.effort = _msg->effort;
111 }
112 
113 // Setup thread to publish messages
115 {
116  static const double timeout = 0.01;
117  while (this->rosNode->ok())
118  {
119  this->rosPublishQueue.callAvailable(ros::WallDuration(timeout));
120  }
121 }
122 
123 // Setup thread to process messages
125 {
126  static const double timeout = 0.01;
127  while (this->rosNode->ok())
128  {
129  this->rosProcessQueue.callAvailable(ros::WallDuration(timeout));
130  }
131 }
132 
133 // Initialize ROS
135 {
136  if (!ros::isInitialized())
137  {
138  int argc = 0;
139  char **argv = NULL;
140  ros::init(
141  argc,
142  argv,
143  "log_utils_node",
144  ros::init_options::NoSigintHandler
145  );
146  }
147 
148  this->rosNode.reset(new ros::NodeHandle("log_utils_node"));
149 
150  ros::SubscribeOptions gen_coord_so =
151  ros::SubscribeOptions::create<std_msgs::Float64MultiArray>(
152  "/my_robot/gen_coord",
153  1,
154  boost::bind(&LogUtils::OnGenCoordMsg, this, _1),
155  ros::VoidPtr(),
156  &this->rosProcessQueue
157  );
158 
159  ros::SubscribeOptions gen_vel_so =
160  ros::SubscribeOptions::create<std_msgs::Float64MultiArray>(
161  "/my_robot/gen_vel",
162  1,
163  boost::bind(&LogUtils::OnGenVelMsg, this, _1),
164  ros::VoidPtr(),
165  &this->rosProcessQueue
166  );
167 
168  ros::SubscribeOptions joint_state_so =
169  ros::SubscribeOptions::create<sensor_msgs::JointState>(
170  "/my_robot/joint_state",
171  1,
172  boost::bind(&LogUtils::OnJointStateMsg, this, _1),
173  ros::VoidPtr(),
174  &this->rosProcessQueue
175  );
176 
177  ros::SubscribeOptions joint_state_cmd_so =
178  ros::SubscribeOptions::create<sensor_msgs::JointState>(
179  "/my_robot/joint_state_cmd",
180  1,
181  boost::bind(&LogUtils::OnJointStateCmdMsg, this, _1),
182  ros::VoidPtr(),
183  &this->rosProcessQueue
184  );
185 
186  ros::AdvertiseOptions gen_coord_log_ao =
187  ros::AdvertiseOptions::create<std_msgs::Float64MultiArray>(
188  "/log/gen_coord",
189  1,
190  ros::SubscriberStatusCallback(),
191  ros::SubscriberStatusCallback(),
192  ros::VoidPtr(),
193  &this->rosPublishQueue
194  );
195 
196  ros::AdvertiseOptions gen_vel_log_ao =
197  ros::AdvertiseOptions::create<std_msgs::Float64MultiArray>(
198  "/log/gen_vel",
199  1,
200  ros::SubscriberStatusCallback(),
201  ros::SubscriberStatusCallback(),
202  ros::VoidPtr(),
203  &this->rosPublishQueue
204  );
205 
206  ros::AdvertiseOptions joint_state_log_ao =
207  ros::AdvertiseOptions::create<sensor_msgs::JointState>(
208  "/log/joint_state",
209  1,
210  ros::SubscriberStatusCallback(),
211  ros::SubscriberStatusCallback(),
212  ros::VoidPtr(),
213  &this->rosPublishQueue
214  );
215 
216  ros::AdvertiseOptions joint_state_cmd_log_ao =
217  ros::AdvertiseOptions::create<sensor_msgs::JointState>(
218  "/log/joint_state_cmd",
219  1,
220  ros::SubscriberStatusCallback(),
221  ros::SubscriberStatusCallback(),
222  ros::VoidPtr(),
223  &this->rosPublishQueue
224  );
225 
226  this->genCoordSub = this->rosNode->subscribe(gen_coord_so);
227 
228  this->genVelSub = this->rosNode->subscribe(gen_vel_so);
229 
230  this->jointStateSub = this->rosNode->subscribe(joint_state_so);
231 
232  this->jointStateCmdSub = this->rosNode->subscribe(joint_state_cmd_so);
233 
234  this->genCoordLogPub = this->rosNode->advertise(gen_coord_log_ao);
235 
236  this->genVelLogPub = this->rosNode->advertise(gen_vel_log_ao);
237 
238  this->jointStatePub = this->rosNode->advertise(joint_state_log_ao);
239 
240  this->jointStateCmdPub = this->rosNode->advertise(joint_state_cmd_log_ao);
241 
242 }
243 
244 // Initialize ROS Publish and Process Queue Threads
246 {
247  this->rosPublishQueueThread = std::thread(
248  std::bind(&LogUtils::PublishQueueThread, this)
249  );
250 
251  this->rosProcessQueueThread = std::thread(
252  std::bind(&LogUtils::ProcessQueueThread, this)
253  );
254 }
bool SolveForwardKinematics(const GeneralizedCoordinates &_q, FootstepPositions &_f_pos)
The SolveForwardKinematics function calculates the Forward Kinematics, i.e. maps joint angles in Join...
Definition: kinematics.cpp:165
void InitRos()
The InitRos function is called to initialize ROS.
Definition: log_utils.cpp:134
LogUtils()
Constructor.
Definition: log_utils.cpp:30
std::unique_ptr< ros::NodeHandle > rosNode
Node used for ROS transport.
Definition: log_utils.h:126
ros::Subscriber genVelSub
ROS Generalized Coordinates Subscriber.
Definition: log_utils.h:132
void ProcessQueueThread()
The ProcessQueueThread function is a ROS helper function that processes messages.
Definition: log_utils.cpp:124
sensor_msgs::JointState jointStateCmd
Joint State Command.
Definition: log_utils.h:117
void OnGenCoordMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
The OnGenCoordMsg function handles an incoming generalized coordinates message from ROS.
Definition: log_utils.cpp:69
std::thread rosProcessQueueThread
Thread running the rosProcessQueue.
Definition: log_utils.h:165
virtual ~LogUtils()
Destructor.
Definition: log_utils.cpp:43
Eigen::Matrix< Eigen::Vector3d, 4, 1 > fPos
Footstep positions.
Definition: log_utils.h:120
sensor_msgs::JointState jointState
Joint State.
Definition: log_utils.h:114
void OnGenVelMsg(const std_msgs::Float64MultiArrayConstPtr &_msg)
The OnGenVelMsg function handles an incoming generalized velocities message from ROS.
Definition: log_utils.cpp:87
std::thread rosPublishQueueThread
Thread running the rosPublishQueue.
Definition: log_utils.h:162
Kinematics kinematics
Kinematics.
Definition: log_utils.h:105
Eigen::Matrix< double, 18, 1 > genVel
Generalized Velocities.
Definition: log_utils.h:111
ros::Publisher genCoordLogPub
ROS Generalized Coordinates Log Publisher.
Definition: log_utils.h:144
Eigen::Matrix< double, 18, 1 > genCoord
Generalized Coordinates.
Definition: log_utils.h:108
ros::Subscriber jointStateSub
ROS Joint State Subscriber.
Definition: log_utils.h:135
ros::Publisher genVelLogPub
ROS Generalized Velocities Log Publisher.
Definition: log_utils.h:147
void OnJointStateMsg(const sensor_msgs::JointStateConstPtr &_msg)
The OnJointStateMsg function handles an incoming joint state message from ROS.
Definition: log_utils.cpp:96
void InitRosQueueThreads()
The InitRosQueueThreads function is called to initialize the ROS Publish and Process Queue Threads.
Definition: log_utils.cpp:245
ros::Publisher jointStateCmdPub
ROS Joint State Command Log Publisher.
Definition: log_utils.h:153
void WriteToLog()
Definition: log_utils.cpp:57
ros::Subscriber genCoordSub
ROS Generalized Coordinates Subscriber.
Definition: log_utils.h:129
void OnJointStateCmdMsg(const sensor_msgs::JointStateConstPtr &_msg)
The OnJointStateCmdMsg function handles an incoming joint state command message from ROS.
Definition: log_utils.cpp:105
ros::CallbackQueue rosProcessQueue
ROS callbackque that helps process messages.
Definition: log_utils.h:159
void PublishQueueThread()
The PublishQueueThread function is a ROS helper function that publish state messages.
Definition: log_utils.cpp:114
ros::Subscriber jointStateCmdSub
ROS Joint State Command Subscriber.
Definition: log_utils.h:138
ros::Publisher jointStatePub
ROS Joint State Log Publisher.
Definition: log_utils.h:150
ros::CallbackQueue rosPublishQueue
ROS callbackque that helps publish messages.
Definition: log_utils.h:156