forked from adeept/adeept_rasptank2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
215 lines (184 loc) · 7.27 KB
/
setup.py
File metadata and controls
215 lines (184 loc) · 7.27 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python3
# File name : setup.py
# Author : Devin
import os
username = (
os.popen("echo ${SUDO_USER:-$(who -m | awk '{ print $1 }')}").readline().strip()
) # pi
user_home = (
os.popen("getent passwd %s | cut -d: -f 6" % username).readline().strip()
) # home
curpath = os.path.realpath(__file__)
thisPath = "/" + os.path.dirname(curpath)
print(thisPath)
def replace_num(file, initial, new_num):
newline = ""
str_num = str(new_num)
with open(file) as f:
for line in f.readlines():
if line.find(initial) == 0:
line = str_num + "\n"
newline += line
with open(file, "w") as f:
f.writelines(newline)
def run_command(cmd=""):
import subprocess
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
result = p.stdout.read().decode("utf-8")
status = p.poll()
return status, result
def check_rpi_model():
_, result = run_command("cat /proc/device-tree/model |awk '{print $3}'")
result = result.strip()
if result == "3":
return int(3)
elif result == "4":
return int(4)
else:
return None
def check_raspbain_version():
_, result = run_command("cat /etc/debian_version|awk -F. '{print $1}'")
return int(result.strip())
def check_python_version():
import sys
major = int(sys.version_info.major)
minor = int(sys.version_info.minor)
micro = int(sys.version_info.micro)
return major, minor, micro
def check_os_bit():
"""
# import platform
# machine_type = platform.machine()
latest bullseye uses a 64-bit kernel
This method is no longer applicable, the latest raspbian will uses 64-bit kernel
(kernel 6.1.x) by default, "uname -m" shows "aarch64",
but the system is still 32-bit.
"""
_, os_bit = run_command("getconf LONG_BIT")
return int(os_bit)
commands_apt = [
"sudo apt-get update",
"sudo apt-get install python3-gpiozero python3-pigpio",
"sudo apt-get install -y python3-opencv",
"sudo apt-get install -y python3-pyqt5 python3-opengl",
"sudo apt-get install -y python3-picamera2",
"sudo apt-get install -y python3-picamera2 --no-install-recommends",
"sudo apt-get install -y python3-opencv",
"sudo apt-get install -y opencv-data",
"sudo apt-get install -y python3-pyaudio",
]
mark_apt = 0
for x in range(3):
for command in commands_apt:
if os.system(command) != 0:
print("Error running installation step apt")
mark_apt = 1
if mark_apt == 0:
break
commands_pip_1 = [
"sudo pip3 install adafruit-circuitpython-motor",
"sudo pip3 install adafruit-circuitpython-pca9685",
"sudo pip3 install flask",
"sudo pip3 install flask_cors",
"sudo pip3 install numpy",
"sudo pip3 install pyzmq",
"sudo pip3 install imutils zmq pybase64 psutil",
"sudo pip3 install websockets==13.0",
"sudo pip3 install rpi_ws281x",
"sudo pip3 install adafruit-circuitpython-ads7830",
]
commands_pip_2 = [
"sudo pip3 install adafruit-circuitpython-motor --break-system-packages",
"sudo pip3 install adafruit-circuitpython-pca9685 --break-system-packages",
"sudo pip3 install flask --break-system-packages",
"sudo pip3 install flask_cors --break-system-packages",
"sudo pip3 install numpy --break-system-packages",
"sudo pip3 install pyzmq --break-system-packages",
"sudo pip3 install imutils zmq pybase64 psutil --break-system-packages",
"sudo pip3 install websockets==13.0 --break-system-packages",
"sudo pip3 install rpi_ws281x --break-system-packages",
"sudo pip3 install adafruit-circuitpython-ads7830 --break-system-packages",
]
mark_pip = 0
OS_version = check_raspbain_version()
if OS_version <= 11:
for x in range(3):
for command in commands_pip_1:
if os.system(command) != 0:
print("Error running installation step pip")
mark_pip = 1
if mark_pip == 0:
break
else:
for x in range(3):
for command in commands_pip_2:
if os.system(command) != 0:
print("Error running installation step pip")
mark_pip = 1
if mark_pip == 0:
break
commands_3 = [
"cd ~",
"sudo git clone https://github.com/oblique/create_ap",
"cd create_ap && sudo make install",
# "cd //home/pi/create_ap && sudo make install",
"sudo apt-get install -y util-linux procps hostapd iproute2 iw haveged dnsmasq",
]
mark_3 = 0
for x in range(3):
for command in commands_3:
if os.system(command) != 0:
print("Error running installation step 3")
mark_2 = 1
if mark_3 == 0:
break
try:
replace_num(
"/boot/config.txt", "#dtparam=i2c_arm=on", "dtparam=i2c_arm=on\nstart_x=1\n"
)
except Exception:
print("Error updating boot config to enable i2c. Please try again.")
try:
os.system("sudo touch /" + user_home + "/startup.sh")
with open("/" + user_home + "/startup.sh", "w") as file_to_write:
# you can choose how to control the robot
file_to_write.write(
"#!/bin/sh\nsleep 5\nsudo python3 " + thisPath + "/web/webServer.py"
)
except Exception:
pass
os.system("sudo chmod 777 /" + user_home + "/startup.sh")
if not os.path.exists("/etc/rc.local"):
print(
"/etc/rc.local does not exist. It is required to run the program when the raspberry pi starts. \nHowever it is not required for function. \nWould you like to create a /etc/rc.local/ file (recommended)?"
)
if input("(y/n): ").strip().lower() == "y":
os.system("sudo touch /etc/rc.local")
os.system("sudo chown root:root /etc/rc.local")
os.system("sudo chmod 755 /etc/rc.local")
try:
with open("/etc/rc.local", "w") as file_to_write:
file_to_write.write(
"#!/bin/sh -e\n/" + user_home + "/startup.sh start\nexit 0"
)
except Exception:
print("Error: writing /etc/rc.local/ failed.")
else:
print(
"Program setup without /etc/rc.local complete. \nNote: you will have to run startup.sh manually to set servos for mechanical assembly."
)
else: # there is /etc/rc.local
try:
replace_num("/etc/rc.local", "fi", "fi\n/" + user_home + "/startup.sh start")
print(
"/etc/rc.local setup complete. After turning the Raspberry Pi on again, the Raspberry Pi will automatically run the program to set the servos port signal to turn the servos to the middle position, which is convenient for mechanical assembly."
)
except Exception:
print("Error adding to /startup.sh")
print(
"The program in Raspberry Pi has been installed, disconnected and restarted. \nYou can now power off the Raspberry Pi to install the camera and driver board (Robot HAT). \nAfter turning on again, the Raspberry Pi will automatically run the program to set the servos port signal to turn the servos to the middle position, which is convenient for mechanical assembly."
)
print("restarting...")
os.system("sudo reboot")