humotion / src / server / reflexxes_motion_generator.cpp @ 7b67834d
History | View | Annotate | Download (4.909 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/eye_motion_generator.h" |
29 |
|
30 |
using namespace std; |
31 |
using namespace humotion; |
32 |
using namespace humotion::server; |
33 |
|
34 |
//! constructor
|
35 |
ReflexxesMotionGenerator::ReflexxesMotionGenerator(JointInterface *j, int dof, float t) : MotionGenerator(j){ |
36 |
dof_count = dof; |
37 |
|
38 |
//create Reflexxes API for <dof> DOF actuator
|
39 |
reflexxes_api = new ReflexxesAPI(dof, t);
|
40 |
reflexxes_position_input = new RMLPositionInputParameters(dof);
|
41 |
reflexxes_position_output = new RMLPositionOutputParameters(dof);
|
42 |
|
43 |
//synchronize phase
|
44 |
reflexxes_motion_flags.SynchronizationBehavior = RMLPositionFlags::PHASE_SYNCHRONIZATION_IF_POSSIBLE; |
45 |
} |
46 |
|
47 |
//! destructor
|
48 |
ReflexxesMotionGenerator::~ReflexxesMotionGenerator(){ |
49 |
|
50 |
} |
51 |
|
52 |
//! feed motion generator with target data:
|
53 |
//! \param dof id
|
54 |
//! \param target angle
|
55 |
//! \param max_speed max reachable speed during accel
|
56 |
//! \param max_accel max allowable acceleration
|
57 |
void ReflexxesMotionGenerator::reflexxes_set_input(int dof, float target, |
58 |
float current_position, float current_speed, |
59 |
humotion::Timestamp timestamp, |
60 |
float max_speed, float max_accel){ |
61 |
assert(dof < dof_count); |
62 |
|
63 |
//set up reflexxes:
|
64 |
reflexxes_position_input->TargetPositionVector->VecData[dof] = target; |
65 |
reflexxes_position_input->SelectionVector->VecData[dof] = true;
|
66 |
reflexxes_position_input->MaxVelocityVector->VecData[dof] = max_speed; |
67 |
reflexxes_position_input->MaxAccelerationVector->VecData[dof] = max_accel; |
68 |
reflexxes_position_input->TargetVelocityVector->VecData[dof] = 0.0; //target speed is zero (really?) |
69 |
|
70 |
// feed back current pos & velocity
|
71 |
// as we have to deal with some latency we will forecast the current
|
72 |
// position using the old speed, position and the latency:
|
73 |
float time_diff = humotion::Timestamp::now().to_seconds() - timestamp.to_seconds();
|
74 |
if (time_diff > 0.1) { |
75 |
printf("WARNING: timestamp of position data older than 0.1s (measured %fs)!\n",time_diff);
|
76 |
printf(" this should never happen! clocks not in sync?\n");
|
77 |
} |
78 |
// assuming this time difference is small we will now do a linear
|
79 |
// approximation of the current position based on these old measurements
|
80 |
// under the assumption that the speed is "constant" during this short period
|
81 |
float position_now = current_position + time_diff * current_speed;
|
82 |
|
83 |
printf("HTS: diff = %f ms, pos=%f --> %f\n", time_diff*1000.0,current_position, position_now); |
84 |
|
85 |
|
86 |
//reflexxes_position_input->CurrentPositionVector->VecData[dof] = position_now;
|
87 |
//reflexxes_position_input->CurrentVelocityVector->VecData[dof] = current_speed;
|
88 |
|
89 |
// safety: libreflexxes does not like zero accellerations...
|
90 |
if (reflexxes_position_input->MaxAccelerationVector->VecData[dof] == 0.0){ |
91 |
reflexxes_position_input->MaxAccelerationVector->VecData[dof] = 0.0001; |
92 |
} |
93 |
} |
94 |
|
95 |
|
96 |
//! calculate motion profile
|
97 |
|
98 |
void ReflexxesMotionGenerator::reflexxes_calculate_profile(){
|
99 |
int res = reflexxes_api->RMLPosition(*reflexxes_position_input, reflexxes_position_output, reflexxes_motion_flags);
|
100 |
|
101 |
if (res < 0){ |
102 |
if (res == ReflexxesAPI::RML_ERROR_INVALID_INPUT_VALUES){
|
103 |
printf("> ReflexxesMotionGenerator --> ReflexxesAPI::RML_ERROR_INVALID_INPUT_VALUES error\n");
|
104 |
}else{
|
105 |
printf("> ReflexxesMotionGenerator --> ReflexxesAPI::UNKNOWN_ERROR: reflexxes error %d\n",res);
|
106 |
} |
107 |
} |
108 |
|
109 |
//feed back values:
|
110 |
for(int i=0; i<dof_count; i++){ |
111 |
reflexxes_position_input->CurrentPositionVector->VecData[i] = reflexxes_position_output->NewPositionVector->VecData[i]; |
112 |
reflexxes_position_input->CurrentVelocityVector->VecData[i] = reflexxes_position_output->NewVelocityVector->VecData[i]; |
113 |
reflexxes_position_input->CurrentAccelerationVector->VecData[i] = reflexxes_position_output->NewAccelerationVector->VecData[i]; |
114 |
} |
115 |
} |