humotion / src / server / server.cpp @ 0c8d22a5
History | View | Annotate | Download (5.172 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 | 8c6c1163 | Simon Schulz | joint_interface = _joint_interface; |
50 | |||
51 | 0c8d22a5 | sschulz | // tell joint interface our framerate
|
52 | 8c6c1163 | Simon Schulz | joint_interface->set_framerate(MOTION_UPDATERATE); |
53 | |||
54 | 0c8d22a5 | sschulz | // create controller
|
55 | 8c6c1163 | Simon Schulz | controller = new Controller(joint_interface);
|
56 | controller->init_motion_generators(); |
||
57 | |||
58 | 0c8d22a5 | sschulz | // start middleware
|
59 | if (mw == "ROS") { |
||
60 | 8c6c1163 | Simon Schulz | 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 | 8c6c1163 | Simon Schulz | return middleware->ok();
|
78 | } |
||
79 | |||
80 | //! start main thread
|
||
81 | 0c8d22a5 | sschulz | void Server::start_motion_generation_thread() {
|
82 | motion_generation_thread_ptr = new boost::thread(boost::bind(
|
||
83 | &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 | while (middleware->ok()) {
|
||
102 | // mw tick
|
||
103 | 4e9221c4 | Simon Schulz | middleware->tick(); |
104 | |||
105 | unsigned int incoming_data_count = joint_interface->get_and_clear_incoming_position_count(); |
||
106 | 0c8d22a5 | sschulz | if (incoming_data_count == 0) { |
107 | 7ed40bef | Simon Schulz | incoming_data_count_invalid++; |
108 | 0c8d22a5 | sschulz | if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
|
109 | 7ed40bef | Simon Schulz | printf("> waiting for valid incoming joint data (position+velocity)\n");
|
110 | incoming_data_count_invalid = 0;
|
||
111 | } |
||
112 | 0c8d22a5 | sschulz | } else {
|
113 | // fine, joint data is arriving, exit waiting loop
|
||
114 | 4e9221c4 | Simon Schulz | break;
|
115 | } |
||
116 | |||
117 | 0c8d22a5 | sschulz | boost::thread::sleep(timeout); |
118 | timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay); |
||
119 | 4e9221c4 | Simon Schulz | } |
120 | |||
121 | 60dfadc5 | Simon Schulz | printf("> joint data arrived, control loop active.\n");
|
122 | 5d7ba19c | Simon Schulz | controller->set_activated(); |
123 | 0b76d42b | Simon Schulz | |
124 | 0c8d22a5 | sschulz | // fine, data is arriving, activate and run control loop
|
125 | while (middleware->ok()) {
|
||
126 | // mw tick
|
||
127 | 8c6c1163 | Simon Schulz | middleware->tick(); |
128 | |||
129 | 0c8d22a5 | sschulz | // do some logging
|
130 | // controller->dump_angles();
|
||
131 | 8c6c1163 | Simon Schulz | |
132 | 0c8d22a5 | sschulz | // calculate all targets
|
133 | 8c6c1163 | Simon Schulz | controller->calculate_targets(); |
134 | |||
135 | 0c8d22a5 | sschulz | // publish data to joints
|
136 | 8c6c1163 | Simon Schulz | controller->publish_targets(); |
137 | |||
138 | 0c8d22a5 | sschulz | // finally execute the movement
|
139 | 8c6c1163 | Simon Schulz | joint_interface->execute_motion(); |
140 | |||
141 | 0c8d22a5 | sschulz | // check if we received joint information in the last step
|
142 | 8c6c1163 | Simon Schulz | unsigned int incoming_data_count = joint_interface->get_and_clear_incoming_position_count(); |
143 | 0c8d22a5 | sschulz | if (incoming_data_count == 0) { |
144 | // printf("> WARNING: no incoming joint data during the last iteration...\n");
|
||
145 | 7ed40bef | Simon Schulz | incoming_data_count_invalid++; |
146 | 0c8d22a5 | sschulz | if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
|
147 | 7ed40bef | Simon Schulz | printf("> ERROR: no incoming joint data for >1 second -> will exit now\n");
|
148 | exit(EXIT_FAILURE); |
||
149 | } |
||
150 | 0c8d22a5 | sschulz | } else {
|
151 | 7ed40bef | Simon Schulz | incoming_data_count_invalid = 0;
|
152 | 8c6c1163 | Simon Schulz | } |
153 | |||
154 | 0c8d22a5 | sschulz | boost::thread::sleep(timeout); |
155 | timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay); |
||
156 | 8c6c1163 | Simon Schulz | } |
157 | |||
158 | printf("> motion generation thread exited.\n");
|
||
159 | } |