humotion / src / server / middleware_ros.cpp @ 2c4bbc89
History | View | Annotate | Download (4.524 KB)
1 | 8c6c1163 | Simon Schulz | /*
|
---|---|---|---|
2 | * This file is part of humotion
|
||
3 | *
|
||
4 | * Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
||
5 | * http://opensource.cit-ec.de/projects/humotion
|
||
6 | *
|
||
7 | * This file may be licensed under the terms of the
|
||
8 | * GNU Lesser General Public License Version 3 (the ``LGPL''),
|
||
9 | * or (at your option) any later version.
|
||
10 | *
|
||
11 | * Software distributed under the License is distributed
|
||
12 | * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
||
13 | * express or implied. See the LGPL for the specific language
|
||
14 | * governing rights and limitations.
|
||
15 | *
|
||
16 | * You should have received a copy of the LGPL along with this
|
||
17 | * program. If not, go to http://www.gnu.org/licenses/lgpl.html
|
||
18 | * or write to the Free Software Foundation, Inc.,
|
||
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * The development of this software was supported by the
|
||
22 | * Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
||
23 | * The Excellence Cluster EXC 277 is a grant of the Deutsche
|
||
24 | * Forschungsgemeinschaft (DFG) in the context of the German
|
||
25 | * Excellence Initiative.
|
||
26 | */
|
||
27 | |||
28 | #include "server/middleware_ros.h" |
||
29 | |||
30 | #ifdef ROS_SUPPORT
|
||
31 | |||
32 | #include <boost/range/algorithm/remove_if.hpp> |
||
33 | #include <boost/algorithm/string/classification.hpp> |
||
34 | using namespace std; |
||
35 | using namespace boost; |
||
36 | using namespace humotion; |
||
37 | using namespace humotion::server; |
||
38 | |||
39 | //! constructor
|
||
40 | 5d7ba19c | Simon Schulz | MiddlewareROS::MiddlewareROS(string scope, Controller *c)
|
41 | : Middleware(scope, c){ |
||
42 | 8c6c1163 | Simon Schulz | |
43 | 5d7ba19c | Simon Schulz | printf("> using ROS middleware\n");
|
44 | 8c6c1163 | Simon Schulz | //start ros core?
|
45 | if (ros::isInitialized()){
|
||
46 | dff8ce51 | Simon Schulz | //oh another process is doing ros comm, fine, we do not need to call it
|
47 | 8c6c1163 | Simon Schulz | tick_necessary = false;
|
48 | }else{
|
||
49 | //we have to take care of ros
|
||
50 | printf("> no active ros middleware, calling ros::init() and we will call tick() periodically!");
|
||
51 | string node_name = "humotion_server__"+ scope; |
||
52 | node_name.erase(boost::remove_if(node_name, boost::is_any_of("/")), node_name.end());
|
||
53 | printf("> registering on ROS as node %s\n",node_name.c_str());
|
||
54 | ros::M_string no_remapping; |
||
55 | ros::init(no_remapping, node_name); |
||
56 | tick_necessary = true;
|
||
57 | } |
||
58 | |||
59 | //create node handle
|
||
60 | ros::NodeHandle n; |
||
61 | |||
62 | //set up subscribers:
|
||
63 | mouth_target_subscriber = n.subscribe(scope + "/humotion/mouth/target", 150, &MiddlewareROS::incoming_mouth_target , this, ros::TransportHints().unreliable()); |
||
64 | gaze_target_subscriber = n.subscribe(scope + "/humotion/gaze/target", 150, &MiddlewareROS::incoming_gaze_target, this, ros::TransportHints().unreliable()); |
||
65 | |||
66 | } |
||
67 | |||
68 | //! destructor
|
||
69 | MiddlewareROS::~MiddlewareROS(){ |
||
70 | } |
||
71 | |||
72 | //! connection ok?
|
||
73 | //! \return true if conn is alive
|
||
74 | bool MiddlewareROS::ok(){
|
||
75 | return ros::ok();
|
||
76 | } |
||
77 | |||
78 | //! do a single tick
|
||
79 | void MiddlewareROS::tick(){
|
||
80 | if (tick_necessary){
|
||
81 | ros::spinOnce(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | //! callback to handle incoming mouth target
|
||
86 | void MiddlewareROS::incoming_mouth_target(const humotion::mouth::ConstPtr& msg){ |
||
87 | //printf("> incoming mouth_target\n");
|
||
88 | MouthState mouth_state; |
||
89 | |||
90 | mouth_state.position_left = msg->position.left; |
||
91 | mouth_state.position_center = msg->position.center; |
||
92 | mouth_state.position_right = msg->position.right; |
||
93 | |||
94 | mouth_state.opening_left = msg->opening.left; |
||
95 | mouth_state.opening_center = msg->opening.center; |
||
96 | mouth_state.opening_right = msg->opening.right; |
||
97 | |||
98 | controller->set_mouth_target(mouth_state); |
||
99 | } |
||
100 | |||
101 | //! this provides a consistent transformation of ros ts to double
|
||
102 | double MiddlewareROS::convert_ros_to_timestamp_ms(ros::Time t){
|
||
103 | return t.sec + t.nsec/1000000000.0; |
||
104 | } |
||
105 | |||
106 | //! callback to handle incoming gaze target
|
||
107 | void MiddlewareROS::incoming_gaze_target(const humotion::gaze::ConstPtr& msg){ |
||
108 | GazeState gaze_state; |
||
109 | 5d7ba19c | Simon Schulz | ///printf("> incoming gaze_target [P:%3.1f, T:%3.1f, R:%3.1f] %s\n", msg->pan, msg->tilt, msg->roll, msg->type==humotion::gaze::ABSOLUTE?"ABSOLUTE":"RELATIVE");
|
110 | 8c6c1163 | Simon Schulz | |
111 | gaze_state.type = msg->type; |
||
112 | gaze_state.pan = msg->pan; |
||
113 | gaze_state.tilt = msg->tilt; |
||
114 | gaze_state.roll = msg->roll; |
||
115 | gaze_state.vergence = msg->vergence; |
||
116 | |||
117 | gaze_state.pan_offset = msg->pan_offset; |
||
118 | gaze_state.tilt_offset = msg->tilt_offset; |
||
119 | gaze_state.roll_offset = msg->roll_offset; |
||
120 | |||
121 | gaze_state.eyelid_opening_upper = msg->eyelid_opening_upper; |
||
122 | gaze_state.eyelid_opening_lower = msg->eyelid_opening_lower; |
||
123 | |||
124 | gaze_state.eyebrow_left = msg->eyebrow_left; |
||
125 | gaze_state.eyebrow_right = msg->eyebrow_right; |
||
126 | |||
127 | gaze_state.eyeblink_request_left = msg->eyeblink_request_left; |
||
128 | gaze_state.eyeblink_request_right = msg->eyeblink_request_right; |
||
129 | |||
130 | gaze_state.timestamp = convert_ros_to_timestamp_ms(msg->header.stamp); |
||
131 | |||
132 | controller->set_gaze_target(gaze_state); |
||
133 | |||
134 | } |
||
135 | |||
136 | #endif |