import rospy from tf_conversions import transformations import tf import csv
classWayPointSaver: def__init__(self): self._pIdx = 0 self._wp = [] self._header = ['Idx','x','y','yaw'] self._filename = 'waypoints.csv' self.tf_listener = tf.TransformListener() withopen(self._filename,'w') as f: wtr = csv.writer(f) wtr.writerow(self._header) def_get_wp(self): ''' get current pose ''' try: trans, rot = self.tf_listener.lookupTransform('/map', '/base_link', rospy.Time(0)) except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException): return roll,pitch,yaw = transformations.euler_from_quaternion(rot) x = trans[0] y = trans[1] if x and y: self._wp = [self._pIdx, x, y, yaw] returnTrue else: returnFalse
def_process_wp(self): ''' process points ''' # your can put your own code here self._pIdx += 1 def_write_file(self): ''' write point to file ''' withopen(self._filename,'a') as f: wtr = csv.writer(f) wtr.writerow(self._wp)
defrun(self): ''' main loop ''' if self._get_wp(): self._process_wp() self._write_file()
if __name__ == "__main__": rospy.init_node('wayPoint_saver') saver = WayPointSaver() r = rospy.Rate(2) whilenot rospy.is_shutdown(): saver.run() r.sleep()