Revision f95312df

View differences:

examples/humotion_meka/CMakeLists.txt
1
PROJECT(meka_humotion_server)
2
cmake_minimum_required(VERSION 2.8)
3
SET(MAIN ${PROJECT_NAME})
4

  
5
set(ENV{ROS_LANG_DISABLE} "genjava")
6
set(ROS_BUILD_TYPE Debug)
7

  
8
################################################################
9
# check for ROS support:
10
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs sensor_msgs message_generation genmsg)
11
IF (catkin_FOUND)
12
    set(ROS_FOUND 1)
13
    message(STATUS "ROS Support is ON")
14
    add_definitions(-DROS_SUPPORT=1)
15
ENDIF (catkin_FOUND)
16

  
17
IF (NOT catkin_FOUND)
18
  message(FATAL_ERROR "Error: could not find ROS!")
19
ENDIF ()
20

  
21
FIND_PACKAGE(Boost REQUIRED COMPONENTS system thread)
22
FIND_PACKAGE(humotion)
23

  
24
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
25
#add_message_files(
26
#    FILES
27
#)
28

  
29
#generate_messages(
30
#    DEPENDENCIES
31
#    std_msgs
32
#)
33

  
34
#hack to allow sub dir calls work
35

  
36
catkin_package(
37
    INCLUDE_DIRS include
38
    LIBRARIES humotion
39
    #CATKIN_DEPENDS message_runtime
40
    #DEPENDS system_lib
41
)
42

  
43
include_directories( ${catkin_INCLUDE_DIRS})
44

  
45
# add include directories
46
INCLUDE_DIRECTORIES(${YARP_INCLUDE_DIRS} ${ICUB_INCLUDE_DIRS} ${humotion_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} include/)
47
link_directories(${Boost_LIBRARY_DIRS} ${humotion_LIBRARY_DIRS})
48

  
49
# add required linker flags
50

  
51
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${ICUB_LINK_FLAGS}")
52
file(GLOB DUMMY_HEADER_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} include/*.h)
53

  
54
#add_dependencies(${MAIN} ${catkin_EXPORTED_TARGETS})
55
ADD_EXECUTABLE(${MAIN} src/main.cpp src/mekajointinterface.cpp ${DUMMY_HEADER_LIST})
56

  
57
TARGET_LINK_LIBRARIES(${MAIN} ${Boost_LIBRARIES} ${catkin_LIBRARIES} ${humotion_LIBRARIES})
58
set_property(TARGET ${MAIN} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
59

  
60

  
61
#INSTALL(TARGETS ${MAIN} DESTINATION bin)
62
install(TARGETS ${MAIN}
63
    ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
64
    LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
65
    RUNTIME DESTINATION bin 
66
)
67

  
examples/humotion_meka/include/mekajointinterface.h
1
#pragma once
2
#include <humotion/server/joint_interface.h>
3
#include <humotion/server/server.h>
4
#include <boost/bimap.hpp>
5
//#include "meka_data_receiver.h"
6
//class MekaDataReceiver;
7
#include "ros/ros.h"
8
#include "sensor_msgs/JointState.h"
9
#include "trajectory_msgs/JointTrajectory.h"
10
#include <m3meka_msgs/M3ControlStates.h>
11

  
12
class MekaJointInterface : public humotion::server::JointInterface{
13
public:
14
    MekaJointInterface(std::string _input_scope, std::string control_scope, std::string _output_scope);
15
    ~MekaJointInterface();
16

  
17
    //void fetch_position(Device *dev, double timestamp);
18
    //void fetch_speed(Device *dev, double timestamp);
19
    //void fetch_position(int id, double value, double timestamp);
20
    //void fetch_speed(int id, double value, double timestamp);
21
    void run();
22

  
23
    static const int MAIN_LOOP_FREQUENCY = 50;
24

  
25
protected:
26
    void disable_joint(int e);
27
    void publish_target(int e, float position, float velocity);
28
    void enable_joint(int e);
29
    void execute_motion();
30

  
31
private:
32
    void incoming_controlstate(const m3meka_msgs::M3ControlStates &control_state);
33
    void incoming_jointstates(const sensor_msgs::JointState & msg);
34
    void store_dummy_data(int id, double timestamp);
35
    void store_min_max(int id, float min, float max);
36
    ros::Subscriber joint_state_subscriber;
37
    ros::Subscriber control_state_subscriber;
38
    ros::Publisher target_publisher;
39

  
40
    double get_timestamp_s();
41

  
42
    void set_eyelid_angle(double angle);
43
    void set_eyebrow_angle(int id);
44
    void set_mouth();
45

  
46
    //iCubDataReceiver *icub_data_receiver;
47
    void init_joints();
48
    double lid_angle;
49
    int lid_opening_previous;
50
    int previous_mouth_state;
51

  
52
    std::string input_scope;
53
    std::string control_scope;
54
    std::string output_scope;
55

  
56
    float last_pos_eye_vergence;
57
    float last_pos_eye_pan;
58
    float last_vel_eye_vergence;
59
    float last_vel_eye_pan;
60

  
61
    bool controller_enabled;
62

  
63
    void store_joint(int id, float value);
64
    void set_target_in_positionmode(int id, double value);
65
    void set_target_in_velocitymode(int id, double value);
66

  
67

  
68
    int convert_enum_to_motorid(int e);
69
    int convert_motorid_to_enum(int id);
70

  
71

  
72
    typedef boost::bimap<int, int > enum_id_bimap_t;
73
    typedef enum_id_bimap_t::value_type enum_id_bimap_entry_t;
74
    enum_id_bimap_t enum_id_bimap;
75
};
examples/humotion_meka/package.xml
1
<?xml version="1.0"?>
2
<package>
3
  <name>meka_humotion_server</name>
4
  <version>0.0.1</version>
5
  <description>humotion server for the mekabot</description>
6

  
7
  <!-- One maintainer tag required, multiple allowed, one person per tag --> 
8
  <maintainer email="sschulz@todo.todo">Simon Schulz</maintainer>
9

  
10

  
11
  <!-- One license tag required, multiple allowed, one license per tag -->
12
  <!-- Commonly used license strings: -->
13
  <!--   BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
14
  <license>TODO</license>
15

  
16

  
17
  <!-- Url tags are optional, but mutiple are allowed, one per tag -->
18
  <!-- Optional attribute type can be: website, bugtracker, or repository -->
19
  <!-- Example: -->
20
  <url type="website">https://projects.cit-ec.uni-bielefeld.de/git/flobidev.core.git</url> -->
21

  
22

  
23
  <!-- Author tags are optional, mutiple are allowed, one per tag -->
24
  <!-- Authors do not have to be maintianers, but could be -->
25
  <!-- Example: -->
26
  <!-- <author email="jane.doe@example.com">Jane Doe</author> -->
27

  
28

  
29
  <!-- The *_depend tags are used to specify dependencies -->
30
  <!-- Dependencies can be catkin packages or system dependencies -->
31
  <!-- Examples: -->
32
  <!-- Use build_depend for packages you need at compile time: -->
33
  <!-- Use buildtool_depend for build tool packages: -->
34
  <!--   <buildtool_depend>catkin</buildtool_depend> -->
35
  <!-- Use run_depend for packages you need at runtime: -->
36
  <!-- <run_depend>message_runtime</run_depend> -->
37
  <!-- Use test_depend for packages you need only for testing: -->
38
  <!--   <test_depend>gtest</test_depend> -->
39
  <buildtool_depend>catkin</buildtool_depend>
40
  <build_depend>roscpp</build_depend>
41
  <build_depend>std_msgs</build_depend>
42
  <!-- <run_depend>roscpp</run_depend> -->
43
  <!-- <run_depend>std_msgs</run_depend> -->
44

  
45
  <!--<build_depend>actionlib</build_depend>
46
  <build_depend>actionlib_msgs</build_depend>
47
  <run_depend>actionlib</run_depend> -->
48
  <!-- <run_depend>actionlib_msgs</run_depend> -->
49

  
50
  <!-- The export tag contains other, unspecified, tags -->
51
  <export>
52
    <!-- You can specify that this package is a metapackage here: -->
53
    <!-- <metapackage/> -->
54

  
55
    <!-- Other tools can request additional information be placed here -->
56

  
57
  </export>
58
</package>
examples/humotion_meka/src/main.cpp
1
#include <stdio.h>
2
#include <humotion/server/server.h>
3
#include <string>
4
#include <iostream>
5
//#include "meka_data_receiver.h"
6
#include "mekajointinterface.h"
7

  
8
using namespace std;
9

  
10
int main(int argc, char *argv[]){
11
    /*Property params;
12
    params.fromCommand(argc, argv);
13

  
14
    if (!params.check("robot")){
15
        fprintf(stderr, "Please specify the name of the robot\n");
16
        fprintf(stderr, "--robot name (e.g. icub or icubSim)\n");
17
        return -1;
18
    }
19

  
20
    string robotName=params.find("robot").asString().c_str();
21
    string scope="/"+robotName;
22

  
23
*/
24

  
25
    if (argc != 5){
26
        printf("> ERROR: invalid number of parameters passed to server!\n\n");
27
        printf("usage   : %s <humotion base topic> <jointstates topic> <controlstates topic> <control output topic>\n\n",argv[0]);
28
        printf("example : %s /meka /joint_states  /meka_roscontrol_state_manager/state /meka_roscontrol/head_position_trajectory_controller/command)\n\n",argv[0]);
29
        exit(EXIT_FAILURE);
30
    }
