Skip to content

Commit a289d94

Browse files
hisashiishidaHisashi Isihda
andauthored
servo_cv command (#2)
* Add servo_cv command and example. * fixed small typo --------- Co-authored-by: Hisashi Isihda <[email protected]>
1 parent 2770f8e commit a289d94

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

scripts/crtk_servo_cv_example.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
3+
# Author: Hisashi Ishida, Anton Deguet
4+
# Created on: 2023-04-01
5+
#
6+
# Copyright (c) 2015-2023 Johns Hopkins University, University of Washington, Worcester Polytechnic Institute
7+
# Released under MIT License
8+
9+
# Start a single arm using
10+
# > rosrun dvrk_robot dvrk_console_json -j <console-file>
11+
12+
# To communicate with the arm using ROS topics, see the python based example dvrk_arm_test.py:
13+
# > rosrun crtk_python_client crtk_arm_test.py <arm-name>
14+
15+
import crtk
16+
import math
17+
import sys
18+
import rospy
19+
import numpy as np
20+
import PyKDL
21+
22+
23+
# example of application using device.py
24+
class crtk_servo_cv_example:
25+
26+
# configuration
27+
def configure(self, device_namespace):
28+
# ROS initialization
29+
if not rospy.get_node_uri():
30+
rospy.init_node('crtk_servo_cv_example', anonymous = True, log_level = rospy.WARN)
31+
32+
print(rospy.get_caller_id() + ' -> configuring crtk_device_test for: ' + device_namespace)
33+
# populate this class with all the ROS topics we need
34+
self.crtk_utils = crtk.utils(self, device_namespace)
35+
self.crtk_utils.add_operating_state()
36+
self.crtk_utils.add_setpoint_cp()
37+
self.crtk_utils.add_servo_cv()
38+
# for all examples
39+
self.duration = 10 # 10 seconds
40+
self.rate = 200 # aiming for 200 Hz
41+
self.samples = self.duration * self.rate
42+
43+
def run_servo_cv(self):
44+
if not self.enable(60):
45+
print("Unable to enable the device, make sure it is connected.")
46+
return
47+
48+
# create a new goal swith constant speed
49+
for i in range(self.samples):
50+
vel = np.array([0.05, 0.0, 0.0, 0.0, 0.0, 0.0]) # move 5 cm/sec along x direction
51+
self.servo_cv(vel)
52+
rospy.sleep(1.0 / self.rate)
53+
54+
# send the zero velcity to stop the robot
55+
vel = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
56+
self.servo_cv(vel)
57+
58+
# use the class now, i.e. main program
59+
if __name__ == '__main__':
60+
try:
61+
if (len(sys.argv) != 2):
62+
print(sys.argv[0], ' requires one argument, i.e. crtk device namespace')
63+
else:
64+
example = crtk_servo_cv_example()
65+
example.configure(sys.argv[1])
66+
example.run_servo_cv()
67+
68+
except rospy.ROSInterruptException:
69+
pass

src/crtk/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,32 @@ def add_servo_cf(self):
739739
self.__class_instance.servo_cf = self.__servo_cf
740740

741741

742+
# internal methods for servo_cv
743+
def __servo_cv(self, setpoint):
744+
# convert to ROS msg and publish
745+
msg = geometry_msgs.msg.TwistStamped()
746+
msg.Twist.linear.x = setpoint[0]
747+
msg.Twist.linear.y = setpoint[1]
748+
msg.Twist.linear.z = setpoint[2]
749+
msg.Twist.angular.x = setpoint[3]
750+
msg.Twist.angular.y = setpoint[4]
751+
msg.Twist.angular.z = setpoint[5]
752+
self.__servo_cv_publisher.publish(msg)
753+
754+
def add_servo_cv(self):
755+
# throw a warning if this has alread been added to the class,
756+
# using the callback name to test
757+
if hasattr(self.__class_instance, 'servo_cv'):
758+
raise RuntimeWarning('servo_cv already exists')
759+
# create the subscriber and keep in list
760+
self.__servo_cv_publisher = rospy.Publisher(self.__ros_namespace + '/servo_cv',
761+
geometry_msgs.msg.TwistStamped,
762+
latch = True, queue_size = 1)
763+
self.__publishers.append(self.__servo_cv_publisher)
764+
# add attributes to class instance
765+
self.__class_instance.servo_cv = self.__servo_cv
766+
767+
742768
# internal methods for move_jp
743769
def __move_jp(self, setpoint):
744770
# convert to ROS msg and publish

0 commit comments

Comments
 (0)