30
30
#include < ignition/gazebo/components/JointPosition.hh>
31
31
#include < ignition/gazebo/components/JointVelocity.hh>
32
32
#include < ignition/gazebo/components/JointVelocityCmd.hh>
33
+ #include < ignition/gazebo/components/JointVelocityReset.hh>
34
+
33
35
#include < ignition/gazebo/components/JointAxis.hh>
34
36
#include < ignition/gazebo/components/JointType.hh>
35
37
#include < ignition/gazebo/components/Joint.hh>
@@ -66,9 +68,11 @@ struct jointData
66
68
67
69
// / \brief Current cmd joint position
68
70
double joint_position_cmd;
71
+ double joint_position_to_force_cmd;
69
72
70
73
// / \brief Current cmd joint velocity
71
74
double joint_velocity_cmd;
75
+ double joint_velocity_to_force_cmd;
72
76
73
77
// / \brief Current cmd joint effort
74
78
double joint_effort_cmd;
@@ -247,10 +251,21 @@ bool IgnitionSystem::initSim(
247
251
_ecm.CreateComponent (simjoint, ignition::gazebo::components::JointForce ());
248
252
}
249
253
254
+ const auto * jointAxis =
255
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
256
+ this ->dataPtr ->joints_ [j].sim_joint );
257
+
258
+ double upper = jointAxis->Data ().Upper ();
259
+ double lower = jointAxis->Data ().Lower ();
260
+ double max_velocity = jointAxis->Data ().MaxVelocity ();
261
+ double max_effort = jointAxis->Data ().Effort ();
262
+
263
+ double dummy_guess_p_pos = 10 * max_velocity / abs (upper - lower);
264
+
250
265
// PID parameters
251
266
double p_gain_pos =
252
267
(hardware_info.joints [j].parameters .find (" p_pos" ) ==
253
- hardware_info.joints [j].parameters .end () ) ? 100.0 : stod (
268
+ hardware_info.joints [j].parameters .end () ) ? dummy_guess_p_pos : stod (
254
269
hardware_info.joints [j].parameters .at (
255
270
" p_pos" ));
256
271
double i_gain_pos =
@@ -260,28 +275,28 @@ bool IgnitionSystem::initSim(
260
275
" i_pos" ));
261
276
double d_gain_pos =
262
277
(hardware_info.joints [j].parameters .find (" d_pos" ) ==
263
- hardware_info.joints [j].parameters .end () ) ? 5 .0 : stod (
278
+ hardware_info.joints [j].parameters .end () ) ? dummy_guess_p_pos / 100 .0 : stod (
264
279
hardware_info.joints [j].parameters .at (
265
280
" d_pos" ));
266
281
// set integral max and min component to 50 percent of the max effort
267
282
double iMax_pos =
268
283
(hardware_info.joints [j].parameters .find (" iMax_pos" ) ==
269
- hardware_info.joints [j].parameters .end () ) ? 500 .0 : stod (
284
+ hardware_info.joints [j].parameters .end () ) ? 0 .0 : stod (
270
285
hardware_info.joints [j].parameters .at (
271
286
" iMax_pos" ));
272
287
double iMin_pos =
273
288
(hardware_info.joints [j].parameters .find (" iMin_pos" ) ==
274
- hardware_info.joints [j].parameters .end () ) ? - 500 : stod (
289
+ hardware_info.joints [j].parameters .end () ) ? 0.0 : stod (
275
290
hardware_info.joints [j].parameters .at (
276
291
" iMin_pos" ));
277
292
double cmdMax_pos =
278
293
(hardware_info.joints [j].parameters .find (" cmdMax_pos" ) ==
279
- hardware_info.joints [j].parameters .end () ) ? 1000.0 : stod (
294
+ hardware_info.joints [j].parameters .end () ) ? max_velocity : stod (
280
295
hardware_info.joints [j].parameters .at (
281
296
" cmdMax_pos" ));
282
297
double cmdMin_pos =
283
298
(hardware_info.joints [j].parameters .find (" cmdMin_pos" ) ==
284
- hardware_info.joints [j].parameters .end () ) ? -1000.0 : stod (
299
+ hardware_info.joints [j].parameters .end () ) ? -1.0 * max_velocity : stod (
285
300
hardware_info.joints [j].parameters .at (
286
301
" cmdMin_pos" ));
287
302
double cmdOffset_pos =
@@ -305,38 +320,38 @@ bool IgnitionSystem::initSim(
305
320
306
321
double p_gain_vel =
307
322
(hardware_info.joints [j].parameters .find (" p_vel" ) ==
308
- hardware_info.joints [j].parameters .end () ) ? 100.0 : stod (
323
+ hardware_info.joints [j].parameters .end () ) ? dummy_guess_p_pos / 100.0 : stod (
309
324
hardware_info.joints [j].parameters .at (
310
325
" p_vel" ));
311
326
double i_gain_vel =
312
327
(hardware_info.joints [j].parameters .find (" i_vel" ) ==
313
- hardware_info.joints [j].parameters .end () ) ? 0 .0 : stod (
328
+ hardware_info.joints [j].parameters .end () ) ? dummy_guess_p_pos / 1000 .0 : stod (
314
329
hardware_info.joints [j].parameters .at (
315
330
" i_vel" ));
316
331
double d_gain_vel =
317
332
(hardware_info.joints [j].parameters .find (" d_vel" ) ==
318
- hardware_info.joints [j].parameters .end () ) ? 5 .0 : stod (
333
+ hardware_info.joints [j].parameters .end () ) ? 0 .0 : stod (
319
334
hardware_info.joints [j].parameters .at (
320
335
" d_vel" ));
321
336
// set integral max and min component to 50 percent of the max effort
322
337
double iMax_vel =
323
338
(hardware_info.joints [j].parameters .find (" iMax_vel" ) ==
324
- hardware_info.joints [j].parameters .end () ) ? 500 .0 : stod (
339
+ hardware_info.joints [j].parameters .end () ) ? max_effort / 2 .0 : stod (
325
340
hardware_info.joints [j].parameters .at (
326
341
" iMax_vel" ));
327
342
double iMin_vel =
328
343
(hardware_info.joints [j].parameters .find (" iMin_vel" ) ==
329
- hardware_info.joints [j].parameters .end () ) ? -500 : stod (
344
+ hardware_info.joints [j].parameters .end () ) ? -1.0 * max_effort / 2.0 : stod (
330
345
hardware_info.joints [j].parameters .at (
331
346
" iMin_vel" ));
332
347
double cmdMax_vel =
333
348
(hardware_info.joints [j].parameters .find (" cmdMax_vel" ) ==
334
- hardware_info.joints [j].parameters .end () ) ? 1000.0 : stod (
349
+ hardware_info.joints [j].parameters .end () ) ? max_effort : stod (
335
350
hardware_info.joints [j].parameters .at (
336
351
" cmdMax_vel" ));
337
352
double cmdMin_vel =
338
353
(hardware_info.joints [j].parameters .find (" cmdMin_vel" ) ==
339
- hardware_info.joints [j].parameters .end () ) ? -1000.0 : stod (
354
+ hardware_info.joints [j].parameters .end () ) ? -1.0 * max_effort : stod (
340
355
hardware_info.joints [j].parameters .at (
341
356
" cmdMin_vel" ));
342
357
double cmdOffset_vel =
@@ -814,19 +829,31 @@ hardware_interface::return_type IgnitionSystem::write(
814
829
name].cmdOffset_vel );
815
830
816
831
if (this ->dataPtr ->joints_ [i].joint_control_method & VELOCITY) {
817
- if (!this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocityCmd>(
818
- this ->dataPtr ->joints_ [i].sim_joint ))
819
- {
832
+
833
+ double velocity_error = this ->dataPtr ->joints_ [i].joint_velocity - std::clamp (
834
+ this ->dataPtr ->joints_ [i].joint_velocity_cmd , -1.0 * jointAxis->Data ().MaxVelocity (),
835
+ jointAxis->Data ().MaxVelocity ());
836
+
837
+ // calculate target force/torque - output of inner pid
838
+ double target_force = this ->dataPtr ->joints_ [i].pid_vel .Update (
839
+ velocity_error, std::chrono::duration<double >(
840
+ period.to_chrono <std::chrono::nanoseconds>()));
841
+
842
+ this ->dataPtr ->joints_ [i].joint_velocity_to_force_cmd = target_force;
843
+
844
+ auto forceCmd =
845
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
846
+ this ->dataPtr ->joints_ [i].sim_joint );
847
+
848
+ if (forceCmd == nullptr ) {
820
849
this ->dataPtr ->ecm ->CreateComponent (
821
850
this ->dataPtr ->joints_ [i].sim_joint ,
822
- ignition::gazebo::components::JointVelocityCmd ({ 0 }));
851
+ ignition::gazebo::components::JointForceCmd ({target_force }));
823
852
} else {
824
- const auto jointVelCmd =
825
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocityCmd>(
826
- this ->dataPtr ->joints_ [i].sim_joint );
827
- *jointVelCmd = ignition::gazebo::components::JointVelocityCmd (
828
- {this ->dataPtr ->joints_ [i].joint_velocity_cmd });
853
+ *forceCmd = ignition::gazebo::components::JointForceCmd (
854
+ {target_force});
829
855
}
856
+
830
857
} else if (this ->dataPtr ->joints_ [i].joint_control_method & POSITION) {
831
858
832
859
// calculate error with clamped position command
@@ -855,6 +882,8 @@ hardware_interface::return_type IgnitionSystem::write(
855
882
velocity_error, std::chrono::duration<double >(
856
883
period.to_chrono <std::chrono::nanoseconds>()));
857
884
885
+ this ->dataPtr ->joints_ [i].joint_position_to_force_cmd = target_force;
886
+
858
887
auto forceCmd =
859
888
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
860
889
this ->dataPtr ->joints_ [i].sim_joint );
@@ -892,49 +921,51 @@ hardware_interface::return_type IgnitionSystem::write(
892
921
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
893
922
this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
894
923
895
- const auto * jointAxisMimicked =
896
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
897
- this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].sim_joint );
898
-
899
924
if (mimic_interface == " position" ) {
900
925
// Get the joint position
901
- const auto * jp_mimicked =
926
+ double position_mimicked_joint =
902
927
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointPosition>(
903
- this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].sim_joint );
904
- double position_mimicked_joint = jp_mimicked->Data ()[0 ];
928
+ this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].sim_joint )->Data ()[0 ];
905
929
906
- const auto * jp_mimic =
930
+ double position_mimic_joint =
907
931
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointPosition>(
908
- this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
909
- double position_mimic_joint = jp_mimic->Data ()[0 ];
932
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint )->Data ()[0 ];
910
933
911
- // calculate error with clamped position command
912
- double position_error = position_mimic_joint - position_mimicked_joint *
913
- mimic_joint.multiplier ;
934
+ double position_error = position_mimic_joint - std::clamp (
935
+ position_mimicked_joint *
936
+ mimic_joint.multiplier , jointAxis->Data ().Lower (),
937
+ jointAxis->Data ().Upper ());
914
938
915
- double velocity_sp = (-1.0 ) * position_error * (*this ->dataPtr ->update_rate );
939
+ position_error =
940
+ copysign (
941
+ 1.0 ,
942
+ position_error) *
943
+ std::clamp (
944
+ std::abs (position_error), 0.0 ,
945
+ std::abs (jointAxis->Data ().Upper () - jointAxis->Data ().Lower ()));
916
946
917
947
// calculate target velocity - output of outer pid - input to inner pid
918
948
double target_vel = this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid_pos .Update (
919
949
position_error, std::chrono::duration<double >(
920
950
period.to_chrono <std::chrono::nanoseconds>()));
921
951
922
- const auto * jv_mimic =
952
+ // get mimic joint velocity
953
+ double velocity_mimic_joint =
923
954
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocity>(
924
- this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
925
- double velocity_mimic_joint = jv_mimic->Data ()[0 ];
955
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint )->Data ()[0 ];
926
956
927
- double velocity_error = velocity_mimic_joint - target_vel;
957
+ double velocity_error = velocity_mimic_joint - std::clamp (
958
+ target_vel, -1.0 * jointAxis->Data ().MaxVelocity (),
959
+ jointAxis->Data ().MaxVelocity ());
928
960
961
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid_vel .SetCmdOffset (
962
+ mimic_joint.multiplier *
963
+ this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].joint_position_to_force_cmd );
929
964
// calculate target force/torque - output of inner pid
930
965
double target_force = this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid_vel .Update (
931
966
velocity_error, std::chrono::duration<double >(
932
967
period.to_chrono <std::chrono::nanoseconds>()));
933
968
934
- igndbg << " [" << this ->dataPtr ->joints_ [mimic_joint.joint_index ].name << " ]\t " <<
935
- position_mimicked_joint << " \t " << position_mimic_joint << " \t " << target_force <<
936
- " \n " ;
937
-
938
969
auto forceCmd =
939
970
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
940
971
this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
@@ -948,25 +979,41 @@ hardware_interface::return_type IgnitionSystem::write(
948
979
{target_force});
949
980
}
950
981
}
951
- /*
952
982
if (mimic_interface == " velocity" ) {
953
983
// get the velocity of mimicked joint
954
984
double velocity_mimicked_joint =
955
985
this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocity>(
956
986
this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].sim_joint )->Data ()[0 ];
957
987
958
- if (!this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocityCmd>(
959
- this->dataPtr->joints_[mimic_joint.joint_index].sim_joint))
960
- {
988
+ // get mimic joint velocity
989
+ double velocity_mimic_joint =
990
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocity>(
991
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint )->Data ()[0 ];
992
+
993
+ double velocity_error = velocity_mimic_joint - std::clamp (
994
+ mimic_joint.multiplier * velocity_mimicked_joint, -1.0 * jointAxis->Data ().MaxVelocity (),
995
+ jointAxis->Data ().MaxVelocity ());
996
+
997
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid_vel .SetCmdOffset (
998
+ mimic_joint.multiplier *
999
+ this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].joint_velocity_to_force_cmd );
1000
+
1001
+ // calculate target force/torque - output of inner pid
1002
+ double target_force = this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid_vel .Update (
1003
+ velocity_error, std::chrono::duration<double >(
1004
+ period.to_chrono <std::chrono::nanoseconds>()));
1005
+
1006
+ auto forceCmd =
1007
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
1008
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
1009
+
1010
+ if (forceCmd == nullptr ) {
961
1011
this ->dataPtr ->ecm ->CreateComponent (
962
1012
this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint ,
963
- ignition::gazebo::components::JointVelocityCmd({0 }));
1013
+ ignition::gazebo::components::JointForceCmd ({target_force }));
964
1014
} else {
965
- const auto jointVelCmd =
966
- this->dataPtr->ecm->Component<ignition::gazebo::components::JointVelocityCmd>(
967
- this->dataPtr->joints_[mimic_joint.joint_index].sim_joint);
968
- *jointVelCmd = ignition::gazebo::components::JointVelocityCmd(
969
- {mimic_joint.multiplier * velocity_mimicked_joint});
1015
+ *forceCmd = ignition::gazebo::components::JointForceCmd (
1016
+ {target_force});
970
1017
}
971
1018
}
972
1019
if (mimic_interface == " effort" ) {
@@ -987,9 +1034,9 @@ hardware_interface::return_type IgnitionSystem::write(
987
1034
this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
988
1035
*jointEffortCmd = ignition::gazebo::components::JointForceCmd (
989
1036
{mimic_joint.multiplier *
990
- this->dataPtr->joints_[mimic_joint.mimicked_joint_index].joint_effort });
1037
+ this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].joint_effort_cmd });
991
1038
}
992
- }*/
1039
+ }
993
1040
}
994
1041
}
995
1042
0 commit comments