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