-
Notifications
You must be signed in to change notification settings - Fork 320
/
kinova_comm.cpp
1699 lines (1451 loc) · 60.2 KB
/
kinova_comm.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* _____
* / _ \
* / _/ \ \
* / / \_/ \
* / \_/ _ \ ___ _ ___ ___ ____ ____ ___ _____ _ _
* \ / \_/ \ / / _\| | | __| / _ \ | ++ \ | ++ \ / _ \ |_ _|| | | |
* \ \_/ \_/ / | | | | | ++ | |_| || ++ / | ++_/| |_| | | | | +-+ |
* \ \_/ / | |_ | |_ | ++ | _ || |\ \ | | | _ | | | | +-+ |
* \_____/ \___/|___||___||_| |_||_| \_\|_| |_| |_| |_| |_| |_|
* ROBOTICS™
*
* File: kinova_comm.cpp
* Desc: Class for moving/querying kinova arm.
* Auth: Alex Bencz, Jeff Schmidt
*
* Copyright (c) 2013, Clearpath Robotics, Inc.
* All Rights Reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Clearpath Robotics, Inc. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CLEARPATH ROBOTICS, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Please send comments, questions, or patches to [email protected]
*
*/
#include <ros/ros.h>
#include "kinova_driver/kinova_comm.h"
#include <string>
#include <vector>
#include <tf/tf.h>
#include <arpa/inet.h>
namespace kinova
{
KinovaComm::KinovaComm(const ros::NodeHandle& node_handle,
boost::recursive_mutex &api_mutex,
const bool is_movement_on_start,
const std::string &kinova_robotType)
: is_software_stop_(false), api_mutex_(api_mutex)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
int result = NO_ERROR_KINOVA;
//initialize kinova api functions
std::string api_type;
node_handle.param<std::string>("connection_type", api_type, "USB");
if (api_type == "USB")
kinova_api_.initializeKinovaAPIFunctions(USB);
else
kinova_api_.initializeKinovaAPIFunctions(ETHERNET);
//Set ethernet parameters
EthernetCommConfig ethernet_settings;
std::string local_IP,subnet_mask;
int local_cmd_port,local_bcast_port;
node_handle.getParam("ethernet/local_machine_IP", local_IP);
node_handle.getParam("ethernet/subnet_mask", subnet_mask);
node_handle.getParam("ethernet/local_cmd_port", local_cmd_port);
node_handle.getParam("ethernet/local_broadcast_port", local_bcast_port);
ethernet_settings.localCmdport = local_cmd_port;
ethernet_settings.localBcastPort = local_bcast_port;
ethernet_settings.localIpAddress = inet_addr(local_IP.c_str());
ethernet_settings.subnetMask = inet_addr(subnet_mask.c_str());
ethernet_settings.rxTimeOutInMs = 1000;
ethernet_settings.robotIpAddress = inet_addr("192.168.100.11");
ethernet_settings.robotPort = 55000;
// Get the serial number parameter for the arm we wish to connect to
std::string serial_number = "";
node_handle.getParam("serial_number", serial_number);
int api_version[API_VERSION_COUNT];
result = kinova_api_.getAPIVersion(api_version);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the Kinova API version", result);
}
ROS_INFO_STREAM("Initializing Kinova "<< api_type.c_str()
<< " API (header version: " << COMMAND_LAYER_VERSION
<< ", library version: " << api_version[0] << "."
<< api_version[1] << "." << api_version[2] << ")");
if (api_type == "USB"){
result = kinova_api_.initAPI();
}
else{
result =kinova_api_.initEthernetAPI(ethernet_settings);
}
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not initialize Kinova API", result);
}
result = kinova_api_.refresDevicesList();
result = NO_ERROR_KINOVA;
int devices_count = kinova_api_.getDevices(devices_list_, result);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get devices list", result);
}
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get devices list count.", result);
}
bool found_arm = false;
for (int device_i = 0; device_i < devices_count; device_i++)
{
// If no device is specified, just use the first available device
if (serial_number == "" || serial_number == "not_set" ||
std::strcmp(serial_number.c_str(), devices_list_[device_i].SerialNumber) == 0)
{
result = kinova_api_.setActiveDevice(devices_list_[device_i]);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set the active device", result);
}
GeneralInformations general_info;
result = kinova_api_.getGeneralInformations(general_info);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get general information about the device", result);
}
ClientConfigurations configuration;
getConfig(configuration);
QuickStatus quick_status;
getQuickStatus(quick_status);
robot_type_ = quick_status.RobotType;
ROS_INFO_STREAM("Found " << devices_count << " device(s), using device at index " << device_i
<< " (model: " << configuration.Model
<< ", serial number: " << devices_list_[device_i].SerialNumber
<< ", code version: " << general_info.CodeVersion
<< ", code revision: " << general_info.CodeRevision << ")");
found_arm = true;
break;
}
}
if (!found_arm)
{
ROS_ERROR("Could not find the specified arm (serial: %s) among the %d attached devices",
serial_number.c_str(), devices_count);
throw KinovaCommException("Could not find the specified arm", 0);
}
//find the number of joints and fingers of the arm using robotType passed from arm node
num_joints_ = kinova_robotType[3]-'0';
num_fingers_ = kinova_robotType[5]-'0';
// On a cold boot the arm may not respond to commands from the API right away.
// This kick-starts the Control API so that it's ready to go.
startAPI();
stopAPI();
startAPI();
//Set robot to use manual COM parameters
bool use_estimated_COM;
node_handle.param("torque_parameters/use_estimated_COM_parameters",
use_estimated_COM,true);
if (use_estimated_COM == true)
kinova_api_.setGravityType(OPTIMAL);
else
kinova_api_.setGravityType(MANUAL_INPUT);
//Set torque safety factor to 1
kinova_api_.setTorqueSafetyFactor(1);
// Set the angular velocity of each of the joints to zero
TrajectoryPoint kinova_velocity;
memset(&kinova_velocity, 0, sizeof(kinova_velocity));
setCartesianVelocities(kinova_velocity.Position.CartesianPosition);
if (is_movement_on_start)
{
initFingers();
}
else
{
ROS_WARN("Movement on connection to the arm has been suppressed on initialization. You may "
"have to home the arm (through the home service) before movement is possible");
}
}
KinovaComm::~KinovaComm()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
kinova_api_.closeAPI();
}
/**
* @brief This function tells the robotical arm that from now on, the API will control the robotical arm. It must been call before sending trajectories or joystick command.
*/
void KinovaComm::startAPI()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
if (is_software_stop_)
{
is_software_stop_ = false;
kinova_api_.stopControlAPI();
ros::Duration(0.05).sleep();
}
int result = kinova_api_.startControlAPI();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not start the control API", result);
}
}
/**
* @brief This function tells the robotical arm the from now on the API is not controlling the robotical arm.
*/
void KinovaComm::stopAPI()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
is_software_stop_ = true;
int result = kinova_api_.stopControlAPI();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not stop the control API", result);
}
result = kinova_api_.eraseAllTrajectories();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not erase all trajectories", result);
}
}
/**
* @brief check if API lost the control of robot.
* @return true if stopAPI() was called.
*/
bool KinovaComm::isStopped()
{
return is_software_stop_;
}
/**
* @brief This function activates the reactive force control for admittance control. Admittance control may be applied to joint or Cartesian depending to the control mode.
* @warning You can only use this function if your robotic device has torque sensors on it. Also, the robotic device must be in a standard vertical position.
*/
void KinovaComm::startForceControl()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
int result = kinova_api_.startForceControl();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not start force control.", result);
}
}
/**
* @brief This function stops the admittance control. Admittance control may be applied to joint or Cartesian depending to the control mode.
*/
void KinovaComm::stopForceControl()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
int result = kinova_api_.stopForceControl();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not stop force control.", result);
}
}
/**
* @brief get robotType
* Index for robot type:
* JACOV1_ASSISTIVE = 0,
* MICO_6DOF_SERVICE = 1,
* MICO_4DOF_SERVICE = 2,
* JACOV2_6DOF_SERVICE = 3,
* JACOV2_4DOF_SERVICE = 4,
* MICO_6DOF_ASSISTIVE = 5,
* JACOV2_6DOF_ASSISTIVE = 6,
* @return index of robot type
*/
int KinovaComm::robotType() const
{
return robot_type_;
}
/**
* @brief This function gets information regarding some status flag of the robotical arm.
* @param quick_status This structure holds various informations but mostly it is flag status, such as robotype, retractType, forceControlStatus,
*/
void KinovaComm::getQuickStatus(QuickStatus &quick_status)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&quick_status, 0, sizeof(quick_status)); // zero structure
int result = kinova_api_.getQuickStatus(quick_status);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get quick status", result);
}
}
/**
* @brief This function set the client configurations of the robotical arm. The configuration data is stored on the arm itself.
* @param config config This structure holds informations relative to the client, including serial number, robot model, limits for position, velocity, acceleration and force, etc.
*/
void KinovaComm::setConfig(const ClientConfigurations &config)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
int result = kinova_api_.setClientConfigurations(config);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set the client configuration", result);
}
}
/**
* @brief obtain the current client configuration.
* @param config This structure holds informations relative to the client, including serial number, robot model, limits for position, velocity, acceleration and force, etc.
*/
void KinovaComm::getConfig(ClientConfigurations &config)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&config, 0, sizeof(config)); // zero structure
int result = kinova_api_.getClientConfigurations(config);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get client configuration", result);
}
}
/**
* @brief Dumps the client configuration onto the screen.
* @param config This structure holds informations relative to the client, including serial number, robot model, limits for position, velocity, acceleration and force, etc.
*/
void KinovaComm::printConfig(const ClientConfigurations &config)
{
ROS_INFO_STREAM("Arm configuration:\n"
"\tClientID: " << config.ClientID <<
"\n\tClientName: " << config.ClientName <<
"\n\tOrganization: " << config.Organization <<
"\n\tSerial:" << config.Serial <<
"\n\tModel: " << config.Model <<
"\n\tMaxForce: " << config.MaxForce <<
"\n\tSensibility: " << config.Sensibility <<
"\n\tDrinkingHeight: " << config.DrinkingHeight <<
"\n\tComplexRetractActive: " << config.ComplexRetractActive <<
"\n\tRetractedPositionAngle: " << config.RetractedPositionAngle <<
"\n\tRetractedPositionCount: " << config.RetractedPositionCount <<
"\n\tDrinkingDistance: " << config.DrinkingDistance <<
"\n\tFingers2and3Inverted: " << config.Fingers2and3Inverted <<
"\n\tDrinkingLength: " << config.DrinkingLenght <<
"\n\tDeletePreProgrammedPositionsAtRetract: " <<
config.DeletePreProgrammedPositionsAtRetract <<
"\n\tEnableFlashErrorLog: " << config.EnableFlashErrorLog <<
"\n\tEnableFlashPositionLog: " << config.EnableFlashPositionLog);
}
/**
* @brief get current control type
* The control in ROS is independent to Joystick control type. For example: even set robot in joint control though api in ROS, robot may still controlled in joint level by joystick.
* @param controlType Cartesian control[0] or joint control[1]
*/
void KinovaComm::getControlType(int &controlType)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&controlType, 0, sizeof(controlType)); //zero structure
int result = kinova_api_.getControlType(controlType);
if (result!=NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get control type", result);
}
}
/**
* @brief get almost all information of the robotical arm.
* @param general_info includes: power statistics, sensor infos, robot position and command, etc.
*/
void KinovaComm::getGeneralInformations(GeneralInformations &general_info)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&general_info, 0, sizeof(general_info));
int result = kinova_api_.getGeneralInformations(general_info);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get general information about the device", result);
}
}
/**
* @brief This function returns information about the robotical arm's sensors. (Voltage, Total current, Temperature, acceleration)
* @param The structure containing the sensor's informations
*/
void KinovaComm::getSensorsInfo(SensorsInfo &sensor_Info)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&sensor_Info, 0, sizeof(sensor_Info));
int result = kinova_api_.getSensorsInfo(sensor_Info);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get sensors information", result);
}
}
/**
* @brief This function gets information regarding all forces.
* @param force_Info A struct containing the information about the forces. Joint torque and end-effector wrench in Nm and N.
*/
void KinovaComm::getForcesInfo(ForcesInfo &force_Info)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&force_Info, 0, sizeof(force_Info));
int result = kinova_api_.getForcesInfo(force_Info);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get force information", result);
}
}
/**
* @brief This function gets informations about the robotical arm's gripper. Some information may be missing, it is still in development.
* @param gripper_status A struct containing the information of the gripper. Most information of each fingers, including model, motion, force, limits, etc.
*/
void KinovaComm::getGripperStatus(Gripper &gripper_status)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&gripper_status, 0, sizeof(gripper_status));
int result = kinova_api_.getGripperStatus(gripper_status);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the gripper status", result);
}
}
/**
* @brief This function sets the robotical arm in angular control mode.
* If robot is not in motion, change control model to Angular control
*/
void KinovaComm::setAngularControl()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
TrajectoryFIFO trajectory_fifo;
memset(&trajectory_fifo, 0, sizeof(trajectory_fifo));
getGlobalTrajectoryInfo(trajectory_fifo);
if(trajectory_fifo.TrajectoryCount > 0)
{
ROS_WARN("Current tranjectory count is %d, Please wait the trajectory to finish to swich to Angular control.", trajectory_fifo.TrajectoryCount);
return;
}
int result = kinova_api_.setAngularControl();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set angular control", result);
}
}
/**
* @brief This function get the angular command of all actuators.
* @param An AngularPosition struct containing the values. Units are degrees. angular_command {AngularInfo, FingersPosition}
* @return
* - @ref NO_ERROR_KINOVA if operation is a success
* - @ref ERROR_API_NOT_INITIALIZED if the API has not been initialized. To initialize it, call the InitAPI() function.
*/
void KinovaComm::getAngularCommand(AngularPosition &angular_command)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&angular_command, 0, sizeof(angular_command));
int result = kinova_api_.getAngularCommand(angular_command);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the angular command", result);
}
}
/**
* @brief This function returns the angular position of the robotical arm's end effector.
* @param angles A structure that contains the position of each actuator. Unit in degrees.
*/
void KinovaComm::getJointAngles(KinovaAngles &angles)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
AngularPosition kinova_angles;
memset(&kinova_angles, 0, sizeof(kinova_angles)); // zero structure
int result = kinova_api_.getAngularPosition(kinova_angles);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the angular position", result);
}
angles = KinovaAngles(kinova_angles.Actuators);
}
/**
* @brief Sends a joint angle command to the Kinova arm.
* This function sends trajectory point(Angular) that will be added in the robotical arm's FIFO. Waits until the arm has stopped moving before releasing control of the API. sendAdvanceTrajectory() is called in api to complete the motion.
* @param angles target joint angle to set, type float, unit in degree
* @param timeout default value 0.0, not used.
* @param push default true, errase all trajectory before request motion..
*/
void KinovaComm::setJointAngles(const KinovaAngles &angles, double speedJoint123, double speedJoint4567, int timeout, bool push)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
if (isStopped())
{
ROS_WARN_STREAM("In class [" << typeid(*this).name() << "], function ["<< __FUNCTION__ << "]: The angles could not be set because the arm is stopped" << std::endl);
return;
}
int result = NO_ERROR_KINOVA;
TrajectoryPoint kinova_joint;
kinova_joint.InitStruct();
memset(&kinova_joint, 0, sizeof(kinova_joint)); // zero structure
if (push)
{
result = kinova_api_.eraseAllTrajectories();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not erase trajectories", result);
}
}
//startAPI();
result = kinova_api_.setAngularControl();
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set angular control", result);
}
kinova_joint.Position.Delay = 0.0;
kinova_joint.Position.Type = ANGULAR_POSITION;
kinova_joint.Position.Actuators = angles;
kinova_joint.Limitations.speedParameter1 = speedJoint123;
kinova_joint.Limitations.speedParameter2 = speedJoint4567;
kinova_joint.LimitationsActive = 1;
result = kinova_api_.sendAdvanceTrajectory(kinova_joint);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not send advanced joint angle trajectory", result);
}
}
/**
* @brief This function get the velocity of each actuator.
* @param vels A kinovaAngles structure contains joint velocity of each actuator. Unit in degrees/second.
*/
void KinovaComm::getJointVelocities(KinovaAngles &vels)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
AngularPosition kinova_vels;
memset(&kinova_vels, 0, sizeof(kinova_vels)); // zero structure
int result = kinova_api_.getAngularVelocity(kinova_vels);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the angular velocity", result);
}
vels = KinovaAngles(kinova_vels.Actuators);
//velocities reported back by firmware seem to be half of actual value
vels.Actuator1 = vels.Actuator1*2;
vels.Actuator2 = vels.Actuator2*2;
vels.Actuator3 = vels.Actuator3*2;
vels.Actuator4 = vels.Actuator4*2;
vels.Actuator5 = vels.Actuator5*2;
vels.Actuator6 = vels.Actuator6*2;
vels.Actuator7 = vels.Actuator7*2;
}
/**
* @brief This function controls robot with joint velocity.
* This function sends trajectory point(ANGULAR_VELOCITY) that will be added in the robotical arm's FIFO. Waits until the arm has stopped moving before releasing control of the API. sendAdvanceTrajectory() is called in api to complete the motion.
* @param joint_vel joint velocity in degree/second
*/
void KinovaComm::setJointVelocities(const AngularInfo &joint_vel)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
if (isStopped())
{
ROS_INFO("The velocities could not be set because the arm is stopped");
return;
}
TrajectoryPoint kinova_velocity;
kinova_velocity.InitStruct();
memset(&kinova_velocity, 0, sizeof(kinova_velocity)); // zero structure
//startAPI();
kinova_velocity.Position.Type = ANGULAR_VELOCITY;
// confusingly, velocity is passed in the position struct
kinova_velocity.Position.Actuators = joint_vel;
int result = kinova_api_.sendAdvanceTrajectory(kinova_velocity);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not send advanced joint velocity trajectory", result);
}
}
void KinovaComm::setJointTorques(float joint_torque[])
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
if (isStopped())
{
ROS_INFO("The joint torques could not be set because the arm is stopped");
return;
}
//memset(&joint_torque, 0, sizeof(joint_torque)); // zero structure
//startAPI();
//ROS_INFO("Torque %f %f %f %f %f %f %f ", joint_torque[0],joint_torque[1],joint_torque[2],
// joint_torque[3],joint_torque[4],joint_torque[5],joint_torque[6]);
int result = kinova_api_.sendAngularTorqueCommand(joint_torque);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not send joint torques", result);
}
}
int KinovaComm::sendCartesianForceCommand(float force_cmd[COMMAND_SIZE])
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
if (isStopped())
{
ROS_INFO("The force cmd could not be set because the arm is stopped");
return 0;
}
//memset(&joint_torque, 0, sizeof(joint_torque)); // zero structure
//startAPI();
//ROS_INFO("Force %f %f %f %f %f %f", force_cmd[0],force_cmd[1],force_cmd[2],
// force_cmd[3],force_cmd[4],force_cmd[5]);
int result = kinova_api_.sendCartesianForceCommand(force_cmd);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not send force cmd", result);
}
return result;
}
/**
* @brief This function get the accelerometer values of each actuator. It does not directly refer to the angular acceleration.
* @param joint_acc An AngularAcceleration struct containing the values. Units are in G
*/
void KinovaComm::getJointAccelerations(AngularAcceleration &joint_acc)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&joint_acc, 0, sizeof(joint_acc));
int result = kinova_api_.getAngularAcceleration(joint_acc);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get joint acceleration.", result);
}
}
/**
* @brief This function returns the torque of each actuator.
* @param tqs A structure that contains the torque of each actuator. Unit is Newton * meter
*/
void KinovaComm::getJointTorques(KinovaAngles &tqs)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
AngularPosition kinova_tqs;
memset(&kinova_tqs, 0, sizeof(kinova_tqs)); // zero structure
int result = kinova_api_.getAngularForce(kinova_tqs);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the joint torques", result);
}
tqs = KinovaAngles(kinova_tqs.Actuators);
}
void KinovaComm::getGravityCompensatedTorques(KinovaAngles &tqs)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
AngularPosition kinova_tqs;
memset(&kinova_tqs, 0, sizeof(kinova_tqs)); // zero structure
int result = kinova_api_.getAngularForceGravityFree(kinova_tqs);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the joint torques", result);
}
tqs = KinovaAngles(kinova_tqs.Actuators);
}
/**
* @brief This function returns the current that each actuator consume on the main supply.
* @param anguler_current A structure that contains the current of each actuator and finger. Unit in A.
*/
void KinovaComm::getJointCurrent(AngularPosition &anguler_current)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&anguler_current, 0, sizeof(anguler_current));
int result = kinova_api_.getAngularCurrent(anguler_current);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the current in joints", result);
}
}
/**
*@brief Set zero torque for all joints
*/
void KinovaComm::setZeroTorque()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
int actuator_address[] = {16,17,18,19,20,21,25};
int result;
for (int i=0;i<num_joints_;i++)
{
result = kinova_api_.setTorqueZero(actuator_address[i]);
}
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set zero torques", result);
}
ROS_WARN("Torques for all joints set to zero");
}
/**
* @brief This function set the angular torque's maximum and minimum values.
* @param min A struct that contains all angular minimum values. (Unit: N * m)
* @param max 6 A struct that contains all angular max values. (Unit: N * m)
*/
void KinovaComm::setJointTorqueMinMax(AngularInfo &min, AngularInfo &max)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
ROS_INFO("Setting min torues - %f %f %f %f %f %f %f", min.Actuator1,
min.Actuator2,min.Actuator3,min.Actuator4,min.Actuator5,
min.Actuator6,min.Actuator7);
ROS_INFO("Setting max torues - %f %f %f %f %f %f %f", max.Actuator1,
max.Actuator2,max.Actuator3,max.Actuator4,max.Actuator5,
max.Actuator6,max.Actuator7);
int result = kinova_api_.setAngularTorqueMinMax(min, max);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set the limits for joint torques", result);
}
}
/**
* @brief setPayload
* @param payload Array - Mass, COMx, COMy, COMz
*/
void KinovaComm::setPayload(std::vector<float> payload)
{
float payload_[4];
std::copy(payload.begin(), payload.end(), payload_);
ROS_INFO("Payload set to - %f %f %f %f", payload_[0],payload_[1],
payload_[2],payload_[3]);
int result = kinova_api_.setGravityPayload(payload_);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set the gravity payload", result);
}
}
/**
* @brief Safety factor defines a velocity threshold at which torque control switches to position control
* @param factor between 0 and 1
*/
void KinovaComm::setToquesControlSafetyFactor(float factor)
{
ROS_INFO("Setting torque safety factor to %f", factor);
int result = kinova_api_.setTorqueSafetyFactor(factor);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set torque safety factor", result);
}
}
/** @brief Sets COM and COMxyz for all links
* @arg command[42] - {m1,m2..m7,x1,x2,..x7,y1,y2,...,y7,z1,z2,...z7}
//! */
void KinovaComm::setRobotCOMParam(GRAVITY_TYPE type,std::vector<float> params)
{
float com_parameters[GRAVITY_PARAM_SIZE];
memset(&com_parameters, 0, sizeof(com_parameters));
std::ostringstream com_params;
com_params<<"Setting COM parameters to ";
for (int i=0; i<params.size(); i++)
{
com_parameters[i] = params[i];
com_params<<params[i]<<", ";
}
ROS_INFO_STREAM(com_params.str());
int result;
if (type == MANUAL_INPUT)
result = kinova_api_.setGravityManualInputParam(com_parameters);
else
result = kinova_api_.setGravityOptimalZParam(com_parameters);
if (result != NO_ERROR_KINOVA && result!=2005)
{
throw KinovaCommException("Could not set the COM parameters", result);
}
}
/**
* @brief This function is used to run a sequence to estimate the optimal gravity parameters when the robot is
* standing (Z).
The arm must be in Trajectory-Position mode before to launch the procedure.
Before using this procedure, you should make sure that the torque sensors are well calibrated. This procedure is
explained in the user guide and in the Advanced Specification Guide.
When the program is launched, the robot will execute a trajectory. The user must remain alert and turn off the
robot if something wrong occurs (for example if the robot collides with an object). When the program ends, it will
output the parameters in the console and in a text file named “ParametersOptimal_Z.txt” in the program folder.
These parameters can then be sent as input to the function SetOptimalZParam().
*
* @param type The robot type
* @param OptimalzParam The result of the sequence
*/
int KinovaComm::runCOMParameterEstimation(ROBOT_TYPE type)
{
float COMparams[GRAVITY_PARAM_SIZE];
memset(&COMparams[0],0,sizeof(COMparams));
int result;
if(type == SPHERICAL_7DOF_SERVICE)
{
ROS_INFO("Running 7 dof robot COM estimation sequence");
result = kinova_api_.runGravityZEstimationSequence7DOF(type,COMparams);
}
else
{
double params[OPTIMAL_Z_PARAM_SIZE];
ROS_INFO("Running COM estimation sequence");
result = kinova_api_.runGravityZEstimationSequence(type,params);
for (int i=0;i<OPTIMAL_Z_PARAM_SIZE;i++)
COMparams[i] = (float)params[i];
}
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not launch COM parameter estimation sequence", result);
}
result = kinova_api_.setGravityOptimalZParam(COMparams);
if (result != NO_ERROR_KINOVA && result!=2005)
{
throw KinovaCommException("Could not set COM Parameters", result);
}
}
/**
* @brief Dumps the current joint angles onto the screen.
* @param angles A structure contains six joint angles. Unit in degrees
*/
void KinovaComm::printAngles(const KinovaAngles &angles)
{
ROS_INFO("Joint angles (deg) -- J1: %f, J2: %f J3: %f, J4: %f, J5: %f, J6: %f, J7: %f \n",
angles.Actuator1, angles.Actuator2, angles.Actuator3,
angles.Actuator4, angles.Actuator5, angles.Actuator6,
angles.Actuator7);
ROS_INFO("Joint angles (rad) -- J1: %f, J2: %f J3: %f, J4: %f, J5: %f, J6: %f, J7: %f \n",
angles.Actuator1/180.0*M_PI, angles.Actuator2/180.0*M_PI, angles.Actuator3/180.0*M_PI,
angles.Actuator4/180.0*M_PI, angles.Actuator5/180.0*M_PI, angles.Actuator6/180.0*M_PI,
angles.Actuator7/180.0*M_PI);
}
/**
* @brief This function sets the robotical arm in cartesian control mode if this is possible.
* If robot is not in motion, change control model to Cartesian control
*/
void KinovaComm::setCartesianControl()
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
TrajectoryFIFO trajectory_fifo;
memset(&trajectory_fifo, 0, sizeof(trajectory_fifo));
getGlobalTrajectoryInfo(trajectory_fifo);
if(trajectory_fifo.TrajectoryCount > 0)
{
ROS_WARN("Current tranjectory count is %d, Please wait the trajectory to finish to swich to Cartesian control.", trajectory_fifo.TrajectoryCount);
return;
}
int result = kinova_api_.setCartesianControl();
ROS_WARN("%d", result);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not set Cartesian control", result);
}
}
/**
* @brief This function get the cartesian command of the end effector. The Cartesian orientation is expressed in Euler-XYZ convention (Rot=Rx*Ry*Rz). However, in ROS by default using Euler-ZYX. tf::Matrix3x3 EulerYPR = Rz(tz)*Ry(ty)*Rx(tx)
* @param cartesian_command An CartesianPosition struct containing the values of end-effector and fingers.
*
* @htmlonly
*
* <table border="0" cellspacing="10">
* <tr>
* <th>Member</th>
* <th>Unit</th>
* </tr>
* <tr><td width="50">X</td><td>meter</td></tr>
* <tr><td>Y</td><td>meter</td></tsr>
* <tr><td>Z</td><td>meter</td></tr>
* <tr><td>Theta X</td><td>RAD</td></tr>
* <tr><td>Theta Y</td><td>RAD</td></tr>
* <tr><td>Theta Z</td><td>RAD</td></tr>
* <tr><td>Finger 1</td><td>No unit</td></tr>
* <tr><td>Finger 2</td><td>No unit</td></tr>
* <tr><td>Finger 3</td><td>No unit</td></tr>
* </table>
*
* @endhtmlonly
*/
void KinovaComm::getCartesianCommand(CartesianPosition &cartesian_command)
{
boost::recursive_mutex::scoped_lock lock(api_mutex_);
memset(&cartesian_command, 0, sizeof(cartesian_command));
int result = kinova_api_.getCartesianCommand(cartesian_command);
if (result != NO_ERROR_KINOVA)
{
throw KinovaCommException("Could not get the Cartesian command", result);
}
}