|
| 1 | +(require :robot-interface "package://pr2eus/robot-interface.l") |
| 2 | + |
| 3 | +(ros::load-ros-manifest "control_msgs") |
| 4 | +(ros::load-ros-manifest "kxr_controller") |
| 5 | + |
| 6 | + |
| 7 | +(defun load-robot-model (&key (port 8000) |
| 8 | + (namespace nil)) |
| 9 | + (let* ((port "8000") |
| 10 | + (hash (if namespace (format nil "~A~A" namespace (ros::get-param "/eusmodel_hash")) |
| 11 | + (ros::get-param "/eusmodel_hash"))) |
| 12 | + (robot-name (if namespace (format nil "~A~A" namespace (ros::get-param "/eus_robot_name")) |
| 13 | + (ros::get-param "/eus_robot_name"))) |
| 14 | + (outpath (ros::resolve-ros-path (format nil "package://kxreus/models/cache/~A.l" hash))) |
| 15 | + (server-url (format nil "http://~A:~A/~A.l" |
| 16 | + (ros::get-host) |
| 17 | + port |
| 18 | + hash))) |
| 19 | + (when (not (probe-file outpath)) |
| 20 | + (while (not (= (unix:system (format nil "wget -O ~A ~A" |
| 21 | + outpath |
| 22 | + server-url |
| 23 | + )) |
| 24 | + 0)) |
| 25 | + (ros::ros-info (format nil "Waiting model file from server url ~A" server-url)) |
| 26 | + (unix::usleep (* 1000 1000)))) |
| 27 | + (load outpath) |
| 28 | + (funcall (read-from-string robot-name)))) |
| 29 | + |
| 30 | + |
| 31 | +(defclass kxr-interface |
| 32 | + :super robot-interface |
| 33 | + :slots (joint-names servo-on-off-client)) |
| 34 | + |
| 35 | + |
| 36 | +(defmethod kxr-interface |
| 37 | + (:init (robot &rest args &key (type :default-controller) &allow-other-keys) |
| 38 | + (let* ((namespace (cadr (memq :namespace args))) |
| 39 | + (joint-param (if namespace (format nil "~A/~A" namespace "/kxr_fullbody_controller/joints") |
| 40 | + "/kxr_fullbody_controller/joints"))) |
| 41 | + (setq joint-names (ros::get-param joint-param nil)) |
| 42 | + (while (and (ros::ok) (null joint-names)) |
| 43 | + (ros::ros-info (format nil "Waiting ~A rosparam set" joint-param)) |
| 44 | + (setq joint-names (ros::get-param joint-param nil)) |
| 45 | + (ros::sleep))) |
| 46 | + |
| 47 | + (send-super* :init :robot robot :type type |
| 48 | + :groupname "kxr_interface" |
| 49 | + args) |
| 50 | + |
| 51 | + (setq servo-on-off-client (instance ros::simple-action-client :init |
| 52 | + (if namespace (format nil "~A/~A" namespace "/kxr_fullbody_controller/servo_on_off") "/kxr_fullbody_controller/servo_on_off") |
| 53 | + kxr_controller::ServoOnOffAction |
| 54 | + :groupname groupname)) |
| 55 | + (dolist (action (list servo-on-off-client)) |
| 56 | + (unless (and joint-action-enable (send action :wait-for-server 3)) |
| 57 | + (setq joint-action-enable nil) |
| 58 | + (ros::ros-warn "~A is not respond, kxr-interface is disabled" action) |
| 59 | + (return))) |
| 60 | + t) |
| 61 | + (:default-controller |
| 62 | + () |
| 63 | + (send self :fullbody-controller)) |
| 64 | + (:fullbody-controller |
| 65 | + () |
| 66 | + (list |
| 67 | + (list |
| 68 | + (cons :controller-action "kxr_fullbody_controller/follow_joint_trajectory") |
| 69 | + (cons :controller-state "kxr_fullbody_controller/state") |
| 70 | + (cons :action-type control_msgs::FollowJointTrajectoryAction) |
| 71 | + (cons :joint-names joint-names)))) |
| 72 | + (:servo-on |
| 73 | + (&key (names nil)) |
| 74 | + (unless joint-action-enable |
| 75 | + (if viewer (send self :draw-objects)) |
| 76 | + (return-from :servo-on t)) |
| 77 | + (when (null names) |
| 78 | + (setq names joint-names)) |
| 79 | + (let* (goal result) |
| 80 | + ;; send current angle-vector |
| 81 | + (send self :angle-vector (send self :state :potentio-vector) 0.1) |
| 82 | + (send self :wait-interpolation) |
| 83 | + |
| 84 | + (setq goal (instance kxr_controller::ServoOnOffGoal :init)) |
| 85 | + (send goal :joint_names names) |
| 86 | + (send goal :servo_on_states (make-array (length names) :initial-element t)) |
| 87 | + (send servo-on-off-client :send-goal goal))) |
| 88 | + (:servo-off |
| 89 | + (&key (names nil)) |
| 90 | + (unless joint-action-enable |
| 91 | + (if viewer (send self :draw-objects)) |
| 92 | + (return-from :servo-off t)) |
| 93 | + (when (null names) |
| 94 | + (setq names joint-names)) |
| 95 | + (let* (goal result) |
| 96 | + (setq goal (instance kxr_controller::ServoOnOffGoal :init)) |
| 97 | + (send goal :joint_names names) |
| 98 | + (send goal :servo_on_states (make-array (length names) :initial-element nil)) |
| 99 | + (send servo-on-off-client :send-goal goal)))) |
| 100 | + |
| 101 | + |
| 102 | +(defun kxr-init (&key |
| 103 | + (namespace nil) |
| 104 | + (create-viewer t)) |
| 105 | + (unless (boundp '*robot*) |
| 106 | + (setq *robot* (load-robot-model :namespace namespace |
| 107 | + :port (ros::get-param (if namespace (format nil "~A/port" namespace) "/port") 8000)))) |
| 108 | + (unless (ros::ok) (ros::roseus "kxr_eus_interface")) |
| 109 | + (unless (boundp '*ri*) |
| 110 | + (setq *ri* (instance kxr-interface :init *robot* :namespace namespace))) |
| 111 | + |
| 112 | + (ros::spin-once) |
| 113 | + (send *ri* :spin-once) |
| 114 | + (send *robot* :angle-vector (send *ri* :angle-vector)) |
| 115 | + (when create-viewer (objects (list *robot*)))) |
0 commit comments