~csiro-asl/csiro-asl-ros-drivers/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// This file is a simple test of constructing a combined controller as a
// plugin similar to the multicontroller plugin, but hard-coded for the
// test robot in this package.
//
// Copyright (c) 2010, CSIRO Autonomous Systems Laboratory, Nick Hillier

// Copyright (C) 2006-2009 Rosen Diankov (rdiankov@cs.cmu.edu)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <rave/plugin.h>
#include <odephysics_ORplugin/csiroplugindefs.h>

#include <openrave-core.h>
#include <vector>
#include <cstring>
#include <sstream>

#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>

using namespace std;




class csiro_combinedcontroller : public OpenRAVE::ControllerBase
{
public:

    csiro_combinedcontroller(OpenRAVE::EnvironmentBasePtr penv) : OpenRAVE::ControllerBase(penv)
    {
        _penv = penv;
        __description = ":Simple controller that redirects input to the velocity and ideal controllers.";
    }


    virtual bool Init(OpenRAVE::RobotBasePtr robot, const std::vector<int>& dofindices, int nControlTransformation)
    {
         _robot = robot;
        if (!!_robot) {
            // the input arguments are ignored
            std::vector<int> mydofindices;
            for (int i=0; i < 6; i++){
                mydofindices.push_back(i);
            }
            _dofindices = mydofindices;
            _nControlTransformation = 0;
        } else {
            return false;
        }
        
        /* Create and attach a velocity controller for the wheels */
        _wheelcontroller = OpenRAVE::RaveCreateController(_penv,"csiro_odevelocity");
        //_wheelcontroller = OpenRAVE::RaveCreateController(_penv,"odevelocity");
        wheelDOFs.push_back(2);wheelDOFs.push_back(3);wheelDOFs.push_back(4);wheelDOFs.push_back(5);
        if( !_wheelcontroller->Init(_robot,wheelDOFs,_nControlTransformation) ) {
            return false;
        }
        _listcontrollers.push_back(_wheelcontroller);
        /* Create and attach an ideal controller for the arm */
        _armcontroller = OpenRAVE::RaveCreateController(_penv,"idealcontroller");
        armDOFs.push_back(1);armDOFs.push_back(0);
        if( !_armcontroller->Init(_robot,armDOFs,_nControlTransformation) ) {
            return false;
        }
        _listcontrollers.push_back(_armcontroller);
        return true;
    }

    virtual void Reset(int options) { }

    virtual const std::vector<int>& GetControlDOFIndices() const { return _dofindices; }
    virtual int IsControlTransformation() const { return _nControlTransformation; }

