Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Related #1725] [jsk_robot_startup/lifelog] Tweet randomly from uptime #1804

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions jsk_robot_common/jsk_robot_startup/lifelog/tweet_client.l
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(load "package://pr2eus/speak.l")

(defun tweet-string (twit-str &key (warning-time) (with-image) (image-wait 30) (speak t)
(tweet-topic-name "/tweet"))
(tweet-topic-name "/tweet") (volume 1.0))
(if (null (ros::get-num-subscribers tweet-topic-name))
(ros::advertise tweet-topic-name std_msgs::String 1))
(let (prev-image-topic img)
Expand All @@ -28,15 +28,17 @@
(8 "はち")
(9 "きゅう")
(10 "じゅう")
(t "じゅういじょう")))))
(t "じゅういじょう")))
:volume volume))
(unix::sleep warning-time))

(cond
(with-image
(unix::system (format nil "rm -f /tmp/tweet_image.jpg"))
;; camera shot sound
(play-sound (pathname (ros::resolve-ros-path "package://jsk_pr2_startup/jsk_pr2_lifelog/camera.wav"))
:topic-name "robotsound_jp" :wait t)
(when speak
(play-sound (pathname (ros::resolve-ros-path "package://jsk_pr2_startup/jsk_pr2_lifelog/camera.wav"))
:topic-name "robotsound_jp" :wait t :volume volume))
;; specify camera
(when (stringp with-image)
(ros::wait-for-service "/tweet_image_mux/list")
Expand Down Expand Up @@ -64,5 +66,5 @@
(ros::service-call "/tweet_image_mux/select" (instance topic_tools::muxselectrequest :init :topic prev-image-topic)))
(t
(ros::publish tweet-topic-name (instance std_msgs::String :init :data twit-str))))
(when speak (speak-jp "ついーとしました" :wait t))))
(when speak (speak-jp "ついーとしました" :wait t :volume volume))))

37 changes: 33 additions & 4 deletions jsk_robot_common/jsk_robot_startup/lifelog/tweet_client_tablet.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@
(ros::roseus "twitter_client_tablet")
(ros::roseus-add-msgs "roseus")

(load "package://roseus/euslisp/dynamic-reconfigure-server.l")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code does not work without jsk-ros-pkg/jsk_roseus#729, so please add comment,
it is also batter to

(if (not (probe-file "...dynamic-reconfigure-server.l"))
  ...
)

