PIDroneAndroid Documentation - YuKyungKim/GitTest02 GitHub Wiki
PIDroneAndroid는 수평계를 포함하고 있는 PIDrone 조종 안드로이드 어플리케이션이다.
이 문서는 PIDroneAndroid에 대해 기술한 문서이다.
table of contents
- Class PIDroneActivity
- Controller
- Class ControllerActivity
- Class Joystick
- Class DrawCanvas
- Joystick의 pitch, roll, yaw, thr 값의 범위 조정
- Network
- Class PacketVO
- Class TCPThread
- Class UDPThread
- Level
- Class LevelActivity
- Class LevelView
- Class LevelPainter
public class PIDroneActivity
extends Activity (android.app.Activity)
A main activity in this application.
ImageButton btnController
When touch event is triggered, start ControllerActiviy class.
ImageButton btnSetting
When touch event is triggered, start SettingActivity class.
ImageButton btnLevel
When touch event is triggered, start LevelActivity class.
@override protected void onCreate (Bundle savedInstanceState)
Binding with layout and setting click listener 3 image button components(
controller
,setting
,level
).
public class ControllerActivity
extends Activity(android.app.Activity)
A controller activity.
TCP, UDP 통신을 통해 드론을 조종하고, pitch, roll, yaw, thr 등의 값을 실시간으로 확인할 수 있음.
RelativeLayout layoutJoystickL, layoutJoystickR
joystick layout. left and right.
TextView txtPitch, txtRoll, txtThr, txtYaw
Changing values as controlling.
Button btnConncet
TCP와 UDP를 연결하는 버튼.
Joystick joystickL, joystickR
조이스틱 객체. 왼쪽과 오른쪽.
View.OnClickListener btnClickListener
TCPThread mTcpThread
UDPThread mUdpThread
PacketVO mMessage
통신 시 전송하는 데이터. pitch, roll, yaw, thr의 값을 전송.
double pitch
double roll
double yaw
int thr
드론의 출력 세기
double lastThr
boolean isConnectTCP
TCP 소켓 연결이 되어있는지 확인하는 변수. false로 초기화.
boolean isConnectUDP
UDP 소켓 연결이 되어있는지 확인하는 변수. false로 초기화.
@override protected void onCreate (Bundle savedInstanceState)
activiy가 실행되었을 때 실행되는 method.
activity의 layout을 set.
initComponents(), initActionListeners(), bindActionListeners(), joystickSet() 호출.
@Override protected void onStop()
앱이 정지되었을 때 실행되는 method.
closeNetworkThreads()를 호출하여 소켓을 닫는다.
private void initComponents()
TextView, Button component와 layout binding.
private void initActionListeners()
Button component에 click event가 발생했을 때 실행할 액션을 선언해놓은 method.
private void bindActionListeners()
Button component와 ClickListener binding.
private void joystickSet()
왼쪽과 오른쪽 조이스틱을 선언하고, 화면에 그리며, touch event에 따라 pitch, roll, yaw, thr 값을 udp를 통해 PIDrone으로 전송.
터치스크린에서 손가락이 떼어진 경우, pitch, roll, yaw의 조이스틱은 원위치로 돌아오는 반면, thr는 마지막 위치를 계속 유지한다.
private void initNetworkThreads()
TCP와 UDP socket thread를 호출하는 method.
각각 연결이 완료되면 isConnectTCP와 isConnectUDP를 true로 변경.
private void closeNetworkThreads()
TCP와 UDP socket thread를 닫는 method.
각각 연결이 해제되면 isConnectTCP와 isConnectUDP를 false로 변경.
public class joystick
joystick class
참고 http://www.akexorcist.com/2012/10/android-code-joystick-controller.html
private int LAYOUT_ALPHA
레이아웃 투명도. 200으로 초기화.
private int OFFSET
조이스틱 영역의 가장자리 사이의 거리. 0으로 초기화.
private Context mContext
private ViewGroup mLayout
private ViewGroup.LayoutParams params
private int stick_width
private int stick_height
private double position_x = 0.0
private double position_y = 0.0
private double min_distance = 0.0
private double distance = 0
private DrawCanvas draw
private Paint paint
private Bitmap stick
private boolean touch_state
false
public Joystick (Context context, ViewGroup layout, int stick_ res_ id)
private void initJoystick()
joystick과 layout의 크기, 투명도, offset, 최소 거리 등을 초기화하는 method.
public void drawStick (double x, double y)
x, y 위치에 스틱(빨간 동그라미)를 그리는 method.
public void drawStick (MotionEvent arg1)
arg1의 멤버 변수의 값을 이용해 스틱(빨간 동그라미)를 그리는 method.
public double getX()
x의 값을 얻는 method. -10.0 ~ 10.0 의 값을 가진다.
public double getY()
y의 값을 얻는 method. -10.0 ~ 10.0 의 값을 가진다.
public double getDistance()
public void setMinimumDistance (int minDistance)
public double getMinimumDistance()
public void setOffset (int offset)
public int getOffset()
public void setStickAlpha (int alpha)
public int getStickAlpha()
public void setLayoutAlpha (int alpha)
public int getLayoutAlpha()
public void setStickSize (int width, int height)
public void setLayoutSize (int width, int height)
public int getLayoutWidth()
public int getLayoutHeight()
private double cal_angle (double x, double y)
private void draw()
private class DrawCanvas
extends View
Joysick inner class
double x
double y
private DrawCanvas (Context mContext)
public void onDraw (Canvas canvas)
private void position (double pos_x, double pos_y)
private class PacketVO
UDP Socket으로 보내는 Packet을 정의하고 객체를 생성하는 class.
private String P_H
header
private String P_X
pitch
private String P_Y
roll
private String P_Z
yaw
private String P_T
thr
public PacketVO()
public PacketVO (String P_H, String P_X, String P_Y, String P_Z, String P_T)
public static PacketVO jsonToPacket (String jsonMessage)
public static String packetToJson (PacketVO message)
public String toString()
public class TCPThread
extends Thread
TCP Socket 통신을 위한 class.
Thread로 실행된다.
각 header의 값에 따른 의미는 다음과 같다.
- -1이면 종료/에러
- 0이면 hello data(연결이 유지되어있는지 확인)
- 1이면 init msg
- 2이면 emergency exit
- 3이면 PID reset
private static String ip
TCP Socket을 연결할 IP 주소. "10.10.0.1"로 초기화. (변경 가능)
private static int port
TCP Socket을 연결할 port 번호. 8003로 초기화. (변경 가능)
private DataInputStream inputStream
입력을 위한 input stream.
private DataOutputStream outputStream
출력을 위한 output stream.
public static boolean flag
false로 초기화.
public Socket mSocket
TCP Socket 객체.
public boolean tcpInit()
TCP Thread를 초기화 하는 method.
ip와 port의 값으로 setSocket 후 char형의 값 1이 읽히면(read) 같은 값을 보내고(send),
다시 그 값을 읽어(read) 제대로 값이 읽혔을 경우 isInit를 false에서 true로 바꾸고 method 종료.
@Override public void run()
public void sendChar (char header)
TCP Socket으로 header 값을 전송.
public char readChar()
TCP Socket으로 char형의 값을 수신.
public PacketVO readJsonMessage()
public int sendPID (String jsonMessage)
public void sendJsonMessage (String jsonMessage)
public void setSocket (String ip, int port)
public void closeSocket()
public class UDPThread
extends Thread
UDP Socket 통신을 위한 class.
Thread로 실행된다.
각 header에 따른 의미는 다음과 같다.
- 1이면 init msg
- 2이면 control data (controller -> drone)
- 3이면 status data (drone -> controller)
public DatagramSocket socket
private DatagramPacket packet
private static InetAddress serverIP
private static int port
8004
public String msg
public boolean udpInit()
public void setSocket (InetAddress ip, int port)
public void run()
public void receive()
public JSONObject receivePacket()
public void sendPacket (String mMessage)
public class LevelActivity
extends Activity
implements OrientationListener
수평계 Activity.
참고 https://github.com/avianey/Level
private static LevelActivity CONTEXT
private LevelView view
private OrientationProvider provider
@Override protected void onCreate (Bundle savedInstanceState)
@Override protected void onPause()
@Override public void onDestroy()
@Override public void onCalibrationReset (boolean success)
@Override public void onCalibrationSaved (boolean success)
protected void onResume()
public static LevelActivity getContext()
public void onOrientationChanged (Orientation orientation, float pitch, float roll, float balance)
public class LevelView
extends SurfaceView
implements SurfaceHolder.Callback
참고 https://github.com/avianey/Level
private LevelPainter painter
public LevelView (Context context, AttributeSet attrs)
@Override public void onWindowFocusChanged (boolean hasWindowFocus)
@Override public void surfaceChanged (SurfaceHolder surfaceHolder, int format, int width, int height)
@Override public void surfaceCreated (SurfaceHolder holder)
@Override public void surfaceDestroyed (SurfaceHolder surfaceHolder)
public void onOrientationChanged (Orientation orientation, float pitch, float roll, float balance)
public class LevelPainter
implements Runnable
참고 https://github.com/avianey/Level
private boolean initialized
private boolean wait
private SurfaceHolder surfaceHolder
private int height
private int width
private int canvasHeight
private int canvasWidth
private int minLevelX
private int maxLevelX
private int minLevelY
private int maxLevelY
private int levelMinusBubbleHeight
private int levelMinusBubbleWidth
private int middleX
private int middleY
private int bubbleHeight;
private int bubbleWidth;
private int halfBubbleHeight;
private int halfBubbleWidth;
private int halfMarkerGap;
private int minBubble;
private int maxBubble;
private int markerThickness;
private int levelBorderHeight;
private int levelBorderWidth;
private int levelMaxDimension;
private int lcdWidth;
private int lcdHeight;
private int displayGap;
private int displayPadding;
private int sensorGap;
private int sensorY;
private int levelWidth;
private int levelHeight;
private int infoHeight;
private int infoY;
private Rect displayRect;
private float angle1;
private float angle2;
private double n, teta, l;
private static final double LEVEL_ASPECT_RATIO = 0.150;
private static final double BUBBLE_WIDTH = 0.150;
private static final double BUBBLE_ASPECT_RATIO = 1.000;
private static final double BUBBLE_CROPPING = 0.500;
private static final double MARKER_GAP = BUBBLE_WIDTH + 0.020;
private static final double MAX_SINUS = Math.sin(Math.PI / 4);
private Orientation orientation;
private long currentTime;
private long lastTime;
private double timeDiff;
private double posX;
private double posY;
private double angleX;
private double angleY;
private double speedX;
private double speedY;
private double x, y;
private Drawable level2D;
private Drawable bubble2D;
private Drawable marker2D;
private Drawable display;
private String infoText;
private Viscosity viscosity;
private double viscosityValue;
private DecimalFormat displayFormat;
private String displayBackgroundText;
private static final String FONT_LCD = "fonts/lcd.ttf";
private Paint lcdForegroundPaint;
private Paint lcdBackgroundPaint;
private Paint infoPaint;
private int backgroundColor;
private boolean showAngle;
private DisplayType angleType;
private final Handler handler;
private long frameRate;
public LevelPainter (SurfaceHolder surfaceHolder, Context context, Handler handler, int width, int height, boolean showAngle, DisplayType angleType, Viscosity viscosity)
@Override public void run()
public void clean()
public void pause (boolean paused)
public void setSurfaceSize (int width, int height)
private void setOrientation (Orientation newOrientation)
private void doDraw (Canvas canvas)
public void onOrientationChanged (Orientation newOrientation, float newPitch, float newRoll, float newBalance)