Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/src/main/java/io/pslab/activity/DataLoggerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -40,6 +41,7 @@
import io.pslab.models.GyroData;
import io.pslab.models.LuxData;
import io.pslab.models.SensorDataBlock;
import io.pslab.models.ServoData;
import io.pslab.models.ThermometerData;
import io.pslab.others.CSVLogger;
import io.pslab.others.LocalDataLog;
Expand Down Expand Up @@ -105,6 +107,8 @@ protected void onCreate(Bundle savedInstanceState) {
case "Thermometer":
categoryData = LocalDataLog.with().getTypeOfSensorBlocks(getString(R.string.thermometer));
break;
case "Robotic Arm":
categoryData = LocalDataLog.with().getTypeOfSensorBlocks(getString(R.string.robotic_arm));
default:
categoryData = LocalDataLog.with().getAllSensorBlocks();
getSupportActionBar().setTitle(getString(R.string.logged_data));
Expand Down Expand Up @@ -450,6 +454,41 @@ private void getFileData(File file) {
} catch (IOException e) {
e.printStackTrace();
}
} else if (selectedDevice != null && selectedDevice.equals(getResources().getString(R.string.robotic_arm))) {
try {
FileInputStream is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
int i = 0;
long block = 0, time = 0;
while (line != null) {
if (i != 0) {
String[] data = line.split(",");
try {
time += 1000;
ServoData servoData = new ServoData(time, block, data[2], data[3], data[4], data[5], Float.valueOf(data[6]), Float.valueOf(data[7]));
realm.beginTransaction();
realm.copyToRealm(servoData);
realm.commitTransaction();
} catch (Exception e) {
Log.d("exception", i + " " + e.getMessage());
Toast.makeText(this, getResources().getString(R.string.incorrect_import_format), Toast.LENGTH_SHORT).show();
}
} else {
block = System.currentTimeMillis();
time = block;
realm.beginTransaction();
realm.copyToRealm(new SensorDataBlock(block, getResources().getString(R.string.robotic_arm)));
realm.commitTransaction();
}
i++;
line = reader.readLine();
}
fillData();
DataLoggerActivity.this.toolbar.getMenu().findItem(R.id.delete_all).setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
108 changes: 81 additions & 27 deletions app/src/main/java/io/pslab/activity/RoboticArmActivity.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package io.pslab.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
Expand All @@ -26,9 +26,19 @@

import com.triggertrap.seekarc.SeekArc;

import java.util.ArrayList;
import java.util.Date;

import io.pslab.R;
import io.pslab.models.SensorDataBlock;
import io.pslab.models.ServoData;
import io.pslab.others.CSVLogger;
import io.pslab.others.CustomSnackBar;
import io.pslab.others.GPSLogger;
import io.pslab.others.LocalDataLog;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;

public class RoboticArmActivity extends AppCompatActivity {

Expand All @@ -42,6 +52,11 @@ public class RoboticArmActivity extends AppCompatActivity {
private CountDownTimer timeLine;
private boolean isPlaying = false;
private CSVLogger servoCSVLogger;
private Realm realm;
private GPSLogger gpsLogger;
private RealmResults<ServoData> recordedServoData;
private final String KEY_LOG = "has_log";
private final String DATA_BLOCK = "data_block";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -53,6 +68,9 @@ protected void onCreate(Bundle savedInstanceState) {
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;
realm = LocalDataLog.with().getRealm();
gpsLogger = new GPSLogger(this,
(LocationManager) getSystemService(Context.LOCATION_SERVICE));

View servo1Layout = findViewById(R.id.servo_1);
View servo2Layout = findViewById(R.id.servo_2);
Expand Down Expand Up @@ -337,9 +355,9 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
@Override
public void onTick(long millisUntilFinished) {
timeIndicatorParams.setMarginStart(timeIndicatorParams
.getMarginStart()+screen_width/6);
.getMarginStart() + screen_width / 6);
timeIndicatorLayout.setLayoutParams(timeIndicatorParams);
scrollView.smoothScrollBy(screen_width/6,0);
scrollView.smoothScrollBy(screen_width / 6, 0);
}

@Override
Expand All @@ -356,7 +374,7 @@ public void onClick(View v) {
isPlaying = false;
playPauseButton.setBackground(getResources().getDrawable(R.drawable.ic_play_arrow_white_24dp));
timeLine.onFinish();
}else {
} else {
isPlaying = true;
playPauseButton.setBackground(getResources().getDrawable(R.drawable.ic_pause_white_24dp));
timeLine.start();
Expand All @@ -380,46 +398,70 @@ public void onClick(View v) {
scrollView.fullScroll(HorizontalScrollView.FOCUS_LEFT);
}
});

if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean(KEY_LOG)) {
recordedServoData = LocalDataLog.with()
.getBlockOfServoRecords(getIntent().getExtras().getLong(DATA_BLOCK));
setReceivedData();
}
}

private void setReceivedData() {
ArrayList servoDataList = new ArrayList(recordedServoData);
for (int i = 0; i < servoDataList.size(); i++) {
ServoData servoData = (ServoData) servoDataList.get(i);
((TextView) servo1TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).setText(servoData.getDegree1());
((TextView) servo2TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).setText(servoData.getDegree2());
((TextView) servo3TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).setText(servoData.getDegree3());
((TextView) servo4TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).setText(servoData.getDegree4());
}
}

private void saveTimeline() {
long block = System.currentTimeMillis();
servoCSVLogger.prepareLogFile();
String data = "Servo1,Servo2,Servo3,Servo4\n";
String data = "Timestamp,DateTime,Servo1,Servo2,Servo3,Servo4,Latitude,Longitude\n";
long timestamp;
recordSensorDataBlockID(new SensorDataBlock(block, getString(R.string.robotic_arm)));
String degree1, degree2, degree3, degree4;
for (int i = 0; i < 60; i ++) {
double lat, lon;
for (int i = 0; i < 60; i++) {
timestamp = System.currentTimeMillis();
degree1 = degree2 = degree3 = degree4 = "0";
if (((TextView)servo1TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
if (((TextView) servo1TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
degree1 = ((TextView) servo1TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().toString();
}
if (((TextView)servo2TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
if (((TextView) servo2TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
degree2 = ((TextView) servo2TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().toString();
}
if (((TextView)servo3TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
if (((TextView) servo3TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
degree3 = ((TextView) servo3TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().toString();
}
if (((TextView)servo4TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
if (((TextView) servo4TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().length() > 0) {
degree4 = ((TextView) servo4TimeLine.getChildAt(i).findViewById(R.id.timeline_box_degree_text)).getText().toString();
}
data += degree1 + "," + degree2 + "," + degree3 + "," + degree4 + "\n";
if (gpsLogger.isGPSEnabled()) {
Location location = gpsLogger.getDeviceLocation();
lat = location.getLatitude();
lon = location.getLongitude();
} else {
lat = lon = 0.0;
}
recordSensorData(new ServoData(timestamp, block, degree1, degree2, degree3, degree4, lat, lon));
if (i == 59) {
data += timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp)) + "," + degree1 + "," + degree2 + "," + degree3 + "," + degree4 + "," + lat + "," + lon;
} else {
data += timestamp + "," + CSVLogger.FILE_NAME_FORMAT.format(new Date(timestamp)) + "," + degree1 + "," + degree2 + "," + degree3 + "," + degree4 + "," + lat + "," + lon + "\n";
}
}
servoCSVLogger.writeCSVFile(data);
CustomSnackBar.showSnackBar(findViewById(R.id.robotic_arm_relative_view),
getString(R.string.csv_store_text) + " " + servoCSVLogger.getCurrentFilePath()
, getString(R.string.delete_capital), new View.OnClickListener() {
, getString(R.string.open), new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(RoboticArmActivity.this, R.style.AlertDialogStyle)
.setTitle(R.string.delete_file)
.setMessage(R.string.delete_warning)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
servoCSVLogger.deleteFile();
}
})
.setNegativeButton(R.string.cancel, null)
.create()
.show();
Intent intent = new Intent(RoboticArmActivity.this, DataLoggerActivity.class);
startActivity(intent);
}
}, Snackbar.LENGTH_SHORT);
}
Expand Down Expand Up @@ -499,4 +541,16 @@ private void removeStatusBar() {
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY));
}
}

public void recordSensorDataBlockID(SensorDataBlock block) {
realm.beginTransaction();
realm.copyToRealm(block);
realm.commitTransaction();
}

public void recordSensorData(RealmObject sensorData) {
realm.beginTransaction();
realm.copyToRealm((ServoData) sensorData);
realm.commitTransaction();
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/io/pslab/adapters/SensorLoggerListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.pslab.activity.LuxMeterActivity;
import io.pslab.activity.MapsActivity;
import io.pslab.activity.CompassActivity;
import io.pslab.activity.RoboticArmActivity;
import io.pslab.activity.ThermometerActivity;
import io.pslab.models.AccelerometerData;
import io.pslab.models.BaroData;
Expand All @@ -37,6 +38,7 @@
import io.pslab.models.LuxData;
import io.pslab.models.PSLabSensor;
import io.pslab.models.SensorDataBlock;
import io.pslab.models.ServoData;
import io.pslab.models.ThermometerData;
import io.pslab.others.CSVLogger;
import io.pslab.others.LocalDataLog;
Expand Down Expand Up @@ -95,6 +97,10 @@ public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
holder.sensor.setText(R.string.thermometer);
holder.tileIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.thermometer_logo));
break;
case PSLabSensor.ROBOTIC_ARM:
holder.sensor.setText(R.string.robotic_arm);
holder.tileIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.gyroscope_logo));
break;
default:
break;
}
Expand Down Expand Up @@ -150,6 +156,11 @@ private void handleCardViewClick(SensorDataBlock block) {
Thermometer.putExtra(KEY_LOG, true);
Thermometer.putExtra(DATA_BLOCK, block.getBlock());
context.startActivity(Thermometer);
} else if (block.getSensorType().equalsIgnoreCase(context.getResources().getString(R.string.robotic_arm))) {
Intent RoboticArm = new Intent(context, RoboticArmActivity.class);
RoboticArm.putExtra(KEY_LOG, true);
RoboticArm.putExtra(DATA_BLOCK, block.getBlock());
context.startActivity(RoboticArm);
}
}

Expand Down Expand Up @@ -179,6 +190,8 @@ public void onClick(DialogInterface dialog, int whichButton) {
LocalDataLog.with().clearBlockOfCompassRecords(block.getBlock());
} else if (block.getSensorType().equalsIgnoreCase(PSLabSensor.ACCELEROMETER_CONFIGURATIONS)) {
LocalDataLog.with().clearBlockOfAccelerometerRecords(block.getBlock());
} else if (block.getSensorType().equalsIgnoreCase(PSLabSensor.ROBOTIC_ARM)) {
LocalDataLog.with().clearBlockOfServoRecords(block.getBlock());
}
LocalDataLog.with().clearSensorBlock(block.getBlock());
dialog.dismiss();
Expand Down Expand Up @@ -301,6 +314,25 @@ private void populateMapData(SensorDataBlock block) {
}
}
setMapDataToIntent(array);
} else if (block.getSensorType().equalsIgnoreCase(PSLabSensor.ROBOTIC_ARM)) {
RealmResults<ServoData> data = LocalDataLog.with().getBlockOfServoRecords(block.getBlock());
JSONArray array = new JSONArray();
for (ServoData d : data) {
try {
JSONObject i = new JSONObject();
i.put("date", CSVLogger.FILE_NAME_FORMAT.format(d.getTime()));
i.put("Servo1", d.getDegree1());
i.put("Servo2", d.getDegree2());
i.put("Servo3", d.getDegree3());
i.put("Servo4", d.getDegree4());
i.put("lon", d.getLon());
i.put("lat", d.getLat());
if (d.getLat() != 0.0 && d.getLon() != 0.0) array.put(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
setMapDataToIntent(array);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.pslab.interfaces.sensorloggers;

import io.pslab.models.ServoData;
import io.realm.RealmResults;

public interface ServoRecordables {
ServoData getServoData(long timeStamp);

void clearAllServoRecords();

void clearBlockOfServoRecords(long block);

RealmResults<ServoData> getAllServoRecords();

RealmResults<ServoData> getBlockOfServoRecords(long block);
}
1 change: 1 addition & 0 deletions app/src/main/java/io/pslab/models/PSLabSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public abstract class PSLabSensor extends AppCompatActivity {
public static final String THERMOMETER = "Thermometer";
public static final String THERMOMETER_CONFIGURATIONS = "Thermometer Configurations";
public static final String THERMOMETER_DATA_FORMAT = "%.2f";
public static final String ROBOTIC_ARM = "Robotic Arm";

@BindView(R.id.sensor_toolbar)
Toolbar sensorToolBar;
Expand Down
Loading