📜 ⬆️ ⬇️

Calculate keyed input using python

At the beginning, a few words about myself: I work as an engineer, respectively, my work is related to the design of various mechanisms. In many of them there is such a thing as a key connection. And despite the fact that a very simple formula is used to calculate it, each time I get bored with handles and a strong decision was made to automate this process.



Initially, automation was a banal Excel-file, in which the corresponding formulas were “hammered”. But then it was decided to write a small program. Moreover, it pursued two goals at once - to get a more convenient “product” and finally write something really useful in Python, which I have long liked. True, my knowledge in this area is very, very modest, but so the task is more interesting.


')
In conclusion, a bit of theory. To calculate the keyed connection, the formula shown in the picture is used. It uses the following values:

Actually with the introduction of done, go directly to the program. The calculation algorithm is really simple:
1. The user enters the shaft diameter.
2. Based on the obtained diameter value, the key parameters are determined.
3. The user chooses the material of the hub on which the permissible shear stress depends.
4. The user enters the value of the moment acting on the shaft.
5. The user enters the key length value.
6. Calculation and output of the result.
In fact, this algorithm should include a few more points, which I did not indicate. This is about checking what the user entered (string or number).
So let's start by entering the original parameters. To enter values ​​from the keyboard, use the following construction:
d = int(raw_input("   , : ")) 

With its help, we get characters from the keyboard and convert them from a string to a number. It would seem that everything is simple. However, after thoughtful deliberation, I decided to somewhat complicate this code:
 while 1: try: d = int(raw_input("   , : ")) break except ValueError: print ("  !") 

As a result, the program itself controls what the user enters - a number or a string. And in the second case, it issues a corresponding warning. The infinite loop is used here in order to get the necessary data from the user sooner or later. With the help of a similar reception, the program receives the rest of the data it needs, immediately checking them for correctness.
We proceed to the definition of the parameters of the key itself. Here, unfortunately, I didn’t think of anything smarter than how to use a relatively long list of if-elif-else:
 if 6 <= d < 8: shponka = {"b": 2, "h": 2, "t1": 1.2, "t2": 1.0} elif 8 <= d < 10: shponka = {"b": 3, "h": 3, "t1": 1.8, "t2": 1.4} elif 10 <= d < 12: shponka = {"b": 4, "h": 4, "t1": 2.5, "t2": 1.8} elif 12 <= d < 17: shponka = {"b": 5, "h": 5, "t1": 3.0, "t2": 2.3} elif 17 <= d < 22: shponka = {"b": 6, "h": 6, "t1": 3.5, "t2": 2.8} elif 22 <= d < 30: shponka = {"b": 8, "h": 7, "t1": 4.0, "t2": 3.3} elif 30 <= d < 38: shponka = {"b": 10, "h": 8, "t1": 5.0, "t2": 3.3} elif 38 <= d < 44: shponka = {"b": 12, "h": 8, "t1": 5.0, "t2": 3.3} elif 44 <= d < 50: shponka = {"b": 14, "h": 9, "t1": 5.5, "t2": 3.8} elif 50 <= d < 58: shponka = {"b": 16, "h": 10, "t1": 6.0, "t2": 4.3} elif 58 <= d < 65: shponka = {"b": 18, "h": 11, "t1": 7.0, "t2": 4.4} elif 65 <= d < 75: shponka = {"b": 20, "h": 12, "t1": 7.5, "t2": 4.9} elif 75 <= d < 85: shponka = {"b": 22, "h": 14, "t1": 9.0, "t2": 5.4} elif 85 <= d < 95: shponka = {"b": 25, "h": 14, "t1": 9.0, "t2": 5.4} elif 95 <= d < 110: shponka = {"b": 28, "h": 16, "t1": 10.0, "t2": 6.4} elif 110 <= d < 130: shponka = {"b": 32, "h": 18, "t1": 11.0, "t2": 7.4} else: print("         ") raw_input("    ") exit() print ("  = " + str(shponka["b"]) + " ,   = " + str(shponka["h"]) + " ,     = " + str(shponka["t1"]) + " ,     = " + str(shponka["t2"]) + " ") 

The key parameters are written to the dictionary - it seemed to me that it would be easier to work with them in this way. Now we determine the working key length:
 l_work = l - shponka["b"] 

And then the actual voltage collapse:
 napr = (2 * 1000 * moment) / (d * l_work * (shponka["h"] - shponka["t1"])) 

