Skip to content

Commit

Permalink
[tools] minor code style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
flixr committed Sep 30, 2013
1 parent 50a8ae9 commit 06c95d8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 54 deletions.
13 changes: 7 additions & 6 deletions sw/tools/calibration/calibrate.py
Expand Up @@ -30,6 +30,7 @@

import calibration_utils


def main():
usage = "usage: %prog [options] log_filename.data" + "\n" + "Run %prog --help to list the options."
parser = OptionParser(usage)
Expand Down Expand Up @@ -58,7 +59,7 @@ def main():
print(args[0] + " not found")
sys.exit(1)
ac_ids = calibration_utils.get_ids_in_log(filename)
if options.ac_id == None:
if options.ac_id is None:
if len(ac_ids) == 1:
options.ac_id = ac_ids[0]
else:
Expand All @@ -69,13 +70,13 @@ def main():
if options.sensor == "ACCEL":
sensor_ref = 9.81
sensor_res = 10
noise_window = 20;
noise_threshold = 40;
noise_window = 20
noise_threshold = 40
elif options.sensor == "MAG":
sensor_ref = 1.
sensor_res = 11
noise_window = 10;
noise_threshold = 1000;
noise_window = 10
noise_threshold = 1000

if not filename.endswith(".data"):
parser.error("Please specify a *.data log file")
Expand All @@ -88,7 +89,7 @@ def main():
print("Error: found zero IMU_"+options.sensor+"_RAW measurements for aircraft with id "+options.ac_id+" in log file!")
sys.exit(1)
if options.verbose:
print("found "+str(len(measurements))+" records")
print("found "+str(len(measurements))+" records")

# estimate the noise threshold
# find the median of measurement vector lenght
Expand Down
14 changes: 7 additions & 7 deletions sw/tools/calibration/calibrate_gyro.py
Expand Up @@ -87,7 +87,7 @@ def main():
if options.verbose:
print("reading file "+filename+" for aircraft "+str(options.ac_id)+" and turntable "+str(options.tt_id))

samples = calibration_utils.read_turntable_log(options.ac_id, options.tt_id, filename, 1, 7)
samples = calibration_utils.read_turntable_log(options.ac_id, options.tt_id, filename, 1, 7)

if len(samples) == 0:
print("Error: found zero matching messages in log file!")
Expand All @@ -106,13 +106,13 @@ def main():
parser.error("Specify a valid axis!")

#Linear regression using stats.linregress
t = samples[:, 0]
t = samples[:, 0]
xn = samples[:, axis_idx]
(a_s, b_s, r, tt, stderr)=stats.linregress(t, xn)
(a_s, b_s, r, tt, stderr) = stats.linregress(t, xn)
print('Linear regression using stats.linregress')
print(('regression: a=%.2f b=%.2f, std error= %.3f' % (a_s, b_s, stderr)))
print(('<define name="GYRO_X_NEUTRAL" value="%d"/>' % (b_s)));
print(('<define name="GYRO_X_SENS" value="%f" integer="16"/>' % (pow(2, 12)/a_s)));
print(('<define name="GYRO_X_NEUTRAL" value="%d"/>' % (b_s)))
print(('<define name="GYRO_X_SENS" value="%f" integer="16"/>' % (pow(2, 12)/a_s)))

#
# overlay fited value
Expand All @@ -125,7 +125,7 @@ def main():
plot(samples[:, 1])
plot(samples[:, 2])
plot(samples[:, 3])
legend(['p', 'q', 'r']);
legend(['p', 'q', 'r'])

subplot(3, 1, 2)
plot(samples[:, 0])
Expand All @@ -134,7 +134,7 @@ def main():
plot(samples[:, 0], samples[:, axis_idx], 'b.')
plot(ovl_omega, ovl_adc, 'r')

show();
show()