31

  
32
    //create humotion interface
33
    string humotion_scope      = argv[1];
34
    string jointstates_scope   = argv[2];
35
    string controlstates_scope = argv[3];
36
    string control_scope       = argv[4];
37

  
38

  
39
    MekaJointInterface *jointinterface = new MekaJointInterface(jointstates_scope, controlstates_scope, control_scope);
40
    humotion::server::Server *humotion_server = new humotion::server::Server(humotion_scope, "ROS", jointinterface);
41

  
42
    //finally run it
43
    jointinterface->run();
44

  
45
    while(humotion_server->ok()){
46
        usleep(100000);
47
    }
48

  
49
    printf("> ros connection died, will exit now\n");
50

  
51
    return 0;
52
}
examples/humotion_meka/src/mekajointinterface.cpp
1
#include "mekajointinterface.h"
2
using namespace std;
3

  
4
//WARNING: DO NOT CHANGE THIS; VELOCITYMODE IS NOT YET IMPLEMENTED
5
#define POSITION_CONTROL 1
6

  
7
void MekaJointInterface::incoming_controlstate(const m3meka_msgs::M3ControlStates &control_state){
8
    //incoming controller status
9
    for (unsigned int i = 0; i<control_state.group_name.size(); i++){
10
        if (control_state.group_name[i] == "head"){
11
            //enable/disable based on controller status
12
            if (control_state.state[i] == m3meka_msgs::M3ControlStates::START){
13
                //controller up and running
14
                if (!controller_enabled){
15
                    printf("> incoming control state (%d), enabling jointstate output\n",control_state.state[i]);
16
                    controller_enabled = true;
17
                }
18
            }else{
19
                //controller in estop/stopped etc
20
                if (controller_enabled){
21
                    printf("> incoming control state (%d), DISABLING jointstate output\n",control_state.state[i]);
22
                    controller_enabled = false;
23
                }
24
            }
25
        }
26
    }
27
}
28

  
29
void MekaJointInterface::incoming_jointstates(const sensor_msgs::JointState & msg){
30
    //fetch current timestamp
31
    double timestamp = msg.header.stamp.toSec();
32

  
33
    //iterate through incoming joints and filter out joints we need:
34
    for(int i=0; i<msg.name.size(); i++){
35

  
36
        string name = msg.name[i];
37
        //printf("incoming data for joint '%s'\n", name.c_str());
38

  
39
        int id = -1;
40
        if (name == "head_j1"){
41
            id = ID_NECK_PAN;
42
        }else if(name == "head_j0"){
43
            id = ID_NECK_TILT;
44
        }
45

  
46
        //store data:
47
        if (id != -1){
48
            //printf("> storing joint data for joint id %d\n", id);
49
            if (i >= msg.position.size()){
50
                printf("> joint state msg is missing position data for joint '%s'...\n", name.c_str());
51
                return;
52
            }
53
            if (i >= msg.velocity.size()){
54
                //printf("> joint state msg is missing velocity data for joint '%s'...\n", name.c_str());
55
                //exit(EXIT_FAILURE);
56
                return;
57
            }
58
            //ok, safe to access data
59
            if (id == ID_NECK_PAN){
60
                //joint is inverted
61
                JointInterface::store_incoming_position(id, -180.0 / M_PI * msg.position[i], timestamp);
62
                JointInterface::store_incoming_speed(   id, -180.0 / M_PI * msg.velocity[i], timestamp);
63
            }else if (id == ID_NECK_TILT){
64
                JointInterface::store_incoming_position(id, 180.0 / M_PI * msg.position[i], timestamp);
65
                JointInterface::store_incoming_speed(   id, 180.0 / M_PI * msg.velocity[i], timestamp);
66
            }
67
        }
68
    }
69

  
70
    //dummy data uses current time
71
    timestamp = get_timestamp_s();
72

  
73
    //store dummy positions for joints we do not know about:
74
    store_dummy_data(ID_LIP_LEFT_UPPER, timestamp);
75
    store_dummy_data(ID_LIP_LEFT_LOWER, timestamp);
76
    store_dummy_data(ID_LIP_CENTER_UPPER, timestamp);
77
    store_dummy_data(ID_LIP_CENTER_LOWER, timestamp);
78
    store_dummy_data(ID_LIP_RIGHT_UPPER, timestamp);
79
    store_dummy_data(ID_LIP_RIGHT_LOWER, timestamp);
80

  
81
    store_dummy_data(ID_NECK_ROLL, timestamp);
82
    store_dummy_data(ID_EYES_BOTH_UD, timestamp);
83
    store_dummy_data(ID_EYES_LEFT_LR, timestamp);
84
    store_dummy_data(ID_EYES_RIGHT_LR, timestamp);
85
    store_dummy_data(ID_EYES_LEFT_LID_LOWER, timestamp);
86
    store_dummy_data(ID_EYES_LEFT_LID_UPPER, timestamp);
87
    store_dummy_data(ID_EYES_LEFT_BROW, timestamp);
88
    store_dummy_data(ID_EYES_RIGHT_LID_LOWER, timestamp);
89
    store_dummy_data(ID_EYES_RIGHT_LID_UPPER, timestamp);
90
    store_dummy_data(ID_EYES_RIGHT_BROW, timestamp);
91

  
92
}
93

  
94
void MekaJointInterface::store_dummy_data(int id, double timestamp){
95
    JointInterface::store_incoming_position(id, 0.0, timestamp);
96
    JointInterface::store_incoming_speed(id, 0.0, timestamp);
97
}
98

  
99
//! constructor
100
MekaJointInterface::MekaJointInterface(string _input_scope, string control_scope, string _output_scope) : humotion::server::JointInterface(){
101
    input_scope = _input_scope;
102
    output_scope = _output_scope;
103

  
104
    controller_enabled = false;
105

  
106
    //subscribe to meka joint states
107
    int argc = 0;
108
    ros::init(argc, (char**)NULL, "meka_humotion");
109
    ros::NodeHandle n;
110

  
111
    printf("> listening on jointstates on '%s'\n",input_scope.c_str());
112
    joint_state_subscriber = n.subscribe(input_scope, 150, &MekaJointInterface::incoming_jointstates, this);
113

  
114
    printf("> listening on controlstates on '%s'\n",control_scope.c_str());
115
    control_state_subscriber = n.subscribe(control_scope, 150, &MekaJointInterface::incoming_controlstate, this);
116

  
117
    printf("> sending targets on '%s'\n", output_scope.c_str());
118
    target_publisher = n.advertise<trajectory_msgs::JointTrajectory>(output_scope, 100);
119

  
120
    //tell humotion about min/max joint values:
121
    init_joints();
122
}
123

  
124
//! destructor
125
MekaJointInterface::~MekaJointInterface(){
126
}
127

  
128

  
129

  
130
void MekaJointInterface::run(){
131
   ros::spin();
132
}
133

  
134

  
135
double MekaJointInterface::get_timestamp_s(){
136
    struct timespec spec;
137
    clock_gettime(CLOCK_REALTIME, &spec);
138
    return spec.tv_sec + spec.tv_nsec / 1.0e9;
139
}
140

  
141
//! set the target position of a joint
142
//! \param enum id of joint
143
//! \param float value
144
void MekaJointInterface::publish_target(int e, float position, float velocity){
145
   //we do this in execute motion for all joints at once...
146
}
147

  
148

  
149
//! actually execute the scheduled motion commands
150
void MekaJointInterface::execute_motion(){
151
    if (controller_enabled){
152
        //build msg
153
        trajectory_msgs::JointTrajectory msg;
154
        msg.joint_names.push_back("head_j0");
155
        msg.joint_names.push_back("head_j1");
156

  
157
        trajectory_msgs::JointTrajectoryPoint p;
158
        p.positions.push_back(get_target_position(ID_NECK_TILT) * M_PI / 180.0);
159
        //pan joint is inverted!
160
        p.positions.push_back(-get_target_position(ID_NECK_PAN) * M_PI / 180.0);
161
        //printf("targets pan=%4.1f tilt=%4.1f (eye p %4.1f t %4.2f)\n",joint_target[ID_NECK_TILT],joint_target[ID_NECK_PAN],joint_target[ID_EYES_LEFT_LR],joint_target[ID_EYES_BOTH_UD]);
162

  
163
        p.time_from_start = ros::Duration(1.2 * 1.0 / humotion::server::Server::MOTION_UPDATERATE);
164

  
165
        msg.points.push_back(p);
166

  
167
        target_publisher.publish(msg);
168
    }
169
}
170

  
171

  
172
//! prepare and enable a joint
173
//! NOTE: this should also prefill the min/max positions for this joint
174
//! \param the enum id of a joint
175
void MekaJointInterface::enable_joint(int e){
176
    //meka does not support this, joints are always enabled
177
}
178

  
179
//! shutdown and disable a joint
180
//! \param the enum id of a joint
181
void MekaJointInterface::disable_joint(int e){
182
    //meka does not support this, joints are always enabled
183
}
184

  
185
void MekaJointInterface::store_min_max(int id, float min, float max){
186
    joint_min[id] = min;
187
    joint_max[id] = max;
188
}
189

  
190
void MekaJointInterface::init_joints(){
191
    store_min_max(ID_NECK_TILT, -37, 1);
192
    store_min_max(ID_NECK_PAN, -70, 70);
193

  
194
    store_min_max(ID_NECK_ROLL, -1, 1);
195
    store_min_max(ID_EYES_BOTH_UD, -1, 1);
196
    store_min_max(ID_EYES_LEFT_LR, -1, 1);
197
    store_min_max(ID_EYES_RIGHT_LR, -1, 1);
198
    store_min_max(ID_EYES_LEFT_LID_UPPER, -1, 1);
199
    store_min_max(ID_EYES_LEFT_LID_LOWER, -1, 1);
200
    store_min_max(ID_EYES_RIGHT_LID_UPPER, -1, 1);
201
    store_min_max(ID_EYES_RIGHT_LID_LOWER, -1, 1);
202
    store_min_max(ID_EYES_LEFT_BROW, -1, 1);
203
    store_min_max(ID_EYES_RIGHT_BROW, -1, 1);
204
    store_min_max(ID_LIP_CENTER_UPPER, -1, 1);
205
    store_min_max(ID_LIP_CENTER_LOWER, -1, 1);
206
    store_min_max(ID_LIP_LEFT_UPPER, -1, 1);
207