-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch_my_turtlebot.py
More file actions
executable file
·193 lines (160 loc) · 5.65 KB
/
launch_my_turtlebot.py
File metadata and controls
executable file
·193 lines (160 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
"""
Author: Sarucha Yanyong
Purpose: Remote control turtlebot3
Revision:
1.0 2019-07-10 Baseline
"""
import sys
import subprocess
import os
import paramiko
import socket
import re
import time
import ConfigParser
def ping(hostname):
# hostname = "google.com" #example
response = os.system("ping -c 1 " + hostname)
if response == 0:
print ("Ping: Destination is UP")
return True
else:
print ("Ping: Destination is Down")
return False
def send(channel, command=None, live=True, once=False, end=None):
buff = ""
_resp = ""
start = time.time()
if command != None:
channel.send(command + '\n')
if once:
return "shutdown"
if end is None:
end = ":~$ "
while not buff.endswith(end):
resp = channel.recv(9999)
buff += str(resp)
print(">>" + str(buff[-20:]) + "<<")
# if str(resp) != _resp: # Detect changing
# start = time.time()
# _resp = str(resp)
if live:
print(str(resp) + "\n")
# print(buff)
# print("OUT")
return buff
def is_valid_ip(ip):
m = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip)
return bool(m) and all(map(lambda n: 0 <= int(n) <= 255, m.groups()))
def main():
config = ConfigParser.ConfigParser()
config.sections()
config.read('ipconfig.ini')
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)
# cliend_ip = input("Client IP Address: ").strip()
master_ip = cliend_ip = None
# cliend_ip = "192.168.10.207"
# master_ip = "192.168.10.238"
master_ip = config.get('DEFAULT', 'PC_IP')
cliend_ip = config.get('DEFAULT', 'TURTLEBOT_IP')
if len(sys.argv) >= 2:
if is_valid_ip(sys.argv[1]) and is_valid_ip(sys.argv[2]):
master_ip = sys.argv[1]
cliend_ip = sys.argv[2]
if master_ip is None and cliend_ip is None:
master_ip = raw_input("Computer IP Address: ").strip()
cliend_ip = raw_input("TurtleBot IP Address: ").strip()
print("Connect %s to %s" % (master_ip, cliend_ip))
if not is_valid_ip(cliend_ip) or not is_valid_ip(master_ip):
print("Wrong IP Address format.")
sys.exit(0)
mode = raw_input("""
Options
=========
[1] Check turtlebot connection.
[2] Bringup Turtlebot
[3] Remote shutdown Turtlebot
[4] Remote restart Turtlebot
"""
).strip()
if mode == "1":
print("Alive." if ping(cliend_ip) else "Lost connection.")
elif mode == "3":
if ping(cliend_ip):
remote = None
try:
remote = paramiko.SSHClient()
remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote.connect(cliend_ip, username="pi", password="raspberry")
channel = remote.invoke_shell()
send(channel)
print(send(channel, command="sudo shutdown now", once=True))
print(send(channel, command="raspberry", once=True))
except Exception as e:
print('Connection Failed')
print(e)
finally:
print ("Close")
if remote:
remote.close()
while ping(cliend_ip):
print("Shunting down...")
time.sleep(1)
print("Robot has been shutdown completely.")
elif mode == "4":
if ping(cliend_ip):
remote = None
try:
remote = paramiko.SSHClient()
remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote.connect(cliend_ip, username="pi", password="raspberry")
channel = remote.invoke_shell()
send(channel)
print(send(channel, command="sudo reboot", once=True))
print(send(channel, command="raspberry", once=True))
except Exception as e:
print('Connection Failed')
print(e)
finally:
print ("Close")
if remote:
remote.close()
while ping(cliend_ip):
print("Shunting down...")
time.sleep(1)
while not ping(cliend_ip):
print("Booting up...")
time.sleep(1)
print("Robot has been reboot completely.")
elif mode == "2":
if ping(cliend_ip):
print(cliend_ip)
remote = None
try:
remote = paramiko.SSHClient()
remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote.connect(cliend_ip, username="pi", password="raspberry")
channel = remote.invoke_shell()
send(channel)
print(send(channel, command="export ROS_MASTER_URI=http://%s:11311" % master_ip))
print(send(channel, command="export ROS_HOSTNAME=%s" % cliend_ip))
print(send(channel, command="env | grep \"ROS\""))
print(send(channel, command="echo $ROS_MASTER_URI"))
print(send(channel, command="roslaunch turtlebot3_bringup turtlebot3_robot.launch",
end=": Calibration End\r\n"))
raw_input("Enter to exit...")
print(send(channel, command="\x03", once=True))
sys.exit()
except Exception as e:
print('Connection Failed')
print(e)
finally:
print ("Close")
if remote:
remote.close()
if __name__ == "__main__":
main()