if __name__ == "__main__":
Expand Down
8 changes: 2 additions & 6 deletions sw/tools/calibration/calibrate_mag_current.py
Expand Up @@ -49,18 +49,14 @@ def main():
print(args[0] + " not found")
sys.exit(1)
ac_ids = calibration_utils.get_ids_in_log(filename)
if options.ac_id == None:
if options.ac_id is None:
if len(ac_ids) == 1:
options.ac_id = ac_ids[0]
else:
parser.error("More than one aircraft id found in log file. Specify the id to use.")
if options.verbose:
print("Using aircraft id "+options.ac_id)

sensor_ref = 1.
sensor_res = 11
noise_window = 10;
noise_threshold = 1000;

if not filename.endswith(".data"):
parser.error("Please specify a *.data log file")
Expand All @@ -73,7 +69,7 @@ def main():
print("Error: found zero IMU_MAG_CURRENT_CALIBRATION measurements for aircraft with id "+options.ac_id+" in log file!")
sys.exit(1)
if options.verbose:
print("found "+str(len(measurements))+" records")
print("found "+str(len(measurements))+" records")

coefficient = calibration_utils.estimate_mag_current_relation(measurements)

Expand Down
69 changes: 34 additions & 35 deletions sw/tools/calibration/calibration_utils.py
Expand Up @@ -38,7 +38,7 @@ def get_ids_in_log(filename):
line = f.readline().strip()
if line == '':
break
m=re.match(pattern, line)
m = re.match(pattern, line)
if m:
id = m.group(1)
if not id in ids:
Expand All @@ -56,7 +56,7 @@ def read_log(ac_id, filename, sensor):
line = f.readline().strip()
if line == '':
break
m=re.match(pattern, line)
m = re.match(pattern, line)
if m:
list_meas.append([float(m.group(2)), float(m.group(3)), float(m.group(4))])
return scipy.array(list_meas)
Expand All @@ -72,7 +72,7 @@ def read_log_mag_current(ac_id, filename):
line = f.readline().strip()
if line == '':
break
m=re.match(pattern, line)
m = re.match(pattern, line)
if m:
list_meas.append([float(m.group(2)), float(m.group(3)), float(m.group(4)), float(m.group(5))])
return scipy.array(list_meas)
Expand Down Expand Up @@ -106,8 +106,8 @@ def get_min_max_guess(meas, scale):
# scale the set of measurements
#
def scale_measurements(meas, p):
l_comp = [];
l_norm = [];
l_comp = []
l_norm = []
for m in meas[:,]:
sm = (m - p[0:3])*p[3:6]
l_comp.append(sm)
Expand All @@ -120,7 +120,7 @@ def scale_measurements(meas, p):
def estimate_mag_current_relation(meas):
coefficient = []
for i in range(0,3):
gradient, intercept, r_value, p_value, std_err = stats.linregress(meas[:,3],meas[:,i])
gradient, intercept, r_value, p_value, std_err = stats.linregress(meas[:,3], meas[:,i])
coefficient.append(gradient)
return coefficient

