Skip to content

Commit 421ec8f

Browse files
committed
merging lint
2 parents a66adc5 + b502b48 commit 421ec8f

23 files changed

+487
-757
lines changed

MPU6050.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from __future__ import print_function
21
"""This program handles the communication over I2C
32
between a Raspberry Pi and a MPU-6050 Gyroscope / Accelerometer combo.
43
Made by: MrTijn/Tijndagamer
54
Released under the MIT License
65
Copyright 2015
76
"""
87

9-
import smbus2 as smbus
108
import time
9+
import smbus2 as smbus
1110

1211
class MPU6050:
1312

@@ -88,10 +87,9 @@ def read_i2c_word(self, register):
8887

8988
value = (high << 8) + low
9089

91-
if (value >= 0x8000):
90+
if value >= 0x8000:
9291
return -((65535 - value) + 1)
93-
else:
94-
return value
92+
return value
9593

9694
# MPU-6050 Methods
9795

@@ -120,7 +118,7 @@ def set_accel_range(self, accel_range):
120118
# Write the new range to the ACCEL_CONFIG register
121119
self.bus.write_byte_data(self.address, self.ACCEL_CONFIG, accel_range)
122120

123-
def read_accel_range(self, raw = False):
121+
def read_accel_range(self, raw=False):
124122
"""Reads the range the accelerometer is set to.
125123
If raw is True, it will return the raw value from the ACCEL_CONFIG
126124
register
@@ -141,10 +139,9 @@ def read_accel_range(self, raw = False):
141139
return 8
142140
elif raw_data == self.ACCEL_RANGE_16G:
143141
return 16
144-
else:
145-
return -1
142+
return -1
146143

147-
def get_accel_data(self, g = False):
144+
def get_accel_data(self, g=False):
148145
"""Gets and returns the X, Y and Z values from the accelerometer.
149146
If g is True, it will return the data in g
150147
If g is False, it will return the data in m/s^2
@@ -174,13 +171,11 @@ def get_accel_data(self, g = False):
174171
y = y / accel_scale_modifier
175172
z = z / accel_scale_modifier
176173

177-
if g is True:
178-
return {'x': x, 'y': y, 'z': z}
179-
elif g is False:
174+
if g is False:
180175
x = x * self.GRAVITIY_MS2
181176
y = y * self.GRAVITIY_MS2
182177
z = z * self.GRAVITIY_MS2
183-
return {'x': x, 'y': y, 'z': z}
178+
return {'x': x, 'y': y, 'z': z}
184179

185180
def set_gyro_range(self, gyro_range):
186181
"""Sets the range of the gyroscope to range.
@@ -193,7 +188,7 @@ def set_gyro_range(self, gyro_range):
193188
# Write the new range to the ACCEL_CONFIG register
194189
self.bus.write_byte_data(self.address, self.GYRO_CONFIG, gyro_range)
195190

196-
def read_gyro_range(self, raw = False):
191+
def read_gyro_range(self, raw=False):
197192
"""Reads the range the gyroscope is set to.
198193
If raw is True, it will return the raw value from the GYRO_CONFIG
199194
register.
@@ -214,8 +209,7 @@ def read_gyro_range(self, raw = False):
214209
return 1000
215210
elif raw_data == self.GYRO_RANGE_2000DEG:
216211
return 2000
217-
else:
218-
return -1
212+
return -1
219213

220214
def get_gyro_data(self):
221215
"""Gets and returns the X, Y and Z values from the gyroscope.
@@ -249,9 +243,9 @@ def get_gyro_data(self):
249243

250244
def get_all_data(self):
251245
"""Reads and returns all the available data."""
252-
temp = get_temp()
253-
accel = get_accel_data()
254-
gyro = get_gyro_data()
246+
temp = self.get_temp()
247+
accel = self.get_accel_data()
248+
gyro = self.get_gyro_data()
255249

256250
return [accel, gyro, temp]
257251

@@ -267,4 +261,4 @@ def get_all_data(self):
267261
if abs(gyro_data['z']) > mpu.GYRO_THRESHOLD_Z:
268262
dz = gyro_data['z'] * dt
269263
z = z + dz
270-
print( "z: ", z, end="\r")
264+
print("z: ", z, end="\r")

api.py

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
This file contains every method called by the API defined in v2.yml
44
"""
55

6-
from flask import jsonify
6+
import os
7+
import subprocess
78
import json
8-
from coderbot import CoderBot
9-
from program import ProgramEngine, Program
10-
from config import Config
119
import connexion
12-
import time
13-
import sqlite3
1410
from tinydb import TinyDB, Query
1511
from tinydb.operations import delete
16-
import os
17-
import subprocess
1812
from cachetools import cached, TTLCache
13+
from coderbot import CoderBot
14+
from program import ProgramEngine
15+
from config import Config
1916

2017
bot_config = Config.get()
2118
bot = CoderBot.get_instance(
@@ -24,20 +21,20 @@
2421
)
2522

2623
def get_serial():
27-
"""
28-
Extract serial from cpuinfo file
29-
"""
30-
cpuserial = "0000000000000000"
31-
try:
32-
f = open('/proc/cpuinfo','r')
33-
for line in f:
34-
if line[0:6]=='Serial':
35-
cpuserial = line[10:26]
36-
f.close()
37-
except:
38-
cpuserial = "ERROR000000000"
39-
40-
return cpuserial
24+
"""
25+
Extract serial from cpuinfo file
26+
"""
27+
cpuserial = "0000000000000000"
28+
try:
29+
f = open('/proc/cpuinfo', 'r')
30+
for line in f:
31+
if line[0:6] == 'Serial':
32+
cpuserial = line[10:26]
33+
f.close()
34+
except Exception:
35+
cpuserial = "ERROR000000000"
36+
37+
return cpuserial
4138