lik https://github.com/jsk-ros-pkg/jsk_pr2eus/blob/8fc634d0d92830c201f9ae922c5e673c0def0b19/pr2eus/make-pr2-model-file.l#L1, but if it is too long, maybe manually write
(ros::advertise-service service-name dynamic_reconfigure::Reconfigure #'send self :set-callback)
woud be ok.

(load "package://jsk_robot_startup/lifelog/tweet_client.l")

(setq *volume* (ros::get-param "~volume" 1.0))
(setq *speak-enable* (ros::get-param "~speak_enable" t))
(setq *enable* (ros::get-param "~enable" t))
(setq *reconfigure-server*
(def-dynamic-reconfigure-server
;;; ((name type level description (default) (min) (max) (edit_method)) ... )
(("volume" double_t 0 "tweet speak volume" 1.0 0.0 1.0)
("speak_enable" bool_t 0 "tweet speak enable" t)
("enable" bool_t 0 "tweet enable" t))
;; use lamda-closure to avoid memory error
'(lambda-closure nil 0 0 (cfg level)
(let ((prev-volume *volume*)
(prev-speak-enable *speak-enable*)
(prev-enable *enable*))
(setq *volume* (cdr (assoc "volume" cfg :test #'equal)))
(setq *speak-enable* (cdr (assoc "speak_enable" cfg :test #'equal)))
(setq *enable* (cdr (assoc "enable" cfg :test #'equal)))
(if (null (equal *volume* prev-volume))
(ros::ros-warn "Volume changed to: ~A" *volume*))
(if (null (equal *enable* prev-enable))
(ros::ros-warn "Enable changed to: ~A" *enable*))
(if (null (equal *speak-enable* prev-speak-enable))
(ros::ros-warn "Speak enable changed to: ~A" *speak-enable*)))
cfg)))

(defun twit-cb (msg)
(let ((twit-str (send msg :data)))
(tweet-string twit-str
:warning-time nil
:with-image "/tablet/marked/image_rect_color")))
(if *enable*
(let ((twit-str (send msg :data)))
(tweet-string twit-str
:warning-time nil
:with-image "/tablet/marked/image_rect_color"
:speak *speak-enable*
:volume *volume*))))

(ros::advertise "/tweet" std_msgs::String 1)
(ros::subscribe "/pr2twit_from_tablet" roseus::StringStamped #'twit-cb)
Expand Down
127 changes: 82 additions & 45 deletions jsk_robot_common/jsk_robot_startup/lifelog/tweet_client_uptime.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(ros::roseus "twitter_client_uptime")

(load "package://roseus/euslisp/dynamic-reconfigure-server.l")
(load "package://jsk_robot_startup/lifelog/tweet_client.l")

(setq *src-lines* nil)
Expand All @@ -17,6 +18,13 @@
(unix::sleep 3)
(ros::ros-info "Wait for /active_user/elapsed_time parameter ..."))

(cond
((ros::has-param "/active_user/tweet_random_range")
(setq *tweet-random-range* (ros::get-param "/active_user/tweet_random_range")))
(t
(setq *tweet-random-range* 600.0)
))

(cond
((ros::has-param "/active_user/tweet_second")
(setq *tweet-second* (ros::get-param "/active_user/tweet_second")))
Expand All @@ -33,56 +41,85 @@
(let ((st (ros::get-param "/active_user/start_time")))
(setq *waking-target-second*
(+ (- (send (ros::time-now) :to-sec) st)
*waking-tweet-second*))))
(+ *waking-tweet-second* (random *tweet-random-range*))))))
(t
(setq *waking-target-second* *waking-tweet-second*)))
(setq *waking-target-second* (+ *waking-tweet-second* (random *tweet-random-range*)))))

(setq *volume* (ros::get-param "~volume" 1.0))
(setq *speak-enable* (ros::get-param "~speak_enable" t))
(setq *enable* (ros::get-param "~enable" t))
(setq *reconfigure-server*
(def-dynamic-reconfigure-server
;;; ((name type level description (default) (min) (max) (edit_method)) ... )
(("volume" double_t 0 "tweet speak volume" 1.0 0.0 1.0)
("speak_enable" bool_t 0 "tweet speak enable" t)
("enable" bool_t 0 "tweet enable" t))
;; use lamda-closure to avoid memory error
'(lambda-closure nil 0 0 (cfg level)
(let ((prev-volume *volume*)
(prev-speak-enable *speak-enable*)
(prev-enable *enable*))
(setq *volume* (cdr (assoc "volume" cfg :test #'equal)))
(setq *speak-enable* (cdr (assoc "speak_enable" cfg :test #'equal)))
(setq *enable* (cdr (assoc "enable" cfg :test #'equal)))
(if (null (equal *volume* prev-volume))
(ros::ros-warn "Volume changed to: ~A" *volume*))
(if (null (equal *enable* prev-enable))
(ros::ros-warn "Enable changed to: ~A" *enable*))
(if (null (equal *speak-enable* prev-speak-enable))
(ros::ros-warn "Speak enable changed to: ~A" *speak-enable*)))
cfg)))

(ros::advertise "/tweet" std_msgs::String 1)
(ros::rate 0.1)
(do-until-key
(setq *user-name* (ros::get-param "/active_user/launch_user_name")
*elapsed-time* (ros::get-param "/active_user/elapsed_time"))
;; tweet depend on up time
(let ((st (ros::get-param "/active_user/start_time")))
(when st
(let ((waking-time (- (send (ros::time-now) :to-sec) st)))
(ros::ros-debug "~A waking ~A sec (~A)" *robot-name* waking-time *waking-target-second*)
(when (> waking-time *waking-target-second*)
(incf *waking-target-second* *waking-tweet-second*)
;; tweet source of robot-interface
(unless *src-lines*
(let* ((dirname (ros::rospack-find "pr2eus"))
(fname (format nil "~A/robot-interface.l" dirname))
str)
(with-open-file (f fname)
(while (setq str (read-line f nil nil))
(push str *src-lines*)))
(setq *src-lines* (nreverse *src-lines*))
))
(if *enable*
(progn
(setq *user-name* (ros::get-param "/active_user/launch_user_name")
*elapsed-time* (ros::get-param "/active_user/elapsed_time"))
;; tweet depend on up time
(let ((st (ros::get-param "/active_user/start_time")))
(when st
(let ((waking-time (- (send (ros::time-now) :to-sec) st)))
(ros::ros-debug "~A waking ~A sec (~A)" *robot-name* waking-time *waking-target-second*)
(when (> waking-time *waking-target-second*)
(incf *waking-target-second* (+ *waking-tweet-second* (random *tweet-random-range*)))
;; tweet source of robot-interface
(unless *src-lines*
(let* ((dirname (ros::rospack-find "pr2eus"))
(fname (format nil "~A/robot-interface.l" dirname))
str)
(with-open-file (f fname)
(while (setq str (read-line f nil nil))
(push str *src-lines*)))
(setq *src-lines* (nreverse *src-lines*))
))

(let* ((len (length *src-lines*))
(start-n (floor (random (float len) *random-state*)))
(spos 0) (str-len 0) lines)
(push (format nil "I am running ~A min." (round (/ waking-time 60.0)))
lines)
(incf str-len (length (car lines)))
(while (< (+ start-n spos) len)
(let ((str (elt *src-lines* (+ start-n spos))))
(incf str-len (length str))
(if (> str-len 140) (return))
(push str lines))
(incf spos))
(let* ((ln (apply #'+ (length lines)
(mapcar #'(lambda (x) (length x)) lines)))
(dt (make-string (1- ln)))
(pos 0))
(dolist (s (nreverse lines))
(replace dt s :start1 pos)
(incf pos (length s))
(if (< pos (- ln 2)) (setf (elt dt pos) 10))
(incf pos))
(tweet-string dt :warning-time 1 :with-image t)
))
))))
(let* ((len (length *src-lines*))
(start-n (floor (random (float len) *random-state*)))
(spos 0) (str-len 0) lines)
(push (format nil "I am running ~A min." (round (/ waking-time 60.0)))
lines)
(incf str-len (length (car lines)))
(while (< (+ start-n spos) len)
(let ((str (elt *src-lines* (+ start-n spos))))
(incf str-len (length str))
(if (> str-len 140) (return))
(push str lines))
(incf spos))
(let* ((ln (apply #'+ (length lines)
(mapcar #'(lambda (x) (length x)) lines)))
(dt (make-string (1- ln)))
(pos 0))
(dolist (s (nreverse lines))
(replace dt s :start1 pos)
(incf pos (length s))
(if (< pos (- ln 2)) (setf (elt dt pos) 10))
(incf pos))
(tweet-string dt :warning-time 1 :with-image t
:speak *speak-enable*
:volume *volume*)
))))))))
(ros::spin-once)
(ros::sleep)
)
88 changes: 57 additions & 31 deletions jsk_robot_common/jsk_robot_startup/lifelog/tweet_client_warning.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,67 @@

(ros::roseus "twitter_client_warning")

(load "package://roseus/euslisp/dynamic-reconfigure-server.l")
(load "package://jsk_robot_startup/lifelog/tweet_client.l")
(ros::load-ros-manifest "diagnostic_msgs")

(setq *volume* (ros::get-param "~volume" 1.0))
(setq *speak-enable* (ros::get-param "~speak_enable" t))
(setq *enable* (ros::get-param "~enable" t))
(setq *reconfigure-server*
(def-dynamic-reconfigure-server
;;; ((name type level description (default) (min) (max) (edit_method)) ... )
(("volume" double_t 0 "tweet speak volume" 1.0 0.0 1.0)
("speak_enable" bool_t 0 "tweet speak enable" t)
("enable" bool_t 0 "tweet enable" t))
;; use lamda-closure to avoid memory error
'(lambda-closure nil 0 0 (cfg level)
(let ((prev-volume *volume*)
(prev-speak-enable *speak-enable*)
(prev-enable *enable*))
(setq *volume* (cdr (assoc "volume" cfg :test #'equal)))
(setq *speak-enable* (cdr (assoc "speak_enable" cfg :test #'equal)))
(setq *enable* (cdr (assoc "enable" cfg :test #'equal)))
(if (null (equal *volume* prev-volume))
(ros::ros-warn "Volume changed to: ~A" *volume*))
(if (null (equal *enable* prev-enable))
(ros::ros-warn "Enable changed to: ~A" *enable*))
(if (null (equal *speak-enable* prev-speak-enable))
(ros::ros-warn "Speak enable changed to: ~A" *speak-enable*)))
cfg)))

(defun diagnostics-cb (msg)
(let ((diagnostics (make-hash-table :test #'equal))
(tm (ros::time-now))
status id)
(ros::ros-debug (format nil "~0,3f diagnostics_msgs~%" (send tm :to-sec)))
(dolist (status (send msg :status))
;; diagnostic_msgs::DiagnosticStatus::*WARN*
(when (>= (send status :level) diagnostic_msgs::DiagnosticStatus::*WARN*) ;; diagnostic_msgs::DiagnosticStatus::*ERROR*)
(cond ((substringp "/Motors" (send status :name))
t) ;; skip motors
((substringp "/Other/Accelerometer" (send status :name)) t)
((substringp "/Other/Pressure" (send status :name)) t)
((and (string= "/Computers/Network/Wifi Status (ddwrt)" (send status :name))
(string= "Updates Stale" (send status :message))) t)
((and (string= "/Computers/Network" (send status :name))
(string= "Error" (send status :message))) t)
((substringp "/Peripherals/PS3 Controller" (send status :name)) t) ;; fetch joystick warning
((position #\/ (send status :name) :count 2) ;; check depth of name
(setq key (subseq (send status :name) 0 (position #\/ (send status :name) :count 2)))
(when (> (length (send status :name)) (length (gethash key diagnostics)))
(setf (gethash key diagnostics) (cons (send status :name) (send status :message)))
) ;; when
))
)) ;; when / dolist
(maphash #'(lambda (k v) (ros::ros-debug (format nil "Warnings ~A ~A~%" (length status) v)) (push v status)) diagnostics)
(when status
(setq id (random (length status)))
(when (= (mod (round (send tm :sec)) 1000) 0)
(tweet-string (format nil "Warning!! ~A is ~A at ~0,3f" (car (elt status id)) (cdr (elt status id)) (send tm :to-sec))
:warning-time 1 :with-image t)
)) ;; when
)) ;; let
(if *enable*
(let ((diagnostics (make-hash-table :test #'equal))
(tm (ros::time-now))
status id)
(ros::ros-debug (format nil "~0,3f diagnostics_msgs~%" (send tm :to-sec)))
(dolist (status (send msg :status))
;; diagnostic_msgs::DiagnosticStatus::*WARN*
(when (>= (send status :level) diagnostic_msgs::DiagnosticStatus::*WARN*) ;; diagnostic_msgs::DiagnosticStatus::*ERROR*)
(cond ((substringp "/Motors" (send status :name))
t) ;; skip motors
((substringp "/Other/Accelerometer" (send status :name)) t)
((substringp "/Other/Pressure" (send status :name)) t)
((and (string= "/Computers/Network/Wifi Status (ddwrt)" (send status :name))
(string= "Updates Stale" (send status :message))) t)
((and (string= "/Computers/Network" (send status :name))
(string= "Error" (send status :message))) t)
((substringp "/Peripherals/PS3 Controller" (send status :name)) t) ;; fetch joystick warning
((position #\/ (send status :name) :count 2) ;; check depth of name
(setq key (subseq (send status :name) 0 (position #\/ (send status :name) :count 2)))
(when (> (length (send status :name)) (length (gethash key diagnostics)))
(setf (gethash key diagnostics) (cons (send status :name) (send status :message)))
) ;; when
))
)) ;; when / dolist
(maphash #'(lambda (k v) (ros::ros-debug (format nil "Warnings ~A ~A~%" (length status) v)) (push v status)) diagnostics)
(when status
(setq id (random (length status)))
(when (= (mod (round (send tm :sec)) 1000) 0)
(tweet-string (format nil "Warning!! ~A is ~A at ~0,3f" (car (elt status id)) (cdr (elt status id)) (send tm :to-sec))
:warning-time 1 :with-image t :volume *volume* :speak *speak-enable*)))
)))

(ros::advertise "/tweet" std_msgs::String 1)
(ros::subscribe "/diagnostics_agg" diagnostic_msgs::DiagnosticArray #'diagnostics-cb)
Expand Down
Loading