1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14:52:12 +01:00

AndroidGCS: Check in a FragmentTester class and add it to the home page.

Use it to the beginnings of the MapPosition fragment.
This commit is contained in:
James Cotton 2012-08-08 16:24:30 -05:00
parent f530702ce9
commit fcb3e8c152
7 changed files with 330 additions and 2 deletions

View File

@ -33,6 +33,7 @@
android:theme="@android:style/Theme.Dialog" />
<activity android:name="Logger" android:label="Logger"
android:theme="@android:style/Theme.Dialog" />
<activity android:name="FragmentTester" android:label="FragmentTester" />
<receiver android:name="TelemetryWidget">
<intent-filter>

View File

@ -65,7 +65,7 @@
android:drawableTop="@drawable/ic_logging"
android:text="@string/logger_name" />
<Button
<Button
android:id="@+id/launch_alarms"
android:layout_width="132dp"
android:layout_height="wrap_content"
@ -76,7 +76,19 @@
android:background="@android:color/transparent"
android:drawableTop="@drawable/ic_alarms"
android:text="@string/alarms" />
<Button
android:id="@+id/launch_tester"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="right"
android:layout_row="6"
android:layout_rowSpan="2"
android:background="@android:color/transparent"
android:drawableTop="@drawable/ic_alarms"
android:text="@string/tester" />
<Space
android:layout_width="1dp"
android:layout_height="32dp"

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SeekBar
android:id="@+id/altitude_slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="50" />
<FrameLayout
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/map_positioner_background" >
</FrameLayout>
</LinearLayout>

View File

@ -32,4 +32,5 @@
<string name="alarms">Alarms</string>
<string name="txrate">TxRate: </string>
<string name="rxrate">RxRate: </string>
<string name="tester">Tester</string>
</resources>

View File

