navcon2udp.py
Go to the documentation of this file.
00001 '''
00002 Created on Dec 19, 2012
00003 
00004 @author: dnad
00005 '''
00006 import roslib; roslib.load_manifest("uwsim_lv");
00007 import rospy;
00008 from navigation_g500.msg import FastraxIt500Gps
00009 from auv_msgs.msg import BodyForceReq
00010 from geometry_msgs.msg import Vector3 
00011 from geometry_msgs.msg import Wrench
00012 from sensor_msgs.msg import Imu
00013 from tf.transformations import euler_from_quaternion
00014 import threading
00015 import PyKDL
00016 from numpy import array
00017 import math
00018 
00019 class NavHandler:
00020     """Handles incoming navigation messages and UDP communication"""
00021     def __init__(self):
00022         rospy.init_node("navcon2udp");
00023         '''Subscribe to sensor messages'''
00024         gpsName = rospy.get_param("~gpsName","navigation_g500/fastrax_it_500_gps");
00025         imuName = rospy.get_param("~imuName","navigation_g500/imu");
00026         
00027         rospy.Subscriber(gpsName, FastraxIt500Gps, self.onGps);
00028         rospy.Subscriber(imuName, Imu, self.onImu);
00029         
00030         '''Sensor transformation matrix'''
00031         self.imu_tf = self.computeTf(array(rospy.get_param("tritech_igc_gyro/tf")));
00032         self.dataMux=threading.Lock();
00033         self.runFlag=1;
00034         self.commsThread = threading.Thread(target=self.comms);
00035         self.commsThread.start();
00036         
00037         '''Initialize values'''
00038         self.roll=self.pitch=self.yaw=self.north=self.east=self.gpsValid=0;
00039         
00040     def onImu(self,data):
00041         roll,pitch,yaw = euler_from_quaternion(
00042             [data.orientation.x, 
00043             data.orientation.y,
00044             data.orientation.z,
00045             data.orientation.w]);
00046         imu_data =  PyKDL.Rotation.RPY(roll, pitch, yaw);
00047         imu_data = imu_data * self.imu_tf.M;
00048         self.dataMux.acquire(); 
00049         self.roll,self.pitch,self.yaw = imu_data.GetRPY();
00050         self.dataMux.release();        
00051         #print("Roll-pitch-yaw (%f,%f,%f)",self.roll,self.pitch,self.yaw);
00052         
00053     def onGps(self,data):
00054         self.dataMux.acquire(); 
00055         self.north = data.north;
00056         self.east = data.east;
00057         self.gpsValid=1;
00058         self.dataMux.release();
00059         #print("Received data (%f,%f).",data.north,data.east);   
00060         
00061     def computeTf(self, tf):
00062         r = PyKDL.Rotation.RPY(math.radians(tf[3]), math.radians(tf[4]), math.radians(tf[5]))
00063         v = PyKDL.Vector(tf[0], tf[1], tf[2])
00064         frame = PyKDL.Frame(r, v)
00065         return frame
00066     
00067     def comms(self):    
00068         '''Create TAU publisher''' 
00069         tauName = rospy.get_param("~tauName","control_g500/velocity_to_force_req");
00070         pub = rospy.Publisher(tauName, BodyForceReq)
00071     
00072         '''Open UDP socket'''
00073         import socket
00074         comms = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
00075         comms.bind(("127.0.0.1",rospy.get_param("/lv_host_port",33346)));
00076 
00077         while self.runFlag:
00078             '''Get and send TAU data'''
00079             data,addr = comms.recvfrom(100);
00080             #print("Recevied:%s",data);
00081             tau=double(data.split(","));      
00082             if len(tau) == 6:      
00083                 pub.publish(BodyForceReq(wrench = Wrench(Vector3(tau[0],tau[1],tau[2]), Vector3(tau[3],tau[4],tau[5]))));
00084     
00085             self.dataMux.acquire();
00086             comms.sendto(",".join([self.gpsValid, str(self.north), str(self.east), str(self.roll), str(self.pitch), str(self.yaw)]),addr);
00087             self.gpsValid = 0;
00088             self.dataMux.release();          
00089         
00090     def stop(self):
00091         self.runFlag = 0;
00092         self.commsThread.join();
00093     
00094 def navcon2udp():
00095     handler = NavHandler();
00096     '''Start ROS node.'''
00097     rospy.spin();
00098     handler.stop();        
00099 
00100 if __name__ == '__main__':
00101     navcon2udp()


caddy_uwsim
Author(s): Dula Nad
autogenerated on Fri Feb 7 2014 11:37:30