When I just tried to print a few things to the screen, the program didn't crash, but the model usually did. But when I printed nothing to the screen, the model crashed a lot less frequently. I started thinking that perhaps this was an efficiency issue. If my code doesn't even have enough time to print to the screen, maybe there were some things I could think of to make it run faster.
I tried some of the tips shown at this web page: https://wiki.python.org/moin/PythonSpeed/PerformanceTips. Some of the info may be outdated, but some of the tips could work. I need to profile the code later to see if it really makes a difference.
1) Instead of using Numpy functions, I used the default math functions. I learned that Numpy functions are much slower than math functions.
2) Apparently the fewer times you use the dot operator, the better. It is best to save a function handle in a variable and use that variable later.
3) I only logged the variables I needed to. When certain flags are set to False, variables that are not needed are not logged.
4) I had defined many things as global variables that did not need to be global variables. Thus, I shortened this list.
Whereas before I could not get the code to run for 60 seconds without the model crashing, I can now run it for four minutes without it crashing. Here are the results for my four-minute test. The roll and yaw references are zero, the x reference is a sinusoidal input with an amplitude of 1 meter and a period of 5 seconds, and the z reference is a sinusoid with an amplitude of 1 meter and a period of 10 seconds.
| (Left) z-position and (right) x-position for sinusoidal z and x inputs |
| (Left) pitch angle and (right) roll angle for sinusoidal z and x inputs |
| Yaw angle for sinusoidal z and x inputs |
No comments:
Post a Comment