turtlebot3_diagnostics.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Taehoon Lim (Darby) */
18 
19 #include <ros/ros.h>
20 #include <sensor_msgs/BatteryState.h>
21 #include <sensor_msgs/Imu.h>
22 #include <sensor_msgs/MagneticField.h>
23 #include <sensor_msgs/LaserScan.h>
24 #include <diagnostic_msgs/DiagnosticArray.h>
25 #include <turtlebot3_msgs/SensorState.h>
26 #include <turtlebot3_msgs/VersionInfo.h>
27 #include <string>
28 
29 #define SOFTWARE_VERSION "1.2.5"
30 #define HARDWARE_VERSION "2020.03.16"
31 #define FIRMWARE_VERSION_MAJOR_NUMBER 1
32 #define FIRMWARE_VERSION_MINOR_NUMBER 2
33 
36 
37 diagnostic_msgs::DiagnosticStatus imu_state;
38 diagnostic_msgs::DiagnosticStatus motor_state;
39 diagnostic_msgs::DiagnosticStatus LDS_state;
40 diagnostic_msgs::DiagnosticStatus battery_state;
41 diagnostic_msgs::DiagnosticStatus button_state;
42 
43 typedef struct
44 {
48 }VERSION;
49 
50 void split(std::string data, std::string separator, std::string* temp)
51 {
52  int cnt = 0;
53  std::string copy = data;
54 
55  while(true)
56  {
57  std::size_t index = copy.find(separator);
58 
59  if (index != std::string::npos)
60  {
61  temp[cnt] = copy.substr(0, index);
62 
63  copy = copy.substr(index+1, copy.length());
64  }
65  else
66  {
67  temp[cnt] = copy.substr(0, copy.length());
68  break;
69  }
70 
71  ++cnt;
72  }
73 }
74 
75 void setDiagnosisMsg(diagnostic_msgs::DiagnosticStatus *diag, uint8_t level, std::string name, std::string message, std::string hardware_id)
76 {
77  diag->level = level;
78  diag->name = name;
79  diag->message = message;
80  diag->hardware_id = hardware_id;
81 }
82 
83 void setIMUDiagnosis(uint8_t level, std::string message)
84 {
85  setDiagnosisMsg(&imu_state, level, "IMU Sensor", message, "MPU9250");
86 }
87 
88 void setMotorDiagnosis(uint8_t level, std::string message)
89 {
90  setDiagnosisMsg(&motor_state, level, "Actuator", message, "DYNAMIXEL X");
91 }
92 
93 void setBatteryDiagnosis(uint8_t level, std::string message)
94 {
95  setDiagnosisMsg(&battery_state, level, "Power System", message, "Battery");
96 }
97 
98 void setLDSDiagnosis(uint8_t level, std::string message)
99 {
100  setDiagnosisMsg(&LDS_state, level, "Lidar Sensor", message, "HLS-LFCD-LDS");
101 }
102 
103 void setButtonDiagnosis(uint8_t level, std::string message)
104 {
105  setDiagnosisMsg(&button_state, level, "Analog Button", message, "OpenCR Button");
106 }
107 
108 void imuMsgCallback(const sensor_msgs::Imu::ConstPtr &msg)
109 {
110  setIMUDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "Good Condition");
111 }
112 
113 void LDSMsgCallback(const sensor_msgs::LaserScan::ConstPtr &msg)
114 {
115  setLDSDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "Good Condition");
116 }
117 
118 void sensorStateMsgCallback(const turtlebot3_msgs::SensorState::ConstPtr &msg)
119 {
120  if (msg->battery > 11.0)
121  setBatteryDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "Good Condition");
122  else
123  setBatteryDiagnosis(diagnostic_msgs::DiagnosticStatus::WARN, "Charge!!! Charge!!!");
124 
125  if (msg->button == turtlebot3_msgs::SensorState::BUTTON0)
126  setButtonDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "BUTTON 0 IS PUSHED");
127  else if (msg->button == turtlebot3_msgs::SensorState::BUTTON1)
128  setButtonDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "BUTTON 1 IS PUSHED");
129  else
130  setButtonDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "Pushed Nothing");
131 
132  if (msg->torque == true)
133  setMotorDiagnosis(diagnostic_msgs::DiagnosticStatus::OK, "Torque ON");
134  else
135  setMotorDiagnosis(diagnostic_msgs::DiagnosticStatus::WARN, "Torque OFF");
136 }
137 
138 void firmwareVersionMsgCallback(const turtlebot3_msgs::VersionInfo::ConstPtr &msg)
139 {
140  static bool check_version = false;
141  std::string get_version[3];
142 
143  split(msg->firmware, ".", get_version);
144 
145  VERSION firmware_version;
146  firmware_version.major_number = std::stoi(get_version[0]);
147  firmware_version.minor_number = std::stoi(get_version[1]);
148  firmware_version.patch_number = std::stoi(get_version[2]);
149 
150  if (check_version == false)
151  {
152  if (firmware_version.major_number == FIRMWARE_VERSION_MAJOR_NUMBER)
153  {
154  if (firmware_version.minor_number > FIRMWARE_VERSION_MINOR_NUMBER)
155  {
156  ROS_WARN("This firmware(v%s) may not compatible with this software (v%s)", msg->firmware.data(), SOFTWARE_VERSION);
157  ROS_WARN("You can find how to update its in `FAQ` section(turtlebot3.robotis.com)");
158  }
159  }
160  else
161  {
162  ROS_WARN("This firmware(v%s) may not compatible with this software (v%s)", msg->firmware.data(), SOFTWARE_VERSION);
163  ROS_WARN("You can find how to update its in `FAQ` section(turtlebot3.robotis.com)");
164  }
165 
166  check_version = true;
167  }
168 
169  turtlebot3_msgs::VersionInfo version;
170 
171  version.software = SOFTWARE_VERSION;
172  version.hardware = HARDWARE_VERSION;
173  version.firmware = msg->firmware;
174 
175  tb3_version_info_pub.publish(version);
176 }
177 
178 void msgPub()
179 {
180  diagnostic_msgs::DiagnosticArray tb3_diagnostics;
181 
182  tb3_diagnostics.header.stamp = ros::Time::now();
183 
184  tb3_diagnostics.status.clear();
185  tb3_diagnostics.status.push_back(imu_state);
186  tb3_diagnostics.status.push_back(motor_state);
187  tb3_diagnostics.status.push_back(LDS_state);
188  tb3_diagnostics.status.push_back(battery_state);
189  tb3_diagnostics.status.push_back(button_state);
190 
191  tb3_diagnostics_pub.publish(tb3_diagnostics);
192 }
193 
194 int main(int argc, char **argv)
195 {
196  ros::init(argc, argv, "turtlebot3_diagnostic");
197  ros::NodeHandle nh;
198 
199  tb3_diagnostics_pub = nh.advertise<diagnostic_msgs::DiagnosticArray>("diagnostics", 10);
200  tb3_version_info_pub = nh.advertise<turtlebot3_msgs::VersionInfo>("version_info", 10);
201 
202  ros::Subscriber imu = nh.subscribe("imu", 10, imuMsgCallback);
203  ros::Subscriber lds = nh.subscribe("scan", 10, LDSMsgCallback);
204  ros::Subscriber tb3_sensor = nh.subscribe("sensor_state", 10, sensorStateMsgCallback);
205  ros::Subscriber version = nh.subscribe("firmware_version", 10, firmwareVersionMsgCallback);
206 
207  ros::Rate loop_rate(1);
208 
209  while (ros::ok())
210  {
211  msgPub();
212  ros::spinOnce();
213  loop_rate.sleep();
214  }
215 
216  return 0;
217 }
#define FIRMWARE_VERSION_MINOR_NUMBER
void setIMUDiagnosis(uint8_t level, std::string message)
void setButtonDiagnosis(uint8_t level, std::string message)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void LDSMsgCallback(const sensor_msgs::LaserScan::ConstPtr &msg)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
diagnostic_msgs::DiagnosticStatus imu_state
void setDiagnosisMsg(diagnostic_msgs::DiagnosticStatus *diag, uint8_t level, std::string name, std::string message, std::string hardware_id)
diagnostic_msgs::DiagnosticStatus battery_state
void setBatteryDiagnosis(uint8_t level, std::string message)
void split(std::string data, std::string separator, std::string *temp)
#define ROS_WARN(...)
diagnostic_msgs::DiagnosticStatus LDS_state
ros::Publisher tb3_diagnostics_pub
void msgPub()
diagnostic_msgs::DiagnosticStatus motor_state
void firmwareVersionMsgCallback(const turtlebot3_msgs::VersionInfo::ConstPtr &msg)
ROSCPP_DECL bool ok()
void setLDSDiagnosis(uint8_t level, std::string message)
void sensorStateMsgCallback(const turtlebot3_msgs::SensorState::ConstPtr &msg)
void setMotorDiagnosis(uint8_t level, std::string message)
diagnostic_msgs::DiagnosticStatus button_state
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)
#define FIRMWARE_VERSION_MAJOR_NUMBER
bool sleep()
#define HARDWARE_VERSION
#define SOFTWARE_VERSION
static Time now()
ROSCPP_DECL void spinOnce()
void imuMsgCallback(const sensor_msgs::Imu::ConstPtr &msg)
ros::Publisher tb3_version_info_pub


turtlebot3_bringup
Author(s): Pyo , Darby Lim , Gilbert
autogenerated on Wed Apr 7 2021 02:10:32