00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <ros/ros.h>
00062 #include <cob_relayboard/SerRelayBoard.h>
00063
00064
00065 #include <std_msgs/Bool.h>
00066 #include <cob_relayboard/EmergencyStopState.h>
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 class NodeClass
00077 {
00078
00079 public:
00080
00081 ros::NodeHandle n;
00082
00083
00084 ros::Publisher topicPub_isEmergencyStop;
00085
00086
00087
00088
00089
00090 NodeClass()
00091 {
00092 topicPub_isEmergencyStop = n.advertise<cob_relayboard::EmergencyStopState>("/emergency_stop_state", 1);
00093
00094
00095 EM_stop_status_ = ST_EM_FREE;
00096 relayboard_available = false;
00097 relayboard_online = false;
00098 relayboard_timeout_ = 2.0;
00099 protocol_version_ = 1;
00100 duration_for_EM_free_ = ros::Duration(1);
00101 }
00102
00103
00104 ~NodeClass()
00105 {
00106 delete m_SerRelayBoard;
00107 }
00108
00109 void sendEmergencyStopStates();
00110 int init();
00111
00112 private:
00113 std::string sComPort;
00114 SerRelayBoard * m_SerRelayBoard;
00115
00116 int EM_stop_status_;
00117 ros::Duration duration_for_EM_free_;
00118 ros::Time time_of_EM_confirmed_;
00119 double relayboard_timeout_;
00120 int protocol_version_;
00121
00122 ros::Time time_last_message_received_;
00123 bool relayboard_online;
00124 bool relayboard_available;
00125
00126
00127 enum
00128 {
00129 ST_EM_FREE = 0,
00130 ST_EM_ACTIVE = 1,
00131 ST_EM_CONFIRMED = 2
00132 };
00133
00134 int requestBoardStatus();
00135 };
00136
00137
00138
00139 int main(int argc, char** argv)
00140 {
00141
00142 ros::init(argc, argv, "cob_relayboard_node");
00143
00144 NodeClass node;
00145 if(node.init() != 0) return 1;
00146
00147 ros::Rate r(20);
00148 while(node.n.ok())
00149 {
00150 node.sendEmergencyStopStates();
00151
00152 ros::spinOnce();
00153 r.sleep();
00154 }
00155
00156 return 0;
00157 }
00158
00159
00160
00161
00162 int NodeClass::init()
00163 {
00164 if (n.hasParam("ComPort"))
00165 {
00166 n.getParam("ComPort", sComPort);
00167 ROS_INFO("Loaded ComPort parameter from parameter server: %s",sComPort.c_str());
00168 }
00169 else
00170 {
00171 sComPort ="/dev/ttyUSB0";
00172 ROS_WARN("ComPort Parameter not available, using default Port: %s",sComPort.c_str());
00173 }
00174
00175 n.param("message_timeout", relayboard_timeout_, 2.0);
00176
00177 n.param("protocol_version", protocol_version_, 1);
00178
00179 m_SerRelayBoard = new SerRelayBoard(sComPort, protocol_version_);
00180 ROS_INFO("Opened Relayboard at ComPort = %s", sComPort.c_str());
00181
00182 m_SerRelayBoard->init();
00183
00184
00185 EM_stop_status_ = ST_EM_ACTIVE;
00186 duration_for_EM_free_ = ros::Duration(1);
00187
00188 return 0;
00189 }
00190
00191 int NodeClass::requestBoardStatus() {
00192 int ret;
00193
00194
00195 ret = m_SerRelayBoard->sendRequest();
00196 if(ret != SerRelayBoard::NO_ERROR) {
00197 ROS_ERROR("Error in sending message to Relayboard over SerialIO, lost bytes during writing");
00198 }
00199
00200 ret = m_SerRelayBoard->evalRxBuffer();
00201 if(ret==SerRelayBoard::NOT_INITIALIZED) {
00202 ROS_ERROR("Failed to read relayboard data over Serial, the device is not initialized");
00203 relayboard_online = false;
00204 } else if(ret==SerRelayBoard::NO_MESSAGES) {
00205 ROS_ERROR("For a long time, no messages from RelayBoard have been received, check com port!");
00206 if(time_last_message_received_.toSec() - ros::Time::now().toSec() > relayboard_timeout_) {relayboard_online = false;}
00207 } else if(ret==SerRelayBoard::TOO_LESS_BYTES_IN_QUEUE) {
00208
00209 } else if(ret==SerRelayBoard::CHECKSUM_ERROR) {
00210 ROS_ERROR("A checksum error occurred while reading from relayboard data");
00211 } else if(ret==SerRelayBoard::NO_ERROR) {
00212 relayboard_online = true;
00213 relayboard_available = true;
00214 time_last_message_received_ = ros::Time::now();
00215 }
00216
00217 return 0;
00218 }
00219
00220 void NodeClass::sendEmergencyStopStates()
00221 {
00222 requestBoardStatus();
00223
00224 if(!relayboard_available) return;
00225
00226
00227 bool EM_signal;
00228 ros::Duration duration_since_EM_confirmed;
00229 cob_relayboard::EmergencyStopState EM_msg;
00230
00231
00232 EM_msg.emergency_button_stop = m_SerRelayBoard->isEMStop();
00233 EM_msg.scanner_stop = m_SerRelayBoard->isScannerStop();
00234
00235
00236 EM_signal = (EM_msg.emergency_button_stop || EM_msg.scanner_stop);
00237
00238 switch (EM_stop_status_)
00239 {
00240 case ST_EM_FREE:
00241 {
00242 if (EM_signal == true)
00243 {
00244 ROS_INFO("Emergency stop was issued");
00245 EM_stop_status_ = EM_msg.EMSTOP;
00246 }
00247 break;
00248 }
00249 case ST_EM_ACTIVE:
00250 {
00251 if (EM_signal == false)
00252 {
00253 ROS_INFO("Emergency stop was confirmed");
00254 EM_stop_status_ = EM_msg.EMCONFIRMED;
00255 time_of_EM_confirmed_ = ros::Time::now();
00256 }
00257 break;
00258 }
00259 case ST_EM_CONFIRMED:
00260 {
00261 if (EM_signal == true)
00262 {
00263 ROS_INFO("Emergency stop was issued");
00264 EM_stop_status_ = EM_msg.EMSTOP;
00265 }
00266 else
00267 {
00268 duration_since_EM_confirmed = ros::Time::now() - time_of_EM_confirmed_;
00269 if( duration_since_EM_confirmed.toSec() > duration_for_EM_free_.toSec() )
00270 {
00271 ROS_INFO("Emergency stop released");
00272 EM_stop_status_ = EM_msg.EMFREE;
00273 }
00274 }
00275 break;
00276 }
00277 };
00278
00279
00280 EM_msg.emergency_state = EM_stop_status_;
00281
00282
00283 if(relayboard_online == false) {
00284 EM_msg.emergency_state = EM_msg.EMSTOP;
00285 }
00286 topicPub_isEmergencyStop.publish(EM_msg);
00287 }