humotion / src / server / server.cpp @ 6de0fc2e
History | View | Annotate | Download (5.261 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 <boost/algorithm/string.hpp> |
||
29 | 0c8d22a5 | sschulz | |
30 | 8c6c1163 | Simon Schulz | #include <string> |
31 | |||
32 | 0c8d22a5 | sschulz | #include "humotion/server/middleware_ros.h" |
33 | #include "humotion/server/server.h" |
||
34 | |||
35 | using humotion::server::Server;
|
||
36 | 8c6c1163 | Simon Schulz | |
37 | //! set up constant for updaterate (50Hz)
|
||
38 | const float Server::MOTION_UPDATERATE = 50.0; |
||
39 | |||
40 | //! constructor
|
||
41 | //! open a new server instance.
|
||
42 | 0c8d22a5 | sschulz | Server::Server(std::string scope, std::string mw, JointInterface *_joint_interface) { |
43 | // convert mw to uppercase
|
||
44 | boost::to_upper(mw); |
||
45 | 8c6c1163 | Simon Schulz | |
46 | 0c8d22a5 | sschulz | printf("> initializing humotion server (on %s, middleware=%s)\n", scope.c_str(), mw.c_str());
|
47 | 8c6c1163 | Simon Schulz | |
48 | 0c8d22a5 | sschulz | // store pointer to joint interface
|
49 | ea068cf1 | sschulz | joint_interface_ = _joint_interface; |
50 | 8c6c1163 | Simon Schulz | |
51 | 0c8d22a5 | sschulz | // tell joint interface our framerate
|
52 | ea068cf1 | sschulz | joint_interface_->set_framerate(MOTION_UPDATERATE); |
53 | 8c6c1163 | Simon Schulz | |
54 | 0c8d22a5 | sschulz | // create controller
|
55 | ea068cf1 | sschulz | controller_ = new Controller(joint_interface_);
|
56 | controller_->init_motion_generators(); |
||
57 | 8c6c1163 | Simon Schulz | |
58 | 0c8d22a5 | sschulz | // start middleware
|
59 | if (mw == "ROS") { |
||
60 | ea068cf1 | sschulz | middleware_ = new MiddlewareROS(scope, controller_);
|
61 | 0c8d22a5 | sschulz | } else {
|
62 | printf("> ERROR: invalid mw '%s' given. RSB support was dropped. Please use ROS\n\n",
|
||
63 | mw.c_str()); |
||
64 | 8c6c1163 | Simon Schulz | exit(EXIT_FAILURE); |
65 | } |
||
66 | |||
67 | 0c8d22a5 | sschulz | // start motion generation thread
|
68 | 8c6c1163 | Simon Schulz | start_motion_generation_thread(); |
69 | } |
||
70 | |||
71 | //! destructor
|
||
72 | 0c8d22a5 | sschulz | Server::~Server() { |
73 | 8c6c1163 | Simon Schulz | } |
74 | |||
75 | //! middleware still running?
|
||
76 | 0c8d22a5 | sschulz | bool Server::ok() {
|
77 | ea068cf1 | sschulz | return middleware_->ok();
|
78 | 8c6c1163 | Simon Schulz | } |
79 | |||
80 | //! start main thread
|
||
81 | 0c8d22a5 | sschulz | void Server::start_motion_generation_thread() {
|
82 | ea068cf1 | sschulz | motion_generation_thread_ptr_ = new boost::thread(boost::bind(
|
83 | 0c8d22a5 | sschulz | &Server::motion_generation_thread, this));
|
84 | 8c6c1163 | Simon Schulz | } |
85 | |||
86 | //! main thread that handles all data in/out
|
||
87 | //! this thread will take care of gathering all input data and
|
||
88 | //! then generate the output targets at a fixed rate of \sa MOTION_UPDATERATE
|
||
89 | 0c8d22a5 | sschulz | void Server::motion_generation_thread() {
|
90 | 8c6c1163 | Simon Schulz | unsigned int incoming_data_count_invalid = 0; |
91 | |||
92 | printf("> started motion generation thread\n");
|
||
93 | |||
94 | 0c8d22a5 | sschulz | // calculate loop delay
|
95 | 8c6c1163 | Simon Schulz | float loop_delay = 1000.0 / MOTION_UPDATERATE; |
96 | 0c8d22a5 | sschulz | boost::system_time timeout = boost::get_system_time() + |
97 | boost::posix_time::milliseconds(loop_delay); |
||
98 | printf("> one loop = %3.1fms\n", loop_delay);
|
||
99 | 0b76d42b | Simon Schulz | |
100 | 0c8d22a5 | sschulz | // wait for incoming joint data
|
101 | ea068cf1 | sschulz | while (middleware_->ok()) {
|
102 | 0c8d22a5 | sschulz | // mw tick
|
103 | ea068cf1 | sschulz | middleware_->tick(); |
104 | |||
105 | unsigned int incoming_data_count = |
||
106 | joint_interface_->get_and_clear_incoming_position_count(); |
||
107 | 4e9221c4 | Simon Schulz | |
108 | 0c8d22a5 | sschulz | if (incoming_data_count == 0) { |
109 | 7ed40bef | Simon Schulz | incoming_data_count_invalid++; |
110 | 0c8d22a5 | sschulz | if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
|
111 | 7ed40bef | Simon Schulz | printf("> waiting for valid incoming joint data (position+velocity)\n");
|
112 | incoming_data_count_invalid = 0;
|
||
113 | } |
||
114 | 0c8d22a5 | sschulz | } else {
|
115 | // fine, joint data is arriving, exit waiting loop
|
||
116 | 4e9221c4 | Simon Schulz | break;
|
117 | } |
||
118 | |||
119 | 0c8d22a5 | sschulz | boost::thread::sleep(timeout); |
120 | timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay); |
||
121 | 4e9221c4 | Simon Schulz | } |
122 | |||
123 | 60dfadc5 | Simon Schulz | printf("> joint data arrived, control loop active.\n");
|
124 | ea068cf1 | sschulz | controller_->set_activated(); |
125 | 0b76d42b | Simon Schulz | |
126 | 0c8d22a5 | sschulz | // fine, data is arriving, activate and run control loop
|
127 | ea068cf1 | sschulz | while (middleware_->ok()) {
|
128 | 0c8d22a5 | sschulz | // mw tick
|
129 | ea068cf1 | sschulz | middleware_->tick(); |
130 | 8c6c1163 | Simon Schulz | |
131 | |||
132 | 0c8d22a5 | sschulz | // calculate all targets
|
133 | ea068cf1 | sschulz | controller_->calculate_targets(); |
134 | 8c6c1163 | Simon Schulz | |
135 | 2aa96942 | sschulz | // do some logging
|
136 | middleware_->publish_debug_dataset(controller_->get_debug_data()); |
||
137 | |||
138 | 0c8d22a5 | sschulz | // publish data to joints
|
139 | ea068cf1 | sschulz | controller_->publish_targets(); |
140 | 8c6c1163 | Simon Schulz | |
141 | 0c8d22a5 | sschulz | // finally execute the movement
|
142 | ea068cf1 | sschulz | joint_interface_->execute_motion(); |
143 | 8c6c1163 | Simon Schulz | |
144 | 0c8d22a5 | sschulz | // check if we received joint information in the last step
|
145 | ea068cf1 | sschulz | unsigned int incoming_data_count = |
146 | joint_interface_->get_and_clear_incoming_position_count(); |
||
147 | |||
148 | 0c8d22a5 | sschulz | if (incoming_data_count == 0) { |
149 | // printf("> WARNING: no incoming joint data during the last iteration...\n");
|
||
150 | 7ed40bef | Simon Schulz | incoming_data_count_invalid++; |
151 | 0c8d22a5 | sschulz | if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
|
152 | 7ed40bef | Simon Schulz | printf("> ERROR: no incoming joint data for >1 second -> will exit now\n");
|
153 | exit(EXIT_FAILURE); |
||
154 | } |
||
155 | 0c8d22a5 | sschulz | } else {
|
156 | 7ed40bef | Simon Schulz | incoming_data_count_invalid = 0;
|
157 | 8c6c1163 | Simon Schulz | } |
158 | |||
159 | 0c8d22a5 | sschulz | boost::thread::sleep(timeout); |
160 | timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay); |
||
161 | 8c6c1163 | Simon Schulz | } |
162 | |||
163 | printf("> motion generation thread exited.\n");
|
||
164 | } |