[chronojump] encoder capture and graphs do same operations



commit 09f3c3d3cbbc6f201592a4832cb1048e932e1f1a
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sun Apr 1 02:40:27 2012 +0800

    encoder capture and graphs do same operations

 encoder/graph.R           |   29 +++++++++++------------
 encoder/pyserial_pyper.py |   54 +++++++++++++++++++++++++++++++++++----------
 src/encoder.cs            |    7 ++++-
 src/gui/encoder.cs        |    4 ++-
 4 files changed, 64 insertions(+), 30 deletions(-)
---
diff --git a/encoder/graph.R b/encoder/graph.R
index 4701ac9..f505173 100644
--- a/encoder/graph.R
+++ b/encoder/graph.R
@@ -47,7 +47,7 @@ findCurves <- function(rawdata, eccon, min_height, draw) {
 		}
 	}
 	if(draw) {
-		plot(a,type="l",xlim=c(1,length(a)),xlab="",ylab="",axes=T)
+		plot(a/10,type="l",xlim=c(1,length(a)),xlab="",ylab="",axes=T) #/10 mm -> cm
 		abline(v=b$maxindex,lty=3); abline(v=b$minindex,lty=3)
 	}
 	return(as.data.frame(cbind(start,end,startH)))
@@ -56,15 +56,18 @@ findCurves <- function(rawdata, eccon, min_height, draw) {
 
 #based on findPics2BySpeed
 #only used in eccon "c"
+#if this changes, change also in python capture file
 reduceCurveBySpeed <- function(startT, rawdata, smoothing) {
 	a=rawdata
 	speed <- smooth.spline( 1:length(a), a, spar=smoothing) 
 	b=extrema(speed$y)
-	xmin=1
-	for(i in b$cross[,2]) 		{ if(i < length(a)-20) { xmin = i; } } #left adjust
-#	for(i in rev(b$cross[,2])) 	{ if(i > xtop2) { xmax = i; } } #right adjust
-#	return (data.frame(ini=xmin, end=xmax))	#return data related to all pics (initial timer count)
-	return(xmin+startT)
+
+	#find the b$cross at left of max speed
+	x.ini=1
+	maxSpeedT <- min(which(speed$y == max(speed$y)))
+	for(i in b$cross[,2]) 		{ if(i < maxSpeedT) { x.ini = i } } #left adjust
+
+	return(startT+x.ini)
 }
 
 #go here with every single jump
@@ -86,7 +89,7 @@ powerBars <- function(kinematics) {
 	maxSpeed <- max(abs(kinematics$speedy))
 	meanPower <- mean(abs(kinematics$power))
 	peakPower <- max(kinematics$power)
-	peakPowerT <- which(kinematics$power == peakPower)
+	peakPowerT <- min(which(kinematics$power == peakPower))
 	return(data.frame(meanSpeed, maxSpeed, meanPower,peakPower,peakPowerT))
 }
 
@@ -299,10 +302,6 @@ paint <- function(rawdata, eccon, xmin, xmax, yrange, knRanges, superpose, highl
 
 paintPowerPeakPowerBars <- function(paf) {
 	pafColors=c("tomato1","tomato4",topo.colors(10)[3])
-
-
-print(paf[,3])
-
 	bp <- barplot(rbind(paf[,3],paf[,4]),beside=T,col=pafColors[1:2],width=c(1.4,.6),
 			names.arg=1:n,xlim=c(1,n*3+.5),xlab="",ylab="Power (W)")
 	par(new=T)
@@ -365,16 +364,18 @@ if(length(args) < 3) {
 		par(mar=c(2,2.5,1,1))
 	}
 	curves=findCurves(rawdata, eccon, minHeight, curvesPlot)
+	print(curves)
 	n=length(curves[,1])
 	for(i in 1:n) { 
 		if(eccon=="c") 
 			curves[i,1]=reduceCurveBySpeed(curves[i,1],rawdata[curves[i,1]:curves[i,2]], smoothingAll)
 	}
 	if(curvesPlot) {
-		arrows(x0=curves[,1],y0=min(rawdata.cumsum),x1=curves[,2],y1=min(rawdata.cumsum),
+		#/10 mm -> cm
+		arrows(x0=curves[,1],y0=min(rawdata.cumsum)/10,x1=curves[,2],y1=min(rawdata.cumsum)/10,
 				col="blue",code=3,length=0.1)
 		for(i in 1:length(curves[,1])) 
-			text(x=(curves[i,1]+curves[i,2])/2,y=min(rawdata.cumsum),labels=i, adj=c(0.5,0),cex=1,col="blue")
+			text(x=(curves[i,1]+curves[i,2])/2,y=min(rawdata.cumsum)/10,labels=i, adj=c(0.5,0),cex=1,col="blue")
 	}
 
 	print(curves)
@@ -426,12 +427,10 @@ if(length(args) < 3) {
 		}
 		if(analysis=="powerBars") {
 			paintPowerPeakPowerBars(paf)
-			#print(paf)
 		} 
 		if(analysis=="curves") {
 			paf=cbind(curves[,2]-curves[,1],rawdata.cumsum[curves[,2]]-curves[,3],paf)
 			colnames(paf)=c("width","height","meanSpeed","maxSpeed","meanPower","peakPower","peakPowerT")
-			#print(paf)
 			write.csv(paf, outputData1, quote=FALSE)
 		}
 	}
diff --git a/encoder/pyserial_pyper.py b/encoder/pyserial_pyper.py
index b816d73..2a60539 100644
--- a/encoder/pyserial_pyper.py
+++ b/encoder/pyserial_pyper.py
@@ -24,14 +24,19 @@ from pyper import *
 
 print(sys.argv)
 
+FALSE = 0
+TRUE = 1
+
 # ============
 # = Variable =
 # ============
 outputFile = sys.argv[1]
 record_time = int(sys.argv[2])*1000		#from s to ms
-mass = float(sys.argv[3])
-smoothingOne = float(sys.argv[4])
-eccon = sys.argv[5]				#contraction "ec" or "c"
+minHeight = int(sys.argv[3])			#all is stored, but only display when vertical range is >= minHeight
+isJump = sys.argv[4]
+mass = float(sys.argv[5])
+smoothingOne = float(sys.argv[6])
+eccon = sys.argv[7]				#contraction "ec" or "c"
 
 delete_initial_time = 20			#delete first records because there's encoder bug
 #w_baudrate = 9600                           # Setting the baudrate of Chronopic(9600)
@@ -72,8 +77,6 @@ def cumulative_sum(n):
 	return cum_sum
 
 #https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Prompt_escapes
-FALSE = 0
-TRUE = 1
 
 BLACK = 30
 RED = 31
@@ -93,7 +96,7 @@ def colorize(text, color, bold):
 	return ESCAPE + (FORMAT % (color, )) + text + RESET
 
 
-def calculate_all_in_r(temp, top_values, bottom_values, direction_now, smoothingOne, eccon):
+def calculate_all_in_r(temp, top_values, bottom_values, direction_now, smoothingOne, eccon, minHeight, isJump):
 	if (len(top_values)>0 and len(bottom_values)>0):
 		if direction_now == 1:
 			start=top_values[len(top_values)-1]
@@ -101,14 +104,37 @@ def calculate_all_in_r(temp, top_values, bottom_values, direction_now, smoothing
 		else:
 			start=bottom_values[len(bottom_values)-1]
 			end=top_values[len(top_values)-1]
+		
 		myR.assign('smoothingOne',smoothingOne)
 		myR.assign('a',temp[start:end])
+		
+		if direction_now == -1:
+			myR.run('speed <- smooth.spline( 1:length(a), a, spar=smoothingOne)')
+
+			#reduce curve by speed, the same way as graph.R
+			myR.run('b=extrema(speed$y)')
+			myR.run('maxSpeedT <- min(which(speed$y == max(speed$y)))')
+			maxSpeedT = myR.get('maxSpeedT')
+			bcrossLen = myR.get('length(b$cross[,2])')
+			bcross = myR.get('b$cross[,2]')
+			if bcrossLen == 1:
+				x_ini = bcrossLen
+			else:
+				for i in bcross:
+					if i < maxSpeedT:
+						x_ini = i  #left adjust
+			
+			myR.assign('a',temp[start+x_ini:end])
+	
+		myR.run('speed <- smooth.spline( 1:length(a), a, spar=smoothingOne)')
 		myR.run('a.cumsum <- cumsum(a)')
 		myR.run('range <- abs(a.cumsum[length(a)]-a.cumsum[1])')
-		myR.run('speed <- smooth.spline( 1:length(a), a, spar=smoothingOne)')
 		myR.run('accel <- predict( speed, deriv=1 )')
 		myR.run('accel$y <- accel$y * 1000') #input data is in mm, conversion to m
-		myR.run('force <- mass*(accel$y+9.81)')
+		if isJump == "True":
+			myR.run('force <- mass*(accel$y+9.81)')
+		else:
+			myR.run('force <- mass*accel$y')
 		myR.run('power <- force*speed$y')
 		myR.run('meanPower <- mean(abs(power))')
 		myR.run('peakPower <- max(power)')
@@ -133,9 +159,12 @@ def calculate_all_in_r(temp, top_values, bottom_values, direction_now, smoothing
 		else: colSpeed = GREEN
 		if(meanPower > 3500): colPower = REDINV
 		else: colPower = RED
+
+		phasRange = phaseRange / 10 #from cm to mm
 		
 		if eccon == "ec" or direction_now == -1:
-			print phaseCol + "%6i," % phaseRange + colorize(meanSpeedCol,colSpeed,TRUE) + "%9.2f," % maxSpeed + colorize(meanPowerCol,colPower,TRUE) + "%10.2f," % peakPower + "%11i" % peakPowerT
+			if phaseRange >= minHeight:
+				print phaseCol + "%6i," % phaseRange + colorize(meanSpeedCol,colSpeed,TRUE) + "%9.2f," % maxSpeed + colorize(meanPowerCol,colPower,TRUE) + "%10.2f," % peakPower + "%11i" % peakPowerT
 
 
 def calculate_range(temp_cumsum, top_values, bottom_values, direction_now):
@@ -167,7 +196,7 @@ def calculate_range(temp_cumsum, top_values, bottom_values, direction_now):
 if __name__ == '__main__':
 	#print "connecting with R"
 	myR = R()
-	#myR.run('library("EMD")') #cal per extrema, pero no per splines
+	myR.run('library("EMD")') #needed on reducing curve by speed (extrema)
 	myR.assign('mass',mass)
 	myR.run('weight=mass*9.81')
 	myR.assign('k',2)
@@ -220,7 +249,6 @@ if __name__ == '__main__':
 			direction_change_count = direction_change_count + 1
 			if direction_change_count >= direction_change_period:
 
-				#k=cumulative_sum(temp[previous_frame_change:i-direction_change_period])
 				k=list(temp_cumsum[previous_frame_change:i-direction_change_period])
 	
 				phase = 0
@@ -235,6 +263,8 @@ if __name__ == '__main__':
 					new_frame_change = previous_frame_change+len(k)-1-k[::-1].index(min(k))
 					frames_push_bottom2.append(new_frame_change)
 					phase = " down"
+					#print previous_frame_change
+					#print new_frame_change
 					speed = min(temp_speed[previous_frame_change:new_frame_change])
 				else:
 					new_frame_change = previous_frame_change+k.index(max(k))
@@ -248,7 +278,7 @@ if __name__ == '__main__':
 
 				if len(frames_pull_top1)>0 and len(frames_push_bottom1)>0:
 					calculate_all_in_r(temp, frames_pull_top1, frames_push_bottom1, 
-							direction_now, smoothingOne, eccon)
+							direction_now, smoothingOne, eccon, minHeight, isJump)
 	
 				file.write(''+','.join([str(i) for i in temp[
 					previous_frame_change:new_frame_change
diff --git a/src/encoder.cs b/src/encoder.cs
index 0df7324..097faca 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -37,9 +37,11 @@ public class EncoderParams
 	private int width;
 	private int height;
 	
-	public EncoderParams(int time, string mass, string smooth, string eccon)
+	public EncoderParams(int time, int minHeight, bool isJump, string mass, string smooth, string eccon)
 	{
 		this.time = time;
+		this.minHeight = minHeight;
+		this.isJump = isJump;
 		this.mass = mass;
 		this.smooth = smooth;
 		this.eccon = eccon;
@@ -47,7 +49,8 @@ public class EncoderParams
 	
 	public string ToString1 () 
 	{
-		return time.ToString() + " " + mass.ToString() + " " + smooth + " " + eccon;
+		return time.ToString() + " " + minHeight.ToString() + " " + isJump.ToString() + 
+			" " + mass.ToString() + " " + smooth + " " + eccon;
 	}
 	
 	public EncoderParams(int minHeight, bool isJump, string mass, string eccon, 
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index f77fd98..d52a238 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -87,6 +87,8 @@ public partial class ChronoJumpWindow
 		//capture data
 		EncoderParams ep = new EncoderParams(
 				(int) spin_encoder_capture_time.Value, 
+				(int) spin_encoder_capture_min_height.Value, 
+				!radiobutton_encoder_capture_bar.Active,
 				findMass(),
 				Util.ConvertToPoint((double) spin_encoder_smooth.Value), //R decimal: '.'
 				findEccon()
@@ -117,7 +119,7 @@ public partial class ChronoJumpWindow
 
 		EncoderParams ep = new EncoderParams(
 				(int) spin_encoder_capture_min_height.Value, 
-				false,			//isJump (1st) is not used in "curves"
+				!radiobutton_encoder_capture_bar.Active,
 				findMass(),
 				findEccon(), "curves",
 				Util.ConvertToPoint((double) spin_encoder_smooth.Value), //R decimal: '.'



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]