Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/wear_carfi'
Browse files Browse the repository at this point in the history
  • Loading branch information
roncapat committed Apr 27, 2019
2 parents 1a2d82a + 0cb1da0 commit fd9cbba
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
43 changes: 35 additions & 8 deletions imu_wear/android_wear_pub/res/layout/activity_sending.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,75 @@
tools:deviceIds="wear">

<TextView
android:id="@+id/sending_data"
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:text="@string/sending_data" />

<Button
android:id="@+id/stopButton"
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_below="@id/lin"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:backgroundTint="@color/red"
android:onClick="stopStreaming"
android:text="@string/stop_button" />
android:onClick="pauseStreaming"
android:text="@string/pause" />



<LinearLayout
android:id="@+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@id/lin">

<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="@color/green"
android:onClick="resumeStreaming"
android:text="@string/play" />

<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="@color/red"
android:onClick="stopStreaming"
android:text="@string/stop_button" />
</LinearLayout>

<LinearLayout
android:id="@+id/lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="@id/sending_data">
android:layout_below="@id/status">

<EditText
android:id="@+id/frequency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sending_data"
android:ems="2"
android:inputType="number"
android:text="50"
android:text="@string/default_frequency"
android:textAlignment="center" />

<TextView
android:id="@+id/Hz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/frequency"
android:text="@string/hz" />

</LinearLayout>
Expand Down
3 changes: 3 additions & 0 deletions imu_wear/android_wear_pub/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<string name="title_activity_sending">SendingActivity</string>
<string name="sending_data">Sending data...</string>
<string name="hz">Hz</string>
<string name="pause">Pause</string>
<string name="play">Play</string>
<string name="default_frequency">50</string>
<string name="stop_button">Stop</string>
<string name="acc_data">Accelerometer:</string>
<string name="data_streamed">data streamed</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

public class ImuPublisher extends AbstractNodeMain {

public boolean sending = true;
public Semaphore semaphore = new Semaphore(1);
public float[] acc = new float[3];
public float[] vel = new float[3];
Expand Down Expand Up @@ -42,21 +43,28 @@ protected void setup() {
}

protected void loop() throws InterruptedException {
msg.getHeader().getStamp().nsecs = connectedNode.getCurrentTime().nsecs;
msg.getHeader().getStamp().secs = connectedNode.getCurrentTime().secs;
msg.getHeader().setFrameId(android_time+"");
if (sending) {
long ros_time_secs = connectedNode.getCurrentTime().secs;
long ros_time_nsecs = connectedNode.getCurrentTime().nsecs + (android_time - System.currentTimeMillis())*1000000L;

msg.getLinearAcceleration().setX(acc[0]);
msg.getLinearAcceleration().setY(acc[1]);
msg.getLinearAcceleration().setZ(acc[2]);
msg.getHeader().getStamp().nsecs = (int)ros_time_nsecs;
msg.getHeader().getStamp().secs = (int)ros_time_secs;
msg.getHeader().setFrameId(android_time + "");

msg.getAngularVelocity().setX(vel[0]);
msg.getAngularVelocity().setY(vel[1]);
msg.getAngularVelocity().setZ(vel[2]);
msg.getLinearAcceleration().setX(acc[0]);
msg.getLinearAcceleration().setY(acc[1]);
msg.getLinearAcceleration().setZ(acc[2]);

publisher.publish(msg);
msg.getAngularVelocity().setX(vel[0]);
msg.getAngularVelocity().setY(vel[1]);
msg.getAngularVelocity().setZ(vel[2]);

Thread.sleep(1000/frequency);
publisher.publish(msg);

Thread.sleep(1000 / frequency);
}else {
Thread.sleep(500);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

Expand All @@ -24,6 +25,8 @@ public class SendingWearActivity extends RosWearActivity implements SensorEventL
String deviceName = myDevice.getName().replaceAll(" ", "_");
private ImuPublisher pub = new ImuPublisher(deviceName+"/imu_data");
private EditText frequency;
private TextView status;
private Button pauseButton, stopButton, playButton;
private SensorManager senSensorManager;

public SendingWearActivity() {
Expand Down Expand Up @@ -56,6 +59,17 @@ public void afterTextChanged(Editable s) {
}
}
});

status = findViewById(R.id.status);

pauseButton = findViewById(R.id.pauseButton);
pauseButton.setVisibility(View.VISIBLE);

stopButton = findViewById(R.id.stopButton);
stopButton.setVisibility(View.GONE);

playButton = findViewById(R.id.playButton);
playButton.setVisibility(View.GONE);
}

/**
Expand All @@ -67,12 +81,12 @@ public void onSensorChanged(SensorEvent sensorEvent) {

if (mySensor.getType() == Sensor.TYPE_GYROSCOPE) {
System.arraycopy(sensorEvent.values, 0, pub.vel, 0, 3);
pub.android_time = sensorEvent.timestamp;
pub.android_time = System.currentTimeMillis() + (sensorEvent.timestamp - System.nanoTime()) / 1000000L;
}

if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
System.arraycopy(sensorEvent.values, 0, pub.acc, 0, 3);
pub.android_time = sensorEvent.timestamp;
pub.android_time = System.currentTimeMillis() + (sensorEvent.timestamp - System.nanoTime()) / 1000000L;
}
}

Expand All @@ -97,6 +111,24 @@ protected void init(NodeMainExecutor nodeMainExecutor) {
senSensorManager.registerListener(this, senGyroscope, SensorManager.SENSOR_DELAY_FASTEST);
}

public void pauseStreaming(View view) {
pub.sending = false;
status.setText("Pause");

pauseButton.setVisibility(View.GONE);
stopButton.setVisibility(View.VISIBLE);
playButton.setVisibility(View.VISIBLE);
}

public void resumeStreaming(View view){
pub.sending = true;
status.setText(getString(R.string.sending_data));

pauseButton.setVisibility(View.VISIBLE);
stopButton.setVisibility(View.GONE);
playButton.setVisibility(View.GONE);
}

public void stopStreaming(View view) {
senSensorManager.unregisterListener(this);
// Exit the app if Stop is pressed
Expand Down

0 comments on commit fd9cbba

Please sign in to comment.