Revision 0c8d22a5 src/server/server.cpp

View differences:

src/server/server.cpp
25 25
* Excellence Initiative.
26 26
*/
27 27

  
28
#include "server/server.h"
29
#include "server/middleware_ros.h"
30 28
#include <boost/algorithm/string.hpp>
29

  
31 30
#include <string>
32 31

  
33
using namespace std;
34
using namespace boost;
35
using namespace humotion;
36
using namespace humotion::server;
32
#include "humotion/server/middleware_ros.h"
33
#include "humotion/server/server.h"
34

  
35
using humotion::server::Server;
37 36

  
38 37
//! set up constant for updaterate (50Hz)
39 38
const float Server::MOTION_UPDATERATE = 50.0;
40 39

  
41 40
//! constructor
42 41
//! open a new server instance.
43
Server::Server(string scope, string mw, JointInterface *_joint_interface){
44
    //convert mw to uppercase:
45
    to_upper(mw);
42
Server::Server(std::string scope, std::string mw, JointInterface *_joint_interface) {
43
    // convert mw to uppercase
44
    boost::to_upper(mw);
46 45

  
47
    printf("> initializing humotion server (on %s, middleware=%s)\n",scope.c_str(),mw.c_str());
46
    printf("> initializing humotion server (on %s, middleware=%s)\n", scope.c_str(), mw.c_str());
48 47

  
49
    //store pointer to joint interface
48
    // store pointer to joint interface
50 49
    joint_interface = _joint_interface;
51 50

  
52
    //tell joint interface our framerate:
51
    // tell joint interface our framerate
53 52
    joint_interface->set_framerate(MOTION_UPDATERATE);
54 53

  
55
    //create controller
54
    // create controller
56 55
    controller = new Controller(joint_interface);
57 56
    controller->init_motion_generators();
58 57

  
59
    //start middleware:
60
    if (mw == "ROS"){
58
    // start middleware
59
    if (mw == "ROS") {
61 60
        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());
61
    } else {
62
        printf("> ERROR: invalid mw '%s' given. RSB support was dropped. Please use ROS\n\n",
63
               mw.c_str());
64 64
        exit(EXIT_FAILURE);
65 65
    }
66 66

  
67
    //start motion generation thread:
67
    // start motion generation thread
68 68
    start_motion_generation_thread();
69

  
70 69
}
71 70

  
72 71
//! destructor
73
Server::~Server(){
72
Server::~Server() {
74 73
}
75 74

  
76 75
//! middleware still running?
77
bool Server::ok(){
76
bool Server::ok() {
78 77
    return middleware->ok();
79 78
}
80 79

  
81 80
//! 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));
81
void Server::start_motion_generation_thread() {
82
    motion_generation_thread_ptr = new boost::thread(boost::bind(
83
                                                         &Server::motion_generation_thread, this));
84 84
}
85 85

  
86 86
//! main thread that handles all data in/out
87 87
//! this thread will take care of gathering all input data and
88 88
//! then generate the output targets at a fixed rate of \sa MOTION_UPDATERATE
89
void Server::motion_generation_thread(){
89
void Server::motion_generation_thread() {
90 90
    unsigned int incoming_data_count_invalid = 0;
91 91

  
92 92
    printf("> started motion generation thread\n");
93 93

  
94
    //calculate loop delay:
94
    // calculate loop delay
95 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);
96
    boost::system_time timeout = boost::get_system_time() +
97
                                 boost::posix_time::milliseconds(loop_delay);
98
    printf("> one loop = %3.1fms\n", loop_delay);
98 99

  
99
    //wait for incoming joint data:
100
    while (middleware->ok()){
101
        //mw tick
100
    // wait for incoming joint data
101
    while (middleware->ok()) {
102
        // mw tick
102 103
        middleware->tick();
103 104

  
104 105
        unsigned int incoming_data_count = joint_interface->get_and_clear_incoming_position_count();
105
        if (incoming_data_count == 0){
106
        if (incoming_data_count == 0) {
106 107
            incoming_data_count_invalid++;
107
            if (incoming_data_count_invalid >= MOTION_UPDATERATE){
108
            if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
108 109
                printf("> waiting for valid incoming joint data (position+velocity)\n");
109 110
                incoming_data_count_invalid = 0;
110 111
            }
111
        }else{
112
            //fine, joint data is arriving, exit waiting loop
112
        } else {
113
            // fine, joint data is arriving, exit waiting loop
113 114
            break;
114 115
        }
115 116

  
116
        thread::sleep(timeout);
117
        timeout = get_system_time() + posix_time::milliseconds(loop_delay);
117
        boost::thread::sleep(timeout);
118
        timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay);
118 119
    }
119 120

  
120 121
    printf("> joint data arrived, control loop active.\n");
121 122
    controller->set_activated();
122 123

  
123
    //fine, data is arriving, activate and run control loop:
124
    while (middleware->ok()){
125
        //mw tick
124
    // fine, data is arriving, activate and run control loop
125
    while (middleware->ok()) {
126
        // mw tick
126 127
        middleware->tick();
127 128

  
128
        //do some logging:
129
        //controller->dump_angles();
129
        // do some logging
130
        // controller->dump_angles();
130 131

  
131
        //calculate all targets
132
        // calculate all targets
132 133
        controller->calculate_targets();
133 134

  
134
        //publish data to joints
135
        // publish data to joints
135 136
        controller->publish_targets();
136 137

  
137
        //finally execute the movement:
138
        // finally execute the movement
138 139
        joint_interface->execute_motion();
139 140

  
140
        //check if we received joint information in the last step:
141
        // check if we received joint information in the last step
141 142
        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");
143
        if (incoming_data_count == 0) {
144
            // printf("> WARNING: no incoming joint data during the last iteration...\n");
144 145
            incoming_data_count_invalid++;
145
            if (incoming_data_count_invalid >= MOTION_UPDATERATE){
146
            if (incoming_data_count_invalid >= MOTION_UPDATERATE) {
146 147
                printf("> ERROR: no incoming joint data for >1 second -> will exit now\n");
147 148
                exit(EXIT_FAILURE);
148 149
            }
149
        }else{
150
        } else {
150 151
            incoming_data_count_invalid = 0;
151 152
        }
152 153

  
153
        thread::sleep(timeout);
154
        timeout = get_system_time() + posix_time::milliseconds(loop_delay);
154
        boost::thread::sleep(timeout);
155
        timeout = boost::get_system_time() + boost::posix_time::milliseconds(loop_delay);
155 156
    }
156 157

  
157 158
    printf("> motion generation thread exited.\n");

Also available in: Unified diff