robot_state.cpp
Go to the documentation of this file.
00001 /*
00002  * robot_state.cpp
00003  *
00004  * Copyright 2015 Thomas Timm Andersen
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #include "ur_modern_driver/robot_state.h"
00020 
00021 RobotState::RobotState(std::condition_variable& msg_cond) {
00022         version_msg_.major_version = 0;
00023         version_msg_.minor_version = 0;
00024         new_data_available_ = false;
00025         pMsg_cond_ = &msg_cond;
00026         RobotState::setDisconnected();
00027         robot_mode_running_ = robotStateTypeV30::ROBOT_MODE_RUNNING;
00028 }
00029 double RobotState::ntohd(uint64_t nf) {
00030         double x;
00031         nf = be64toh(nf);
00032         memcpy(&x, &nf, sizeof(x));
00033         return x;
00034 }
00035 void RobotState::unpack(uint8_t* buf, unsigned int buf_length) {
00036         /* Returns missing bytes to unpack a message, or 0 if all data was parsed */
00037         unsigned int offset = 0;
00038         while (buf_length > offset) {
00039                 int len;
00040                 unsigned char message_type;
00041                 memcpy(&len, &buf[offset], sizeof(len));
00042                 len = ntohl(len);
00043                 if (len + offset > buf_length) {
00044                         return;
00045                 }
00046                 memcpy(&message_type, &buf[offset + sizeof(len)], sizeof(message_type));
00047                 switch (message_type) {
00048                 case messageType::ROBOT_MESSAGE:
00049                         RobotState::unpackRobotMessage(buf, offset, len); //'len' is inclusive the 5 bytes from messageSize and messageType
00050                         break;
00051                 case messageType::ROBOT_STATE:
00052                         RobotState::unpackRobotState(buf, offset, len); //'len' is inclusive the 5 bytes from messageSize and messageType
00053                         break;
00054                 case messageType::PROGRAM_STATE_MESSAGE:
00055                         //Don't do anything atm...
00056                 default:
00057                         break;
00058                 }
00059                 offset += len;
00060 
00061         }
00062         return;
00063 }
00064 
00065 void RobotState::unpackRobotMessage(uint8_t * buf, unsigned int offset,
00066                 uint32_t len) {
00067         offset += 5;
00068         uint64_t timestamp;
00069         int8_t source, robot_message_type;
00070         memcpy(&timestamp, &buf[offset], sizeof(timestamp));
00071         offset += sizeof(timestamp);
00072         memcpy(&source, &buf[offset], sizeof(source));
00073         offset += sizeof(source);
00074         memcpy(&robot_message_type, &buf[offset], sizeof(robot_message_type));
00075         offset += sizeof(robot_message_type);
00076         switch (robot_message_type) {
00077         case robotMessageType::ROBOT_MESSAGE_VERSION:
00078                 val_lock_.lock();
00079                 version_msg_.timestamp = timestamp;
00080                 version_msg_.source = source;
00081                 version_msg_.robot_message_type = robot_message_type;
00082                 RobotState::unpackRobotMessageVersion(buf, offset, len);
00083                 val_lock_.unlock();
00084                 break;
00085         default:
00086                 break;
00087         }
00088 
00089 }
00090 
00091 void RobotState::unpackRobotState(uint8_t * buf, unsigned int offset,
00092                 uint32_t len) {
00093         offset += 5;
00094         while (offset < len) {
00095                 int32_t length;
00096                 uint8_t package_type;
00097                 memcpy(&length, &buf[offset], sizeof(length));
00098                 length = ntohl(length);
00099                 memcpy(&package_type, &buf[offset + sizeof(length)],
00100                                 sizeof(package_type));
00101                 switch (package_type) {
00102                 case packageType::ROBOT_MODE_DATA:
00103                         val_lock_.lock();
00104                         RobotState::unpackRobotMode(buf, offset + 5);
00105                         val_lock_.unlock();
00106                         break;
00107 
00108                 case packageType::MASTERBOARD_DATA:
00109                         val_lock_.lock();
00110                         RobotState::unpackRobotStateMasterboard(buf, offset + 5);
00111                         val_lock_.unlock();
00112                         break;
00113                 default:
00114                         break;
00115                 }
00116                 offset += length;
00117         }
00118         new_data_available_ = true;
00119         pMsg_cond_->notify_all();
00120 
00121 }
00122 
00123 void RobotState::unpackRobotMessageVersion(uint8_t * buf, unsigned int offset,
00124                 uint32_t len) {
00125         memcpy(&version_msg_.project_name_size, &buf[offset],
00126                         sizeof(version_msg_.project_name_size));
00127         offset += sizeof(version_msg_.project_name_size);
00128         memcpy(&version_msg_.project_name, &buf[offset],
00129                         sizeof(char) * version_msg_.project_name_size);
00130         offset += version_msg_.project_name_size;
00131         version_msg_.project_name[version_msg_.project_name_size] = '\0';
00132         memcpy(&version_msg_.major_version, &buf[offset],
00133                         sizeof(version_msg_.major_version));
00134         offset += sizeof(version_msg_.major_version);
00135         memcpy(&version_msg_.minor_version, &buf[offset],
00136                         sizeof(version_msg_.minor_version));
00137         offset += sizeof(version_msg_.minor_version);
00138         memcpy(&version_msg_.svn_revision, &buf[offset],
00139                         sizeof(version_msg_.svn_revision));
00140         offset += sizeof(version_msg_.svn_revision);
00141         version_msg_.svn_revision = ntohl(version_msg_.svn_revision);
00142         memcpy(&version_msg_.build_date, &buf[offset], sizeof(char) * len - offset);
00143         version_msg_.build_date[len - offset] = '\0';
00144         if (version_msg_.major_version < 2) {
00145                 robot_mode_running_ = robotStateTypeV18::ROBOT_RUNNING_MODE;
00146         }
00147 }
00148 
00149 void RobotState::unpackRobotMode(uint8_t * buf, unsigned int offset) {
00150         memcpy(&robot_mode_.timestamp, &buf[offset], sizeof(robot_mode_.timestamp));
00151         offset += sizeof(robot_mode_.timestamp);
00152         uint8_t tmp;
00153         memcpy(&tmp, &buf[offset], sizeof(tmp));
00154         if (tmp > 0)
00155                 robot_mode_.isRobotConnected = true;
00156         else
00157                 robot_mode_.isRobotConnected = false;
00158         offset += sizeof(tmp);
00159         memcpy(&tmp, &buf[offset], sizeof(tmp));
00160         if (tmp > 0)
00161                 robot_mode_.isRealRobotEnabled = true;
00162         else
00163                 robot_mode_.isRealRobotEnabled = false;
00164         offset += sizeof(tmp);
00165         memcpy(&tmp, &buf[offset], sizeof(tmp));
00166         //printf("PowerOnRobot: %d\n", tmp);
00167         if (tmp > 0)
00168                 robot_mode_.isPowerOnRobot = true;
00169         else
00170                 robot_mode_.isPowerOnRobot = false;
00171         offset += sizeof(tmp);
00172         memcpy(&tmp, &buf[offset], sizeof(tmp));
00173         if (tmp > 0)
00174                 robot_mode_.isEmergencyStopped = true;
00175         else
00176                 robot_mode_.isEmergencyStopped = false;
00177         offset += sizeof(tmp);
00178         memcpy(&tmp, &buf[offset], sizeof(tmp));
00179         if (tmp > 0)
00180                 robot_mode_.isProtectiveStopped = true;
00181         else
00182                 robot_mode_.isProtectiveStopped = false;
00183         offset += sizeof(tmp);
00184         memcpy(&tmp, &buf[offset], sizeof(tmp));
00185         if (tmp > 0)
00186                 robot_mode_.isProgramRunning = true;
00187         else
00188                 robot_mode_.isProgramRunning = false;
00189         offset += sizeof(tmp);
00190         memcpy(&tmp, &buf[offset], sizeof(tmp));
00191         if (tmp > 0)
00192                 robot_mode_.isProgramPaused = true;
00193         else
00194                 robot_mode_.isProgramPaused = false;
00195         offset += sizeof(tmp);
00196         memcpy(&robot_mode_.robotMode, &buf[offset], sizeof(robot_mode_.robotMode));
00197         offset += sizeof(robot_mode_.robotMode);
00198         uint64_t temp;
00199         if (RobotState::getVersion() > 2.) {
00200                 memcpy(&robot_mode_.controlMode, &buf[offset],
00201                                 sizeof(robot_mode_.controlMode));
00202                 offset += sizeof(robot_mode_.controlMode);
00203                 memcpy(&temp, &buf[offset], sizeof(temp));
00204                 offset += sizeof(temp);
00205                 robot_mode_.targetSpeedFraction = RobotState::ntohd(temp);
00206         }
00207         memcpy(&temp, &buf[offset], sizeof(temp));
00208         offset += sizeof(temp);
00209         robot_mode_.speedScaling = RobotState::ntohd(temp);
00210 }
00211 
00212 void RobotState::unpackRobotStateMasterboard(uint8_t * buf,
00213                 unsigned int offset) {
00214         if (RobotState::getVersion() < 3.0) {
00215                 int16_t digital_input_bits, digital_output_bits;
00216                 memcpy(&digital_input_bits, &buf[offset], sizeof(digital_input_bits));
00217                 offset += sizeof(digital_input_bits);
00218                 memcpy(&digital_output_bits, &buf[offset], sizeof(digital_output_bits));
00219                 offset += sizeof(digital_output_bits);
00220                 mb_data_.digitalInputBits = ntohs(digital_input_bits);
00221                 mb_data_.digitalOutputBits = ntohs(digital_output_bits);
00222         } else {
00223                 memcpy(&mb_data_.digitalInputBits, &buf[offset],
00224                                 sizeof(mb_data_.digitalInputBits));
00225                 offset += sizeof(mb_data_.digitalInputBits);
00226                 mb_data_.digitalInputBits = ntohl(mb_data_.digitalInputBits);
00227                 memcpy(&mb_data_.digitalOutputBits, &buf[offset],
00228                                 sizeof(mb_data_.digitalOutputBits));
00229                 offset += sizeof(mb_data_.digitalOutputBits);
00230                 mb_data_.digitalOutputBits = ntohl(mb_data_.digitalOutputBits);
00231         }
00232 
00233         memcpy(&mb_data_.analogInputRange0, &buf[offset],
00234                         sizeof(mb_data_.analogInputRange0));
00235         offset += sizeof(mb_data_.analogInputRange0);
00236         memcpy(&mb_data_.analogInputRange1, &buf[offset],
00237                         sizeof(mb_data_.analogInputRange1));
00238         offset += sizeof(mb_data_.analogInputRange1);
00239         uint64_t temp;
00240         memcpy(&temp, &buf[offset], sizeof(temp));
00241         offset += sizeof(temp);
00242         mb_data_.analogInput0 = RobotState::ntohd(temp);
00243         memcpy(&temp, &buf[offset], sizeof(temp));
00244         offset += sizeof(temp);
00245         mb_data_.analogInput1 = RobotState::ntohd(temp);
00246         memcpy(&mb_data_.analogOutputDomain0, &buf[offset],
00247                         sizeof(mb_data_.analogOutputDomain0));
00248         offset += sizeof(mb_data_.analogOutputDomain0);
00249         memcpy(&mb_data_.analogOutputDomain1, &buf[offset],
00250                         sizeof(mb_data_.analogOutputDomain1));
00251         offset += sizeof(mb_data_.analogOutputDomain1);
00252         memcpy(&temp, &buf[offset], sizeof(temp));
00253         offset += sizeof(temp);
00254         mb_data_.analogOutput0 = RobotState::ntohd(temp);
00255         memcpy(&temp, &buf[offset], sizeof(temp));
00256         offset += sizeof(temp);
00257         mb_data_.analogOutput1 = RobotState::ntohd(temp);
00258 
00259         memcpy(&mb_data_.masterBoardTemperature, &buf[offset],
00260                         sizeof(mb_data_.masterBoardTemperature));
00261         offset += sizeof(mb_data_.masterBoardTemperature);
00262         mb_data_.masterBoardTemperature = ntohl(mb_data_.masterBoardTemperature);
00263         memcpy(&mb_data_.robotVoltage48V, &buf[offset],
00264                         sizeof(mb_data_.robotVoltage48V));
00265         offset += sizeof(mb_data_.robotVoltage48V);
00266         mb_data_.robotVoltage48V = ntohl(mb_data_.robotVoltage48V);
00267         memcpy(&mb_data_.robotCurrent, &buf[offset], sizeof(mb_data_.robotCurrent));
00268         offset += sizeof(mb_data_.robotCurrent);
00269         mb_data_.robotCurrent = ntohl(mb_data_.robotCurrent);
00270         memcpy(&mb_data_.masterIOCurrent, &buf[offset],
00271                         sizeof(mb_data_.masterIOCurrent));
00272         offset += sizeof(mb_data_.masterIOCurrent);
00273         mb_data_.masterIOCurrent = ntohl(mb_data_.masterIOCurrent);
00274 
00275         memcpy(&mb_data_.safetyMode, &buf[offset], sizeof(mb_data_.safetyMode));
00276         offset += sizeof(mb_data_.safetyMode);
00277         memcpy(&mb_data_.masterOnOffState, &buf[offset],
00278                         sizeof(mb_data_.masterOnOffState));
00279         offset += sizeof(mb_data_.masterOnOffState);
00280 
00281         memcpy(&mb_data_.euromap67InterfaceInstalled, &buf[offset],
00282                         sizeof(mb_data_.euromap67InterfaceInstalled));
00283         offset += sizeof(mb_data_.euromap67InterfaceInstalled);
00284         if (mb_data_.euromap67InterfaceInstalled != 0) {
00285                 memcpy(&mb_data_.euromapInputBits, &buf[offset],
00286                                 sizeof(mb_data_.euromapInputBits));
00287                 offset += sizeof(mb_data_.euromapInputBits);
00288                 mb_data_.euromapInputBits = ntohl(mb_data_.euromapInputBits);
00289                 memcpy(&mb_data_.euromapOutputBits, &buf[offset],
00290                                 sizeof(mb_data_.euromapOutputBits));
00291                 offset += sizeof(mb_data_.euromapOutputBits);
00292                 mb_data_.euromapOutputBits = ntohl(mb_data_.euromapOutputBits);
00293                 if (RobotState::getVersion() < 3.0) {
00294                         int16_t euromap_voltage, euromap_current;
00295                         memcpy(&euromap_voltage, &buf[offset], sizeof(euromap_voltage));
00296                         offset += sizeof(euromap_voltage);
00297                         memcpy(&euromap_current, &buf[offset], sizeof(euromap_current));
00298                         offset += sizeof(euromap_current);
00299                         mb_data_.euromapVoltage = ntohs(euromap_voltage);
00300                         mb_data_.euromapCurrent = ntohs(euromap_current);
00301                 } else {
00302                         memcpy(&mb_data_.euromapVoltage, &buf[offset],
00303                                         sizeof(mb_data_.euromapVoltage));
00304                         offset += sizeof(mb_data_.euromapVoltage);
00305                         mb_data_.euromapVoltage = ntohl(mb_data_.euromapVoltage);
00306                         memcpy(&mb_data_.euromapCurrent, &buf[offset],
00307                                         sizeof(mb_data_.euromapCurrent));
00308                         offset += sizeof(mb_data_.euromapCurrent);
00309                         mb_data_.euromapCurrent = ntohl(mb_data_.euromapCurrent);
00310                 }
00311 
00312         }
00313 }
00314 
00315 double RobotState::getVersion() {
00316         double ver;
00317         val_lock_.lock();
00318         ver = version_msg_.major_version + 0.1 * version_msg_.minor_version
00319                         + .0000001 * version_msg_.svn_revision;
00320         val_lock_.unlock();
00321         return ver;
00322 
00323 }
00324 
00325 void RobotState::finishedReading() {
00326         new_data_available_ = false;
00327 }
00328 
00329 bool RobotState::getNewDataAvailable() {
00330         return new_data_available_;
00331 }
00332 
00333 int RobotState::getDigitalInputBits() {
00334         return mb_data_.digitalInputBits;
00335 }
00336 int RobotState::getDigitalOutputBits() {
00337         return mb_data_.digitalOutputBits;
00338 }
00339 double RobotState::getAnalogInput0() {
00340         return mb_data_.analogInput0;
00341 }
00342 double RobotState::getAnalogInput1() {
00343         return mb_data_.analogInput1;
00344 }
00345 double RobotState::getAnalogOutput0() {
00346         return mb_data_.analogOutput0;
00347 
00348 }
00349 double RobotState::getAnalogOutput1() {
00350         return mb_data_.analogOutput1;
00351 }
00352 bool RobotState::isRobotConnected() {
00353         return robot_mode_.isRobotConnected;
00354 }
00355 bool RobotState::isRealRobotEnabled() {
00356         return robot_mode_.isRealRobotEnabled;
00357 }
00358 bool RobotState::isPowerOnRobot() {
00359         return robot_mode_.isPowerOnRobot;
00360 }
00361 bool RobotState::isEmergencyStopped() {
00362         return robot_mode_.isEmergencyStopped;
00363 }
00364 bool RobotState::isProtectiveStopped() {
00365         return robot_mode_.isProtectiveStopped;
00366 }
00367 bool RobotState::isProgramRunning() {
00368         return robot_mode_.isProgramRunning;
00369 }
00370 bool RobotState::isProgramPaused() {
00371         return robot_mode_.isProgramPaused;
00372 }
00373 unsigned char RobotState::getRobotMode() {
00374         return robot_mode_.robotMode;
00375 }
00376 bool RobotState::isReady() {
00377         if (robot_mode_.robotMode == robot_mode_running_) {
00378                 return true;
00379         }
00380         return false;
00381 }
00382 
00383 void RobotState::setDisconnected() {
00384         robot_mode_.isRobotConnected = false;
00385         robot_mode_.isRealRobotEnabled = false;
00386         robot_mode_.isPowerOnRobot = false;
00387 }


ur_modern_driver
Author(s): Thomas Timm Andersen
autogenerated on Wed Apr 3 2019 02:55:31