@ -0,0 +1,57 @@
/**
******************************************************************************
* @file FragmentTester.java
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
* @brief An activity that displays the SystemAlarmsFragment.
* @see The GNU Public License (GPL) Version 3
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.openpilot.androidgcs;
import org.openpilot.androidgcs.fragments.MapPositioner;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
public class FragmentTester extends ObjectManagerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(createUI());
}
private View createUI() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.MATCH_PARENT));
layout.setId(0x101);
{
FragmentTransaction fragmentTransaction = getFragmentManager()
.beginTransaction();
fragmentTransaction.add(0x101, new MapPositioner());
fragmentTransaction.commit();
}
return layout;
}
}

View File

@ -83,6 +83,14 @@ public class HomePage extends ObjectManagerActivity {
startActivity(new Intent(HomePage.this, SystemAlarmActivity.class));
}
});
Button tester = (Button) findViewById(R.id.launch_tester);
tester.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(HomePage.this, FragmentTester.class));
}
});
}
}

View File

@ -0,0 +1,229 @@
/**
******************************************************************************
* @file MapPositioner.java
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
* @brief A fragmenet to move the UAV around by dragging
* @see The GNU Public License (GPL) Version 3
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.openpilot.androidgcs.fragments;
import org.openpilot.androidgcs.R;
import org.openpilot.uavtalk.UAVObject;
import org.openpilot.uavtalk.UAVObjectManager;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.SeekBar;
public class MapPositioner extends ObjectManagerFragment {
private static final String TAG = ObjectManagerFragment.class
.getSimpleName();
private static final int LOGLEVEL = 0;
private static final boolean DEBUG = LOGLEVEL > 0;
private final float MAX_DISTANCE_M = 50.0f;
float px_to_m(View v, float px) {
final float MAX_DISTANCE_PX = Math.max(v.getMeasuredHeight(),
v.getMeasuredWidth());
float scale = MAX_DISTANCE_M / MAX_DISTANCE_PX;
return px * scale;
}
float m_to_px(View v, float m) {
final float MAX_DISTANCE_PX = Math.max(v.getMeasuredHeight(),
v.getMeasuredWidth());
float scale = MAX_DISTANCE_M / MAX_DISTANCE_PX;
return m / scale;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
return inflater.inflate(R.layout.map_positioner, container, false);
}
private float pos_x = 0, pos_y = 0;
private float desired_x = 0, desired_y = 0, desired_z = 0;
//private final float poi_x = 1, poi_y = 1;
private UAVOverlay uav = null;
public class UAVOverlay extends View {
Paint paint = null;
public UAVOverlay(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
}
private void draw(Canvas canvas, int resource, float x, float y) {
Drawable icon = getContext().getResources().getDrawable(resource);
final int offset_x = canvas.getWidth() / 2;
final int offset_y = canvas.getHeight() / 2;
final int SIZE = 50 / 2;
icon.setBounds((int) x - SIZE + offset_x, (int) y - SIZE + offset_y,
(int) x + SIZE + offset_x, (int) y + SIZE + offset_y);
icon.draw(canvas);
}
@Override
/**
* Draw the UAV, the position desired, and the camera POI
*/
protected void onDraw(Canvas canvas) {
draw(canvas, R.drawable.map_positioner_uav, (int) m_to_px(this, pos_x), (int) m_to_px(this, pos_y));
draw(canvas, R.drawable.map_positioner_waypoint, (int) m_to_px(this, desired_x), (int) -m_to_px(this, desired_y));
//draw(canvas, R.drawable.map_positioner_poi, (int) m_to_px(this, poi_x), (int) -m_to_px(this, poi_y));
// Annotate the desired position
canvas.drawText("(" + String.format("%.1f",desired_x) + "," + String.format("%.1f",desired_y) + "," + String.format("%.1f",desired_z) + ")",
m_to_px(this, desired_x) + canvas.getWidth() / 2 + 25,
-m_to_px(this, desired_y) + canvas.getHeight() / 2, paint);
}
};
public void connectTouch() {
if (DEBUG)
Log.d(TAG, "Connected touch listener()");
View v = getActivity().findViewById(R.id.map_view);
if (v != null)
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
UAVObject desired = objMngr.getObject("PathDesired");
if (desired != null) {
if (DEBUG) Log.d(TAG, "Updating path desired");
desired.getField("End").setDouble(px_to_m(v, (int) event.getX() - v.getMeasuredWidth() / 2), 1);
desired.getField("End").setDouble(px_to_m(v, -(event.getY() - v.getMeasuredHeight() / 2)), 0);
desired.updated();
if (uav != null)
uav.invalidate();
}
break;
}
return true;
}
});
else
if (DEBUG)
Log.d(TAG, "No view");
FrameLayout f = (FrameLayout) getActivity().findViewById(R.id.map_view);
uav = new UAVOverlay(getActivity().getBaseContext());
f.addView(uav);
}
@Override
public void onStart() {
super.onStart();
if (DEBUG)
Log.d(TAG, "onStart()");
connectTouch();
SeekBar altitudeBar = (SeekBar) getActivity().findViewById(R.id.altitude_slider);
if(altitudeBar != null)
altitudeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
UAVObject desired = objMngr.getObject("PathDesired");
if (desired != null) {
if (DEBUG) Log.d(TAG, "Updating path desired");
desired.getField("End").setDouble(-progress, 2);
desired.updated();
if (uav != null)
uav.invalidate();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onOPConnected(UAVObjectManager objMngr) {
super.onOPConnected(objMngr);
if (DEBUG)
Log.d(TAG, "On connected");
UAVObject obj = objMngr.getObject("PositionActual");
if (obj != null)
registerObjectUpdates(obj);
objectUpdated(obj);
obj = objMngr.getObject("PathDesired");
if (obj != null)
registerObjectUpdates(obj);
objectUpdated(obj);
}
/**
* Called whenever any objects subscribed to via registerObjects. Store the
* local copy of the information.
*/
@Override
public void objectUpdated(UAVObject obj) {
if (obj.getName().compareTo("PositionActual") == 0) {
pos_x = (int) obj.getField("East").getDouble();
pos_y = (int) obj.getField("North").getDouble();
uav.invalidate();
}
if (obj.getName().compareTo("PathDesired") == 0) {
desired_x = (float) obj.getField("End").getDouble(1);
desired_y = (float) obj.getField("End").getDouble(0);
desired_z = (float) obj.getField("End").getDouble(2);
uav.invalidate();
}
}
}