Skip to content

Commit

Permalink
Added error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed May 30, 2016
1 parent 6083d68 commit ee8a150
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
8 changes: 5 additions & 3 deletions scripts/rtbuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

def callback(message):
devfile = '/dev/rtbuzzer0'
#rospy.loginfo("Buzzer %d", message.data)
with open(devfile,'w') as f:
print >> f, message.data
try:
with open(devfile,'w') as f:
print >> f, message.data
except:
rospy.logerr("cannot open " + devfile)


def listner():
Expand Down
22 changes: 13 additions & 9 deletions scripts/rtlightsensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
from raspimouse_ros.msg import LightSensorValues

def talker():
devfile = '/dev/rtlightsensor0'
rospy.init_node('lightsensors')
pub = rospy.Publisher('lightsensors', LightSensorValues, queue_size=1)
rate = rospy.Rate(10)

while not rospy.is_shutdown():
with open('/dev/rtlightsensor0','r') as f:
data = f.readline().split()
d = LightSensorValues()
d.right_forward = int(data[0])
d.right_side = int(data[1])
d.left_side = int(data[2])
d.left_forward = int(data[3])
pub.publish(d)
rate.sleep()
try:
with open(devfile,'r') as f:
data = f.readline().split()
d = LightSensorValues()
d.right_forward = int(data[0])
d.right_side = int(data[1])
d.left_side = int(data[2])
d.left_forward = int(data[3])
pub.publish(d)
rate.sleep()
except:
rospy.logerr("cannot open " + devfile)

if __name__ == '__main__':
try:
Expand Down
31 changes: 19 additions & 12 deletions scripts/rtmotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,41 @@
def callback_motor_sw(message):
enfile = '/dev/rtmotoren0'

with open(enfile,'w') as f:
if message.on: print >> f, '1'
else: print >> f, '0'
try:
with open(enfile,'w') as f:
if message.on: print >> f, '1'
else: print >> f, '0'
except:
rospy.logerr("cannot write to " + enfile)
return False

return True

def callback_motor_raw(message):
lfile = '/dev/rtmotor_raw_l0'
rfile = '/dev/rtmotor_raw_r0'

print message
try:
lf = open(lfile,'w')
rf = open(rfile,'w')
print >> lf, str(message.left)
print >> rf, str(message.right)
except:
print >> sys.stderr, "cannot write to rtmotor_raw_*"
sys.exit(1)
else:
lf.close()
rf.close()
rospy.logerr("cannot write to rtmotor_raw_*")

lf.close()
rf.close()

def callback_put_freqs(message):
devfile = '/dev/rtmotor0'
putstr = "%s %s %s" % (message.left, message.right, message.duration)
print putstr
with open(devfile,'w') as f:
print >> f, putstr

try:
with open(devfile,'w') as f:
print >> f, "%s %s %s" % (message.left, message.right, message.duration)
except:
rospy.logerr("cannot write to " + devfile)
return False

return True

Expand Down
15 changes: 9 additions & 6 deletions scripts/rtswitches.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ def talker():

d = Switches()
while not rospy.is_shutdown():
with open(devfile + '0','r') as f:
d.front = True if '0' in f.readline() else False
with open(devfile + '1','r') as f:
d.center = True if '0' in f.readline() else False
with open(devfile + '2','r') as f:
d.rear = True if '0' in f.readline() else False
try:
with open(devfile + '0','r') as f:
d.front = True if '0' in f.readline() else False
with open(devfile + '1','r') as f:
d.center = True if '0' in f.readline() else False
with open(devfile + '2','r') as f:
d.rear = True if '0' in f.readline() else False
except:
rospy.logerr("cannot open " + devfile + "[0,1,2]")

pub.publish(d)
rate.sleep()
Expand Down

0 comments on commit ee8a150

Please sign in to comment.