buzzer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #encoding: utf8
3 import rospy, actionlib
4 from std_msgs.msg import UInt16
5 from raspicat.msg import MusicAction, MusicResult, MusicFeedback # 行を追加
6 
7 def write_freq(hz=0):
8  bfile = "/dev/rtbuzzer0"
9  try:
10  with open(bfile,"w") as f:
11  f.write(str(hz) + "\n")
12  except IOError:
13  rospy.logerr("can't write to " + bfile)
14 
15 def exec_music(goal):
16  r = MusicResult()
17  fb = MusicFeedback()
18 
19  for i, f in enumerate(goal.freqs):
20  fb.remaining_steps = len(goal.freqs) - i
21  music.publish_feedback(fb)
22 
23  if music.is_preempt_requested():
24  write_freq(0)
25  r.finished = False
26  music.set_preempted(r)
27  return
28 
29  write_freq(f)
30  rospy.sleep(1.0 if i >= len(goal.durations) else goal.durations[i])
31 
32  r.finished = True
33  music.set_succeeded(r)
34 
35 def recv_buzzer(data):
36  write_freq(data.data)
37 if __name__ == '__main__':
38  rospy.init_node('buzzer')
39  rospy.Subscriber("buzzer", UInt16, recv_buzzer)
40  music = actionlib.SimpleActionServer('music', MusicAction, exec_music, False) # 追加
41  music.start() # 追加
42  rospy.on_shutdown(write_freq) # 追加
43  rospy.spin()
def write_freq(hz=0)
Definition: buzzer.py:7
def recv_buzzer(data)
Definition: buzzer.py:35
def exec_music(goal)
Definition: buzzer.py:15


raspicat
Author(s): Ryuichi Ueda , Daisuke Sato
autogenerated on Mon Jun 10 2019 14:27:50