1
1
"""Defines the CLI for the OpenLCH project."""
2
2
3
3
import subprocess
4
-
5
4
import click
5
+ from .grpc import Servo
6
6
7
+ DEFAULT_IP = "192.168.42.1"
7
8
8
9
@click .group ()
9
10
def cli () -> None :
10
- """OpenLCH CLI tool for interacting with MilkV boards."""
11
- raise NotImplementedError ("The CLI is not yet implemented" )
11
+ """OpenLCH CLI tool for interacting with MilkV boards.
12
+
13
+ Available commands:
14
+ - ping: Ping the MilkV board
15
+ - get-positions: Get current positions of all servos
16
+ - set-position: Set position for a specific servo
17
+ - set-wifi: Set WiFi credentials for the MilkV board
18
+ - get-servo-info: Get information about a specific servo
19
+ - scan-servos: Scan for connected servos
20
+ - change-servo-id: Change the ID of a servo
12
21
22
+ Use 'openlch COMMAND --help' for more information on a specific command.
23
+ """
24
+ pass
13
25
14
26
@cli .command ()
15
- @click .option ( "-- ip" , required = True , help = "IP address of the MilkV board" )
27
+ @click .argument ( " ip" , default = DEFAULT_IP )
16
28
def ping (ip : str ) -> None :
17
29
"""Ping the MilkV board at the specified IP address."""
18
30
try :
19
- result = subprocess .run (["ping" , "-c" , "4 " , ip ], capture_output = True , text = True , check = False )
31
+ result = subprocess .run (["ping" , "-c" , "1 " , ip ], capture_output = True , text = True , check = False )
20
32
if result .returncode == 0 :
21
33
click .echo (f"Successfully pinged { ip } " )
22
34
click .echo (result .stdout )
@@ -26,6 +38,99 @@ def ping(ip: str) -> None:
26
38
except Exception as e :
27
39
click .echo (f"An error occurred: { str (e )} " )
28
40
41
+ @cli .command ()
42
+ @click .argument ("ip" , default = DEFAULT_IP )
43
+ def get_positions (ip : str ) -> None :
44
+ """Get current positions of all servos."""
45
+ client = Servo (ip )
46
+ try :
47
+ positions = client .get_positions ()
48
+ click .echo ("Current positions:" )
49
+ for id , position in positions :
50
+ click .echo (f"Servo { id } : { position } " )
51
+ except Exception as e :
52
+ click .echo (f"An error occurred: { str (e )} " )
53
+ finally :
54
+ client .close ()
55
+
56
+ @cli .command ()
57
+ @click .argument ("id" , type = int )
58
+ @click .argument ("position" , type = float )
59
+ @click .argument ("ip" , default = DEFAULT_IP )
60
+ def set_position (id : int , position : float , ip : str ) -> None :
61
+ """Set position for a specific servo."""
62
+ client = Servo (ip )
63
+ try :
64
+ client .set_positions ([(id , position )])
65
+ click .echo (f"Position set for servo { id } to { position } " )
66
+ except Exception as e :
67
+ click .echo (f"An error occurred: { str (e )} " )
68
+ finally :
69
+ client .close ()
70
+
71
+ @cli .command ()
72
+ @click .argument ("ssid" )
73
+ @click .argument ("password" )
74
+ @click .argument ("ip" , default = DEFAULT_IP )
75
+ def set_wifi (ssid : str , password : str , ip : str ) -> None :
76
+ """Set WiFi credentials for the MilkV board."""
77
+ client = Servo (ip )
78
+ try :
79
+ client .set_wifi_info (ssid , password )
80
+ click .echo ("WiFi credentials set successfully" )
81
+ except Exception as e :
82
+ click .echo (f"An error occurred: { str (e )} " )
83
+ finally :
84
+ client .close ()
85
+
86
+ @cli .command ()
87
+ @click .argument ("id" , type = int )
88
+ @click .argument ("ip" , default = DEFAULT_IP )
89
+ def get_servo_info (id : int , ip : str ) -> None :
90
+ """Get information about a specific servo."""
91
+ client = Servo (ip )
92
+ try :
93
+ info = client .get_servo_info (id )
94
+ click .echo (f"Servo { id } info:" )
95
+ for key , value in info .items ():
96
+ click .echo (f"{ key } : { value } " )
97
+ except Exception as e :
98
+ click .echo (f"An error occurred: { str (e )} " )
99
+ finally :
100
+ client .close ()
101
+
102
+ @cli .command ()
103
+ @click .argument ("ip" , default = DEFAULT_IP )
104
+ def scan_servos (ip : str ) -> None :
105
+ """Scan for connected servos."""
106
+ client = Servo (ip )
107
+ try :
108
+ servo_ids = client .scan ()
109
+ click .echo ("Found servo IDs:" )
110
+ for id in servo_ids :
111
+ click .echo (id )
112
+ except Exception as e :
113
+ click .echo (f"An error occurred: { str (e )} " )
114
+ finally :
115
+ client .close ()
116
+
117
+ @cli .command ()
118
+ @click .argument ("old_id" , type = int )
119
+ @click .argument ("new_id" , type = int )
120
+ @click .argument ("ip" , default = DEFAULT_IP )
121
+ def change_servo_id (old_id : int , new_id : int , ip : str ) -> None :
122
+ """Change the ID of a servo."""
123
+ client = Servo (ip )
124
+ try :
125
+ success = client .change_id (old_id , new_id )
126
+ if success :
127
+ click .echo (f"Successfully changed servo ID from { old_id } to { new_id } " )
128
+ else :
129
+ click .echo ("Failed to change servo ID" )
130
+ except Exception as e :
131
+ click .echo (f"An error occurred: { str (e )} " )
132
+ finally :
133
+ client .close ()
29
134
30
135
if __name__ == "__main__" :
31
136
# python -m openlch.cli
0 commit comments