simple_dp.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 '''
00003 Created on Feb 04, 2013
00004 
00005 @author: dnad
00006 '''
00007 import roslib; roslib.load_manifest("cart_sim");
00008 import rospy;
00009 from auv_msgs.msg import BodyVelocityReq;
00010 from auv_msgs.msg import NavSts;
00011 from std_msgs.msg import Byte
00012 import numpy
00013 import math
00014       
00015 class DynamicPositioning:
00016     def __init__(self):
00017         rospy.Subscriber("etaRef", NavSts, self.onRef)
00018         rospy.Subscriber("stateHat", NavSts, self.onStateHat);              
00019         self.stateHat = NavSts();
00020         self.out = rospy.Publisher("nuRef", BodyVelocityReq);
00021          
00022         w = numpy.array(rospy.get_param(
00023                             "dynamic_positioning/closed_loop_freq"));
00024         
00025         self.Kp = numpy.array([[2*w[0], 0],[0, 2*w[1]]],
00026                               dtype=numpy.float32);     
00027         self.Kp_yaw = 2*w[3];
00028         self.Kp_z = 2*w[2];
00029                
00030         self.state = numpy.array([0,0,0,0], numpy.float32);
00031         self.desired = numpy.array([0,0,0,0], numpy.float32);
00032         
00033         
00034         self.R = numpy.identity(2, numpy.float32);  
00035         
00036     def onRef(self,data):
00037         self.desired = numpy.array([data.position.north,
00038                                     data.position.east,
00039                                     data.position.depth,
00040                                     self.wrapAngle(data.orientation.yaw)], 
00041                                    dtype=numpy.float32);         
00042         print "Have ref:",self.desired                    
00043             
00044     def wrapAngle(self, angle):
00045         if angle>=math.pi:
00046             return -2*math.pi + angle;
00047         elif angle<-math.pi:
00048             return 2*math.pi + angle;
00049         else:
00050             return angle;
00051                
00052     def onStateHat(self,data):
00053         self.stateHat = data;
00054         self.state = numpy.array([data.position.north, 
00055                                   data.position.east, 
00056                                   data.position.depth, 
00057                                   self.wrapAngle(data.orientation.yaw)],
00058                                  dtype=numpy.float32);
00059         yaw = data.orientation.yaw;
00060         self.R = numpy.array([[math.cos(yaw),-math.sin(yaw)],
00061                               [math.sin(yaw),math.cos(yaw)]],numpy.float32);
00062                 
00063     def step(self):
00064         d = self.desired - self.state;                                        
00065         pub = BodyVelocityReq();
00066         u = numpy.dot(numpy.transpose(self.R),numpy.dot(self.Kp,[d[0],d[1]]));
00067         pub.twist.linear.x = u[0];
00068         pub.twist.linear.y = u[1];
00069         pub.twist.linear.z = self.Kp_z*d[2];
00070         pub.twist.angular.z = self.Kp_yaw*self.wrapAngle(d[3]);
00071         pub.goal.priority = pub.goal.PRIORITY_NORMAL;
00072         pub.disable_axis.pitch = 0;
00073         pub.disable_axis.roll = pub.disable_axis.z = 0;
00074         self.out.publish(pub); 
00075         
00076 if __name__ == "__main__":
00077     rospy.init_node("simpledp");
00078     dp = DynamicPositioning();
00079         
00080     rate = rospy.Rate(10.0);
00081         
00082     while not rospy.is_shutdown():
00083         dp.step();
00084         rate.sleep();
00085         
00086         
00087         


ldtravocean
Author(s):
autogenerated on Fri Feb 7 2014 11:37:01