iCub-main
Loading...
Searching...
No Matches
tutorial_arm_joint_impedance.cpp
Go to the documentation of this file.
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#include <stdio.h>
4#include <yarp/os/Network.h>
5#include <yarp/dev/ControlBoardInterfaces.h>
6#include <yarp/dev/PolyDriver.h>
7#include <yarp/os/Time.h>
8#include <yarp/sig/Vector.h>
9
10#include <string>
11
12using namespace yarp::dev;
13using namespace yarp::sig;
14using namespace yarp::os;
15
16int main(int argc, char *argv[])
17{
18 Network yarp;
19
20 Property params;
21 params.fromCommand(argc, argv);
22
23 if (!params.check("robot"))
24 {
25 fprintf(stderr, "Please specify the name of the robot\n");
26 fprintf(stderr, "--robot name (e.g. icub)\n");
27 return 1;
28 }
29 std::string robotName=params.find("robot").asString();
30 std::string remotePorts="/";
31 remotePorts+=robotName;
32 remotePorts+="/right_arm";
33
34 std::string localPorts="/test/client";
35
36 Property options;
37 options.put("device", "remote_controlboard");
38 options.put("local", localPorts); //local port names
39 options.put("remote", remotePorts); //where we connect to
40
41 // create a device
42 PolyDriver robotDevice(options);
43 if (!robotDevice.isValid()) {
44 printf("Device not available. Here are the known devices:\n");
45 printf("%s", Drivers::factory().toString().c_str());
46 return 1;
47 }
48
49 IPositionControl *pos;
50 IEncoders *encs;
51 IControlMode *ictrl;
52 IInteractionMode *iint;
53 IImpedanceControl *iimp;
54 ITorqueControl *itrq;
55
56
57 bool ok;
58 ok = robotDevice.view(pos);
59 ok = ok && robotDevice.view(encs);
60 ok = ok && robotDevice.view(ictrl);
61 ok = ok && robotDevice.view(iimp);
62 ok = ok && robotDevice.view(itrq);
63 ok = ok && robotDevice.view(iint);
64
65 if (!ok) {
66 printf("Problems acquiring interfaces\n");
67 return 1;
68 }
69
70 int nj=0;
71 pos->getAxes(&nj);
72 Vector encoders;
73 Vector torques;
74 Vector command;
75 Vector tmp;
76 int control_mode;
77 yarp::dev::InteractionModeEnum interaction_mode;
78 encoders.resize(nj);
79 torques.resize(nj);
80 tmp.resize(nj);
81 command.resize(nj);
82
83 int i;
84 for (i = 0; i < nj; i++) {
85 tmp[i] = 50.0;
86 }
87 pos->setRefAccelerations(tmp.data());
88
89 for (i = 0; i < nj; i++) {
90 tmp[i] = 10.0;
91 pos->setRefSpeed(i, tmp[i]);
92 //SET THE IMPEDANCE:
93 //0.111 is the stiffness coefficient. units: Nm/deg
94 //0.014 is the damping coefficient. units: Nm/(deg/s)
95 //0 is the additional torque offset
96 //WARNING: playing with this value may lead to undamped oscillatory behaviours.
97 //when you raise the stiffness, you should increase the damping coefficient accordingly.
98 iimp->setImpedance(i, 0.111, 0.014);
99 }
100
101 //pos->setRefSpeeds(tmp.data()))
102
103 //fisrst zero all joints
104 //
105 command=0;
106 //now set the shoulder to some value
107 command[0]=-50;
108 command[1]=20;
109 command[2]=-10;
110 command[3]=50;
111 pos->positionMove(command.data());
112
113 /*
114 bool done=false;
115
116 while(!done)
117 {
118 pos->checkMotionDone(&done);
119 Time::delay(0.1);
120 }
121 */
122
123 int times=0;
124 while(true)
125 {
126 times++;
127 if (times%2)
128 {
129 // set the elbow joint only in compliant mode
130 ictrl->setControlMode(3,VOCAB_CM_POSITION);
131 iint->setInteractionMode(3,VOCAB_IM_COMPLIANT);
132 // set new reference positions
133 command[0]=-50;
134 command[1]=20;
135 command[2]=-10;
136 command[3]=60;
137 }
138 else
139 {
140 // set the elbow joint in stiff mode
141 ictrl->setControlMode(3,VOCAB_CM_POSITION);
142 iint->setInteractionMode(3,VOCAB_IM_STIFF);
143 // set new reference positions
144 command[0]=-20;
145 command[1]=40;
146 command[2]=-10;
147 command[3]=30;
148 }
149
150 pos->positionMove(command.data());
151
152 int count=50;
153 while(count--)
154 {
155 Time::delay(0.1);
156 encs->getEncoders(encoders.data());
157 itrq->getTorques(torques.data());
158 printf("Encoders: %+5.1lf %+5.1lf %+5.1lf %+5.1lf ", encoders[0], encoders[1], encoders[2], encoders[3]);
159 printf("Torques: %+5.1lfNm %+5.1lfNm %+5.1lfNm %+5.1lfNm ", torques[0], torques[1], torques[2], torques[3]);
160 printf("Control: ");
161 for (i = 0; i < 4; i++)
162 {
163 ictrl->getControlMode(i, &control_mode);
164 iint->getInteractionMode(i, &interaction_mode);
165 switch (control_mode)
166 {
167 case VOCAB_CM_IDLE: printf("IDLE "); break;
168 case VOCAB_CM_POSITION: printf("POSITION "); break;
169 case VOCAB_CM_POSITION_DIRECT: printf("POSITION DIRECT "); break;
170 case VOCAB_CM_VELOCITY: printf("VELOCITY "); break;
171 case VOCAB_CM_MIXED: printf("MIXED POS/VEL"); break;
172 case VOCAB_CM_TORQUE: printf("TORQUE "); break;
173 default:
174 case VOCAB_CM_UNKNOWN: printf("UNKNOWN "); break;
175 }
176 }
177 printf("\n");
178 printf("Interaction: ");
179 for (i = 0; i < 4; i++)
180 {
181 switch (interaction_mode)
182 {
183 case VOCAB_IM_COMPLIANT: printf("(COMPLIANT MODE)"); break;
184 case VOCAB_IM_STIFF: printf("(STIFF MODE)"); break;
185 default:
186 case VOCAB_CM_UNKNOWN: printf("(UNKNOWN) "); break;
187 }
188 }
189 printf("\n");
190 }
191 }
192
193 robotDevice.close();
194
195 return 0;
196}
int main()
Definition main.cpp:67
Copyright (C) 2008 RobotCub Consortium.
fprintf(fid,'\n')