The resulting value of the voltage of the collapse is compared with the allowable. There are two possible options:
1. The permissible voltage value is not exceeded, so the selected key satisfies us.
2. Allowable voltage value exceeded. It would be nice to know how to fix it. In most cases, the easiest way to do this is to change the key length. Therefore, the program recalculates the length of the key assuming that the collapse stresses were 20 units below the allowable ones (usually this is enough).
In Python it will look like this:
 if napr <= dopysk_napr: print("     " + str(napr) + " /^2.    " + str(dopysk_napr) + " /^2.") raw_input("    ") exit() else: print("      !       !") #     l_work_rek = (2 * 1000 * moment) / ((dopysk_napr - 20) * d * (shponka["h"] - shponka["t1"])) #       l_rek = l_work_rek + shponka["b"] #      napr_rek = (2 * 1000 * moment) / (d * l_work_rek * (shponka["h"] - shponka["t1"])) print("    " + str(l_rek) + " .      " + str(napr_rek) + " /^2.") raw_input("    ") exit() 

And finally, the full program code:
 # -*- coding: cp866 -*- #       (  ) while 1: try: d = int(raw_input("   , : ")) break except ValueError: print ("  !") #,        if 6 <= d < 8: shponka = {"b": 2, "h": 2, "t1": 1.2, "t2": 1.0} elif 8 <= d < 10: shponka = {"b": 3, "h": 3, "t1": 1.8, "t2": 1.4} elif 10 <= d < 12: shponka = {"b": 4, "h": 4, "t1": 2.5, "t2": 1.8} elif 12 <= d < 17: shponka = {"b": 5, "h": 5, "t1": 3.0, "t2": 2.3} elif 17 <= d < 22: shponka = {"b": 6, "h": 6, "t1": 3.5, "t2": 2.8} elif 22 <= d < 30: shponka = {"b": 8, "h": 7, "t1": 4.0, "t2": 3.3} elif 30 <= d < 38: shponka = {"b": 10, "h": 8, "t1": 5.0, "t2": 3.3} elif 38 <= d < 44: shponka = {"b": 12, "h": 8, "t1": 5.0, "t2": 3.3} elif 44 <= d < 50: shponka = {"b": 14, "h": 9, "t1": 5.5, "t2": 3.8} elif 50 <= d < 58: shponka = {"b": 16, "h": 10, "t1": 6.0, "t2": 4.3} elif 58 <= d < 65: shponka = {"b": 18, "h": 11, "t1": 7.0, "t2": 4.4} elif 65 <= d < 75: shponka = {"b": 20, "h": 12, "t1": 7.5, "t2": 4.9} elif 75 <= d < 85: shponka = {"b": 22, "h": 14, "t1": 9.0, "t2": 5.4} elif 85 <= d < 95: shponka = {"b": 25, "h": 14, "t1": 9.0, "t2": 5.4} elif 95 <= d < 110: shponka = {"b": 28, "h": 16, "t1": 10.0, "t2": 6.4} elif 110 <= d < 130: shponka = {"b": 32, "h": 18, "t1": 11.0, "t2": 7.4} else: print("         ") raw_input("    ") exit() print ("  = " + str(shponka["b"]) + " ,   = " + str(shponka["h"]) + " ,     = " + str(shponka["t1"]) + " ,     = " + str(shponka["t2"]) + " ") #       while 1: try: material = raw_input("   :  (1)   (2). ") break except ValueError: print ("     ,   .") if material == "1": dopysk_napr = 130 #  /^2 elif material == "2": dopysk_napr = 80 #  /^2 else: print("   !") raw_input("    ") exit() #          while 1: try: moment = int(raw_input("       : ")) break except ValueError: print ("       !") #        while 1: try: l = int(raw_input("     : ")) break except ValueError: print ("  !") #    (,     ) l_work = l - shponka["b"] #           napr = (2 * 1000 * moment) / (d * l_work * (shponka["h"] - shponka["t1"])) if napr <= dopysk_napr: print("     " + str(napr) + " /^2.    " + str(dopysk_napr) + " /^2.") raw_input("    ") exit() else: print("      !       !") #     l_work_rek = (2 * 1000 * moment) / ((dopysk_napr - 20) * d * (shponka["h"] - shponka["t1"])) #       l_rek = l_work_rek + shponka["b"] #      napr_rek = (2 * 1000 * moment) / (d * l_work_rek * (shponka["h"] - shponka["t1"])) print("    " + str(l_rek) + " .      " + str(napr_rek) + " /^2.") raw_input("    ") exit() 

With this, my work on this program most likely will not end, since I really liked Python and its use in my work is really possible (at least for the same COMPASS writing scripts). In the future I plan to add several additional calculation modes depending on what needs to be determined. But in the very distant future - the appearance of the user interface is possible.
I also understand that with this modest work I’m unlikely to surprise the venerable Habrahabr audience (unless we take into account the curvature of the source code). But I hope for understanding and (possibly) recommendations for improvement.

Source: https://habr.com/ru/post/189980/


All Articles