Expand Down Expand Up @@ -154,26 +154,26 @@ def plot_results(block, measurements, flt_idx, flt_meas, cp0, np0, cp1, np1, sen
title('Raw sensors')

subplot(3, 2, 3)
plot(cp0[:, 0]);
plot(cp0[:, 1]);
plot(cp0[:, 2]);
plot(-sensor_ref*scipy.ones(len(flt_meas)));
plot(sensor_ref*scipy.ones(len(flt_meas)));
plot(cp0[:, 0])
plot(cp0[:, 1])
plot(cp0[:, 2])
plot(-sensor_ref*scipy.ones(len(flt_meas)))
plot(sensor_ref*scipy.ones(len(flt_meas)))

subplot(3, 2, 4)
plot(np0);
plot(sensor_ref*scipy.ones(len(flt_meas)));
plot(np0)
plot(sensor_ref*scipy.ones(len(flt_meas)))

subplot(3, 2, 5)
plot(cp1[:, 0]);
plot(cp1[:, 1]);
plot(cp1[:, 2]);
plot(-sensor_ref*scipy.ones(len(flt_meas)));
plot(sensor_ref*scipy.ones(len(flt_meas)));
plot(cp1[:, 0])
plot(cp1[:, 1])
plot(cp1[:, 2])
plot(-sensor_ref*scipy.ones(len(flt_meas)))
plot(sensor_ref*scipy.ones(len(flt_meas)))

subplot(3, 2, 6)
plot(np1);
plot(sensor_ref*scipy.ones(len(flt_meas)));
plot(np1)
plot(sensor_ref*scipy.ones(len(flt_meas)))

# if we want to have another plot we only draw the figure (non-blocking)
# also in matplotlib before 1.0.0 there is only one call to show possible
Expand All @@ -187,14 +187,14 @@ def plot_results(block, measurements, flt_idx, flt_meas, cp0, np0, cp1, np1, sen
#
def plot_mag_3d(measured, calibrated, p):
# set up points for sphere and ellipsoid wireframes
u=r_[0:2*pi:20j]
v=r_[0:pi:20j]
wx=outer(cos(u),sin(v))
wy=outer(sin(u),sin(v))
wz=outer(ones(size(u)),cos(v))
ex=p[0]*ones(size(u)) + outer(cos(u),sin(v))/p[3]
ey=p[1]*ones(size(u)) + outer(sin(u),sin(v))/p[4]
ez=p[2]*ones(size(u)) + outer(ones(size(u)),cos(v))/p[5]
u = r_[0:2 * pi:20j]
v = r_[0:pi:20j]
wx = outer(cos(u), sin(v))
wy = outer(sin(u), sin(v))
wz = outer(ones(size(u)), cos(v))
ex = p[0] * ones(size(u)) + outer(cos(u), sin(v)) / p[3]
ey = p[1] * ones(size(u)) + outer(sin(u), sin(v)) / p[4]
ez = p[2] * ones(size(u)) + outer(ones(size(u)), cos(v)) / p[5]

# measurements
mx = measured[:, 0]
Expand Down Expand Up @@ -228,10 +228,10 @@ def plot_mag_3d(measured, calibrated, p):
ax.plot_wireframe(ex, ey, ez, color='grey', alpha=0.5)

# Create cubic bounding box to simulate equal aspect ratio
max_range = np.array([mx.max()-mx.min(), my.max()-my.min(), mz.max()-mz.min()]).max()
Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(mx.max()+mx.min())
Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(my.max()+my.min())
Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(mz.max()+mz.min())
max_range = np.array([mx.max() - mx.min(), my.max() - my.min(), mz.max() - mz.min()]).max()
Xb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][0].flatten() + 0.5 * (mx.max() + mx.min())
Yb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][1].flatten() + 0.5 * (my.max() + my.min())
Zb = 0.5 * max_range * np.mgrid[-1:2:2, -1:2:2, -1:2:2][2].flatten() + 0.5 * (mz.max() + mz.min())
# uncomment following both lines to test the fake bounding box:
#for xb, yb, zb in zip(Xb, Yb, Zb):
# ax.plot([xb], [yb], [zb], 'r*')
Expand All @@ -241,7 +241,6 @@ def plot_mag_3d(measured, calibrated, p):
ax.set_ylabel('y')
ax.set_zlabel('z')


if matplotlib.__version__.startswith('0'):
ax = Axes3D(fig, rect=rect_r)
else:
Expand Down Expand Up @@ -273,10 +272,10 @@ def read_turntable_log(ac_id, tt_id, filename, _min, _max):
line = f.readline().strip()
if line == '':
break
m=re.match(pattern_t, line)
m = re.match(pattern_t, line)
if m:
last_tt = float(m.group(2))
m=re.match(pattern_g, line)
m = re.match(pattern_g, line)
if m and last_tt and last_tt > _min and last_tt < _max:
list_tt.append([last_tt, float(m.group(2)), float(m.group(3)), float(m.group(4))])
return scipy.array(list_tt)
Expand Down

0 comments on commit 06c95d8

Please sign in to comment.