    virtual bool SetDesired(const std::vector<OpenRAVE::dReal>& values, OpenRAVE::TransformConstPtr trans) {
        vector<dReal> v;
        vector<dReal> q;
        bool bsuccess = true;
        {
            EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
            FOREACH(itcontroller,_listcontrollers) {
                v.resize(0);
                FOREACHC(it, (*itcontroller)->GetControlDOFIndices()) {
                    v.push_back(values.at(*it));
                }
                {
                    if ((*itcontroller) == _wheelcontroller) {
                        //RAVELOG_INFO("Sending command to wheels\n");
                        stringstream sout,ss; ss << "setvelocity ";
                        for(size_t i = 0; i < wheelDOFs.size(); ++i) {
                            ss << v[i] << " ";
                        }
                        bsuccess &= _wheelcontroller->SendCommand(sout,ss); 
                    } else {
                        //RAVELOG_INFO("Sending command to arm\n");
                        
                        // just set the joint positions discontinuously
                        //bsuccess &= (*itcontroller)->SetDesired(v,trans);
                        
                        // use a trajectory generator to move the joint positions smoothly
                        OpenRAVE::TrajectoryBasePtr traj = OpenRAVE::RaveCreateTrajectory(_penv,armDOFs.size());
                        _robot->SetActiveDOFs(armDOFs);
                        _robot->GetActiveDOFValues(q); // get current values
                        traj->AddPoint(OpenRAVE::TrajectoryBase::TPOINT(q,0.0f)); // start point for the trajectory
                        traj->AddPoint(OpenRAVE::TrajectoryBase::TPOINT(v,2.0f)); // end point for the trajectory
                        traj->CalcTrajTiming(_robot,OpenRAVE::TrajectoryBase::CUBIC,false,true); // initialize the trajectory structures
                        bsuccess &= (*itcontroller)->SetPath(traj);
                    }
                }
            }
        }
        return bsuccess;
    }
    virtual bool SetPath(OpenRAVE::TrajectoryBaseConstPtr ptraj) {
        return false;
    }
    virtual void SimulationStep(OpenRAVE::dReal fTimeElapsed) {
        EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
        FOREACH(it,_listcontrollers) {
            (*it)->SimulationStep(fTimeElapsed);
        }
    }
    virtual bool IsDone() {
        EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
        bool bdone=true;
        FOREACH(it,_listcontrollers) {
            bdone &= (*it)->IsDone();
        }
        return bdone; 
    }
    virtual OpenRAVE::dReal GetTime() const { 
        dReal t = 0;
        {
            EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
            FOREACHC(it,_listcontrollers) {
                if( it == _listcontrollers.begin() ) {
                    t = (*it)->GetTime();
                }
                else {
                    dReal tnew = (*it)->GetTime();
                    if( RaveFabs(t-tnew) > 0.000001 ) {
                        RAVELOG_WARN(str(boost::format("multi-controller time is different! %f!=%f\n")%t%tnew));
                    }
                    t = max(t,tnew);
                }
            }
        }
        return t;
    }
    void GetVelocity(std::vector<dReal>& vel) const
    {
        vel.resize(_dofindices.size());
        FOREACH(it,vel) {
            *it = 0;
        }
        vector<dReal> v;
        {
            EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
            FOREACHC(itcontroller,_listcontrollers) {
                (*itcontroller)->GetVelocity(v);
                int index=0;
                FOREACH(it,v) {
                    vel.at((*itcontroller)->GetControlDOFIndices().at(index++)) = *it;
                }
            }
        }
    }

    void GetTorque(std::vector<dReal>& torque) const
    {
        torque.resize(_dofindices.size());
        FOREACH(it,torque) {
            *it = 0;
        }
        vector<dReal> v;
        {
            EnvironmentMutex::scoped_lock lock(GetEnv()->GetMutex());
            FOREACHC(itcontroller,_listcontrollers) {
                (*itcontroller)->GetTorque(v);
                int index=0;
                FOREACH(it,v) {
                    torque.at((*itcontroller)->GetControlDOFIndices().at(index++)) = *it;
                }
            }
        }
    }
    
    virtual OpenRAVE::RobotBasePtr GetRobot() const { return _robot; }

    bool SendCommand(std::ostream& os, std::istream& is)
    {
        return false;
    }
    
protected:
    OpenRAVE::EnvironmentBasePtr _penv;
    OpenRAVE::RobotBasePtr _robot;
    std::vector<int> _dofindices;
    OpenRAVE::ControllerBasePtr _wheelcontroller, _armcontroller;
    std::list<OpenRAVE::ControllerBasePtr> _listcontrollers;
    int _nControlTransformation;

    std::vector<int> wheelDOFs, armDOFs;

};

static list< boost::shared_ptr<void> >* s_listRegisteredReaders=NULL;
InterfaceBasePtr CreateInterfaceValidated(InterfaceType type, const std::string& interfacename, std::istream& sinput, EnvironmentBasePtr penv)
{
    switch(type) {
    case OpenRAVE::PT_Controller:
        //printf("Interface name to match = %s\n",interfacename.c_str());
        if(  (interfacename == "csiro_combinedcontroller")){
            return InterfaceBasePtr(new csiro_combinedcontroller(penv));
        }    
        break;
    default:
        break;
    }

    return InterfaceBasePtr();
}

void GetPluginAttributesValidated(PLUGININFO& info)
{
    info.interfacenames[OpenRAVE::PT_Controller].push_back("csiro_combinedcontroller");
}

RAVE_PLUGIN_API void DestroyPlugin()
{
    delete s_listRegisteredReaders;
    s_listRegisteredReaders = NULL;
}