io_state_relay_handler.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * FSRobo-R Package BSDL
3 * ---------
4 * Copyright (C) 2019 FUJISOFT. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its contributors
14 * may be used to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *********************************************************************/
28 
30 #include "fsrobo_r_msgs/IOStates.h"
32 #include <sstream>
33 
34 using namespace industrial::shared_types;
35 using namespace industrial::smpl_msg_connection;
36 using namespace industrial::simple_message;
39 
40 namespace fsrobo_r_driver
41 {
42 namespace io_state_relay_handler
43 {
44 
45 bool IOStateRelayHandler::init(SmplMsgConnection *connection)
46 {
47  ROS_WARN("IOStateRelayHandler::init!");
48  this->pub_io_states_ = this->node_.advertise<fsrobo_r_msgs::IOStates>("io_states", 1);
50 }
51 
52 bool IOStateRelayHandler::internalCB(SimpleMessage &in)
53 {
54  IOStateMessage state_msg;
55 
56  if (!state_msg.init(in))
57  {
58  LOG_ERROR("Failed to initialize state message");
59  return false;
60  }
61 
62  return internalCB(state_msg);
63 }
64 
65 bool IOStateRelayHandler::internalCB(IOStateMessage &in)
66 {
67  fsrobo_r_msgs::IOStates io_states;
68  bool rtn = true;
69 
70  //ROS_WARN("%08x %08x %08x", in.state_.getDigital(0), in.state_.getDigital(1), in.state_.getAnalog(0));
71 
72  std::vector<fsrobo_r_msgs::Digital> d_in_list;
73  for (int i = 0; i < 16; i++) {
74  fsrobo_r_msgs::Digital d;
75  createDigitalMessage(in.state_.getDigital(0), i, 0, d);
76  d_in_list.push_back(d);
77  }
78  for (int i = 0; i < 16; i++) {
79  fsrobo_r_msgs::Digital d;
80  createDigitalMessage(in.state_.getDigital(1), i, 32, d);
81  d_in_list.push_back(d);
82  }
83 
84  std::vector<fsrobo_r_msgs::Digital> d_out_list;
85  for (int i = 0; i < 16; i++) {
86  fsrobo_r_msgs::Digital d;
87  createDigitalMessage(in.state_.getDigital(0), i + 16, 0, d);
88  d_out_list.push_back(d);
89  }
90  for (int i = 0; i < 16; i++) {
91  fsrobo_r_msgs::Digital d;
92  createDigitalMessage(in.state_.getDigital(1), i + 16, 32, d);
93  d_out_list.push_back(d);
94  }
95 
96  std::vector<fsrobo_r_msgs::Analog> a_in_list;
97  for (int i = 0; i < 2; i++) {
98  fsrobo_r_msgs::Analog a;
99  createAnalogMessage(in.state_.getAnalog(0), i, 0, a);
100  a_in_list.push_back(a);
101  }
102 
103  io_states.digital_in_states = d_in_list;
104  io_states.digital_out_states = d_out_list;
105  io_states.analog_in_states = a_in_list;
106 
107  this->pub_io_states_.publish(io_states);
108 
109  // Reply back to the controller if the sender requested it.
110  if (CommTypes::SERVICE_REQUEST == in.getCommType())
111  {
112  SimpleMessage reply;
113  in.toReply(reply, rtn ? ReplyTypes::SUCCESS : ReplyTypes::FAILURE);
114  this->getConnection()->sendMsg(reply);
115  }
116 
117  return rtn;
118 }
119 
120 void IOStateRelayHandler::createDigitalMessage(int data, int n, int base_addr, fsrobo_r_msgs::Digital &d)
121 {
122  int mask = 1 << n;
123 
124  d.addr = base_addr + n;
125  d.state = (bool)(data & mask);
126  // ROS_WARN("%d %d %08x %08x %08x %d %d %d", base_addr, n, mask, data, data & mask, !!(data & mask), d.state, (bool)(data & mask));
127 }
128 
129 void IOStateRelayHandler::createAnalogMessage(int data, int n, int base_ch, fsrobo_r_msgs::Analog &a)
130 {
131  const int data_mask = 0x0fff;
132  const int mode_mask = 0x3000;
133 
134  int d = data >> (16 * n);
135 
136  a.ch = base_ch + n;
137  a.state = d & data_mask;
138  a.mode = (d & mode_mask) >> 12;
139 }
140 
141 }
142 }
143 
d
Class encapsulated robot status message generation methods (either to or from a industrial::simple_me...
industrial::shared_types::shared_int getAnalog(int idx)
Definition: io_state.h:105
bool init(industrial::simple_message::SimpleMessage &msg)
Initializes message from a simple message.
industrial::shared_types::shared_int getDigital(int idx)
Initializes a full robot status message.
Definition: io_state.h:100
#define ROS_WARN(...)
#define LOG_ERROR(format,...)
fsrobo_r_driver::simple_message::io_state::IOState state_
virtual bool toReply(industrial::simple_message::SimpleMessage &msg, industrial::simple_message::ReplyType reply)


fsrobo_r_driver
Author(s): F-ROSROBO
autogenerated on Sun Feb 9 2020 03:58:29