public void onCreate(Bundle savedInstanceState)

Download Report

Transcript public void onCreate(Bundle savedInstanceState)

UNIT
13 로봇을 만나자
로봇 SW 콘텐츠 교육원
조용수
학습 목표
• 라이브러리를 사용할 수 있다.
• 로봇을 움직일 수 있다.
2
미션
3
안드로이드 라이브러리 프로젝트
• www.smartrobot.org
4
안드로이드 라이브러리 프로젝트 넣기
• File > Import…
5
안드로이드 라이브러리 프로젝트 넣기
• Select archive file
6
내 프로젝트에서 사용하기
• File > Properties > Android
또는 Project > Properties > Android
7
내 프로젝트에서 사용하기
• Add…
8
내 프로젝트에서 사용하기
9
계층구조
10
Activity
ViewGroup
ViewGroup
View
View
Robot
Roboid
Device
Roboid
Device
기본 형태
11
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
View view = findViewById(R.id.title);
}
public void onInitialized(Robot robot)
{
super.onInitialized(robot);
...
Device device = robot.findDeviceById(Albert.EFFECTOR_SPEAKER);
}
device.read(...)
device.write(...)
디바이스 종류
• 센서
• 이펙터
• 커맨드
• 이벤트
12
액티비티 수정
public class MainActivity extends RobotActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
}
@Override
public void onExecute()
{
}
}
13
디바이스 얻기/데이터 쓰기
14
• Robot
– Device findDeviceById(int deviceId);
디바이스 ID
데이터 형
배열 크기
단위 값의 범위
EFFECTOR_LEFT_WHEEL
int[]
1
%
-100 ~ 100 0
EFFECTOR_RIGHT_WHEEL
int[]
1
%
-100 ~ 100 0
• Device
– boolean write(int data);
– boolean write(int index, int data);
– int write(int[] data);
초기 값
전진하기
15
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
}
@Override
public void onExecute()
{
leftWheelDevice.write(20);
rightWheelDevice.write(20);
}
}
후진하기
16
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
}
@Override
public void onExecute()
{
leftWheelDevice.write(-20);
rightWheelDevice.write(-20);
}
}
제자리 돌기
17
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
}
@Override
public void onExecute()
{
leftWheelDevice.write(-20);
rightWheelDevice.write(20);
}
}
피봇 턴
18
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
}
@Override
public void onExecute()
{
leftWheelDevice.write(0);
rightWheelDevice.write(20);
}
}
라운드 턴
19
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
}
@Override
public void onExecute()
{
leftWheelDevice.write(20);
rightWheelDevice.write(50);
}
}
2초 전진
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
private int count;
...
@Override
public void onExecute()
{
if(count < 100)
{
leftWheelDevice.write(20);
rightWheelDevice.write(20);
count ++;
}
else
{
leftWheelDevice.write(0);
rightWheelDevice.write(0);
}
}
}
20
근접 센서
21
디바이스 ID
데이터 형
배열 크기
SENSOR_LEFT_PROXIMITY
int[]
4
0 ~ 255
0
SENSOR_RIGHT_PROXIMITY
int[]
4
0 ~ 255
0
• Device
– int read();
– int read(int index);
– int read(int[] data);
단위 값의 범위
초기 값
근접 센서
22
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
private Device leftProximityDevice;
private Device rightProximityDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
leftProximityDevice = robot.findDeviceById(Albert.SENSOR_LEFT_PROXIITY);
rightProximityDevice = robot.findDeviceById(Albert.SENSOR_RIGHT_PROXIITY);
}
근접 센서
@Override
public void onExecute()
{
int leftProximity = leftProximityDevice.read();
int rightProximity = rightProximityDevice.read();
if(leftProximity > 50 || rightProximity > 50)
{
leftWheelDevice.write(-20);
rightWheelDevice.write(-20);
}
else
{
leftWheelDevice.write(20);
rightWheelDevice.write(20);
}
}
}
23
눈 색상
24
디바이스 ID
데이터 형
배열 크기
단위 값의 범위
EFFECTOR_LEFT_EYE
int[]
3
0 ~ 255
0
EFFECTOR_RIGHT_EYE
int[]
3
0 ~ 255
0
• Device
– boolean write(int data);
– boolean write(int index, int data);
– int write(int[] data);
초기 값
근접 센서
25
public class MainActivity extends RobotActivity
{
private Device leftWheelDevice;
private Device rightWheelDevice;
private Device leftProximityDevice;
private Device rightProximityDevice;
private Device leftEyeDevice;
private Device rightEyeDevice;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onInitialized(Robot robot)
{
leftWheelDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_WHEEL);
rightWheelDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_WHEEL);
leftProximityDevice = robot.findDeviceById(Albert.SENSOR_LEFT_PROXIITY);
rightProximityDevice = robot.findDeviceById(Albert.SENSOR_RIGHT_PROXIITY);
leftEyeDevice = robot.findDeviceById(Albert.EFFECTOR_LEFT_EYE);
rightEyeDevice = robot.findDeviceById(Albert.EFFECTOR_RIGHT_EYE);
}
근접 센서
@Override
public void onExecute()
{
int leftProximity = leftProximityDevice.read();
int rightProximity = rightProximityDevice.read();
if(leftProximity > 50 || rightProximity > 50)
{
leftWheelDevice.write(-20);
rightWheelDevice.write(-20);
leftEyeDevice.write(0, 255);
leftEyeDevice.write(1, 0);
leftEyeDevice.write(2, 0);
rightEyeDevice.write(new int[] { 255, 0, 0 });
}
else
{
leftWheelDevice.write(20);
rightWheelDevice.write(20);
int[] green = new int[] { 0, 255, 0 };
leftEyeDevice.write(green);
rightEyeDevice.write(green);
}
}
}
26