4239
@cached(cache=TTLCache(maxsize=1, ttl=10))
4340
def get_status():
@@ -47,10 +44,10 @@ def get_status():
4744
(Cached method)
4845
"""
4946
try:
50-
temp = os.popen("vcgencmd measure_temp").readline().replace("temp=","")
51-
except:
47+
temp = os.popen("vcgencmd measure_temp").readline().replace("temp=", "")
48+
except Exception:
5249
temp = "undefined"
53-
50+
5451
uptime = subprocess.check_output(["uptime"]).decode('utf-8').replace('\n', '')
5552
internet_status = subprocess.check_output(["./utils/check_conn.sh"]).decode('utf-8').replace('\n', '')
5653
return {'internet_status': internet_status,
@@ -68,28 +65,28 @@ def get_info():
6865
with open('manifest.json', 'r') as f:
6966
metadata = json.load(f)
7067
backend_commit = metadata["backendCommit"][0:7]
71-
except:
68+
except Exception:
7269
backend_commit = "undefined"
7370

7471
try:
7572
coderbot_version = subprocess.check_output(["cat", "/etc/coderbot/version"]).decode('utf-8').replace('\n', '')
76-
except:
77-
coderbot_version = 'undefined'
73+
except Exception:
74+
coderbot_version = 'undefined'
7875
try:
7976
kernel = subprocess.check_output(["uname", "-r"]).decode('utf-8').replace('\n', '')
80-
except:
77+
except Exception:
8178
kernel = 'undefined'
8279
try:
8380
update_status = subprocess.check_output(["cat", "/etc/coderbot/update_status"]).decode('utf-8').replace('\n', '')
84-
except:
81+
except Exception:
8582
update_status = 'undefined'
8683

87-
serial = get_serial();
88-
return {'backend_commit':backend_commit,
89-
'coderbot_version':coderbot_version,
84+
serial = get_serial()
85+
return {'backend_commit': backend_commit,
86+
'coderbot_version': coderbot_version,
9087
'update_status': update_status,
91-
'kernel':kernel,
92-
'serial':serial}
88+
'kernel': kernel,
89+
'serial': serial}
9390

9491
prog = None
9592
prog_engine = ProgramEngine.get_instance()
@@ -100,7 +97,7 @@ def get_info():
10097

10198
query = Query()
10299

103-
## Robot control
100+
## Robot control
104101

105102
def stop():
106103
bot.stop()
@@ -117,43 +114,43 @@ def turn(data):
117114
return 200
118115

119116
def exec(data):
120-
prog = prog_engine.create(data["name"], data["code"])
121-
return json.dumps(prog.execute())
117+
program = prog_engine.create(data["name"], data["code"])
118+
return json.dumps(program.execute())
122119

123120
## System
124121

125122
def status():
126-
status = get_status()
123+
sts = get_status()
127124

128125
return {
129126
"status": "ok",
130-
"internetConnectivity": status["internet_status"],
131-
"temp": status["temp"],
132-
"uptime": status["uptime"],
127+
"internetConnectivity": sts["internet_status"],
128+
"temp": sts["temp"],
129+
"uptime": sts["uptime"],
133130
}
134131

135132
def info():
136-
info = get_info()
133+
inf = get_info()
137134
return {
138135
"model": 1,
139-
"version": info["coderbot_version"],
140-
"backend commit build": info["backend_commit"],
141-
"kernel" : info["kernel"],
142-
"update status": info["update_status"],
143-
"serial": info["serial"]
136+
"version": inf["coderbot_version"],
137+
"backend commit build": inf["backend_commit"],
138+
"kernel" : inf["kernel"],
139+
"update status": inf["update_status"],
140+
"serial": inf["serial"]
144141
}
145142

146143
def restoreSettings():
147144
with open("data/defaults/config.json") as f:
148145
Config.write(json.loads(f.read()))
149-
bot_config = Config.get()
146+
Config.get()
150147
return "ok"
151148

152149
def updateFromPackage():
153150
os.system('sudo bash /home/pi/clean-update.sh')
154151
file_to_upload = connexion.request.files['file_to_upload']
155152
file_to_upload.save(os.path.join('/home/pi/', 'update.tar'))
156-
os.system('sudo coderbot_update /home/pi/update.tar && sudo reboot')
153+
os.system('sudo reboot')
157154
return 200
158155

159156

@@ -171,7 +168,7 @@ def saveProgram(data, overwrite):
171168
return "defaultOverwrite"
172169
# Overwrite existing program with the same name
173170
else:
174-
if (overwrite == "1"):
171+
if overwrite == "1":
175172
programs.update(data, query.name == data["name"])
176173
return 200
177174
else:
@@ -218,4 +215,4 @@ def resetDefaultPrograms():
218215
if filename.endswith(".json"):
219216
with open("data/defaults/programs/" + filename) as p:
220217
q = p.read()
221-
programs.insert(json.loads(q))
218+
programs.insert(json.loads(q))

0 commit comments

Comments
 (0)