Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 import rospy
00032 import csv
00033 from dbw_mkz_msgs.msg import BrakeCmd, BrakeReport, BrakeInfoReport
00034 from dbw_mkz_msgs.msg import GearReport, SteeringReport
00035 from math import fabs
00036
00037 class BrakeSweep:
00038 def __init__(self):
00039 rospy.init_node('brake_sweep')
00040
00041
00042 self.brake_cmd = 0.0
00043 self.msg_brake_report = BrakeReport()
00044 self.msg_brake_report_ready = False
00045 self.msg_brake_info_report = BrakeInfoReport()
00046 self.msg_brake_info_report_ready = False
00047
00048
00049 self.msg_gear_report = GearReport()
00050 self.msg_gear_report_ready = False
00051 self.msg_steering_report = SteeringReport()
00052 self.msg_steering_report_ready = False
00053
00054
00055 self.i = -1
00056 self.start = 0.15
00057 self.end = 0.35
00058 self.resolution = 0.001
00059 self.duration = rospy.Duration(3.0)
00060 rospy.loginfo('Recording brake pedal data every ' + str(self.duration.to_sec()) + ' seconds from ' + str(self.start) + ' to ' + str(self.end) + ' with ' + str(self.resolution) + ' increments.')
00061 rospy.loginfo('This will take ' + str((self.end - self.start) / self.resolution * self.duration.to_sec() / 60.0) + ' minutes.')
00062
00063
00064 self.csv_file = open('brake_sweep_data.csv', 'w')
00065 self.csv_writer = csv.writer(self.csv_file, delimiter=',')
00066 self.csv_writer.writerow(['Brake (%)', 'Torque Request (Nm)', 'Torque Actual (Nm)'])
00067
00068
00069 self.pub = rospy.Publisher('/vehicle/brake_cmd', BrakeCmd, queue_size=1)
00070 rospy.Subscriber('/vehicle/brake_report', BrakeReport, self.recv_brake)
00071 rospy.Subscriber('/vehicle/brake_info_report', BrakeInfoReport, self.recv_brake_info)
00072 rospy.Subscriber('/vehicle/gear_report', GearReport, self.recv_gear)
00073 rospy.Subscriber('/vehicle/steering_report', SteeringReport, self.recv_steering)
00074 rospy.Timer(rospy.Duration(0.02), self.timer_cmd)
00075 rospy.Timer(self.duration, self.timer_process)
00076
00077 def timer_process(self, event):
00078 if self.i < 0:
00079
00080 if not self.msg_steering_report_ready:
00081 rospy.logerr('Speed check failed. No messages on topic \'/vehicle/steering_report\'')
00082 rospy.signal_shutdown('')
00083 if self.msg_steering_report.speed > 1.0:
00084 rospy.logerr('Speed check failed. Vehicle speed is greater than 1 m/s.')
00085 rospy.signal_shutdown('')
00086 if not self.msg_gear_report_ready:
00087 rospy.logerr('Gear check failed. No messages on topic \'/vehicle/gear_report\'')
00088 rospy.signal_shutdown('')
00089 if not self.msg_gear_report.state.gear == self.msg_gear_report.state.PARK:
00090 rospy.logerr('Gear check failed. Vehicle not in park.')
00091 rospy.signal_shutdown('')
00092 elif self.i < int((self.end - self.start) / self.resolution + 1):
00093
00094 if not self.msg_brake_report_ready:
00095 rospy.logerr('No new messages on topic \'/vehicle/brake_report\'')
00096 rospy.signal_shutdown('')
00097 if not self.msg_brake_info_report_ready:
00098 rospy.logerr('No new messages on topic \'/vehicle/brake_info_report\'')
00099 rospy.signal_shutdown('')
00100 if not self.msg_brake_report.enabled:
00101 rospy.logerr('Brake module not enabled!')
00102 rospy.signal_shutdown('')
00103 if self.msg_brake_report.pedal_input > 0.19:
00104 rospy.logwarn('Take your foot off the brake pedal! This will corrupt the measurement.')
00105
00106
00107 diff = self.brake_cmd - self.msg_brake_report.pedal_output
00108 if (fabs(diff) > 0.01):
00109 rospy.logwarn('Not saving data point. Large disparity between pedal request and actual: ' + str(diff))
00110 else:
00111
00112 rospy.loginfo('Data point: ' + "{:.03f}".format(self.msg_brake_report.pedal_output) + ', ' +
00113 str(self.msg_brake_info_report.brake_torque_request) + ', ' +
00114 str(self.msg_brake_info_report.brake_torque_actual))
00115 self.csv_writer.writerow(["{:.03f}".format(self.msg_brake_report.pedal_output),
00116 "{:.01f}".format(self.msg_brake_info_report.brake_torque_request),
00117 "{:.01f}".format(self.msg_brake_info_report.brake_torque_actual)])
00118 else:
00119 rospy.signal_shutdown('')
00120
00121
00122 self.i += 1
00123 self.msg_brake_report_ready = False
00124 self.msg_brake_info_report_ready = False
00125 self.brake_cmd = self.start + self.i * self.resolution
00126
00127 def timer_cmd(self, event):
00128 if self.brake_cmd > 0:
00129 msg = BrakeCmd()
00130 msg.enable = True
00131 msg.pedal_cmd_type = BrakeCmd.CMD_PEDAL
00132 msg.pedal_cmd = self.brake_cmd
00133 if msg.pedal_cmd > 0.2:
00134 msg.boo_cmd = True
00135 self.pub.publish(msg)
00136
00137 def recv_brake(self, msg):
00138 self.msg_brake_report = msg
00139 self.msg_brake_report_ready = True
00140
00141 def recv_brake_info(self, msg):
00142 self.msg_brake_info_report = msg
00143 self.msg_brake_info_report_ready = True
00144
00145 def recv_gear(self, msg):
00146 self.msg_gear_report = msg
00147 self.msg_gear_report_ready = True
00148
00149 def recv_steering(self, msg):
00150 self.msg_steering_report = msg
00151 self.msg_steering_report_ready = True
00152
00153 def shutdown_handler(self):
00154 rospy.loginfo('Saving csv file')
00155 self.csv_file.close()
00156
00157 if __name__ == '__main__':
00158 try:
00159 node = BrakeSweep()
00160 rospy.on_shutdown(node.shutdown_handler)
00161 rospy.spin()
00162 except rospy.ROSInterruptException:
00163 pass