1
1
'''
2
2
Unit test for all muscle commands and status variables
3
- '''
3
+ '''
4
+ import thermoflex as tf
5
+ import threading as th
6
+ import time as t
7
+
8
+ COMMAND_CHANGE_INTERVAL = 5
9
+ STATUS_THREAD_INTERVAL = 10
10
+ END_TEST_FLAG = th .Event ()
11
+ END_TEST_FLAG .clear ()
12
+
13
+ def threaded (func ):
14
+ global threadlist
15
+ threadlist = []
16
+
17
+ def wrapper (* args , ** kwargs ):
18
+ thread = th .Thread (target = func , args = args , kwargs = kwargs )
19
+ threadlist .append (thread )
20
+ thread .start ()
21
+ return thread
22
+
23
+ return wrapper
24
+
25
+ #Establish a connection to the testnet and testnode
26
+ network = tf .discover ()
27
+ testnet = network [0 ]
28
+ testnode = testnet .node_list [0 ]
29
+
30
+ #Test the muscle command and status variables
31
+
32
+ muscle1 = testnode .muscle0
33
+
34
+ # Test the Muscle class methods
35
+ @threaded
36
+ def test_muscle_cmd (muscle ):
37
+
38
+ # Set the mode of the muscle
39
+ muscle .setMode (conmode = "amps" )
40
+ print (f"Muscle Mode: { muscle .cmode } " )
41
+ t .sleep (COMMAND_CHANGE_INTERVAL )
42
+
43
+ # Set a setpoint for the muscle
44
+ muscle .setSetpoint (conmode = "percent" , setpoint = 0.75 )
45
+ print ("Setpoint set to 0.75 in percent mode." )
46
+ t .sleep (COMMAND_CHANGE_INTERVAL )
47
+
48
+ # Enable the muscle
49
+ muscle .setEnable (True )
50
+ print ("Muscle enabled." )
51
+ t .sleep (COMMAND_CHANGE_INTERVAL )
52
+
53
+ # Disable the muscle
54
+ muscle .setEnable (False )
55
+ print ("Muscle disabled." )
56
+ t .sleep (COMMAND_CHANGE_INTERVAL )
57
+
58
+ @threaded
59
+ def muscle_status_test (muscle ):
60
+
61
+ while not END_TEST_FLAG .is_set ():
62
+ t .sleep (STATUS_THREAD_INTERVAL )
63
+ # Get the current setpoint of the muscle
64
+ setpoint = muscle .getSetpoint ()
65
+ print (f"Muscle Setpoint: { setpoint } " )
66
+
67
+ # Get the current mode of the muscle
68
+ mode = muscle .getMode ()
69
+ print (f"Muscle Mode: { mode } " )
70
+
71
+ # Get the current current of the muscle
72
+ current = muscle .getCurrent ()
73
+ print (f"Muscle Current: { current } " )
74
+
75
+ # Get the current voltage of the muscle
76
+ voltage = muscle .getVoltage ()
77
+ print (f"Muscle Voltage: { voltage } " )
78
+ # Get the muscle status
79
+
80
+ status = muscle .muscleStatus ()
81
+ print (f"Muscle Status: { status } " )
82
+
83
+ # Get the resistance of the muscle
84
+ resistance = muscle .getResistance ()
85
+ print (f"Muscle Resistance: { resistance } " )
86
+
87
+
88
+
89
+ # Run the test
90
+ muscle_status_test (muscle1 )
91
+ test_muscle_cmd (muscle1 )
92
+
93
+
94
+
95
+ for thread in threadlist :
96
+ thread .join ()
0 commit comments