android - Data from accelerometer to achartengine in realtime -
i'm new android , still playing it. however, ran problem , got stuck. i'm trying take data accelerometer , plot using achartengine in real time. found tutorial first saves data accelerometer , plots it. though can run on emulator, once deploy on nexus 4 running jelly bean, application crashes. can't seem figure out why. have suggestions?
public class mainactivity extends activity implements sensoreventlistener,onclicklistener { private button butstart, butstop; private linearlayout layout; private boolean started = false; private sensormanager sensormanager; private static graphicalview view; private linechart line = new linechart(); private long t; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); layout = (linearlayout) findviewbyid(r.id.chart_container); sensormanager = (sensormanager) getsystemservice(context.sensor_service); new arraylist<accelemoterdata>(); butstart = (button) findviewbyid(r.id.butstart); butstop = (button) findviewbyid(r.id.butstop); butstart.setonclicklistener(this); butstop.setonclicklistener(this); butstart.setenabled(true); butstop.setenabled(false); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onaccuracychanged(sensor sensor, int accuracy) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent event) { if (started) { double x = event.values[0]; double y = event.values[1]; double z = event.values[2]; long time = system.currenttimemillis(); accelemoterdata data = new accelemoterdata(time, x, y, z); line.addnewdata(data, t); view.repaint(); } } @override public void onclick(view v) { switch (v.getid()) { case r.id.butstart: view = line.getview(this); setcontentview(view); butstop.setenabled(true); butstart.setenabled(false); new arraylist<accelemoterdata>(); started = true; t = system.currenttimemillis(); sensor accelerometer = sensormanager.getdefaultsensor(sensor.type_accelerometer); sensormanager.registerlistener(this, accelerometer,sensormanager.sensor_delay_fastest); break; case r.id.butstop: butstop.setenabled(false); butstart.setenabled(true); started = false; sensormanager.unregisterlistener(this); layout.removeallviews(); break; default: break; } }
}
public class linechart { private graphicalview view; private timeseries xseries = new timeseries("x"); private timeseries yseries = new timeseries("y"); private timeseries zseries = new timeseries("z"); private xymultipleseriesdataset dataset = new xymultipleseriesdataset(); xyseriesrenderer xrenderer = new xyseriesrenderer(); xyseriesrenderer yrenderer = new xyseriesrenderer(); xyseriesrenderer zrenderer = new xyseriesrenderer(); xymultipleseriesrenderer multirenderer = new xymultipleseriesrenderer(); public linechart() { dataset.addseries(xseries); dataset.addseries(yseries); dataset.addseries(zseries); xrenderer.setcolor(color.red); xrenderer.setpointstyle(pointstyle.circle); xrenderer.setfillpoints(true); xrenderer.setlinewidth(1); xrenderer.setdisplaychartvalues(false); yrenderer.setcolor(color.green); yrenderer.setpointstyle(pointstyle.circle); yrenderer.setfillpoints(true); yrenderer.setlinewidth(1); yrenderer.setdisplaychartvalues(false); zrenderer.setcolor(color.blue); zrenderer.setpointstyle(pointstyle.circle); zrenderer.setfillpoints(true); zrenderer.setlinewidth(1); zrenderer.setdisplaychartvalues(false); multirenderer.setxlabels(0); multirenderer.setlabelscolor(color.red); multirenderer.setcharttitle("t vs (x,y,z)"); multirenderer.setxtitle("sensor data"); multirenderer.setytitle("values of acceleration"); multirenderer.setzoombuttonsvisible(true); multirenderer.addseriesrenderer(xrenderer); multirenderer.addseriesrenderer(yrenderer); multirenderer.addseriesrenderer(zrenderer); } public graphicalview getview(context context) { view = chartfactory.getlinechartview(context, dataset, multirenderer); return view; } public void addnewdata(accelemoterdata data, long t) { xseries.add(data.gettime()-t, data.getx()); yseries.add(data.gettime()-t, data.gety()); zseries.add(data.gettime()-t, data.getz()); }
}
of course need class accelerometerdata, copy , paste code , write class accelerometerdata using criteria, , works me. except, changed this:
line.addnewdata(data, time);
in main activity.
and class accelerometerdata (well in case changed name because spanish), change name accelerometerdata , should work.
public class datosacelerometro { private long time; private float x; private float y; private float z; public datosacelerometro(long time, float x, float y,float z) { this.time=time; this.x = x; this.y = y; this.z = z; } public long gettime(){ return time; } public float getx() { return x; } public float gety() { return y; } public float getz(){ return z; }
}
i hope fix unknowing error. ;)
Comments
Post a Comment