rtmotor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import sys,math
3 import rospy
4 from raspimouse_ros.srv import PutMotorFreqs
5 from raspimouse_ros.srv import SwitchMotors
6 from raspimouse_ros.msg import MotorFreqs
7 from geometry_msgs.msg import Twist
8 from std_msgs.msg import Bool
9 
10 def callback_motor_sw(message):
11  enfile = '/dev/rtmotoren0'
12 
13  try:
14  with open(enfile,'w') as f:
15  if message.on: print >> f, '1'
16  else: print >> f, '0'
17  except:
18  rospy.logerr("cannot write to " + enfile)
19  return False
20 
21  return True
22 
23 def callback_motor_raw(message):
24  lfile = '/dev/rtmotor_raw_l0'
25  rfile = '/dev/rtmotor_raw_r0'
26 
27  try:
28  lf = open(lfile,'w')
29  rf = open(rfile,'w')
30  print >> lf, str(message.left)
31  print >> rf, str(message.right)
32  except:
33  rospy.logerr("cannot write to rtmotor_raw_*")
34 
35  lf.close()
36  rf.close()
37 
38 def callback_cmd_vel(message):
39  lfile = '/dev/rtmotor_raw_l0'
40  rfile = '/dev/rtmotor_raw_r0'
41 
42  #for forwarding
43  forward_hz = 80000.0*message.linear.x/(9*math.pi)
44  #for rotation
45  rot_hz = 400.0*message.angular.z/math.pi
46  try:
47  lf = open(lfile,'w')
48  rf = open(rfile,'w')
49  lf.write(str(int(round(forward_hz - rot_hz))) + '\n')
50  rf.write(str(int(round(forward_hz + rot_hz))) + '\n')
51  except:
52  rospy.logerr("cannot write to rtmotor_raw_*")
53 
54  lf.close()
55  rf.close()
56 
57 def callback_put_freqs(message):
58  devfile = '/dev/rtmotor0'
59 
60  try:
61  with open(devfile,'w') as f:
62  print >> f, "%s %s %s" % (message.left, message.right, message.duration)
63  except:
64  rospy.logerr("cannot write to " + devfile)
65  return False
66 
67  return True
68 
69 if __name__ == '__main__':
70  rospy.init_node('rtmotor')
71  sub = rospy.Subscriber('motor_raw', MotorFreqs, callback_motor_raw)
72  sub = rospy.Subscriber('cmd_vel', Twist, callback_cmd_vel)
73  srv = rospy.Service('switch_motors', SwitchMotors, callback_motor_sw)
74  srv = rospy.Service('put_motor_freqs', PutMotorFreqs, callback_put_freqs)
75  rospy.spin()
def callback_motor_sw(message)
Definition: rtmotor.py:10
def callback_put_freqs(message)
Definition: rtmotor.py:57
def callback_motor_raw(message)
Definition: rtmotor.py:23
def callback_cmd_vel(message)
Definition: rtmotor.py:38


raspimouse_ros
Author(s):
autogenerated on Mon Jun 10 2019 14:27:02