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