@@ -352,32 +352,39 @@ bool IgnitionSystem::initSim(
352
352
// init position PID
353
353
354
354
// assuming every joint has axis
355
- const auto * jointAxis =
356
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
357
- this ->dataPtr ->joints_ [i].sim_joint );
355
+ const auto * jointAxis =
356
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
357
+ this ->dataPtr ->joints_ [i].sim_joint );
358
358
// PID parameters
359
- double p_gain = 100.0 ;
360
- double i_gain = 0.0 ;
361
- double d_gain = 50.0 ;
359
+ double p_gain = 100.0 ;
360
+ double i_gain = 0.0 ;
361
+ double d_gain = 50.0 ;
362
362
// set integral max and min component to 10 percent of the max effort
363
- double iMax = std::isnan (jointAxis->Data ().Effort ()) ? 1.0 : jointAxis->Data ().Effort () * 0.1 ;
364
- double iMin = std::isnan (jointAxis->Data ().Effort ()) ? 1.0 : -jointAxis->Data ().Effort () * 0.1 ;
365
- double cmdMax = std::isnan (jointAxis->Data ().Effort ()) ? 1000.0 : jointAxis->Data ().Effort ();
366
- double cmdMin = std::isnan (jointAxis->Data ().Effort ()) ? -1000.0 : -jointAxis->Data ().Effort ();
363
+ double iMax = std::isnan (jointAxis->Data ().Effort ()) ? 1.0 : jointAxis->Data ().Effort () *
364
+ 0.1 ;
365
+ double iMin = std::isnan (jointAxis->Data ().Effort ()) ? 1.0 : -jointAxis->Data ().Effort () *
366
+ 0.1 ;
367
+ double cmdMax =
368
+ std::isnan (jointAxis->Data ().Effort ()) ? 1000.0 : jointAxis->Data ().Effort ();
369
+ double cmdMin =
370
+ std::isnan (jointAxis->Data ().Effort ()) ? -1000.0 : -jointAxis->Data ().Effort ();
367
371
double cmdOffset = 0.0 ;
368
372
369
- igndbg << " [JointController " <<joint_name<<" ] Position based PID with Force/Torque output:" << std::endl;
370
- igndbg << " p_gain: [" << p_gain << " ]" << std::endl;
371
- igndbg << " i_gain: [" << i_gain << " ]" << std::endl;
372
- igndbg << " d_gain: [" << d_gain << " ]" << std::endl;
373
- igndbg << " i_max: [" << iMax << " ]" << std::endl;
374
- igndbg << " i_min: [" << iMin << " ]" << std::endl;
375
- igndbg << " cmd_max: [" << cmdMax << " ]" << std::endl;
376
- igndbg << " cmd_min: [" << cmdMin << " ]" << std::endl;
377
- igndbg << " cmd_offset: [" << cmdOffset << " ]" << std::endl;
373
+ igndbg << " [JointController " << joint_name <<
374
+ " ] Position based PID with Force/Torque output:" << std::endl;
375
+ igndbg << " p_gain: [" << p_gain << " ]" << std::endl;
376
+ igndbg << " i_gain: [" << i_gain << " ]" << std::endl;
377
+ igndbg << " d_gain: [" << d_gain << " ]" << std::endl;
378
+ igndbg << " i_max: [" << iMax << " ]" << std::endl;
379
+ igndbg << " i_min: [" << iMin << " ]" << std::endl;
380
+ igndbg << " cmd_max: [" << cmdMax << " ]" << std::endl;
381
+ igndbg << " cmd_min: [" << cmdMin << " ]" << std::endl;
382
+ igndbg << " cmd_offset: [" << cmdOffset << " ]" << std::endl;
378
383
379
384
380
- this ->dataPtr ->joints_ [j].pid .Init (p_gain, i_gain, d_gain, iMax, iMin, cmdMax, cmdMin, cmdOffset);
385
+ this ->dataPtr ->joints_ [j].pid .Init (
386
+ p_gain, i_gain, d_gain, iMax, iMin, cmdMax, cmdMin,
387
+ cmdOffset);
381
388
382
389
} else if (joint_info.command_interfaces [i].name == " velocity" ) {
383
390
RCLCPP_INFO_STREAM (this ->nh_ ->get_logger (), " \t\t velocity" );
@@ -411,6 +418,18 @@ bool IgnitionSystem::initSim(
411
418
412
419
registerSensors (hardware_info);
413
420
421
+ this ->node_ = rclcpp::Node::make_shared (
422
+ hardware_info.name ,
423
+ rclcpp::NodeOptions ().allow_undeclared_parameters (
424
+ true ).automatically_declare_parameters_from_overrides (true ));
425
+
426
+ auto spin = [this ]()
427
+ {
428
+ rclcpp::spin (node_);
429
+ };
430
+
431
+ spin_thread_ = std::thread (spin);
432
+
414
433
return true ;
415
434
}
416
435
@@ -487,6 +506,14 @@ CallbackReturn
487
506
IgnitionSystem::on_init (const hardware_interface::HardwareInfo & system_info)
488
507
{
489
508
RCLCPP_WARN (this ->nh_ ->get_logger (), " On init..." );
509
+ try {
510
+ // Create the parameter listener and get the parameters
511
+ param_listener_ = std::make_shared<ParamListener>(this ->node_ );
512
+ params_ = param_listener_->get_params ();
513
+ } catch (const std::exception & e) {
514
+ fprintf (stderr, " Exception thrown during init stage with message: %s \n " , e.what ());
515
+ return CallbackReturn::ERROR;
516
+ }
490
517
if (hardware_interface::SystemInterface::on_init (system_info) != CallbackReturn::SUCCESS) {
491
518
return CallbackReturn::ERROR;
492
519
}
@@ -623,10 +650,10 @@ hardware_interface::return_type IgnitionSystem::write(
623
650
const rclcpp::Duration & period)
624
651
{
625
652
for (unsigned int i = 0 ; i < this ->dataPtr ->joints_ .size (); ++i) {
626
- // assuming every joint has axis
627
- const auto * jointAxis =
628
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
629
- this ->dataPtr ->joints_ [i].sim_joint );
653
+ // assuming every joint has axis
654
+ const auto * jointAxis =
655
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
656
+ this ->dataPtr ->joints_ [i].sim_joint );
630
657
631
658
if (this ->dataPtr ->joints_ [i].joint_control_method & VELOCITY) {
632
659
if (!this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointVelocityCmd>(
@@ -644,27 +671,37 @@ hardware_interface::return_type IgnitionSystem::write(
644
671
}
645
672
} else if (this ->dataPtr ->joints_ [i].joint_control_method & POSITION) {
646
673
647
- // calculate error with clamped position command
648
- double error = this ->dataPtr ->joints_ [i].joint_position - std::clamp (this ->dataPtr ->joints_ [i].joint_position_cmd , jointAxis->Data ().Lower (), jointAxis->Data ().Upper ());
649
-
650
- // error can be maximal 10 percent of the range
651
- error = copysign (1.0 , error) * std::clamp (std::abs (error), 0.0 , std::abs (jointAxis->Data ().Upper () - jointAxis->Data ().Lower ())*0.1 );
652
-
653
- // calculate target force/torque
654
- double target_force = this ->dataPtr ->joints_ [i].pid .Update (error, std::chrono::duration<double >(period.to_chrono <std::chrono::nanoseconds>()));
655
-
656
- auto forceCmd =
657
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(this ->dataPtr ->joints_ [i].sim_joint );
674
+ // calculate error with clamped position command
675
+ double error = this ->dataPtr ->joints_ [i].joint_position - std::clamp (
676
+ this ->dataPtr ->joints_ [i].joint_position_cmd , jointAxis->Data ().Lower (),
677
+ jointAxis->Data ().Upper ());
678
+
679
+ // error can be maximal 10 percent of the range
680
+ error =
681
+ copysign (
682
+ 1.0 ,
683
+ error) *
684
+ std::clamp (
685
+ std::abs (error), 0.0 ,
686
+ std::abs (jointAxis->Data ().Upper () - jointAxis->Data ().Lower ()) * 0.1 );
687
+
688
+ // calculate target force/torque
689
+ double target_force = this ->dataPtr ->joints_ [i].pid .Update (
690
+ error, std::chrono::duration<double >(
691
+ period.to_chrono <std::chrono::nanoseconds>()));
692
+
693
+ auto forceCmd =
694
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
695
+ this ->dataPtr ->joints_ [i].sim_joint );
658
696
659
- if (forceCmd == nullptr )
660
- {
661
- this ->dataPtr ->ecm ->CreateComponent (
662
- this ->dataPtr ->joints_ [i].sim_joint ,
663
- ignition::gazebo::components::JointForceCmd ({0 }));
664
- } else {
665
- *forceCmd = ignition::gazebo::components::JointForceCmd (
666
- {target_force});
667
- }
697
+ if (forceCmd == nullptr ) {
698
+ this ->dataPtr ->ecm ->CreateComponent (
699
+ this ->dataPtr ->joints_ [i].sim_joint ,
700
+ ignition::gazebo::components::JointForceCmd ({0 }));
701
+ } else {
702
+ *forceCmd = ignition::gazebo::components::JointForceCmd (
703
+ {target_force});
704
+ }
668
705
} else if (this ->dataPtr ->joints_ [i].joint_control_method & EFFORT) {
669
706
if (!this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
670
707
this ->dataPtr ->joints_ [i].sim_joint ))
@@ -701,39 +738,48 @@ hardware_interface::return_type IgnitionSystem::write(
701
738
// set values of all mimic joints with respect to mimicked joint
702
739
for (const auto & mimic_joint : this ->dataPtr ->mimic_joints_ ) {
703
740
for (const auto & mimic_interface : mimic_joint.interfaces_to_mimic ) {
704
- // assuming every mimic joint has axis
705
- const auto * jointAxis =
706
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
707
- this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
741
+ // assuming every mimic joint has axis
742
+ const auto * jointAxis =
743
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointAxis>(
744
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
708
745
709
746
if (mimic_interface == " position" ) {
710
747
// Get the joint position
711
- double position_mimicked_joint = this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].joint_position ;
748
+ double position_mimicked_joint =
749
+ this ->dataPtr ->joints_ [mimic_joint.mimicked_joint_index ].joint_position ;
712
750
713
- double position_mimic_joint = this ->dataPtr ->joints_ [mimic_joint.joint_index ].joint_position ;
751
+ double position_mimic_joint =
752
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].joint_position ;
714
753
715
- // calculate error with clamped position command
716
- double position_error =
717
- position_mimic_joint - std::clamp (position_mimicked_joint * mimic_joint.multiplier , jointAxis->Data ().Lower (), jointAxis->Data ().Upper ());
754
+ // calculate error with clamped position command
755
+ double position_error =
756
+ position_mimic_joint - std::clamp (
757
+ position_mimicked_joint * mimic_joint.multiplier ,
758
+ jointAxis->Data ().Lower (), jointAxis->Data ().Upper ());
718
759
719
- // error can be maximal 10 percent of the range
720
- position_error = copysign (1.0 , position_error) * std::clamp (std::abs (position_error), 0.0 , std::abs (jointAxis->Data ().Upper () - jointAxis->Data ().Lower ())*0.1 );
760
+ // error can be maximal 10 percent of the range
761
+ position_error = copysign (1.0 , position_error) * std::clamp (
762
+ std::abs (
763
+ position_error), 0.0 , std::abs (
764
+ jointAxis->Data ().Upper () - jointAxis->Data ().Lower ()) * 0.1 );
721
765
722
- // calculate target force/torque
723
- double target_force = this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid .Update (position_error, std::chrono::duration<double >(period.to_chrono <std::chrono::nanoseconds>()));
766
+ // calculate target force/torque
767
+ double target_force = this ->dataPtr ->joints_ [mimic_joint.joint_index ].pid .Update (
768
+ position_error,
769
+ std::chrono::duration<double >(period.to_chrono <std::chrono::nanoseconds>()));
724
770
725
- auto forceCmd =
726
- this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
771
+ auto forceCmd =
772
+ this ->dataPtr ->ecm ->Component <ignition::gazebo::components::JointForceCmd>(
773
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint );
727
774
728
- if (forceCmd == nullptr )
729
- {
730
- this ->dataPtr ->ecm ->CreateComponent (
731
- this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint ,
732
- ignition::gazebo::components::JointForceCmd ({0 }));
733
- } else {
734
- *forceCmd = ignition::gazebo::components::JointForceCmd (
735
- {target_force});
736
- }
775
+ if (forceCmd == nullptr ) {
776
+ this ->dataPtr ->ecm ->CreateComponent (
777
+ this ->dataPtr ->joints_ [mimic_joint.joint_index ].sim_joint ,
778
+ ignition::gazebo::components::JointForceCmd ({0 }));
779
+ } else {
780
+ *forceCmd = ignition::gazebo::components::JointForceCmd (
781
+ {target_force});
782
+ }
737
783
}/*
738
784
if (mimic_interface == "velocity") {
739
785
// get the velocity of mimicked joint
0 commit comments