From 50c1d34f709997fbac75e94940d247e6c48ef45a Mon Sep 17 00:00:00 2001 From: Raspberry Pi Date: Tue, 26 Nov 2019 19:55:38 -0600 Subject: Improve logging: add timestamps for each log. Add a new function to add other messages to the log file. Update swingup test to use new logging behavior. --- System/system.py | 23 +++++++++++++++++------ System/system_swingup_test_2.py | 7 ++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/System/system.py b/System/system.py index 63d0ecc..8dd9f38 100644 --- a/System/system.py +++ b/System/system.py @@ -77,7 +77,7 @@ class System: self.result_filename = "Results/" + os.path.basename(sys.argv[0]).split('.')[0] + "_results.csv" result_file = open(self.result_filename, "w+") - result_file.write("angle(" + angular_units + "),position(inches),speed(percentage)\n") + result_file.write("timestamp,angle(" + angular_units + "),position(inches),speed(percentage)\n") result_file.close() # Setup a thread to constantly be measuring encoder positions @@ -173,7 +173,7 @@ class System: self.motor.brake() # Print negative soft limit violation to the results file. result_file = open(self.result_filename, "a") - result_file.write("Negative software limit %f has been reached!" % self.negative_soft_limit) + result_file.write("Negative software limit %f has been reached!\n" % self.negative_soft_limit) result_file.close() # Fire the limit trigger method self.sw_limit_routine() @@ -185,7 +185,7 @@ class System: self.motor.brake() # Print positive soft limit violation to the results file. result_file = open(self.result_filename, "a") - result_file.write("Positive software limit %f has been reached!" % self.positive_soft_limit) + result_file.write("Positive software limit %f has been reached!\n" % self.positive_soft_limit) result_file.close() # Fire the limit trigger method self.sw_limit_routine() @@ -220,13 +220,24 @@ class System: # open the results file result_file = open(self.result_filename, "a") # Write the results - result_file.write("%f," % angle) # Write angle - result_file.write("%f," % position) # Write position - result_file.write("%f\n" % speed) # Write speed (end of line) + result_file.write("%s," % datetime.now().strftime("%H:%M:%S.%f")) # Write current time + result_file.write("%f," % angle) # Write angle + result_file.write("%f," % position) # Write position + result_file.write("%f\n" % speed) # Write speed (end of line) # Close the results file result_file.close() # END add_results + def add_log(self, message): + # open the results file + result_file = open(self.result_filename, "a") + # Write the log + result_file.write("%s\n" % message) + # re-write the csv headers for next logging + result_file.write("timestamp,angle(" + self.angular_units + "),position(inches),speed(percentage)\n") + # Close the results file + result_file.close() + # Go back to the zero position (linear) so that the next execution starts in the correct place. def return_home(self): position = self.linear_position diff --git a/System/system_swingup_test_2.py b/System/system_swingup_test_2.py index 5c0ac5a..cb646ef 100644 --- a/System/system_swingup_test_2.py +++ b/System/system_swingup_test_2.py @@ -156,6 +156,10 @@ class SwingUpEnv(): def end(self): self.sys.deinitialize() + + def log(self, message): + self.sys.add_log(message) + print(message) class nnQ(pt.nn.Module): @@ -368,7 +372,8 @@ try: R.append(C) UpTime.append(max_up_time) #print('t:',ep+1,', R:',C,', L:',t-1,', G:',G,', Q:', Q_est, 'U:', max_up_time) - print('Episode:',ep, 'Total Steps:',step, ', Ave. Reward:',C, ', Episode Length:',t-1, 'Max Up-Time:',max_up_time) + log = "Episode:" + str(ep) + " Total Steps:" + str(step) + " Ave. Reward:" + str(C) + " Episode Length:" + str(t-1) + " Max Up-Time:" + str(max_up_time) + env.log(log) except: env.end() exit(-1) -- cgit v1.2.3